Skip to content
Open
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
33 changes: 28 additions & 5 deletions src/subcommand/status_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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
{
Expand Down
72 changes: 72 additions & 0 deletions src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::detached_head_info> repository_wrapper::get_detached_head_info() const
{
git_reflog* reflog = nullptr;
throw_if_error(git_reflog_read(&reflog, *this, "HEAD"));

std::optional<detached_head_info> 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<commit_wrapper>().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;
Expand Down
9 changes: 9 additions & 0 deletions src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ class repository_wrapper : public wrapper_base<git_repository>

// 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<detached_head_info> get_detached_head_info() const;
reference_wrapper head() const;
std::string head_short_name() const;

Expand Down
113 changes: 113 additions & 0 deletions test/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Loading