From c69ba2ad122cda6b4cb8f496f7a810e35d2ba7f5 Mon Sep 17 00:00:00 2001 From: Johan Mabille Date: Fri, 12 Dec 2025 15:30:30 +0100 Subject: [PATCH 1/2] Fixed status and branch command on newly created repos --- src/subcommand/branch_subcommand.cpp | 2 +- src/subcommand/status_subcommand.cpp | 16 +--------------- src/wrapper/repository_wrapper.cpp | 27 ++++++++++++++++++++++++++- src/wrapper/repository_wrapper.hpp | 6 +++++- test/test_branch.py | 12 ++++++++++++ test/test_status.py | 12 ++++++++++++ 6 files changed, 57 insertions(+), 18 deletions(-) diff --git a/src/subcommand/branch_subcommand.cpp b/src/subcommand/branch_subcommand.cpp index c321fa8..82decf1 100644 --- a/src/subcommand/branch_subcommand.cpp +++ b/src/subcommand/branch_subcommand.cpp @@ -39,7 +39,7 @@ void branch_subcommand::run() void branch_subcommand::run_list(const repository_wrapper& repo) { - auto head_name = repo.head().short_name(); + auto head_name = repo.head_short_name(); git_branch_t type = m_all_flag ? GIT_BRANCH_ALL : (m_remote_flag ? GIT_BRANCH_REMOTE : GIT_BRANCH_LOCAL); auto iter = repo.iterate_branches(type); auto br = iter.next(); diff --git a/src/subcommand/status_subcommand.cpp b/src/subcommand/status_subcommand.cpp index 36aa213..9e98840 100644 --- a/src/subcommand/status_subcommand.cpp +++ b/src/subcommand/status_subcommand.cpp @@ -167,7 +167,7 @@ void status_subcommand::run() auto directory = get_current_git_path(); auto repo = repository_wrapper::open(directory); auto sl = status_list_wrapper::status_list(repo); - auto branch_name = repo.head().short_name(); + auto branch_name = repo.head_short_name(); std::set tracked_dir_set{}; std::set untracked_dir_set{}; @@ -276,18 +276,4 @@ void status_subcommand::run() { std::cout << treeclean_message << std::endl; } - - // if (sl.has_ignored_header()) - // { - // stream_colour_fn colour = termcolor::red; - // if (is_long) - // { - // std::cout << ignored_header; - // } - // print_not_tracked(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of), tracked_dir_set, untracked_dir_set, is_long, colour); - // if (is_long) - // { - // std::cout << std::endl; - // } - // } } diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index 93aae24..d58bb4d 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -51,7 +51,12 @@ bool repository_wrapper::is_bare() const return git_repository_is_bare(*this); } -// References +// Head + +bool repository_wrapper::is_head_unborn() const +{ + return git_repository_head_unborn(*this) == 1; +} reference_wrapper repository_wrapper::head() const { @@ -60,6 +65,26 @@ reference_wrapper repository_wrapper::head() const return reference_wrapper(ref); } +std::string repository_wrapper::head_short_name() const +{ + git_reference* ref; + std::string name; + throw_if_error(git_reference_lookup(&ref, *this, "HEAD")); + if (git_reference_type(ref) == GIT_REFERENCE_DIRECT) + { + name = git_reference_shorthand(ref); + } + else + { + name = git_reference_symbolic_target(ref); + name = name.substr(name.find_last_of('/') + 1); + } + git_reference_free(ref); + return name; +} + +// References + reference_wrapper repository_wrapper::find_reference(std::string_view ref_name) const { git_reference* ref; diff --git a/src/wrapper/repository_wrapper.hpp b/src/wrapper/repository_wrapper.hpp index 5cce074..c117e8a 100644 --- a/src/wrapper/repository_wrapper.hpp +++ b/src/wrapper/repository_wrapper.hpp @@ -35,8 +35,12 @@ class repository_wrapper : public wrapper_base bool is_bare() const; - // References + // Head + bool is_head_unborn() const; reference_wrapper head() const; + std::string head_short_name() const; + + // References reference_wrapper find_reference(std::string_view ref_name) const; std::optional find_reference_dwim(std::string_view ref_name) const; diff --git a/test/test_branch.py b/test/test_branch.py index f81bc8c..fbe05fa 100644 --- a/test/test_branch.py +++ b/test/test_branch.py @@ -38,3 +38,15 @@ def test_branch_nogit(git2cpp_path, tmp_path): cmd = [git2cpp_path, 'branch'] p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True) assert p.returncode != 0 + +def tets_branch_new_repo(git2cpp_path, tmp_path, run_in_tmp_path): + # tmp_path exists and is empty. + assert list(tmp_path.iterdir()) == [] + + cmd = [git2cpp_path, 'init'] + p = subprocess.run(cmd, cwd = tmp_path) + + branch_cmd = [git2cpp_path, 'branch'] + p_branch = subprocess.run(branch_cmd, cwd = tmp_path) + + assert p_branch.returncode == 0 diff --git a/test/test_status.py b/test/test_status.py index ab7288e..6e97e19 100644 --- a/test/test_status.py +++ b/test/test_status.py @@ -76,3 +76,15 @@ def test_status_add_file(xtl_clone, git2cpp_path, tmp_path, short_flag, long_fla elif short_flag in ["-s", "--short"]: assert "A " in p_status.stdout assert "D " in p_status.stdout + +def test_status_new_repo(git2cpp_path, tmp_path, run_in_tmp_path): + # tmp_path exists and is empty. + assert list(tmp_path.iterdir()) == [] + + cmd = [git2cpp_path, 'init'] + p = subprocess.run(cmd, cwd = tmp_path) + + status_cmd = [git2cpp_path, 'status'] + p_status = subprocess.run(status_cmd, cwd = tmp_path) + + assert p_status.returncode == 0 From da9aab05be49790f4cd15575f51f27c89bb21450 Mon Sep 17 00:00:00 2001 From: Johan Mabille Date: Fri, 12 Dec 2025 16:41:36 +0100 Subject: [PATCH 2/2] Fixed typo --- test/test_branch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_branch.py b/test/test_branch.py index fbe05fa..20c1149 100644 --- a/test/test_branch.py +++ b/test/test_branch.py @@ -39,7 +39,7 @@ def test_branch_nogit(git2cpp_path, tmp_path): p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True) assert p.returncode != 0 -def tets_branch_new_repo(git2cpp_path, tmp_path, run_in_tmp_path): +def test_branch_new_repo(git2cpp_path, tmp_path, run_in_tmp_path): # tmp_path exists and is empty. assert list(tmp_path.iterdir()) == []