Skip to content

Commit 01b4244

Browse files
committed
Displays current branch when listing
1 parent 3ee5cfc commit 01b4244

File tree

7 files changed

+32
-12
lines changed

7 files changed

+32
-12
lines changed

src/subcommand/branch_subcommand.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ void branch_subcommand::run()
2626

2727
if (m_list_flag || m_branch_name.empty())
2828
{
29+
auto head_name = repo.head().short_name();
30+
std::cout << "* " << head_name << std::endl;
2931
git_branch_t type = m_all_flag ? GIT_BRANCH_ALL : (m_remote_flag ? GIT_BRANCH_REMOTE : GIT_BRANCH_LOCAL);
3032
auto iter = repo.iterate_branches(type);
3133
auto br = iter.next();
3234
while (br)
3335
{
34-
std::cout << br->name() << std::endl;
36+
if (br->name() != head_name)
37+
{
38+
std::cout << " " << br->name() << std::endl;
39+
}
3540
br = iter.next();
3641
}
3742
}

src/subcommand/status_subcommand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void status_subcommand::run()
186186
auto bare = false;
187187
auto repo = repository_wrapper::init(directory, bare);
188188
auto sl = status_list_wrapper::status_list(repo);
189-
auto branch_name = reference_wrapper::get_ref_name(repo);
189+
auto branch_name = repo.head().short_name();
190190

191191
std::set<std::string> tracked_dir_set{};
192192
std::set<std::string> untracked_dir_set{};

src/wrapper/refs_wrapper.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#include "../utils/git_exception.hpp"
22
#include "../wrapper/refs_wrapper.hpp"
33

4+
reference_wrapper::reference_wrapper(git_reference* jaila_ref)
5+
: base_type(jaila_ref)
6+
{
7+
}
48

59
reference_wrapper::~reference_wrapper()
610
{
711
git_reference_free(p_resource);
812
p_resource=nullptr;
913
}
1014

11-
std::string reference_wrapper::get_ref_name(const repository_wrapper& rw)
15+
std::string reference_wrapper::short_name() const
1216
{
13-
reference_wrapper ref;
14-
throwIfError(git_repository_head(&(ref.p_resource), rw));
15-
return git_reference_shorthand(ref.p_resource);
17+
return git_reference_shorthand(p_resource);
1618
}

src/wrapper/refs_wrapper.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44

55
#include <git2.h>
66

7-
#include "../wrapper/repository_wrapper.hpp"
87
#include "../wrapper/wrapper_base.hpp"
98

109
class reference_wrapper : public wrapper_base<git_reference>
1110
{
1211
public:
1312

13+
using base_type = wrapper_base<git_reference>;
14+
1415
~reference_wrapper();
1516

1617
reference_wrapper(reference_wrapper&&) noexcept = default;
1718
reference_wrapper& operator=(reference_wrapper&&) noexcept = default;
1819

19-
static std::string get_ref_name(const repository_wrapper& repo);
20+
std::string short_name() const;
2021

2122
private:
2223

23-
reference_wrapper() = default;
24+
reference_wrapper(git_reference* ref);
25+
26+
friend class repository_wrapper;
2427
};

src/wrapper/repository_wrapper.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ repository_wrapper repository_wrapper::init(const std::string& directory, bool b
2121
return rw;
2222
}
2323

24+
reference_wrapper repository_wrapper::head() const
25+
{
26+
git_reference* ref;
27+
throwIfError(git_repository_head(&ref, *this));
28+
return reference_wrapper(ref);
29+
}
30+
2431
index_wrapper repository_wrapper::make_index()
2532
{
2633
index_wrapper index = index_wrapper::init(*this);

src/wrapper/repository_wrapper.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "../wrapper/branch_wrapper.hpp"
88
#include "../wrapper/commit_wrapper.hpp"
99
#include "../wrapper/index_wrapper.hpp"
10+
#include "../wrapper/refs_wrapper.hpp"
1011
#include "../wrapper/wrapper_base.hpp"
1112

1213
class repository_wrapper : public wrapper_base<git_repository>
@@ -21,6 +22,8 @@ class repository_wrapper : public wrapper_base<git_repository>
2122
static repository_wrapper init(const std::string& directory, bool bare);
2223
static repository_wrapper open(const std::string& directory);
2324

25+
reference_wrapper head() const;
26+
2427
index_wrapper make_index();
2528

2629
branch_wrapper create_branch(const std::string& name, bool force);

test/test_branch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ def rename_git():
1212
def test_branch_list(rename_git, git2cpp_path):
1313
cmd = [git2cpp_path, 'branch']
1414
p = subprocess.run(cmd, capture_output=True, cwd="test/data/status_data", text=True)
15-
assert(p.stdout == 'main\n')
15+
assert(p.stdout == '* main\n')
1616

1717
def test_branch_create_delete(rename_git, git2cpp_path):
1818
create_cmd = [git2cpp_path, 'branch', 'foregone']
1919
subprocess.run(create_cmd, capture_output=True, cwd="test/data/status_data", text=True)
2020
list_cmd = [git2cpp_path, 'branch']
2121
p = subprocess.run(list_cmd, capture_output=True, cwd="test/data/status_data", text=True)
22-
assert(p.stdout == 'foregone\nmain\n')
22+
assert(p.stdout == '* main\n foregone\n')
2323
del_cmd = [git2cpp_path, 'branch', '-d', 'foregone']
2424
subprocess.run(del_cmd, capture_output=True, cwd="test/data/status_data", text=True)
2525
p2 = subprocess.run(list_cmd, capture_output=True, cwd="test/data/status_data", text=True)
26-
assert(p2.stdout == 'main\n')
26+
assert(p2.stdout == '* main\n')
2727

2828

0 commit comments

Comments
 (0)