@@ -1027,26 +1027,29 @@ def pull(
1027
1027
def init (
1028
1028
self ,
1029
1029
* ,
1030
- template : str | None = None ,
1030
+ template : str | pathlib . Path | None = None ,
1031
1031
separate_git_dir : StrOrBytesPath | None = None ,
1032
1032
object_format : t .Literal ["sha1" , "sha256" ] | None = None ,
1033
1033
branch : str | None = None ,
1034
1034
initial_branch : str | None = None ,
1035
1035
shared : bool
1036
- | Literal [false , true , umask , group , all , world , everybody ]
1037
- | str
1036
+ | t . Literal [" false" , " true" , " umask" , " group" , " all" , " world" , " everybody" ]
1037
+ | str # Octal number string (e.g., "0660")
1038
1038
| None = None ,
1039
1039
quiet : bool | None = None ,
1040
1040
bare : bool | None = None ,
1041
+ ref_format : t .Literal ["files" , "reftable" ] | None = None ,
1042
+ default : bool | None = None ,
1041
1043
# libvcs special behavior
1042
1044
check_returncode : bool | None = None ,
1045
+ make_parents : bool = True ,
1043
1046
** kwargs : t .Any ,
1044
1047
) -> str :
1045
1048
"""Create empty repo. Wraps `git init <https://git-scm.com/docs/git-init>`_.
1046
1049
1047
1050
Parameters
1048
1051
----------
1049
- template : str, optional
1052
+ template : str | pathlib.Path , optional
1050
1053
Directory from which templates will be used. The template directory
1051
1054
contains files and directories that will be copied to the $GIT_DIR
1052
1055
after it is created. The template directory will be one of the
@@ -1080,17 +1083,27 @@ def init(
1080
1083
- umask: Use permissions specified by umask
1081
1084
- group: Make the repository group-writable
1082
1085
- all, world, everybody: Same as world, make repo readable by all users
1083
- - An octal number: Explicit mode specification (e.g., "0660")
1086
+ - An octal number string : Explicit mode specification (e.g., "0660")
1084
1087
quiet : bool, optional
1085
1088
Only print error and warning messages; all other output will be
1086
1089
suppressed. Useful for scripting.
1087
1090
bare : bool, optional
1088
1091
Create a bare repository. If GIT_DIR environment is not set, it is set
1089
1092
to the current working directory. Bare repositories have no working
1090
1093
tree and are typically used as central repositories.
1094
+ ref_format : "files" | "reftable", optional
1095
+ Specify the reference storage format. Requires git version >= 2.37.0.
1096
+ - files: Classic format with packed-refs and loose refs (default)
1097
+ - reftable: New format that is more efficient for large repositories
1098
+ default : bool, optional
1099
+ Use default permissions for directories and files. This is the same as
1100
+ running git init without any options.
1091
1101
check_returncode : bool, optional
1092
1102
If True, check the return code of the git command and raise a
1093
1103
CalledProcessError if it is non-zero.
1104
+ make_parents : bool, default: True
1105
+ If True, create the target directory if it doesn't exist. If False,
1106
+ raise an error if the directory doesn't exist.
1094
1107
1095
1108
Returns
1096
1109
-------
@@ -1101,6 +1114,10 @@ def init(
1101
1114
------
1102
1115
CalledProcessError
1103
1116
If the git command fails and check_returncode is True.
1117
+ ValueError
1118
+ If invalid parameters are provided.
1119
+ FileNotFoundError
1120
+ If make_parents is False and the target directory doesn't exist.
1104
1121
1105
1122
Examples
1106
1123
--------
@@ -1142,6 +1159,14 @@ def init(
1142
1159
>>> git.init(shared='group')
1143
1160
'Initialized empty shared Git repository in ...'
1144
1161
1162
+ Create with octal permissions:
1163
+
1164
+ >>> shared_repo = tmp_path / 'shared_octal_example'
1165
+ >>> shared_repo.mkdir()
1166
+ >>> git = Git(path=shared_repo)
1167
+ >>> git.init(shared='0660')
1168
+ 'Initialized empty shared Git repository in ...'
1169
+
1145
1170
Create with a template directory:
1146
1171
1147
1172
>>> template_repo = tmp_path / 'template_example'
@@ -1214,18 +1239,31 @@ def init(
1214
1239
shared_str .isdigit ()
1215
1240
and len (shared_str ) <= 4
1216
1241
and all (c in string .octdigits for c in shared_str )
1242
+ and int (shared_str , 8 ) <= 0o777 # Validate octal range
1217
1243
)
1218
1244
):
1219
1245
msg = (
1220
1246
f"Invalid shared value. Must be one of { valid_shared_values } "
1221
- "or an octal number"
1247
+ "or a valid octal number between 0000 and 0777 "
1222
1248
)
1223
1249
raise ValueError (msg )
1224
1250
local_flags .append (f"--shared={ shared } " )
1251
+
1225
1252
if quiet is True :
1226
1253
local_flags .append ("--quiet" )
1227
1254
if bare is True :
1228
1255
local_flags .append ("--bare" )
1256
+ if ref_format is not None :
1257
+ local_flags .append (f"--ref-format={ ref_format } " )
1258
+ if default is True :
1259
+ local_flags .append ("--default" )
1260
+
1261
+ # libvcs special behavior
1262
+ if make_parents and not self .path .exists ():
1263
+ self .path .mkdir (parents = True )
1264
+ elif not self .path .exists ():
1265
+ msg = f"Directory does not exist: { self .path } "
1266
+ raise FileNotFoundError (msg )
1229
1267
1230
1268
return self .run (
1231
1269
["init" , * local_flags , "--" , * required_flags ],
0 commit comments