Skip to content

Commit 31c6191

Browse files
Thomas Rastgitster
authored andcommitted
log -L: store the path instead of a diff_filespec
line_log_data has held a diff_filespec* since the very early versions of the code. However, the only place in the code where we actually need the full filespec is parse_range_arg(); in all other cases, we are only interested in the path, so there is hardly a reason to store a filespec. Even worse, it causes a lot of redundant ->spec->path pointer dereferencing. And *even* worse, it caused the following bug. If you merge a rename with a modification to the old filename, like so: * Merge | \ | * Modify foo | | * | Rename foo->bar | / * Create foo we internally -- in process_ranges_merge_commit() -- scan all parents. We are mainly looking for one that doesn't have any modifications, so that we can assign all the blame to it and simplify away the merge. In doing so, we run the normal machinery on all parents in a loop. For each parent, we prepare a "working set" line_log_data by making a copy with line_log_data_copy(), which does *not* make a copy of the spec. Now suppose the rename is the first parent. The diff machinery tells us that the filepair is ('foo', 'bar'). We duly update the path we are interested in: rg->spec->path = xstrdup(pair->one->path); But that 'struct spec' is shared between the output line_log_data and the original input line_log_data. So we just wrecked the state of process_ranges_merge_commit(). When we get around to the second parent, the ranges tell us we are interested in a file 'foo' while the commits touch 'bar'. So most of this patch is just s/->spec->path/->path/ and associated management changes. This implicitly fixes the bug because we removed the shared parts between input and output of line_log_data_copy(); it is now safe to overwrite the path in the copy. There's one only somewhat related change: the comment in process_all_files() explains the reasoning behind using 'range' there. That bit of half-correct code had me sidetracked for a while. Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d51c527 commit 31c6191

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

line-log.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ search_line_log_data(struct line_log_data *list, const char *path,
265265
if (insertion_point)
266266
*insertion_point = NULL;
267267
while (p) {
268-
int cmp = strcmp(p->spec->path, path);
268+
int cmp = strcmp(p->path, path);
269269
if (!cmp)
270270
return p;
271271
if (insertion_point && cmp < 0)
@@ -275,22 +275,26 @@ search_line_log_data(struct line_log_data *list, const char *path,
275275
return NULL;
276276
}
277277

278+
/*
279+
* Note: takes ownership of 'path', which happens to be what the only
280+
* caller needs.
281+
*/
278282
static void line_log_data_insert(struct line_log_data **list,
279-
struct diff_filespec *spec,
283+
char *path,
280284
long begin, long end)
281285
{
282286
struct line_log_data *ip;
283-
struct line_log_data *p = search_line_log_data(*list, spec->path, &ip);
287+
struct line_log_data *p = search_line_log_data(*list, path, &ip);
284288

285289
if (p) {
286290
range_set_append_unsafe(&p->ranges, begin, end);
287291
sort_and_merge_range_set(&p->ranges);
288-
free_filespec(spec);
292+
free(path);
289293
return;
290294
}
291295

292296
p = xcalloc(1, sizeof(struct line_log_data));
293-
p->spec = spec;
297+
p->path = path;
294298
range_set_append(&p->ranges, begin, end);
295299
if (ip) {
296300
p->next = ip->next;
@@ -354,7 +358,7 @@ static void dump_line_log_data(struct line_log_data *r)
354358
{
355359
char buf[4096];
356360
while (r) {
357-
snprintf(buf, 4096, "file %s\n", r->spec->path);
361+
snprintf(buf, 4096, "file %s\n", r->path);
358362
dump_range_set(&r->ranges, buf);
359363
r = r->next;
360364
}
@@ -561,7 +565,7 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
561565

562566
for_each_string_list_item(item, args) {
563567
const char *name_part, *range_part;
564-
const char *full_name;
568+
char *full_name;
565569
struct diff_filespec *spec;
566570
long begin = 0, end = 0;
567571

@@ -584,7 +588,7 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
584588

585589
if (parse_range_arg(range_part, nth_line, &cb_data,
586590
lines, &begin, &end,
587-
spec->path))
591+
full_name))
588592
die("malformed -L argument '%s'", range_part);
589593
if (begin < 1)
590594
begin = 1;
@@ -593,8 +597,9 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
593597
begin--;
594598
if (lines < end || lines < begin)
595599
die("file %s has only %ld lines", name_part, lines);
596-
line_log_data_insert(&ranges, spec, begin, end);
600+
line_log_data_insert(&ranges, full_name, begin, end);
597601

602+
free_filespec(spec);
598603
free(ends);
599604
ends = NULL;
600605
}
@@ -610,9 +615,7 @@ static struct line_log_data *line_log_data_copy_one(struct line_log_data *r)
610615
line_log_data_init(ret);
611616
range_set_copy(&ret->ranges, &r->ranges);
612617

613-
ret->spec = r->spec;
614-
assert(ret->spec);
615-
ret->spec->count++;
618+
ret->path = xstrdup(r->path);
616619

617620
return ret;
618621
}
@@ -652,7 +655,7 @@ static struct line_log_data *line_log_data_merge(struct line_log_data *a,
652655
else if (!b)
653656
cmp = -1;
654657
else
655-
cmp = strcmp(a->spec->path, b->spec->path);
658+
cmp = strcmp(a->path, b->path);
656659
if (cmp < 0) {
657660
src = a;
658661
a = a->next;
@@ -667,8 +670,7 @@ static struct line_log_data *line_log_data_merge(struct line_log_data *a,
667670
}
668671
d = xmalloc(sizeof(struct line_log_data));
669672
line_log_data_init(d);
670-
d->spec = src->spec;
671-
d->spec->count++;
673+
d->path = xstrdup(src->path);
672674
*pp = d;
673675
pp = &d->next;
674676
if (src2)
@@ -741,7 +743,7 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_list
741743
paths = xmalloc((count+1)*sizeof(char *));
742744
r = range;
743745
for (i = 0; i < count; i++) {
744-
paths[i] = xstrdup(r->spec->path);
746+
paths[i] = xstrdup(r->path);
745747
r = r->next;
746748
}
747749
paths[count] = NULL;
@@ -797,7 +799,7 @@ static void filter_diffs_for_paths(struct line_log_data *range, int keep_deletio
797799
continue;
798800
}
799801
for (rg = range; rg; rg = rg->next) {
800-
if (!strcmp(rg->spec->path, p->two->path))
802+
if (!strcmp(rg->path, p->two->path))
801803
break;
802804
}
803805
if (rg)
@@ -1021,8 +1023,8 @@ static int process_diff_filepair(struct rev_info *rev,
10211023

10221024
assert(pair->two->path);
10231025
while (rg) {
1024-
assert(rg->spec->path);
1025-
if (!strcmp(rg->spec->path, pair->two->path))
1026+
assert(rg->path);
1027+
if (!strcmp(rg->path, pair->two->path))
10261028
break;
10271029
rg = rg->next;
10281030
}
@@ -1050,7 +1052,8 @@ static int process_diff_filepair(struct rev_info *rev,
10501052
collect_diff(&file_parent, &file_target, &diff);
10511053

10521054
/* NEEDSWORK should apply some heuristics to prevent mismatches */
1053-
rg->spec->path = xstrdup(pair->one->path);
1055+
free(rg->path);
1056+
rg->path = xstrdup(pair->one->path);
10541057

10551058
range_set_init(&tmp, 0);
10561059
range_set_map_across_diff(&tmp, &rg->ranges, &diff, diff_out);
@@ -1096,7 +1099,7 @@ static int process_all_files(struct line_log_data **range_out,
10961099
struct line_log_data *rg = range;
10971100
changed++;
10981101
/* NEEDSWORK tramples over data structures not owned here */
1099-
while (rg && strcmp(rg->spec->path, queue->queue[i]->two->path))
1102+
while (rg && strcmp(rg->path, queue->queue[i]->two->path))
11001103
rg = rg->next;
11011104
assert(rg);
11021105
rg->pair = diff_filepair_dup(queue->queue[i]);

line-log.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ struct diff_ranges {
2626
};
2727

2828
/* Linked list of interesting files and their associated ranges. The
29-
* list must be kept sorted by spec->path */
29+
* list must be kept sorted by path.
30+
*
31+
* For simplicity, even though this is highly redundant, each
32+
* line_log_data owns its 'path'.
33+
*/
3034
struct line_log_data {
3135
struct line_log_data *next;
32-
struct diff_filespec *spec;
36+
char *path;
3337
char status;
3438
struct range_set ranges;
3539
int arg_alloc, arg_nr;

t/t4211-line-log.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ canned_test "-L '/long f/',/^}/:a.c -L /main/,/^}/:a.c simple" two-ranges
4545
canned_test "-L 24,+1:a.c simple" vanishes-early
4646

4747
canned_test "-M -L '/long f/,/^}/:b.c' move-support" move-support-f
48-
canned_test_failure "-M -L ':f:b.c' parallel-change" parallel-change-f-to-main
48+
canned_test "-M -L ':f:b.c' parallel-change" parallel-change-f-to-main
4949

5050
canned_test "-L 4,12:a.c -L :main:a.c simple" multiple
5151
canned_test "-L 4,18:a.c -L :main:a.c simple" multiple-overlapping

0 commit comments

Comments
 (0)