Add show-ref subcommand · QuantStack/git2cpp@b4326ff · GitHub
Skip to content

Commit b4326ff

Browse files
committed
Add show-ref subcommand
1 parent 22db9ed commit b4326ff

7 files changed

Lines changed: 103 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "subcommand/revlist_subcommand.hpp"
2424
#include "subcommand/revparse_subcommand.hpp"
2525
#include "subcommand/rm_subcommand.hpp"
26+
#include "subcommand/showref_subcommand.hpp"
2627
#include "subcommand/stash_subcommand.hpp"
2728
#include "subcommand/status_subcommand.hpp"
2829
#include "subcommand/tag_subcommand.hpp"
@@ -63,6 +64,7 @@ int main(int argc, char** argv)
6364
rm_subcommand rm(lg2_obj, app);
6465
stash_subcommand stash(lg2_obj, app);
6566
tag_subcommand tag(lg2_obj, app);
67+
showref_subcommand showref(lg2_obj, app);
6668

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "showref_subcommand.hpp"
2+
3+
#include <git2.h>
4+
5+
#include "../wrapper/repository_wrapper.hpp"
6+
7+
showref_subcommand::showref_subcommand(const libgit2_object&, CLI::App& app)
8+
{
9+
auto* sub = app.add_subcommand("show-ref", "List references in a local repository");
10+
11+
sub->callback(
12+
[this]()
13+
{
14+
this->run();
15+
}
16+
);
17+
};
18+
19+
void showref_subcommand::run()
20+
{
21+
auto directory = get_current_git_path();
22+
auto repo = repository_wrapper::open(directory);
23+
24+
auto repo_refs = repo.refs_list();
25+
26+
for (auto r:repo_refs)
27+
{
28+
git_oid oid = repo.ref_name_to_id(r);
29+
std::cout << oid_to_hex(oid) << " " << r << std::endl;
30+
}
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <CLI/CLI.hpp>
4+
5+
#include "../utils/common.hpp"
6+
7+
class showref_subcommand
8+
{
9+
public:
10+
11+
explicit showref_subcommand(const libgit2_object&, CLI::App& app);
12+
void run();
13+
14+
private:
15+
16+
17+
};

src/wrapper/repository_wrapper.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <git2.h>
88

9+
#include "../utils/common.hpp"
910
#include "../utils/git_exception.hpp"
1011
#include "../wrapper/annotated_commit_wrapper.hpp"
1112
#include "../wrapper/branch_wrapper.hpp"

test/test_fetch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import pytest
21
import subprocess
32

3+
import pytest
4+
45

56
@pytest.mark.parametrize("protocol", ["http", "https"])
67
def test_fetch_private_repo(git2cpp_path, tmp_path, run_in_tmp_path, private_test_repo, protocol):

test/test_showref.py

Lines changed: 48 additions & 0 deletions

0 commit comments

Comments
 (0)