@@ -21,7 +21,7 @@ class Git:
21
21
submodule : "GitSubmoduleCmd"
22
22
remote : "GitRemoteCmd"
23
23
stash : "GitStashCmd"
24
- branch : "GitBranchCmd "
24
+ branch : "GitBranchManager "
25
25
26
26
def __init__ (
27
27
self ,
@@ -83,7 +83,7 @@ def __init__(
83
83
self .submodule = GitSubmoduleCmd (path = self .path , cmd = self )
84
84
self .remote = GitRemoteCmd (path = self .path , cmd = self )
85
85
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 )
87
87
88
88
def __repr__ (self ) -> str :
89
89
"""Representation of Git repo command object."""
@@ -2974,7 +2974,15 @@ def save(
2974
2974
class GitBranchCmd :
2975
2975
"""Run commands directly against a git branch for a git repo."""
2976
2976
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 :
2978
2986
"""Lite, typed, pythonic wrapper for git-branch(1).
2979
2987
2980
2988
Parameters
@@ -2984,13 +2992,128 @@ def __init__(self, *, path: StrPath, cmd: Optional[Git] = None) -> None:
2984
2992
2985
2993
Examples
2986
2994
--------
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=...>
2989
3111
2990
- >>> GitBranchCmd (path=tmp_path).run(quiet=True)
3112
+ >>> GitBranchManager (path=tmp_path).run(quiet=True)
2991
3113
'fatal: not a git repository (or any of the parent directories): .git'
2992
3114
2993
- >>> GitBranchCmd(path=git_local_clone.path).run(quiet=True)
3115
+ >>> GitBranchManager(
3116
+ ... path=git_local_clone.path).run(quiet=True)
2994
3117
'* master'
2995
3118
"""
2996
3119
#: Directory to check out
@@ -3003,8 +3126,8 @@ def __init__(self, *, path: StrPath, cmd: Optional[Git] = None) -> None:
3003
3126
self .cmd = cmd if isinstance (cmd , Git ) else Git (path = self .path )
3004
3127
3005
3128
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 } >"
3008
3131
3009
3132
def run (
3010
3133
self ,
@@ -3018,13 +3141,13 @@ def run(
3018
3141
check_returncode : Optional [bool ] = None ,
3019
3142
** kwargs : Any ,
3020
3143
) -> str :
3021
- """Run a command against a git repository's branch storage .
3144
+ """Run a command against a git repository's branches .
3022
3145
3023
3146
Wraps `git branch <https://git-scm.com/docs/git-branch>`_.
3024
3147
3025
3148
Examples
3026
3149
--------
3027
- >>> GitBranchCmd (path=git_local_clone.path).run()
3150
+ >>> GitBranchManager (path=git_local_clone.path).run()
3028
3151
'* master'
3029
3152
"""
3030
3153
local_flags = local_flags if isinstance (local_flags , list ) else []
@@ -3047,7 +3170,7 @@ def checkout(self, *, branch: str) -> str:
3047
3170
3048
3171
Examples
3049
3172
--------
3050
- >>> GitBranchCmd (path=git_local_clone.path).checkout(branch='master')
3173
+ >>> GitBranchManager (path=git_local_clone.path).checkout(branch='master')
3051
3174
"Your branch is up to date with 'origin/master'."
3052
3175
"""
3053
3176
return self .cmd .run (
@@ -3062,7 +3185,7 @@ def create(self, *, branch: str) -> str:
3062
3185
3063
3186
Examples
3064
3187
--------
3065
- >>> GitBranchCmd (path=git_local_clone.path).create(branch='master')
3188
+ >>> GitBranchManager (path=git_local_clone.path).create(branch='master')
3066
3189
"fatal: a branch named 'master' already exists"
3067
3190
"""
3068
3191
return self .cmd .run (
@@ -3079,7 +3202,7 @@ def ls(self) -> str:
3079
3202
3080
3203
Examples
3081
3204
--------
3082
- >>> GitBranchCmd (path=git_local_clone.path).ls()
3205
+ >>> GitBranchManager (path=git_local_clone.path).ls()
3083
3206
'* master'
3084
3207
"""
3085
3208
return self .run (
0 commit comments