@@ -21,6 +21,7 @@ class Git:
2121 submodule : "GitSubmoduleCmd"
2222 remote : "GitRemoteCmd"
2323 stash : "GitStashCmd"
24+ branch : "GitBranchCmd"
2425
2526 def __init__ (
2627 self ,
@@ -82,6 +83,7 @@ def __init__(
8283 self .submodule = GitSubmoduleCmd (path = self .path , cmd = self )
8384 self .remote = GitRemoteCmd (path = self .path , cmd = self )
8485 self .stash = GitStashCmd (path = self .path , cmd = self )
86+ self .branch = GitBranchCmd (path = self .path , cmd = self )
8587
8688 def __repr__ (self ) -> str :
8789 """Representation of Git repo command object."""
@@ -2952,3 +2954,83 @@ def save(
29522954 check_returncode = check_returncode ,
29532955 log_in_real_time = log_in_real_time ,
29542956 )
2957+
2958+
2959+ GitBranchCommandLiteral = Literal [
2960+ "create" , # checkout -b
2961+ "move" ,
2962+ "copy" ,
2963+ "delete" ,
2964+ ]
2965+
2966+
2967+ class GitBranchCmd :
2968+ """Run commands directly against a git branch for a git repo."""
2969+
2970+ def __init__ (self , * , path : StrPath , cmd : Optional [Git ] = None ) -> None :
2971+ """Lite, typed, pythonic wrapper for git-branch(1).
2972+
2973+ Parameters
2974+ ----------
2975+ path :
2976+ Operates as PATH in the corresponding git subcommand.
2977+
2978+ Examples
2979+ --------
2980+ >>> GitBranchCmd(path=tmp_path)
2981+ <GitBranchCmd path=...>
2982+
2983+ >>> GitBranchCmd(path=tmp_path).run(quiet=True)
2984+ 'fatal: not a git repository (or any of the parent directories): .git'
2985+
2986+ >>> GitBranchCmd(path=git_local_clone.path).run(quiet=True)
2987+ ''
2988+ """
2989+ #: Directory to check out
2990+ self .path : pathlib .Path
2991+ if isinstance (path , pathlib .Path ):
2992+ self .path = path
2993+ else :
2994+ self .path = pathlib .Path (path )
2995+
2996+ self .cmd = cmd if isinstance (cmd , Git ) else Git (path = self .path )
2997+
2998+ def __repr__ (self ) -> str :
2999+ """Representation of git branch storage command object."""
3000+ return f"<GitBranchCmd path={ self .path } >"
3001+
3002+ def run (
3003+ self ,
3004+ command : Optional [GitBranchCommandLiteral ] = None ,
3005+ local_flags : Optional [list [str ]] = None ,
3006+ * ,
3007+ quiet : Optional [bool ] = None ,
3008+ cached : Optional [bool ] = None , # Only when no command entered and status
3009+ # Pass-through to run()
3010+ log_in_real_time : bool = False ,
3011+ check_returncode : Optional [bool ] = None ,
3012+ ** kwargs : Any ,
3013+ ) -> str :
3014+ """Run a command against a git repository's branch storage.
3015+
3016+ Wraps `git branch <https://git-scm.com/docs/git-branch>`_.
3017+
3018+ Examples
3019+ --------
3020+ >>> GitBranchCmd(path=git_local_clone.path).run()
3021+ '* master'
3022+ """
3023+ local_flags = local_flags if isinstance (local_flags , list ) else []
3024+ if command is not None :
3025+ local_flags .insert (0 , command )
3026+
3027+ if quiet is True :
3028+ local_flags .append ("--quiet" )
3029+ if cached is True :
3030+ local_flags .append ("--cached" )
3031+
3032+ return self .cmd .run (
3033+ ["branch" , * local_flags ],
3034+ check_returncode = check_returncode ,
3035+ log_in_real_time = log_in_real_time ,
3036+ )
0 commit comments