7
7
8
8
from git_dummy .settings import settings
9
9
10
+
11
+
12
+
10
13
app = typer .Typer (context_settings = {"help_option_names" : ["-h" , "--help" ]})
11
14
12
15
@@ -40,13 +43,18 @@ def main(
40
43
settings .no_subdir ,
41
44
help = "Initialize the dummy Git repo in the current directory instead of in a subdirectory" ,
42
45
),
46
+ constant_sha : bool = typer .Option (
47
+ settings .constant_sha ,
48
+ help = "Use constant values for commit author, email, and commit date parameters to yield consistent sha1 values across git-dummy runs" ,
49
+ ),
43
50
):
44
51
settings .name = name
45
52
settings .commits = commits
46
53
settings .branches = branches
47
54
settings .diverge_at = diverge_at
48
55
settings .merge = merge
49
56
settings .no_subdir = no_subdir
57
+ settings .constant_sha = constant_sha
50
58
51
59
settings .git_dir = os .path .expanduser (git_dir )
52
60
if not settings .no_subdir :
@@ -71,46 +79,65 @@ def main(
71
79
)
72
80
73
81
repo = git .Repo .init (settings .git_dir )
74
- repo .config_writer ().set_value ("init" , "defaultBranch" , "main" ).release ()
75
82
76
- for c in range (1 , settings .commits + 1 ):
77
- open (os .path .join (settings .git_dir , f"main.{ c } " ), "a" ).close ()
78
- repo .index .add ([f"main.{ c } " ])
79
- repo .index .commit (f"Dummy commit #{ c } on main" )
83
+ config_writer = repo .config_writer ()
84
+ config_writer .set_value ("init" , "defaultBranch" , "main" ).release ()
85
+ if settings .constant_sha :
86
+ config_writer .set_value ("user" , "name" , "Git Dummy" )
87
+ config_writer .
set_value (
"user" ,
"email" ,
"[email protected] " )
88
+ config_writer .release ()
89
+
90
+ if settings .constant_sha :
91
+ os .environ ["GIT_AUTHOR_DATE" ] = "2023-01-01T00:00:00"
92
+ os .environ ["GIT_COMMITTER_DATE" ] = "2023-01-01T00:00:00"
93
+
94
+ try :
95
+ for c in range (1 , settings .commits + 1 ):
96
+ open (os .path .join (settings .git_dir , f"main.{ c } " ), "a" ).close ()
97
+ repo .index .add ([f"main.{ c } " ])
98
+ repo .index .commit (f"Dummy commit #{ c } on main" )
99
+
100
+ while settings .branches - 1 > 0 :
101
+ repo .git .checkout ("main" )
102
+ r = (
103
+ (settings .commits - settings .diverge_at )
104
+ if settings .diverge_at
105
+ else random .choice (range (1 , settings .commits ))
106
+ )
107
+ repo .git .checkout (f"HEAD~{ r } " )
108
+ branch_name = f"branch{ settings .branches - 1 } "
109
+ repo .create_head (branch_name )
110
+ repo .git .checkout (branch_name )
111
+ for d in range (settings .commits - r + 1 , settings .commits + 1 ):
112
+ open (os .path .join (settings .git_dir , f"{ branch_name } .{ d } " ), "a" ).close ()
113
+ repo .index .add ([f"{ branch_name } .{ d } " ])
114
+ repo .index .commit (f"Dummy commit #{ d } on { branch_name } " )
115
+ if settings .merge :
116
+ to_merge = settings .merge .split ("," )
117
+ if str (settings .branches - 1 ) in to_merge :
118
+ repo .git .checkout ("main" )
119
+ main = repo .branches ["main" ]
120
+ branch = repo .branches [branch_name ]
121
+ base = repo .git .merge_base (main , branch )
122
+ repo .index .merge_tree (branch , base = base )
123
+ repo .index .commit (
124
+ f"Merge { branch_name } into main" ,
125
+ parent_commits = (branch .commit , main .commit ),
126
+ )
127
+ main .checkout (force = True )
128
+
129
+ settings .branches -= 1
80
130
81
- while settings .branches - 1 > 0 :
82
131
repo .git .checkout ("main" )
83
- r = (
84
- (settings .commits - settings .diverge_at )
85
- if settings .diverge_at
86
- else random .choice (range (1 , settings .commits ))
87
- )
88
- repo .git .checkout (f"HEAD~{ r } " )
89
- branch_name = f"branch{ settings .branches - 1 } "
90
- repo .create_head (branch_name )
91
- repo .git .checkout (branch_name )
92
- for d in range (settings .commits - r + 1 , settings .commits + 1 ):
93
- open (os .path .join (settings .git_dir , f"{ branch_name } .{ d } " ), "a" ).close ()
94
- repo .index .add ([f"{ branch_name } .{ d } " ])
95
- repo .index .commit (f"Dummy commit #{ d } on { branch_name } " )
96
- if settings .merge :
97
- to_merge = settings .merge .split ("," )
98
- if str (settings .branches - 1 ) in to_merge :
99
- repo .git .checkout ("main" )
100
- main = repo .branches ["main" ]
101
- branch = repo .branches [branch_name ]
102
- base = repo .git .merge_base (main , branch )
103
- repo .index .merge_tree (branch , base = base )
104
- repo .index .commit (
105
- f"Merge { branch_name } into main" ,
106
- parent_commits = (branch .commit , main .commit ),
107
- )
108
- main .checkout (force = True )
109
-
110
- settings .branches -= 1
111
-
112
- repo .git .checkout ("main" )
113
132
133
+ except Exception as e :
134
+ raise e
135
+
136
+ finally :
137
+ # If needed, delete the environment variables set by git-dummy
138
+ if settings .constant_sha :
139
+ del os .environ ["GIT_AUTHOR_DATE" ]
140
+ del os .environ ["GIT_COMMITTER_DATE" ]
114
141
115
142
if __name__ == "__main__" :
116
143
app ()
0 commit comments