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.refs_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:
};
1 change: 1 addition & 0 deletions src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#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
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