@@ -21,7 +21,7 @@ class Git:
2121 submodule : "GitSubmoduleCmd"
2222 remote : "GitRemoteCmd"
2323 stash : "GitStashCmd"
24- branch : "GitBranchCmd "
24+ branch : "GitBranchManager "
2525
2626 def __init__ (
2727 self ,
@@ -83,7 +83,7 @@ def __init__(
8383 self .submodule = GitSubmoduleCmd (path = self .path , cmd = self )
8484 self .remote = GitRemoteCmd (path = self .path , cmd = self )
8585 self .stash = GitStashCmd (path = self .path , cmd = self )
86- self .branch = GitBranchCmd (path = self .path , cmd = self )
86+ self .branch = GitBranchManager (path = self .path , cmd = self )
8787
8888 def __repr__ (self ) -> str :
8989 """Representation of Git repo command object."""
@@ -2974,7 +2974,15 @@ def save(
29742974class GitBranchCmd :
29752975 """Run commands directly against a git branch for a git repo."""
29762976
2977- def __init__ (self , * , path : StrPath , cmd : Optional [Git ] = None ) -> None :
2977+ branch_name : str
2978+
2979+ def __init__ (
2980+ self ,
2981+ * ,
2982+ path : StrPath ,
2983+ branch_name : str ,
2984+ cmd : Optional [Git ] = None ,
2985+ ) -> None :
29782986 """Lite, typed, pythonic wrapper for git-branch(1).
29792987
29802988 Parameters
@@ -2984,13 +2992,128 @@ def __init__(self, *, path: StrPath, cmd: Optional[Git] = None) -> None:
29842992
29852993 Examples
29862994 --------
2987- >>> GitBranchCmd(path=tmp_path)
2988- <GitBranchCmd path=...>
2995+ >>> GitBranchCmd(path=tmp_path, branch_name='master')
2996+ <GitBranchCmd path=... branch_name=master>
2997+
2998+ >>> GitBranchCmd(path=tmp_path, branch_name="master").run(quiet=True)
2999+ 'fatal: not a git repository (or any of the parent directories): .git'
3000+
3001+ >>> GitBranchCmd(
3002+ ... path=git_local_clone.path, branch_name="master").run(quiet=True)
3003+ '* master'
3004+ """
3005+ #: Directory to check out
3006+ self .path : pathlib .Path
3007+ if isinstance (path , pathlib .Path ):
3008+ self .path = path
3009+ else :
3010+ self .path = pathlib .Path (path )
3011+
3012+ self .cmd = cmd if isinstance (cmd , Git ) else Git (path = self .path )
3013+
3014+ self .branch_name = branch_name
3015+
3016+ def __repr__ (self ) -> str :
3017+ """Representation of git branch command object."""
3018+ return f"<GitBranchCmd path={ self .path } branch_name={ self .branch_name } >"
3019+
3020+ def run (
3021+ self ,
3022+ command : Optional [GitBranchCommandLiteral ] = None ,
3023+ local_flags : Optional [list [str ]] = None ,
3024+ * ,
3025+ quiet : Optional [bool ] = None ,
3026+ cached : Optional [bool ] = None , # Only when no command entered and status
3027+ # Pass-through to run()
3028+ log_in_real_time : bool = False ,
3029+ check_returncode : Optional [bool ] = None ,
3030+ ** kwargs : Any ,
3031+ ) -> str :
3032+ """Run a command against a git repository's branch.
3033+
3034+ Wraps `git branch <https://git-scm.com/docs/git-branch>`_.
3035+
3036+ Examples
3037+ --------
3038+ >>> GitBranchCmd(path=git_local_clone.path, branch_name='master').run()
3039+ '* master'
3040+ """
3041+ local_flags = local_flags if isinstance (local_flags , list ) else []
3042+ if command is not None :
3043+ local_flags .insert (0 , command )
3044+
3045+ if quiet is True :
3046+ local_flags .append ("--quiet" )
3047+ if cached is True :
3048+ local_flags .append ("--cached" )
3049+
3050+ return self .cmd .run (
3051+ ["branch" , * local_flags ],
3052+ check_returncode = check_returncode ,
3053+ log_in_real_time = log_in_real_time ,
3054+ )
3055+
3056+ def checkout (self ) -> str :
3057+ """Git branch checkout.
3058+
3059+ Examples
3060+ --------
3061+ >>> GitBranchCmd(path=git_local_clone.path, branch_name='master').checkout()
3062+ "Your branch is up to date with 'origin/master'."
3063+ """
3064+ return self .cmd .run (
3065+ [
3066+ "checkout" ,
3067+ * [self .branch_name ],
3068+ ],
3069+ )
3070+
3071+ def create (self ) -> str :
3072+ """Create a git branch.
3073+
3074+ Examples
3075+ --------
3076+ >>> GitBranchCmd(path=git_local_clone.path, branch_name='master').create()
3077+ "fatal: a branch named 'master' already exists"
3078+ """
3079+ return self .cmd .run (
3080+ [
3081+ "checkout" ,
3082+ * ["-b" , self .branch_name ],
3083+ ],
3084+ # Pass-through to run()
3085+ check_returncode = False ,
3086+ )
3087+
3088+
3089+ class GitBranchManager :
3090+ """Run commands directly related to git branches of a git repo."""
3091+
3092+ branch_name : str
3093+
3094+ def __init__ (
3095+ self ,
3096+ * ,
3097+ path : StrPath ,
3098+ cmd : Optional [Git ] = None ,
3099+ ) -> None :
3100+ """Wrap some of git-branch(1), git-checkout(1), manager.
3101+
3102+ Parameters
3103+ ----------
3104+ path :
3105+ Operates as PATH in the corresponding git subcommand.
3106+
3107+ Examples
3108+ --------
3109+ >>> GitBranchManager(path=tmp_path)
3110+ <GitBranchManager path=...>
29893111
2990- >>> GitBranchCmd (path=tmp_path).run(quiet=True)
3112+ >>> GitBranchManager (path=tmp_path).run(quiet=True)
29913113 'fatal: not a git repository (or any of the parent directories): .git'
29923114
2993- >>> GitBranchCmd(path=git_local_clone.path).run(quiet=True)
3115+ >>> GitBranchManager(
3116+ ... path=git_local_clone.path).run(quiet=True)
29943117 '* master'
29953118 """
29963119 #: Directory to check out
@@ -3003,8 +3126,8 @@ def __init__(self, *, path: StrPath, cmd: Optional[Git] = None) -> None:
30033126 self .cmd = cmd if isinstance (cmd , Git ) else Git (path = self .path )
30043127
30053128 def __repr__ (self ) -> str :
3006- """Representation of git branch storage command object."""
3007- return f"<GitBranchCmd path={ self .path } >"
3129+ """Representation of git branch manager object."""
3130+ return f"<GitBranchManager path={ self .path } >"
30083131
30093132 def run (
30103133 self ,
@@ -3018,13 +3141,13 @@ def run(
30183141 check_returncode : Optional [bool ] = None ,
30193142 ** kwargs : Any ,
30203143 ) -> str :
3021- """Run a command against a git repository's branch storage .
3144+ """Run a command against a git repository's branches .
30223145
30233146 Wraps `git branch <https://git-scm.com/docs/git-branch>`_.
30243147
30253148 Examples
30263149 --------
3027- >>> GitBranchCmd (path=git_local_clone.path).run()
3150+ >>> GitBranchManager (path=git_local_clone.path).run()
30283151 '* master'
30293152 """
30303153 local_flags = local_flags if isinstance (local_flags , list ) else []
@@ -3047,7 +3170,7 @@ def checkout(self, *, branch: str) -> str:
30473170
30483171 Examples
30493172 --------
3050- >>> GitBranchCmd (path=git_local_clone.path).checkout(branch='master')
3173+ >>> GitBranchManager (path=git_local_clone.path).checkout(branch='master')
30513174 "Your branch is up to date with 'origin/master'."
30523175 """
30533176 return self .cmd .run (
@@ -3062,7 +3185,7 @@ def create(self, *, branch: str) -> str:
30623185
30633186 Examples
30643187 --------
3065- >>> GitBranchCmd (path=git_local_clone.path).create(branch='master')
3188+ >>> GitBranchManager (path=git_local_clone.path).create(branch='master')
30663189 "fatal: a branch named 'master' already exists"
30673190 """
30683191 return self .cmd .run (
@@ -3079,7 +3202,7 @@ def ls(self) -> str:
30793202
30803203 Examples
30813204 --------
3082- >>> GitBranchCmd (path=git_local_clone.path).ls()
3205+ >>> GitBranchManager (path=git_local_clone.path).ls()
30833206 '* master'
30843207 """
30853208 return self .run (
0 commit comments