Skip to content

Commit f98c2f7

Browse files
peffgitster
authored andcommitted
diffcore-rename: split locate_rename_dst into two functions
This function manages the mapping of destination pathnames to filepairs, and it handles both insertion and lookup. This makes the return value a bit confusing, as we return a newly created entry (even though no caller cares), and have no room to indicate to the caller that an entry already existed. Instead, let's break this up into two distinct functions, both backed by a common binary search. The binary search will use our normal "return the index if we found something, or negative index minus one to show where it would have gone" semantics. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 282616c commit f98c2f7

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

diffcore-rename.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ static struct diff_rename_dst {
1515
} *rename_dst;
1616
static int rename_dst_nr, rename_dst_alloc;
1717

18-
static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
19-
int insert_ok)
18+
static int find_rename_dst(struct diff_filespec *two)
2019
{
2120
int first, last;
2221

@@ -27,16 +26,33 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
2726
struct diff_rename_dst *dst = &(rename_dst[next]);
2827
int cmp = strcmp(two->path, dst->two->path);
2928
if (!cmp)
30-
return dst;
29+
return next;
3130
if (cmp < 0) {
3231
last = next;
3332
continue;
3433
}
3534
first = next+1;
3635
}
37-
/* not found */
38-
if (!insert_ok)
39-
return NULL;
36+
return -first - 1;
37+
}
38+
39+
static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two)
40+
{
41+
int ofs = find_rename_dst(two);
42+
return ofs < 0 ? NULL : &rename_dst[ofs];
43+
}
44+
45+
/*
46+
* Returns 0 on success, -1 if we found a duplicate.
47+
*/
48+
static int add_rename_dst(struct diff_filespec *two)
49+
{
50+
int first = find_rename_dst(two);
51+
52+
if (first >= 0)
53+
return -1;
54+
first = -first - 1;
55+
4056
/* insert to make it at "first" */
4157
ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
4258
rename_dst_nr++;
@@ -46,7 +62,7 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
4662
rename_dst[first].two = alloc_filespec(two->path);
4763
fill_filespec(rename_dst[first].two, two->sha1, two->sha1_valid, two->mode);
4864
rename_dst[first].pair = NULL;
49-
return &(rename_dst[first]);
65+
return 0;
5066
}
5167

5268
/* Table of rename/copy src files */
@@ -452,7 +468,7 @@ void diffcore_rename(struct diff_options *options)
452468
is_empty_blob_sha1(p->two->sha1))
453469
continue;
454470
else
455-
locate_rename_dst(p->two, 1);
471+
add_rename_dst(p->two);
456472
}
457473
else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
458474
is_empty_blob_sha1(p->one->sha1))
@@ -583,8 +599,7 @@ void diffcore_rename(struct diff_options *options)
583599
* We would output this create record if it has
584600
* not been turned into a rename/copy already.
585601
*/
586-
struct diff_rename_dst *dst =
587-
locate_rename_dst(p->two, 0);
602+
struct diff_rename_dst *dst = locate_rename_dst(p->two);
588603
if (dst && dst->pair) {
589604
diff_q(&outq, dst->pair);
590605
pair_to_free = p;
@@ -614,8 +629,7 @@ void diffcore_rename(struct diff_options *options)
614629
*/
615630
if (DIFF_PAIR_BROKEN(p)) {
616631
/* broken delete */
617-
struct diff_rename_dst *dst =
618-
locate_rename_dst(p->one, 0);
632+
struct diff_rename_dst *dst = locate_rename_dst(p->one);
619633
if (dst && dst->pair)
620634
/* counterpart is now rename/copy */
621635
pair_to_free = p;

0 commit comments

Comments
 (0)