@@ -157,6 +157,8 @@ def merge_baseline(old_baseline, new_baseline):
157
157
to the new baseline, and will only work with baselines created
158
158
after v0.9.
159
159
160
+ Note: that the exclude regex is handled separately.
161
+
160
162
:type old_baseline: dict
161
163
:param old_baseline: baseline dict, loaded from previous baseline
162
164
@@ -174,14 +176,6 @@ def merge_baseline(old_baseline, new_baseline):
174
176
def merge_results (old_results , new_results ):
175
177
"""Update results in baseline with latest information.
176
178
177
- As a rule of thumb, we want to favor the new results, yet at the same
178
- time, transfer non-modified data from the old results set.
179
-
180
- Assumptions:
181
- * The list of results in each secret set is in the same order.
182
- This means that new_results cannot have *more* results than
183
- old_results.
184
-
185
179
:type old_results: dict
186
180
:param old_results: results of status quo
187
181
@@ -190,42 +184,25 @@ def merge_results(old_results, new_results):
190
184
191
185
:rtype: dict
192
186
"""
193
- for filename , secrets in old_results .items ():
187
+ for filename , old_secrets in old_results .items ():
194
188
if filename not in new_results :
195
- new_results [filename ] = secrets
196
189
continue
197
190
198
- if len (secrets ) == len (new_results [filename ]):
199
- # Assuming that secrets remain in order.
200
- for index , secrets_tuple in enumerate (zip (secrets , new_results [filename ])):
201
- old_secret , new_secret = secrets_tuple
202
- if old_secret ['hashed_secret' ] != new_secret ['hashed_secret' ]:
203
- # We don't join the two secret sets, because if the later
204
- # result set did not discover an old secret, it's probably
205
- # moved.
206
- # If it did discover it, then lengths would be different.
207
- continue
208
-
209
- if 'is_secret' in old_secret and 'is_secret' not in new_secret :
210
- # If the new_secret has a label, then go with the later
211
- # version.
212
- new_results [filename ][index ] = old_secret
191
+ old_secrets_mapping = dict ()
192
+ for old_secret in old_secrets :
193
+ old_secrets_mapping [old_secret ['hashed_secret' ]] = old_secret
213
194
214
- continue
195
+ for new_secret in new_results [filename ]:
196
+ if new_secret ['hashed_secret' ] not in old_secrets_mapping :
197
+ # We don't join the two secret sets, because if the newer
198
+ # result set did not discover an old secret, it probably
199
+ # moved.
200
+ continue
215
201
216
- # Need to figure out starting point. That is, while
217
- # len(new_results) < len(old_results), they may not start at the same
218
- # place.
219
- #
220
- # e.g. old_results = A,B,C,D
221
- # new_results = B,C
222
- first_secret_hash = new_results [filename ][0 ]['hashed_secret' ]
223
- for index , secret in enumerate (secrets ):
224
- if secret ['hashed_secret' ] == first_secret_hash :
225
- new_results [filename ] = secrets [:index ] + \
226
- new_results [filename ] + \
227
- secrets [index + len (new_results [filename ]):]
228
- break
202
+ old_secret = old_secrets_mapping [new_secret ['hashed_secret' ]]
203
+ # Only propogate 'is_secret' if it's not already there
204
+ if 'is_secret' in old_secret and 'is_secret' not in new_secret :
205
+ new_secret ['is_secret' ] = old_secret ['is_secret' ]
229
206
230
207
return new_results
231
208
@@ -246,7 +223,7 @@ def format_baseline_for_output(baseline):
246
223
indent = 2 ,
247
224
sort_keys = True ,
248
225
separators = (',' , ': ' ),
249
- )
226
+ ) + ' \n '
250
227
251
228
252
229
def _get_git_tracked_files (rootdir = '.' ):
0 commit comments