diff --git a/src/subcommand/status_subcommand.cpp b/src/subcommand/status_subcommand.cpp index e2c0f0c..6dd3728 100644 --- a/src/subcommand/status_subcommand.cpp +++ b/src/subcommand/status_subcommand.cpp @@ -259,7 +259,7 @@ namespace std::sort(untracked_items.begin(), untracked_items.end()); for (const auto& it : untracked_items) { - std::cout << colour << "?? " << termcolor::reset << it << std::endl; + std::cout << colour << "??" << termcolor::reset << " " << it << std::endl; } std::sort(ignored_items.begin(), ignored_items.end()); @@ -425,8 +425,8 @@ void print_tracking_info(repository_wrapper& repo, status_list_wrapper& sl, bool } std::cout << "]"; } - std::cout << std::endl; } + std::cout << std::endl; } } @@ -618,18 +618,41 @@ void status_run(status_subcommand_options options) bool is_long; is_long = ((of == output_format::DEFAULT) || (of == output_format::LONG)); - auto branch_name = repo.head_short_name(); + bool is_detached = repo.is_head_detached(); + std::string branch_name = is_detached ? "HEAD (no branch)" : repo.head_short_name(); bool is_coloured = true; bool branch_flag = options.m_branch_flag; if (is_long) { - std::cout << "On branch " << branch_name << std::endl; + if (is_detached) + { + if (auto info = repo.get_detached_head_info()) + { + std::cout << termcolor::red << "HEAD detached " << (info->is_at ? "at " : "from ") + << termcolor::reset << info->name << std::endl; + } + else + { + std::cout << termcolor::red << "Not currently on any branch." << termcolor::reset << std::endl; + } + } + else + { + std::cout << "On branch " << branch_name << std::endl; + } } else if (branch_flag) { if (is_coloured) { - std::cout << "## " << termcolor::green << branch_name << termcolor::reset; + if (is_detached) + { + std::cout << "## " << termcolor::red << branch_name << termcolor::reset; + } + else + { + std::cout << "## " << termcolor::green << branch_name << termcolor::reset; + } } else { diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index 8b102cb..2944709 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -98,6 +98,78 @@ bool repository_wrapper::is_head_unborn() const return git_repository_head_unborn(*this) == 1; } +bool repository_wrapper::is_head_detached() const +{ + return git_repository_head_detached(*this) == 1; +} + +std::optional repository_wrapper::get_detached_head_info() const +{ + git_reflog* reflog = nullptr; + throw_if_error(git_reflog_read(&reflog, *this, "HEAD")); + + std::optional result; + constexpr std::string_view prefix = "checkout: moving from "; + const size_t count = git_reflog_entrycount(reflog); + for (size_t i = 0; i < count; ++i) // index 0 is the most recent entry + { + const git_reflog_entry* entry = git_reflog_entry_byindex(reflog, i); + const char* msg = git_reflog_entry_message(entry); + if (msg == nullptr) + { + continue; + } + std::string_view message(msg); + if (!message.starts_with(prefix)) + { + continue; + } + auto to_pos = message.find(" to ", prefix.size()); + if (to_pos == std::string_view::npos) + { + continue; + } + std::string target(message.substr(to_pos + 4)); + if (auto nl = target.find('\n'); nl != std::string::npos) + { + target.erase(nl); + } + + const git_oid new_oid = *git_reflog_entry_id_new(entry); + auto checked_out = this->find_commit(new_oid); + + detached_head_info info; + // As in git, a checkout to "HEAD" is relative, so use the abbreviated SHA instead + auto ref = (target == "HEAD") ? std::nullopt : this->find_reference_dwim(target); + if (ref && git_oid_equal(&ref->peel().oid(), &new_oid)) + { + std::string_view name = git_reference_name(*ref); + for (std::string_view p : {"refs/tags/", "refs/remotes/"}) + { + if (name.starts_with(p)) + { + name.remove_prefix(p.size()); + break; + } + } + info.name = name; + } + else + { + git_buf abbrev = GIT_BUF_INIT; + throw_if_error(git_object_short_id(&abbrev, checked_out)); + info.name = abbrev.ptr; + git_buf_dispose(&abbrev); + } + info.is_at = git_oid_equal(&new_oid, &this->find_commit("HEAD").oid()); + result = info; + break; + } + + git_reflog_free(reflog); + return result; +} + reference_wrapper repository_wrapper::head() const { git_reference* ref; diff --git a/src/wrapper/repository_wrapper.hpp b/src/wrapper/repository_wrapper.hpp index 86f6d1e..c606768 100644 --- a/src/wrapper/repository_wrapper.hpp +++ b/src/wrapper/repository_wrapper.hpp @@ -58,6 +58,15 @@ class repository_wrapper : public wrapper_base // Head bool is_head_unborn() const; + bool is_head_detached() const; + + struct detached_head_info + { + std::string name; // e.g. "v1.0", "origin/main", or an abbreviated SHA + bool is_at; // true: "detached at", false: "detached from" + }; + + std::optional get_detached_head_info() const; reference_wrapper head() const; std::string head_short_name() const; diff --git a/test/test_status.py b/test/test_status.py index 104eb68..f066f13 100644 --- a/test/test_status.py +++ b/test/test_status.py @@ -253,6 +253,7 @@ def test_status_untracked_directory(repo_init_with_commit, git2cpp_path, tmp_pat p = subprocess.run(cmd_status, capture_output=True, cwd=tmp_path, text=True) assert p.returncode == 0 + p.stdout = strip_ansi_colours(p.stdout) if short_flag == "-s": assert "?? " in p.stdout else: @@ -427,3 +428,115 @@ def test_status_modified_staged_then_modified(repo_init_with_commit, git2cpp_pat assert "Changes not staged for commit" in out # Ensure the filename appears (it should appear in one or both sections) assert "initial.txt" in out + + +def test_status_detached_at_tag(repo_init_with_commit, git2cpp_path, tmp_path): + subprocess.run([git2cpp_path, "tag", "v1.0"], cwd=tmp_path, check=True) + subprocess.run([git2cpp_path, "checkout", "v1.0"], cwd=tmp_path, check=True) + + p = subprocess.run( + [git2cpp_path, "status"], capture_output=True, cwd=tmp_path, text=True, check=True + ) + out = strip_ansi_colours(p.stdout) + assert out.splitlines()[0] == "HEAD detached at v1.0" + assert "On branch" not in out + + +def test_status_detached_at_sha(repo_init_with_commit, git2cpp_path, tmp_path): + p_sha = subprocess.run( + [git2cpp_path, "rev-parse", "HEAD"], + capture_output=True, + cwd=tmp_path, + text=True, + check=True, + ) + sha = p_sha.stdout.strip() + subprocess.run([git2cpp_path, "checkout", sha], cwd=tmp_path, check=True) + + p = subprocess.run( + [git2cpp_path, "status"], capture_output=True, cwd=tmp_path, text=True, check=True + ) + out = strip_ansi_colours(p.stdout) + assert out.splitlines()[0] == f"HEAD detached at {sha[:7]}" + assert "On branch" not in out + + +def test_status_detached_from(repo_init_with_commit, git2cpp_path, tmp_path): + subprocess.run([git2cpp_path, "tag", "v1.0"], cwd=tmp_path, check=True) + subprocess.run([git2cpp_path, "checkout", "v1.0"], cwd=tmp_path, check=True) + + second = tmp_path / "second.txt" + second.write_text("second") + subprocess.run([git2cpp_path, "add", "second.txt"], cwd=tmp_path, check=True) + subprocess.run([git2cpp_path, "commit", "-m", "Second commit"], cwd=tmp_path, check=True) + + p = subprocess.run( + [git2cpp_path, "status"], capture_output=True, cwd=tmp_path, text=True, check=True + ) + out = strip_ansi_colours(p.stdout) + assert out.splitlines()[0] == "HEAD detached from v1.0" + assert "On branch" not in out + + +def test_status_detached_at_remote_branch(commit_env_config, git2cpp_path, tmp_path): + repo_path = tmp_path / "repo" + repo_path.mkdir() + subprocess.run([git2cpp_path, "init", "-b", "main"], cwd=repo_path, check=True) + (repo_path / "file.txt").write_text("initial") + subprocess.run([git2cpp_path, "add", "file.txt"], cwd=repo_path, check=True) + subprocess.run([git2cpp_path, "commit", "-m", "initial"], cwd=repo_path, check=True) + + clone_path = tmp_path / "clone" + subprocess.run([git2cpp_path, "clone", str(repo_path), str(clone_path)], check=True) + subprocess.run([git2cpp_path, "checkout", "origin/main"], cwd=clone_path, check=True) + + p = subprocess.run( + [git2cpp_path, "status"], capture_output=True, cwd=clone_path, text=True, check=True + ) + out = strip_ansi_colours(p.stdout) + assert out.splitlines()[0] == "HEAD detached at origin/main" + assert "On branch" not in out + + +def test_status_detached_no_reflog(repo_init_with_commit, git2cpp_path, tmp_path): + p_sha = subprocess.run( + [git2cpp_path, "rev-parse", "HEAD"], + capture_output=True, + cwd=tmp_path, + text=True, + check=True, + ) + sha = p_sha.stdout.strip() + subprocess.run([git2cpp_path, "checkout", sha], cwd=tmp_path, check=True) + os.remove(tmp_path / ".git" / "logs" / "HEAD") + + p = subprocess.run( + [git2cpp_path, "status"], capture_output=True, cwd=tmp_path, text=True, check=True + ) + out = strip_ansi_colours(p.stdout) + assert out.splitlines()[0] == "Not currently on any branch." + assert "On branch" not in out + + +def test_status_short_branch_detached(repo_init_with_commit, git2cpp_path, tmp_path): + subprocess.run([git2cpp_path, "tag", "v1.0"], cwd=tmp_path, check=True) + subprocess.run([git2cpp_path, "checkout", "v1.0"], cwd=tmp_path, check=True) + + p = subprocess.run( + [git2cpp_path, "status", "-sb"], capture_output=True, cwd=tmp_path, text=True, check=True + ) + assert strip_ansi_colours(p.stdout) == "## HEAD (no branch)\n" + + +@pytest.mark.parametrize("dirty", [False, True]) +def test_status_short_branch_newline(repo_init_with_commit, git2cpp_path, tmp_path, dirty): + if dirty: + (tmp_path / "new.txt").write_text("new") + expected = "## main\n?? new.txt\n" + else: + expected = "## main\n" + + p = subprocess.run( + [git2cpp_path, "status", "-sb"], capture_output=True, cwd=tmp_path, text=True, check=True + ) + assert strip_ansi_colours(p.stdout) == expected