124
124
125
125
connection = sqlite3 .connect (input_file )
126
126
cursor = connection .cursor ()
127
+
128
+ build_len_min : int = cursor .execute ("SELECT MIN(LENGTH(build_commit)) from test;" ).fetchone ()[0 ]
129
+ build_len_max : int = cursor .execute ("SELECT MAX(LENGTH(build_commit)) from test;" ).fetchone ()[0 ]
130
+
131
+ if build_len_min != build_len_max :
132
+ logger .warning (f"{ input_file } contains commit hashes of differing lengths. It's possible that the wrong commits will be compared. "
133
+ "Try purging the the database of old commits." )
134
+ cursor .execute (f"UPDATE test SET build_commit = SUBSTRING(build_commit, 1, { build_len_min } );" )
135
+
136
+ build_len : int = build_len_min
137
+
127
138
builds = cursor .execute ("SELECT DISTINCT build_commit FROM test;" ).fetchall ()
139
+ builds = list (map (lambda b : b [0 ], builds )) # list[tuple[str]] -> list[str]
128
140
129
- commit_short_len = len (builds [0 ][0 ])
141
+ if not builds :
142
+ raise RuntimeError (f"{ input_file } does not contain any builds." )
130
143
131
144
try :
132
145
repo = git .Repo ("." , search_parent_directories = True )
@@ -140,11 +153,11 @@ def find_parent_in_data(commit: git.Commit):
140
153
seen_hexsha8 = set ()
141
154
while heap :
142
155
depth , current_commit = heapq .heappop (heap )
143
- current_hexsha8 = commit .hexsha [:commit_short_len ]
144
- if ( current_hexsha8 ,) in builds :
156
+ current_hexsha8 = commit .hexsha [:build_len ]
157
+ if current_hexsha8 in builds :
145
158
return current_hexsha8
146
159
for parent in commit .parents :
147
- parent_hexsha8 = parent .hexsha [:commit_short_len ]
160
+ parent_hexsha8 = parent .hexsha [:build_len ]
148
161
if parent_hexsha8 not in seen_hexsha8 :
149
162
seen_hexsha8 .add (parent_hexsha8 )
150
163
heapq .heappush (heap , (depth + 1 , parent ))
@@ -158,48 +171,48 @@ def get_all_parent_hexsha8s(commit: git.Commit):
158
171
159
172
while unvisited :
160
173
current_commit = unvisited .pop (0 )
161
- visited .append (current_commit .hexsha [:commit_short_len ])
174
+ visited .append (current_commit .hexsha [:build_len ])
162
175
for parent in current_commit .parents :
163
- if parent .hexsha [:commit_short_len ] not in visited :
176
+ if parent .hexsha [:build_len ] not in visited :
164
177
unvisited .append (parent )
165
178
166
179
return visited
167
180
168
181
169
- def get_commit_name (hexsha8 ):
182
+ def get_commit_name (hexsha8 : str ):
170
183
"""Helper function to find a human-readable name for a commit if possible."""
171
184
if repo is None :
172
185
return hexsha8
173
186
for h in repo .heads :
174
- if h .commit .hexsha [:commit_short_len ] == hexsha8 :
187
+ if h .commit .hexsha [:build_len ] == hexsha8 :
175
188
return h .name
176
189
for t in repo .tags :
177
- if t .commit .hexsha [:commit_short_len ] == hexsha8 :
190
+ if t .commit .hexsha [:build_len ] == hexsha8 :
178
191
return t .name
179
192
return hexsha8
180
193
181
194
182
- def get_commit_hexsha8 (name ):
195
+ def get_commit_hexsha8 (name : str ):
183
196
"""Helper function to search for a commit given a human-readable name."""
184
197
if repo is None :
185
198
return None
186
199
for h in repo .heads :
187
200
if h .name == name :
188
- return h .commit .hexsha [:commit_short_len ]
201
+ return h .commit .hexsha [:build_len ]
189
202
for t in repo .tags :
190
203
if t .name == name :
191
- return t .commit .hexsha [:commit_short_len ]
204
+ return t .commit .hexsha [:build_len ]
192
205
for c in repo .iter_commits ("--all" ):
193
- if c .hexsha [:commit_short_len ] == name [:commit_short_len ]:
194
- return c .hexsha [:commit_short_len ]
206
+ if c .hexsha [:build_len ] == name [:build_len ]:
207
+ return c .hexsha [:build_len ]
195
208
return None
196
209
197
210
198
211
hexsha8_baseline = name_baseline = None
199
212
200
213
# If the user specified a baseline, try to find a commit for it:
201
214
if known_args .baseline is not None :
202
- if ( known_args .baseline ,) in builds :
215
+ if known_args .baseline in builds :
203
216
hexsha8_baseline = known_args .baseline
204
217
if hexsha8_baseline is None :
205
218
hexsha8_baseline = get_commit_hexsha8 (known_args .baseline )
@@ -228,7 +241,7 @@ def get_commit_hexsha8(name):
228
241
229
242
# If the user has specified a compare value, try to find a corresponding commit:
230
243
if known_args .compare is not None :
231
- if ( known_args .compare ,) in builds :
244
+ if known_args .compare in builds :
232
245
hexsha8_compare = known_args .compare
233
246
if hexsha8_compare is None :
234
247
hexsha8_compare = get_commit_hexsha8 (known_args .compare )
0 commit comments