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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ set(GIT2CPP_SRC
${GIT2CPP_SOURCE_DIR}/subcommand/revparse_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/rm_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/rm_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/showref_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/showref_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/stash_subcommand.cpp
${GIT2CPP_SOURCE_DIR}/subcommand/stash_subcommand.hpp
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "subcommand/revlist_subcommand.hpp"
#include "subcommand/revparse_subcommand.hpp"
#include "subcommand/rm_subcommand.hpp"
#include "subcommand/showref_subcommand.hpp"
#include "subcommand/stash_subcommand.hpp"
#include "subcommand/status_subcommand.hpp"
#include "subcommand/tag_subcommand.hpp"
Expand Down Expand Up @@ -63,6 +64,7 @@ int main(int argc, char** argv)
rm_subcommand rm(lg2_obj, app);
stash_subcommand stash(lg2_obj, app);
tag_subcommand tag(lg2_obj, app);
showref_subcommand showref(lg2_obj, app);

app.require_subcommand(/* min */ 0, /* max */ 1);

Expand Down
31 changes: 31 additions & 0 deletions src/subcommand/showref_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "showref_subcommand.hpp"

#include <git2.h>

#include "../wrapper/repository_wrapper.hpp"

showref_subcommand::showref_subcommand(const libgit2_object&, CLI::App& app)
{
auto* sub = app.add_subcommand("show-ref", "List references in a local repository");

sub->callback(
[this]()
{
this->run();
}
);
};

void showref_subcommand::run()
{
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);

auto repo_refs = repo.reference_list();

for (auto r : repo_refs)
{
git_oid oid = repo.ref_name_to_id(r);
std::cout << oid_to_hex(oid) << " " << r << std::endl;
}
}
15 changes: 15 additions & 0 deletions src/subcommand/showref_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <CLI/CLI.hpp>

#include "../utils/common.hpp"

class showref_subcommand
{
public:

explicit showref_subcommand(const libgit2_object&, CLI::App& app);
void run();

private:
};
8 changes: 8 additions & 0 deletions src/utils/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,11 @@ std::string trim(const std::string& str)
auto s = std::regex_replace(str, std::regex("^\\s+"), "");
return std::regex_replace(s, std::regex("\\s+$"), "");
}

std::string oid_to_hex(const git_oid& oid)
{
char oid_str[GIT_OID_SHA1_HEXSIZE + 1];
git_oid_fmt(oid_str, &oid);
oid_str[GIT_OID_SHA1_HEXSIZE] = '\0';
return std::string(oid_str);
}
2 changes: 2 additions & 0 deletions src/utils/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ std::vector<std::string> split_input_at_newlines(std::string_view str);

// Remove whitespace from start and end of a string.
std::string trim(const std::string& str);

std::string oid_to_hex(const git_oid& oid);
20 changes: 20 additions & 0 deletions src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ std::optional<reference_wrapper> repository_wrapper::find_reference_dwim(std::st
return rc == 0 ? std::make_optional(reference_wrapper(ref)) : std::nullopt;
}

std::vector<std::string> repository_wrapper::reference_list() const
{
git_strarray refs = {0};
throw_if_error(git_reference_list(&refs, *this));
std::vector<std::string> result;
for (size_t i = 0; i < refs.count; ++i)
{
result.push_back(refs.strings[i]);
}
git_strarray_free(&refs);
return result;
}

const git_oid repository_wrapper::ref_name_to_id(std::string ref_name) const
{
git_oid ref_id;
throw_if_error(git_reference_name_to_id(&ref_id, *this, ref_name.c_str()));
return ref_id;
}

// Index

index_wrapper repository_wrapper::make_index()
Expand Down
3 changes: 2 additions & 1 deletion src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <git2.h>

#include "../utils/common.hpp"
#include "../utils/git_exception.hpp"
#include "../wrapper/annotated_commit_wrapper.hpp"
#include "../wrapper/branch_wrapper.hpp"
Expand Down Expand Up @@ -63,6 +62,8 @@ class repository_wrapper : public wrapper_base<git_repository>
// References
reference_wrapper find_reference(std::string_view ref_name) const;
std::optional<reference_wrapper> find_reference_dwim(std::string_view ref_name) const;
std::vector<std::string> reference_list() const;
const git_oid ref_name_to_id(std::string ref_name) const;

// Index
index_wrapper make_index();
Expand Down
3 changes: 2 additions & 1 deletion test/test_fetch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import subprocess

import pytest


@pytest.mark.parametrize("protocol", ["http", "https"])
def test_fetch_private_repo(git2cpp_path, tmp_path, run_in_tmp_path, private_test_repo, protocol):
Expand Down
48 changes: 48 additions & 0 deletions test/test_showref.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import re
import subprocess


def test_showref_list(repo_init_with_commit, git2cpp_path, tmp_path):
"""`show-ref` lists the repository references (heads present after init+commit)."""
cmd = [git2cpp_path, "show-ref"]
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
assert p.returncode == 0
# repo_init_with_commit in conftest creates the branch "main"
assert "refs/heads/main" in p.stdout


def test_showref_includes_tag(repo_init_with_commit, git2cpp_path, tmp_path):
"""A created tag appears in show-ref output as refs/tags/<name>."""
# create a lightweight tag using the CLI under test
subprocess.run([git2cpp_path, "tag", "v1.0"], cwd=tmp_path, check=True)

p = subprocess.run([git2cpp_path, "show-ref"], capture_output=True, cwd=tmp_path, text=True)
assert p.returncode == 0
assert "refs/tags/v1.0" in p.stdout


def test_showref_line_format(repo_init_with_commit, git2cpp_path, tmp_path):
"""Each line of show-ref is: <40-hex-oid> <refname>."""
p = subprocess.run([git2cpp_path, "show-ref"], capture_output=True, cwd=tmp_path, text=True)
assert p.returncode == 0
print(p.stdout)

hex_re = re.compile(r"^[0-9a-f]{40}$")
for line in p.stdout.splitlines():
line = line.strip()
if not line:
continue
parts = line.split()
# Expect at least two tokens: oid and refname
assert len(parts) >= 2
oid, refname = parts[0], parts[1]
assert hex_re.match(oid), f"OID not a 40-char hex: {oid!r}"
assert refname.startswith("refs/"), f"Refname does not start with refs/: {refname!r}"


def test_showref_nogit(git2cpp_path, tmp_path):
"""Running show-ref outside a repository returns an error and non-zero exit."""
cmd = [git2cpp_path, "show-ref"]
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
assert p.returncode != 0
assert "error: could not find repository at" in p.stderr