@@ -9,7 +9,7 @@ def test_branch_list(repo_init_with_commit, git2cpp_path, tmp_path):
99 cmd = [git2cpp_path , "branch" ]
1010 p = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
1111 assert p .returncode == 0
12- assert "* ma " in p .stdout
12+ assert "* main " in p .stdout
1313
1414
1515def test_branch_create_delete (repo_init_with_commit , git2cpp_path , tmp_path ):
@@ -22,15 +22,15 @@ def test_branch_create_delete(repo_init_with_commit, git2cpp_path, tmp_path):
2222 list_cmd = [git2cpp_path , "branch" ]
2323 p_list = subprocess .run (list_cmd , capture_output = True , cwd = tmp_path , text = True )
2424 assert p_list .returncode == 0
25- assert " foregone\n * ma " in p_list .stdout
25+ assert " foregone\n * main " in p_list .stdout
2626
2727 del_cmd = [git2cpp_path , "branch" , "-d" , "foregone" ]
2828 p_del = subprocess .run (del_cmd , capture_output = True , cwd = tmp_path , text = True )
2929 assert p_del .returncode == 0
3030
3131 p_list2 = subprocess .run (list_cmd , capture_output = True , cwd = tmp_path , text = True )
3232 assert p_list2 .returncode == 0
33- assert "* ma " in p_list2 .stdout
33+ assert "* main " in p_list2 .stdout
3434
3535
3636def test_branch_nogit (git2cpp_path , tmp_path ):
@@ -51,3 +51,174 @@ def test_branch_new_repo(git2cpp_path, tmp_path, run_in_tmp_path):
5151 p_branch = subprocess .run (branch_cmd , cwd = tmp_path )
5252
5353 assert p_branch .returncode == 0
54+
55+
56+ def test_branch_list_flag (repo_init_with_commit , git2cpp_path , tmp_path ):
57+ """Explicit -l/--list flag behaves the same as bare 'branch'."""
58+ assert (tmp_path / "initial.txt" ).exists ()
59+
60+ subprocess .run ([git2cpp_path , "branch" , "feature-a" ], cwd = tmp_path , check = True )
61+
62+ for flag in ["-l" , "--list" ]:
63+ cmd = [git2cpp_path , "branch" , flag ]
64+ p = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
65+ assert p .returncode == 0
66+ assert " feature-a" in p .stdout
67+ assert "* main" in p .stdout
68+
69+
70+ def test_branch_list_all (xtl_clone , git2cpp_path , tmp_path ):
71+ """The -a/--all flag lists both local and remote-tracking branches."""
72+ xtl_path = tmp_path / "xtl"
73+
74+ cmd = [git2cpp_path , "branch" , "-a" ]
75+ p = subprocess .run (cmd , capture_output = True , cwd = xtl_path , text = True )
76+ assert p .returncode == 0
77+ assert "* master" in p .stdout
78+ assert "origin/" in p .stdout
79+
80+
81+ def test_branch_list_remotes (xtl_clone , git2cpp_path , tmp_path ):
82+ """The -r/--remotes flag lists only remote-tracking branches."""
83+ xtl_path = tmp_path / "xtl"
84+
85+ cmd = [git2cpp_path , "branch" , "-r" ]
86+ p = subprocess .run (cmd , capture_output = True , cwd = xtl_path , text = True )
87+ assert p .returncode == 0
88+ assert "origin/" in p .stdout
89+ # Local branch should NOT appear with * prefix
90+ assert "* master" not in p .stdout
91+
92+
93+ def test_branch_create_already_exists (repo_init_with_commit , git2cpp_path , tmp_path ):
94+ """Creating a branch that already exists should fail without --force."""
95+ assert (tmp_path / "initial.txt" ).exists ()
96+
97+ subprocess .run ([git2cpp_path , "branch" , "duplicate" ], cwd = tmp_path , check = True )
98+
99+ cmd = [git2cpp_path , "branch" , "duplicate" ]
100+ p = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
101+ assert p .returncode != 0
102+
103+
104+ def test_branch_create_force_overwrite (
105+ repo_init_with_commit , commit_env_config , git2cpp_path , tmp_path
106+ ):
107+ """--force allows overwriting an existing branch."""
108+ assert (tmp_path / "initial.txt" ).exists ()
109+
110+ subprocess .run ([git2cpp_path , "branch" , "my-branch" ], cwd = tmp_path , check = True )
111+
112+ # Add a second commit so HEAD moves forward
113+ (tmp_path / "second.txt" ).write_text ("second" )
114+ subprocess .run ([git2cpp_path , "add" , "second.txt" ], cwd = tmp_path , check = True )
115+ subprocess .run (
116+ [git2cpp_path , "commit" , "-m" , "Second commit" ], cwd = tmp_path , check = True
117+ )
118+
119+ # Without --force this would fail; with -f it should reset the branch to current HEAD
120+ cmd = [git2cpp_path , "branch" , "-f" , "my-branch" ]
121+ p = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
122+ assert p .returncode == 0
123+
124+
125+ def test_branch_delete_nonexistent (repo_init_with_commit , git2cpp_path , tmp_path ):
126+ """Deleting a branch that doesn't exist should fail."""
127+ assert (tmp_path / "initial.txt" ).exists ()
128+
129+ cmd = [git2cpp_path , "branch" , "-d" , "no-such-branch" ]
130+ p = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
131+ assert p .returncode != 0
132+
133+
134+ def test_branch_create_multiple (repo_init_with_commit , git2cpp_path , tmp_path ):
135+ """Creating multiple branches and verifying they all appear in the listing."""
136+ assert (tmp_path / "initial.txt" ).exists ()
137+
138+ branches = ["alpha" , "beta" , "gamma" ]
139+ for name in branches :
140+ p = subprocess .run (
141+ [git2cpp_path , "branch" , name ], capture_output = True , cwd = tmp_path , text = True
142+ )
143+ assert p .returncode == 0
144+
145+ cmd = [git2cpp_path , "branch" ]
146+ p_list = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
147+ assert p_list .returncode == 0
148+ for name in branches :
149+ assert f" { name } " in p_list .stdout
150+ # Current branch is still starred
151+ assert "* main" in p_list .stdout
152+
153+
154+ def test_branch_show_current (repo_init_with_commit , git2cpp_path , tmp_path ):
155+ """--show-current prints the current branch name."""
156+ assert (tmp_path / "initial.txt" ).exists ()
157+
158+ cmd = [git2cpp_path , "branch" , "--show-current" ]
159+ p = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
160+ assert p .returncode == 0
161+ print (p .stdout )
162+ # Default branch after init is "main" or "master" depending on git config
163+ # TODO: update when -b flag is added to the init subcommand
164+ assert p .stdout .strip () in ("main" , "master" )
165+
166+
167+ def test_branch_show_current_after_create_and_switch (
168+ repo_init_with_commit , git2cpp_path , tmp_path
169+ ):
170+ """--show-current reflects the branch we switched to."""
171+ assert (tmp_path / "initial.txt" ).exists ()
172+
173+ subprocess .run (
174+ [git2cpp_path , "checkout" , "-b" , "new-feature" ], cwd = tmp_path , check = True
175+ )
176+
177+ cmd = [git2cpp_path , "branch" , "--show-current" ]
178+ p = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
179+ assert p .returncode == 0
180+ assert p .stdout == "new-feature\n "
181+
182+
183+ def test_branch_show_current_detached_head (
184+ repo_init_with_commit , git2cpp_path , tmp_path
185+ ):
186+ """--show-current prints nothing when HEAD is detached."""
187+ assert (tmp_path / "initial.txt" ).exists ()
188+
189+ result = subprocess .run (
190+ [git2cpp_path , "rev-parse" , "HEAD" ],
191+ capture_output = True ,
192+ cwd = tmp_path ,
193+ text = True ,
194+ check = True ,
195+ )
196+ head_sha = result .stdout .strip ()
197+ subprocess .run ([git2cpp_path , "checkout" , head_sha ], cwd = tmp_path , check = True )
198+
199+ cmd = [git2cpp_path , "branch" , "--show-current" ]
200+ p = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
201+ assert p .returncode == 0
202+ assert p .stdout == ""
203+
204+
205+ def test_branch_show_current_new_repo (git2cpp_path , tmp_path , run_in_tmp_path ):
206+ """--show-current prints the branch name even on a fresh repo with no commits (unborn HEAD)."""
207+ assert list (tmp_path .iterdir ()) == []
208+
209+ subprocess .run ([git2cpp_path , "init" ], cwd = tmp_path , check = True )
210+
211+ cmd = [git2cpp_path , "branch" , "--show-current" ]
212+ p = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
213+ assert p .returncode == 0
214+ # Default branch after init is "main" or "master" depending on git config
215+ # TODO: update when -b flag is added to the init subcommand
216+ assert p .stdout .strip () in ("main" , "master" )
217+
218+
219+ def test_branch_show_current_nogit (git2cpp_path , tmp_path ):
220+ """--show-current fails gracefully outside a git repository."""
221+ cmd = [git2cpp_path , "branch" , "--show-current" ]
222+ p = subprocess .run (cmd , capture_output = True , cwd = tmp_path , text = True )
223+ assert p .returncode != 0
224+ assert "error: could not find repository at" in p .stderr
0 commit comments