Skip to content

Commit 5478628

Browse files
committed
Fixed status and branch command on newly created repos
1 parent 6510681 commit 5478628

File tree

6 files changed

+57
-18
lines changed

6 files changed

+57
-18
lines changed

src/subcommand/branch_subcommand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void branch_subcommand::run()
3939

4040
void branch_subcommand::run_list(const repository_wrapper& repo)
4141
{
42-
auto head_name = repo.head().short_name();
42+
auto head_name = repo.head_short_name();
4343
git_branch_t type = m_all_flag ? GIT_BRANCH_ALL : (m_remote_flag ? GIT_BRANCH_REMOTE : GIT_BRANCH_LOCAL);
4444
auto iter = repo.iterate_branches(type);
4545
auto br = iter.next();

src/subcommand/status_subcommand.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ void status_subcommand::run()
167167
auto directory = get_current_git_path();
168168
auto repo = repository_wrapper::open(directory);
169169
auto sl = status_list_wrapper::status_list(repo);
170-
auto branch_name = repo.head().short_name();
170+
auto branch_name = repo.head_short_name();
171171

172172
std::set<std::string> tracked_dir_set{};
173173
std::set<std::string> untracked_dir_set{};
@@ -276,18 +276,4 @@ void status_subcommand::run()
276276
{
277277
std::cout << treeclean_message << std::endl;
278278
}
279-
280-
// if (sl.has_ignored_header())
281-
// {
282-
// stream_colour_fn colour = termcolor::red;
283-
// if (is_long)
284-
// {
285-
// std::cout << ignored_header;
286-
// }
287-
// print_not_tracked(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of), tracked_dir_set, untracked_dir_set, is_long, colour);
288-
// if (is_long)
289-
// {
290-
// std::cout << std::endl;
291-
// }
292-
// }
293279
}

src/wrapper/repository_wrapper.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ bool repository_wrapper::is_bare() const
5151
return git_repository_is_bare(*this);
5252
}
5353

54-
// References
54+
// Head
55+
56+
bool repository_wrapper::is_head_unborn() const
57+
{
58+
return git_repository_head_unborn(*this) == 0;
59+
}
5560

5661
reference_wrapper repository_wrapper::head() const
5762
{
@@ -60,6 +65,26 @@ reference_wrapper repository_wrapper::head() const
6065
return reference_wrapper(ref);
6166
}
6267

68+
std::string repository_wrapper::head_short_name() const
69+
{
70+
git_reference* ref;
71+
std::string name;
72+
throw_if_error(git_reference_lookup(&ref, *this, "HEAD"));
73+
if (git_reference_type(ref) == GIT_REFERENCE_DIRECT)
74+
{
75+
name = git_reference_shorthand(ref);
76+
}
77+
else
78+
{
79+
name = git_reference_symbolic_target(ref);
80+
name = name.substr(name.find_last_of('/') + 1);
81+
}
82+
git_reference_free(ref);
83+
return name;
84+
}
85+
86+
// References
87+
6388
reference_wrapper repository_wrapper::find_reference(std::string_view ref_name) const
6489
{
6590
git_reference* ref;

src/wrapper/repository_wrapper.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ class repository_wrapper : public wrapper_base<git_repository>
3535

3636
bool is_bare() const;
3737

38-
// References
38+
// Head
39+
bool is_head_unborn() const;
3940
reference_wrapper head() const;
41+
std::string head_short_name() const;
42+
43+
// References
4044
reference_wrapper find_reference(std::string_view ref_name) const;
4145
std::optional<reference_wrapper> find_reference_dwim(std::string_view ref_name) const;
4246

test/test_branch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ def test_branch_nogit(git2cpp_path, tmp_path):
3838
cmd = [git2cpp_path, 'branch']
3939
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
4040
assert p.returncode != 0
41+
42+
def tets_branch_new_repo(git2cpp_path, tmp_path, run_in_tmp_path):
43+
# tmp_path exists and is empty.
44+
assert list(tmp_path.iterdir()) == []
45+
46+
cmd = [git2cpp_path, 'init']
47+
p = subprocess.run(cmd, cwd = tmp_path)
48+
49+
branch_cmd = [git2cpp_path, 'branch']
50+
p_branch = subprocess.run(branch_cmd, cwd = tmp_path)
51+
52+
assert p_branch.returncode == 0

test/test_status.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,15 @@ def test_status_add_file(xtl_clone, git2cpp_path, tmp_path, short_flag, long_fla
7676
elif short_flag in ["-s", "--short"]:
7777
assert "A " in p_status.stdout
7878
assert "D " in p_status.stdout
79+
80+
def test_status_new_repo(git2cpp_path, tmp_path, run_in_tmp_path):
81+
# tmp_path exists and is empty.
82+
assert list(tmp_path.iterdir()) == []
83+
84+
cmd = [git2cpp_path, 'init']
85+
p = subprocess.run(cmd, cwd = tmp_path)
86+
87+
status_cmd = [git2cpp_path, 'status']
88+
p_status = subprocess.run(status_cmd, cwd = tmp_path)
89+
90+
assert p_status.returncode == 0

0 commit comments

Comments
 (0)