Skip to content

Commit 88708e6

Browse files
committed
Small fix in log and status
1 parent 70ee6b9 commit 88708e6

File tree

4 files changed

+74
-40
lines changed

4 files changed

+74
-40
lines changed

src/subcommand/log_subcommand.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ void log_subcommand::run()
8787
auto repo = repository_wrapper::open(directory);
8888
// auto branch_name = repo.head().short_name();
8989

90+
if (repo.is_head_unborn())
91+
{
92+
std::cout << "fatal: your current branch 'main' does not have any commits yet" << std::endl;
93+
return;
94+
}
95+
9096
revwalk_wrapper walker = repo.new_walker();
9197
walker.push_head();
9298

src/subcommand/status_subcommand.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ void status_subcommand::run()
194194
{
195195
std::cout << "On branch " << branch_name << "\n" << std::endl;
196196

197+
if (repo.is_head_unborn())
198+
{
199+
std::cout << "No commits yet\n" << std::endl;
200+
}
201+
197202
if (sl.has_unmerged_header())
198203
{
199204
std::cout << "You have unmerged paths.\n (fix conflicts and run \"git commit\")\n (use \"git merge --abort\" to abort the merge)\n" << std::endl;
@@ -274,6 +279,9 @@ void status_subcommand::run()
274279
// TODO: check if this message should be displayed even if there are untracked files
275280
if (!(sl.has_tobecommited_header() | sl.has_notstagged_header() | sl.has_unmerged_header() | sl.has_untracked_header()))
276281
{
277-
std::cout << treeclean_message << std::endl;
282+
if (is_long)
283+
{
284+
std::cout << treeclean_message << std::endl;
285+
}
278286
}
279287
}

test/test_init.py

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
from pathlib import Path
21
import subprocess
2+
from pathlib import Path
33

44

55
def test_init_in_directory(git2cpp_path, tmp_path):
66
# tmp_path exists and is empty.
77
assert list(tmp_path.iterdir()) == []
88

9-
cmd = [git2cpp_path, 'init', '--bare', str(tmp_path)]
9+
cmd = [git2cpp_path, "init", "--bare", str(tmp_path)]
1010
p = subprocess.run(cmd, capture_output=True)
1111
assert p.returncode == 0
12-
assert p.stdout == b''
13-
assert p.stderr == b''
12+
assert p.stdout == b""
13+
assert p.stderr == b""
1414

1515
assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [
16-
'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs'
16+
"HEAD",
17+
"config",
18+
"description",
19+
"hooks",
20+
"info",
21+
"objects",
22+
"refs",
1723
]
1824

1925
# TODO: check this is a valid git repo
@@ -24,14 +30,20 @@ def test_init_in_cwd(git2cpp_path, tmp_path, run_in_tmp_path):
2430
assert list(tmp_path.iterdir()) == []
2531
assert Path.cwd() == tmp_path
2632

27-
cmd = [git2cpp_path, 'init', '--bare']
33+
cmd = [git2cpp_path, "init", "--bare"]
2834
p = subprocess.run(cmd, capture_output=True)
2935
assert p.returncode == 0
30-
assert p.stdout == b''
31-
assert p.stderr == b''
36+
assert p.stdout == b""
37+
assert p.stderr == b""
3238

3339
assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [
34-
'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs'
40+
"HEAD",
41+
"config",
42+
"description",
43+
"hooks",
44+
"info",
45+
"objects",
46+
"refs",
3547
]
3648

3749
# TODO: check this is a valid git repo
@@ -41,38 +53,43 @@ def test_init_not_bare(git2cpp_path, tmp_path):
4153
# tmp_path exists and is empty.
4254
assert list(tmp_path.iterdir()) == []
4355

44-
cmd = [git2cpp_path, 'init', '.']
56+
cmd = [git2cpp_path, "init", "."]
4557
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path)
4658
assert p.returncode == 0
47-
assert p.stdout == b''
48-
assert p.stderr == b''
59+
assert p.stdout == b""
60+
assert p.stderr == b""
4961

50-
# Directory contains just .git directory.
51-
assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == ['.git']
62+
# Directory contains just .git directory.
63+
assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [".git"]
5264
# .git directory is a valid repo.
53-
assert sorted(map(lambda path: path.name, (tmp_path / '.git').iterdir())) == [
54-
'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs'
65+
assert sorted(map(lambda path: path.name, (tmp_path / ".git").iterdir())) == [
66+
"HEAD",
67+
"config",
68+
"description",
69+
"hooks",
70+
"info",
71+
"objects",
72+
"refs",
5573
]
5674

5775
# Would like to use `git2cpp status` but it complains that 'refs/heads/master' not found
58-
cmd = [git2cpp_path, 'log']
76+
cmd = [git2cpp_path, "log"]
5977
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path)
6078
assert p.returncode == 0
61-
assert p.stdout == b''
62-
assert p.stderr == b''
79+
assert b"does not have any commits yet" in p.stdout
6380

6481

6582
def test_error_on_unknown_option(git2cpp_path):
66-
cmd = [git2cpp_path, 'init', '--unknown']
83+
cmd = [git2cpp_path, "init", "--unknown"]
6784
p = subprocess.run(cmd, capture_output=True)
6885
assert p.returncode == 109
69-
assert p.stdout == b''
86+
assert p.stdout == b""
7087
assert p.stderr.startswith(b"The following argument was not expected: --unknown")
7188

7289

7390
def test_error_on_repeated_directory(git2cpp_path):
74-
cmd = [git2cpp_path, 'init', 'abc', 'def']
91+
cmd = [git2cpp_path, "init", "abc", "def"]
7592
p = subprocess.run(cmd, capture_output=True)
7693
assert p.returncode == 109
77-
assert p.stdout == b''
94+
assert p.stdout == b""
7895
assert p.stderr.startswith(b"The following argument was not expected: def")

test/test_status.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ def test_status_new_file(xtl_clone, git2cpp_path, tmp_path, short_flag, long_fla
1111
assert (tmp_path / "xtl").exists()
1212
xtl_path = tmp_path / "xtl"
1313

14-
p = xtl_path / "mook_file.txt" # Untracked files
15-
p.write_text('')
14+
p = xtl_path / "mook_file.txt" # Untracked files
15+
p.write_text("")
1616

17-
pw = xtl_path / "CMakeLists.txt" # Changes not staged for commit / modified
17+
pw = xtl_path / "CMakeLists.txt" # Changes not staged for commit / modified
1818
pw.write_text("blablabla")
1919

20-
os.remove(xtl_path / "README.md") # Changes not staged for commit / deleted
20+
os.remove(xtl_path / "README.md") # Changes not staged for commit / deleted
2121

22-
cmd = [git2cpp_path, 'status']
22+
cmd = [git2cpp_path, "status"]
2323
if short_flag != "":
2424
cmd.append(short_flag)
2525
if long_flag != "":
@@ -38,27 +38,29 @@ def test_status_new_file(xtl_clone, git2cpp_path, tmp_path, short_flag, long_fla
3838
assert " D " in p.stdout
3939
assert "?? " in p.stdout
4040

41+
4142
def test_status_nogit(git2cpp_path, tmp_path):
42-
cmd = [git2cpp_path, 'status']
43+
cmd = [git2cpp_path, "status"]
4344
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
4445
assert p.returncode != 0
4546

47+
4648
@pytest.mark.parametrize("short_flag", ["", "-s", "--short"])
4749
@pytest.mark.parametrize("long_flag", ["", "--long"])
4850
def test_status_add_file(xtl_clone, git2cpp_path, tmp_path, short_flag, long_flag):
4951
assert (tmp_path / "xtl").exists()
5052
xtl_path = tmp_path / "xtl"
5153

52-
p = xtl_path / "mook_file.txt" # Changes to be committed / new file
53-
p.write_text('')
54+
p = xtl_path / "mook_file.txt" # Changes to be committed / new file
55+
p.write_text("")
5456

55-
os.remove(xtl_path / "README.md") # Changes to be committed / deleted
57+
os.remove(xtl_path / "README.md") # Changes to be committed / deleted
5658

57-
cmd_add = [git2cpp_path, 'add', "--all"]
59+
cmd_add = [git2cpp_path, "add", "--all"]
5860
p_add = subprocess.run(cmd_add, cwd=xtl_path, text=True)
5961
assert p_add.returncode == 0
6062

61-
cmd_status = [git2cpp_path, 'status']
63+
cmd_status = [git2cpp_path, "status"]
6264
if short_flag != "":
6365
cmd_status.append(short_flag)
6466
if long_flag != "":
@@ -77,14 +79,15 @@ def test_status_add_file(xtl_clone, git2cpp_path, tmp_path, short_flag, long_fla
7779
assert "A " in p_status.stdout
7880
assert "D " in p_status.stdout
7981

82+
8083
def test_status_new_repo(git2cpp_path, tmp_path, run_in_tmp_path):
8184
# tmp_path exists and is empty.
8285
assert list(tmp_path.iterdir()) == []
8386

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)
87+
cmd = [git2cpp_path, "init"]
88+
p = subprocess.run(cmd, cwd=tmp_path)
89+
assert p.returncode == 0
8990

91+
status_cmd = [git2cpp_path, "status"]
92+
p_status = subprocess.run(status_cmd, cwd=tmp_path)
9093
assert p_status.returncode == 0

0 commit comments

Comments
 (0)