Skip to content

Commit 01b269b

Browse files
committed
add branch info and flag to status subcommand
1 parent f9af969 commit 01b269b

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

src/subcommand/status_subcommand.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "status_subcommand.hpp"
88
#include "../wrapper/status_wrapper.hpp"
9+
#include "../wrapper/refs_wrapper.hpp"
10+
911

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

2629
sub->callback([this]() { this->run(); });
2730
};
@@ -122,6 +125,7 @@ void status_subcommand::run()
122125
auto bare = false;
123126
auto repo = repository_wrapper::init(directory, bare);
124127
auto sl = status_list_wrapper::status_list(repo);
128+
auto branch_name = reference_wrapper::get_ref_name(repo);
125129

126130
// TODO: add branch info
127131

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

142146
bool is_long;
143147
is_long = ((of == output_format::DEFAULT) || (of == output_format::LONG));
148+
if (is_long)
149+
{
150+
std::cout << "On branch " << branch_name << std::endl;
151+
}
152+
else
153+
{
154+
if (branch_flag)
155+
{
156+
std::cout << "## " << branch_name << std::endl;
157+
}
158+
}
144159
if (sl.has_tobecommited_header())
145160
{
146161
if (is_long)

src/subcommand/status_subcommand.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class status_subcommand
1212
void run();
1313

1414
private:
15-
bool short_flag = false;
15+
bool branch_flag = false;
1616
bool long_flag = false;
17+
bool short_flag = false;
1718
};

src/wrapper/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
wrapper_files = files([
2+
'refs_wrapper.cpp',
23
'repository_wrapper.cpp',
34
'status_wrapper.cpp',
45
])

src/wrapper/refs_wrapper.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "../utils/git_exception.hpp"
2+
#include "../wrapper/refs_wrapper.hpp"
3+
4+
5+
reference_wrapper::~reference_wrapper()
6+
{
7+
git_reference_free(p_resource);
8+
p_resource=nullptr;
9+
}
10+
11+
const char* reference_wrapper::get_ref_name(const repository_wrapper& rw)
12+
{
13+
reference_wrapper ref;
14+
throwIfError(git_repository_head(&(ref.p_resource), rw));
15+
const char* ref_name = git_reference_shorthand(ref.p_resource);
16+
return ref_name;
17+
}

src/wrapper/refs_wrapper.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
// #include <string>
4+
5+
#include <git2.h>
6+
7+
#include "../wrapper/repository_wrapper.hpp"
8+
9+
class reference_wrapper : public wrapper_base<git_reference>
10+
{
11+
public:
12+
13+
~reference_wrapper();
14+
15+
reference_wrapper(reference_wrapper&&) = default;
16+
reference_wrapper& operator=(reference_wrapper&&) = default;
17+
18+
static const char* get_ref_name(const repository_wrapper& repo);
19+
20+
private:
21+
22+
reference_wrapper() = default;
23+
};

0 commit comments

Comments
 (0)