Skip to content

Commit 537fa86

Browse files
committed
cmd/git(feat[GitBranchCmd]): add delete, rename, copy, upstream methods
why: Complete GitBranchCmd per-entity operations what: - Add delete() method with force flag (-d/-D) - Add rename() method with force flag (-m/-M) - Add copy() method with force flag (-c/-C) - Add set_upstream() for --set-upstream-to - Add unset_upstream() for --unset-upstream
1 parent 44a8b41 commit 537fa86

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

src/libvcs/cmd/git.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3586,6 +3586,154 @@ def create(self) -> str:
35863586
check_returncode=False,
35873587
)
35883588

3589+
def delete(
3590+
self,
3591+
*,
3592+
force: bool = False,
3593+
# Pass-through to run()
3594+
log_in_real_time: bool = False,
3595+
check_returncode: bool | None = None,
3596+
) -> str:
3597+
"""Delete this git branch.
3598+
3599+
Parameters
3600+
----------
3601+
force :
3602+
Use ``-D`` instead of ``-d`` to force deletion.
3603+
3604+
Examples
3605+
--------
3606+
>>> GitBranchCmd(
3607+
... path=example_git_repo.path,
3608+
... branch_name='nonexistent'
3609+
... ).delete()
3610+
"error: branch 'nonexistent' not found"
3611+
"""
3612+
flag = "-D" if force else "-d"
3613+
return self.cmd.run(
3614+
["branch", flag, self.branch_name],
3615+
check_returncode=check_returncode,
3616+
log_in_real_time=log_in_real_time,
3617+
)
3618+
3619+
def rename(
3620+
self,
3621+
new_name: str,
3622+
*,
3623+
force: bool = False,
3624+
# Pass-through to run()
3625+
log_in_real_time: bool = False,
3626+
check_returncode: bool | None = None,
3627+
) -> str:
3628+
"""Rename this git branch.
3629+
3630+
Parameters
3631+
----------
3632+
new_name :
3633+
New name for the branch.
3634+
force :
3635+
Use ``-M`` instead of ``-m`` to force rename.
3636+
3637+
Examples
3638+
--------
3639+
>>> GitBranchCmd(
3640+
... path=example_git_repo.path,
3641+
... branch_name='master'
3642+
... ).rename('main')
3643+
''
3644+
"""
3645+
flag = "-M" if force else "-m"
3646+
return self.cmd.run(
3647+
["branch", flag, self.branch_name, new_name],
3648+
check_returncode=check_returncode,
3649+
log_in_real_time=log_in_real_time,
3650+
)
3651+
3652+
def copy(
3653+
self,
3654+
new_name: str,
3655+
*,
3656+
force: bool = False,
3657+
# Pass-through to run()
3658+
log_in_real_time: bool = False,
3659+
check_returncode: bool | None = None,
3660+
) -> str:
3661+
"""Copy this git branch.
3662+
3663+
Parameters
3664+
----------
3665+
new_name :
3666+
Name for the copied branch.
3667+
force :
3668+
Use ``-C`` instead of ``-c`` to force copy.
3669+
3670+
Examples
3671+
--------
3672+
>>> GitBranchCmd(
3673+
... path=example_git_repo.path,
3674+
... branch_name='master'
3675+
... ).copy('master-copy')
3676+
''
3677+
"""
3678+
flag = "-C" if force else "-c"
3679+
return self.cmd.run(
3680+
["branch", flag, self.branch_name, new_name],
3681+
check_returncode=check_returncode,
3682+
log_in_real_time=log_in_real_time,
3683+
)
3684+
3685+
def set_upstream(
3686+
self,
3687+
upstream: str,
3688+
*,
3689+
# Pass-through to run()
3690+
log_in_real_time: bool = False,
3691+
check_returncode: bool | None = None,
3692+
) -> str:
3693+
"""Set the upstream (tracking) branch.
3694+
3695+
Parameters
3696+
----------
3697+
upstream :
3698+
The upstream branch in format ``remote/branch`` (e.g., ``origin/main``).
3699+
3700+
Examples
3701+
--------
3702+
>>> GitBranchCmd(
3703+
... path=example_git_repo.path,
3704+
... branch_name='master'
3705+
... ).set_upstream('origin/master')
3706+
"branch 'master' set up to track 'origin/master'."
3707+
"""
3708+
return self.cmd.run(
3709+
["branch", f"--set-upstream-to={upstream}", self.branch_name],
3710+
check_returncode=check_returncode,
3711+
log_in_real_time=log_in_real_time,
3712+
)
3713+
3714+
def unset_upstream(
3715+
self,
3716+
*,
3717+
# Pass-through to run()
3718+
log_in_real_time: bool = False,
3719+
check_returncode: bool | None = None,
3720+
) -> str:
3721+
"""Remove the upstream (tracking) information.
3722+
3723+
Examples
3724+
--------
3725+
>>> GitBranchCmd(
3726+
... path=example_git_repo.path,
3727+
... branch_name='master'
3728+
... ).unset_upstream()
3729+
''
3730+
"""
3731+
return self.cmd.run(
3732+
["branch", "--unset-upstream", self.branch_name],
3733+
check_returncode=check_returncode,
3734+
log_in_real_time=log_in_real_time,
3735+
)
3736+
35893737

35903738
class GitBranchManager:
35913739
"""Run commands directly related to git branches of a git repo."""

0 commit comments

Comments
 (0)