Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion src/subcommand/branch_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void branch_subcommand::run()

void branch_subcommand::run_list(const repository_wrapper& repo)
{
auto head_name = repo.head().short_name();
auto head_name = repo.head_short_name();
git_branch_t type = m_all_flag ? GIT_BRANCH_ALL : (m_remote_flag ? GIT_BRANCH_REMOTE : GIT_BRANCH_LOCAL);
auto iter = repo.iterate_branches(type);
auto br = iter.next();
Expand Down
16 changes: 1 addition & 15 deletions src/subcommand/status_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void status_subcommand::run()
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);
auto sl = status_list_wrapper::status_list(repo);
auto branch_name = repo.head().short_name();
auto branch_name = repo.head_short_name();

std::set<std::string> tracked_dir_set{};
std::set<std::string> untracked_dir_set{};
Expand Down Expand Up @@ -276,18 +276,4 @@ void status_subcommand::run()
{
std::cout << treeclean_message << std::endl;
}

// if (sl.has_ignored_header())
// {
// stream_colour_fn colour = termcolor::red;
// if (is_long)
// {
// std::cout << ignored_header;
// }
// print_not_tracked(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of), tracked_dir_set, untracked_dir_set, is_long, colour);
// if (is_long)
// {
// std::cout << std::endl;
// }
// }
}
27 changes: 26 additions & 1 deletion src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ bool repository_wrapper::is_bare() const
return git_repository_is_bare(*this);
}

// References
// Head

bool repository_wrapper::is_head_unborn() const
{
return git_repository_head_unborn(*this) == 1;
}

reference_wrapper repository_wrapper::head() const
{
Expand All @@ -60,6 +65,26 @@ reference_wrapper repository_wrapper::head() const
return reference_wrapper(ref);
}

std::string repository_wrapper::head_short_name() const
{
git_reference* ref;
std::string name;
throw_if_error(git_reference_lookup(&ref, *this, "HEAD"));
if (git_reference_type(ref) == GIT_REFERENCE_DIRECT)
{
name = git_reference_shorthand(ref);
}
else
{
name = git_reference_symbolic_target(ref);
name = name.substr(name.find_last_of('/') + 1);
}
git_reference_free(ref);
return name;
}

// References

reference_wrapper repository_wrapper::find_reference(std::string_view ref_name) const
{
git_reference* ref;
Expand Down
6 changes: 5 additions & 1 deletion src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ class repository_wrapper : public wrapper_base<git_repository>

bool is_bare() const;

// References
// Head
bool is_head_unborn() const;
reference_wrapper head() const;
std::string head_short_name() const;

// References
reference_wrapper find_reference(std::string_view ref_name) const;
std::optional<reference_wrapper> find_reference_dwim(std::string_view ref_name) const;

Expand Down
12 changes: 12 additions & 0 deletions test/test_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ def test_branch_nogit(git2cpp_path, tmp_path):
cmd = [git2cpp_path, 'branch']
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
assert p.returncode != 0

def tets_branch_new_repo(git2cpp_path, tmp_path, run_in_tmp_path):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def tets_branch_new_repo(git2cpp_path, tmp_path, run_in_tmp_path):
def test_branch_new_repo(git2cpp_path, tmp_path, run_in_tmp_path):

Typo in function name.

# tmp_path exists and is empty.
assert list(tmp_path.iterdir()) == []

cmd = [git2cpp_path, 'init']
p = subprocess.run(cmd, cwd = tmp_path)

branch_cmd = [git2cpp_path, 'branch']
p_branch = subprocess.run(branch_cmd, cwd = tmp_path)

assert p_branch.returncode == 0
12 changes: 12 additions & 0 deletions test/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ def test_status_add_file(xtl_clone, git2cpp_path, tmp_path, short_flag, long_fla
elif short_flag in ["-s", "--short"]:
assert "A " in p_status.stdout
assert "D " in p_status.stdout

def test_status_new_repo(git2cpp_path, tmp_path, run_in_tmp_path):
# tmp_path exists and is empty.
assert list(tmp_path.iterdir()) == []

cmd = [git2cpp_path, 'init']
p = subprocess.run(cmd, cwd = tmp_path)

status_cmd = [git2cpp_path, 'status']
p_status = subprocess.run(status_cmd, cwd = tmp_path)

assert p_status.returncode == 0