Skip to content

feat: add branch info and flag to status subcommand #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/subcommand/status_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "status_subcommand.hpp"
#include "../wrapper/status_wrapper.hpp"
#include "../wrapper/refs_wrapper.hpp"


status_subcommand::status_subcommand(const libgit2_object&, CLI::App& app)
{
Expand All @@ -22,6 +24,7 @@ status_subcommand::status_subcommand(const libgit2_object&, CLI::App& app)
// This is similar to the short output, but will remain stable across Git versions and regardless of user configuration.
// See below for details. The version parameter is used to specify the format version. This is optional and defaults
// to the original version v1 format.");
sub->add_flag("-b,--branch", branch_flag, "Show the branch and tracking info even in short-format.");

sub->callback([this]() { this->run(); });
};
Expand Down Expand Up @@ -122,6 +125,7 @@ void status_subcommand::run()
auto bare = false;
auto repo = repository_wrapper::init(directory, bare);
auto sl = status_list_wrapper::status_list(repo);
auto branch_name = reference_wrapper::get_ref_name(repo);

// TODO: add branch info

Expand All @@ -141,6 +145,17 @@ void status_subcommand::run()

bool is_long;
is_long = ((of == output_format::DEFAULT) || (of == output_format::LONG));
if (is_long)
{
std::cout << "On branch " << branch_name << std::endl;
}
else
{
if (branch_flag)
{
std::cout << "## " << branch_name << std::endl;
}
}
if (sl.has_tobecommited_header())
{
if (is_long)
Expand Down
3 changes: 2 additions & 1 deletion src/subcommand/status_subcommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class status_subcommand
void run();

private:
bool short_flag = false;
bool branch_flag = false;
bool long_flag = false;
bool short_flag = false;
};
1 change: 1 addition & 0 deletions src/wrapper/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
wrapper_files = files([
'refs_wrapper.cpp',
'repository_wrapper.cpp',
'status_wrapper.cpp',
])
16 changes: 16 additions & 0 deletions src/wrapper/refs_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "../utils/git_exception.hpp"
#include "../wrapper/refs_wrapper.hpp"


reference_wrapper::~reference_wrapper()
{
git_reference_free(p_resource);
p_resource=nullptr;
}

std::string reference_wrapper::get_ref_name(const repository_wrapper& rw)
{
reference_wrapper ref;
throwIfError(git_repository_head(&(ref.p_resource), rw));
return git_reference_shorthand(ref.p_resource);
}
23 changes: 23 additions & 0 deletions src/wrapper/refs_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

// #include <string>

#include <git2.h>

#include "../wrapper/repository_wrapper.hpp"

class reference_wrapper : public wrapper_base<git_reference>
{
public:

~reference_wrapper();

reference_wrapper(reference_wrapper&&) = default;
reference_wrapper& operator=(reference_wrapper&&) = default;

static std::string get_ref_name(const repository_wrapper& repo);

private:

reference_wrapper() = default;
};