@@ -248,6 +248,18 @@ struct file_similarity {
248248 struct file_similarity * next ;
249249};
250250
251+ static unsigned int hash_filespec (struct diff_filespec * filespec )
252+ {
253+ unsigned int hash ;
254+ if (!filespec -> sha1_valid ) {
255+ if (diff_populate_filespec (filespec , 0 ))
256+ return 0 ;
257+ hash_sha1_file (filespec -> data , filespec -> size , "blob" , filespec -> sha1 );
258+ }
259+ memcpy (& hash , filespec -> sha1 , sizeof (hash ));
260+ return hash ;
261+ }
262+
251263static int find_identical_files (struct file_similarity * src ,
252264 struct file_similarity * dst ,
253265 struct diff_options * options )
@@ -258,46 +270,46 @@ static int find_identical_files(struct file_similarity *src,
258270 * Walk over all the destinations ...
259271 */
260272 do {
261- struct diff_filespec * target = dst -> filespec ;
262- struct file_similarity * p , * best ;
263- int i = 100 , best_score = -1 ;
264-
265- /*
266- * .. to find the best source match
267- */
268- best = NULL ;
269- for (p = src ; p ; p = p -> next ) {
270- int score ;
271- struct diff_filespec * source = p -> filespec ;
272-
273- /* False hash collision? */
274- if (hashcmp (source -> sha1 , target -> sha1 ))
275- continue ;
276- /* Non-regular files? If so, the modes must match! */
277- if (!S_ISREG (source -> mode ) || !S_ISREG (target -> mode )) {
278- if (source -> mode != target -> mode )
279- continue ;
280- }
281- /* Give higher scores to sources that haven't been used already */
282- score = !source -> rename_used ;
283- if (source -> rename_used && options -> detect_rename != DIFF_DETECT_COPY )
284- continue ;
285- score += basename_same (source , target );
286- if (score > best_score ) {
287- best = p ;
288- best_score = score ;
289- if (score == 2 )
290- break ;
291- }
273+ struct diff_filespec * target = dst -> filespec ;
274+ struct file_similarity * p , * best ;
275+ int i = 100 , best_score = -1 ;
292276
293- /* Too many identical alternatives? Pick one */
294- if (!-- i )
295- break ;
277+ /*
278+ * .. to find the best source match
279+ */
280+ best = NULL ;
281+ for (p = src ; p ; p = p -> next ) {
282+ int score ;
283+ struct diff_filespec * source = p -> filespec ;
284+
285+ /* False hash collision? */
286+ if (hashcmp (source -> sha1 , target -> sha1 ))
287+ continue ;
288+ /* Non-regular files? If so, the modes must match! */
289+ if (!S_ISREG (source -> mode ) || !S_ISREG (target -> mode )) {
290+ if (source -> mode != target -> mode )
291+ continue ;
296292 }
297- if (best ) {
298- record_rename_pair (dst -> index , best -> index , MAX_SCORE );
299- renames ++ ;
293+ /* Give higher scores to sources that haven't been used already */
294+ score = !source -> rename_used ;
295+ if (source -> rename_used && options -> detect_rename != DIFF_DETECT_COPY )
296+ continue ;
297+ score += basename_same (source , target );
298+ if (score > best_score ) {
299+ best = p ;
300+ best_score = score ;
301+ if (score == 2 )
302+ break ;
300303 }
304+
305+ /* Too many identical alternatives? Pick one */
306+ if (!-- i )
307+ break ;
308+ }
309+ if (best ) {
310+ record_rename_pair (dst -> index , best -> index , MAX_SCORE );
311+ renames ++ ;
312+ }
301313 } while ((dst = dst -> next ) != NULL );
302314 return renames ;
303315}
@@ -343,18 +355,6 @@ static int find_same_files(void *ptr, void *data)
343355 return ret ;
344356}
345357
346- static unsigned int hash_filespec (struct diff_filespec * filespec )
347- {
348- unsigned int hash ;
349- if (!filespec -> sha1_valid ) {
350- if (diff_populate_filespec (filespec , 0 ))
351- return 0 ;
352- hash_sha1_file (filespec -> data , filespec -> size , "blob" , filespec -> sha1 );
353- }
354- memcpy (& hash , filespec -> sha1 , sizeof (hash ));
355- return hash ;
356- }
357-
358358static void insert_file_table (struct hash_table * table , int src_dst , int index , struct diff_filespec * filespec )
359359{
360360 void * * pos ;
0 commit comments