Skip to content

Commit 4b8a0f7

Browse files
committed
cmd/git(fix[create]): Use git branch instead of checkout -b for branch creation
why: git branch should create a branch without switching HEAD, but the implementation used checkout -b which always switches. This violated git-branch semantics and surprised callers expecting create-only behavior. what: - Change create() to use git branch instead of checkout -b - Add checkout parameter to opt-in to switching after creation
1 parent 62c48c6 commit 4b8a0f7

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

src/libvcs/cmd/git.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5385,9 +5385,19 @@ def checkout(self) -> str:
53855385
],
53865386
)
53875387

5388-
def create(self) -> str:
5388+
def create(
5389+
self,
5390+
*,
5391+
checkout: bool = False,
5392+
) -> str:
53895393
"""Create a git branch.
53905394
5395+
Parameters
5396+
----------
5397+
checkout :
5398+
If True, also checkout the newly created branch.
5399+
Defaults to False (create only, don't switch HEAD).
5400+
53915401
Examples
53925402
--------
53935403
>>> GitBranchCmd(
@@ -5396,14 +5406,13 @@ def create(self) -> str:
53965406
... ).create()
53975407
"fatal: a branch named 'master' already exists"
53985408
"""
5399-
return self.cmd.run(
5400-
[
5401-
"checkout",
5402-
*["-b", self.branch_name],
5403-
],
5404-
# Pass-through to run()
5409+
result = self.cmd.run(
5410+
["branch", self.branch_name],
54055411
check_returncode=False,
54065412
)
5413+
if checkout and "fatal" not in result.lower():
5414+
self.cmd.run(["checkout", self.branch_name])
5415+
return result
54075416

54085417
def delete(
54095418
self,
@@ -5682,22 +5691,34 @@ def checkout(self, *, branch: str) -> str:
56825691
],
56835692
)
56845693

5685-
def create(self, *, branch: str) -> str:
5694+
def create(
5695+
self,
5696+
*,
5697+
branch: str,
5698+
checkout: bool = False,
5699+
) -> str:
56865700
"""Create a git branch.
56875701
5702+
Parameters
5703+
----------
5704+
branch :
5705+
Name of the branch to create.
5706+
checkout :
5707+
If True, also checkout the newly created branch.
5708+
Defaults to False (create only, don't switch HEAD).
5709+
56885710
Examples
56895711
--------
56905712
>>> GitBranchManager(path=example_git_repo.path).create(branch='master')
56915713
"fatal: a branch named 'master' already exists"
56925714
"""
5693-
return self.cmd.run(
5694-
[
5695-
"checkout",
5696-
*["-b", branch],
5697-
],
5698-
# Pass-through to run()
5715+
result = self.cmd.run(
5716+
["branch", branch],
56995717
check_returncode=False,
57005718
)
5719+
if checkout and "fatal" not in result.lower():
5720+
self.cmd.run(["checkout", branch])
5721+
return result
57015722

57025723
def _ls(
57035724
self,

0 commit comments

Comments
 (0)