1 #include "git-compat-util.h"
2 #include "line-range.h"
11 #include "xdiff-interface.h"
17 static void range_set_grow(struct range_set
*rs
, size_t extra
)
19 ALLOC_GROW(rs
->ranges
, rs
->nr
+ extra
, rs
->alloc
);
22 /* Either initialization would be fine */
23 #define RANGE_SET_INIT {0}
25 static void range_set_init(struct range_set
*rs
, size_t prealloc
)
27 rs
->alloc
= rs
->nr
= 0;
30 range_set_grow(rs
, prealloc
);
33 static void range_set_release(struct range_set
*rs
)
36 rs
->alloc
= rs
->nr
= 0;
40 /* dst must be uninitialized! */
41 static void range_set_copy(struct range_set
*dst
, struct range_set
*src
)
43 range_set_init(dst
, src
->nr
);
44 memcpy(dst
->ranges
, src
->ranges
, src
->nr
*sizeof(struct range_set
));
47 static void range_set_move(struct range_set
*dst
, struct range_set
*src
)
49 range_set_release(dst
);
50 dst
->ranges
= src
->ranges
;
52 dst
->alloc
= src
->alloc
;
54 src
->alloc
= src
->nr
= 0;
57 /* tack on a _new_ range _at the end_ */
58 static void range_set_append(struct range_set
*rs
, long a
, long b
)
61 assert(rs
->nr
== 0 || rs
->ranges
[rs
->nr
-1].end
<= a
);
62 range_set_grow(rs
, 1);
63 rs
->ranges
[rs
->nr
].start
= a
;
64 rs
->ranges
[rs
->nr
].end
= b
;
68 static int range_cmp(const void *_r
, const void *_s
)
70 const struct range
*r
= _r
;
71 const struct range
*s
= _s
;
73 /* this could be simply 'return r.start-s.start', but for the types */
74 if (r
->start
== s
->start
)
76 if (r
->start
< s
->start
)
82 * Helper: In-place pass of sorting and merging the ranges in the
83 * range set, to re-establish the invariants after another operation
85 * NEEDSWORK currently not needed
87 static void sort_and_merge_range_set(struct range_set
*rs
)
90 int o
= 1; /* output cursor */
92 qsort(rs
->ranges
, rs
->nr
, sizeof(struct range
), range_cmp
);
94 for (i
= 1; i
< rs
->nr
; i
++) {
95 if (rs
->ranges
[i
].start
<= rs
->ranges
[o
-1].end
) {
96 rs
->ranges
[o
-1].end
= rs
->ranges
[i
].end
;
98 rs
->ranges
[o
].start
= rs
->ranges
[i
].start
;
99 rs
->ranges
[o
].end
= rs
->ranges
[i
].end
;
108 * Union of range sets (i.e., sets of line numbers). Used to merge
109 * them when searches meet at a common ancestor.
111 * This is also where the ranges are consolidated into canonical form:
112 * overlapping and adjacent ranges are merged, and empty ranges are
115 static void range_set_union(struct range_set
*out
,
116 struct range_set
*a
, struct range_set
*b
)
118 int i
= 0, j
= 0, o
= 0;
119 struct range
*ra
= a
->ranges
;
120 struct range
*rb
= b
->ranges
;
121 /* cannot make an alias of out->ranges: it may change during grow */
123 assert(out
->nr
== 0);
124 while (i
< a
->nr
|| j
< b
->nr
) {
126 if (i
< a
->nr
&& j
< b
->nr
) {
127 if (ra
[i
].start
< rb
[j
].start
)
129 else if (ra
[i
].start
> rb
[j
].start
)
131 else if (ra
[i
].end
< rb
[j
].end
)
135 } else if (i
< a
->nr
) /* b exhausted */
137 else /* a exhausted */
139 if (new->start
== new->end
)
141 else if (!o
|| out
->ranges
[o
-1].end
< new->start
) {
142 range_set_grow(out
, 1);
143 out
->ranges
[o
].start
= new->start
;
144 out
->ranges
[o
].end
= new->end
;
146 } else if (out
->ranges
[o
-1].end
< new->end
) {
147 out
->ranges
[o
-1].end
= new->end
;
154 * Difference of range sets (out = a \ b). Pass the "interesting"
155 * ranges as 'a' and the target side of the diff as 'b': it removes
156 * the ranges for which the commit is responsible.
158 static void range_set_difference(struct range_set
*out
,
159 struct range_set
*a
, struct range_set
*b
)
162 for (i
= 0; i
< a
->nr
; i
++) {
163 long start
= a
->ranges
[i
].start
;
164 long end
= a
->ranges
[i
].end
;
165 while (start
< end
) {
166 while (j
< b
->nr
&& start
>= b
->ranges
[j
].end
)
172 if (j
>= b
->nr
|| end
< b
->ranges
[j
].start
) {
178 range_set_append(out
, start
, end
);
181 if (start
>= b
->ranges
[j
].start
) {
186 start
= b
->ranges
[j
].end
;
187 } else if (end
> b
->ranges
[j
].start
) {
192 if (start
< b
->ranges
[j
].start
)
193 range_set_append(out
, start
, b
->ranges
[j
].start
);
194 start
= b
->ranges
[j
].end
;
200 static void diff_ranges_init(struct diff_ranges
*diff
)
202 range_set_init(&diff
->parent
, 0);
203 range_set_init(&diff
->target
, 0);
206 static void diff_ranges_release(struct diff_ranges
*diff
)
208 range_set_release(&diff
->parent
);
209 range_set_release(&diff
->target
);
212 void line_log_data_init(struct line_log_data
*r
)
214 memset(r
, 0, sizeof(struct line_log_data
));
215 range_set_init(&r
->ranges
, 0);
218 static void line_log_data_clear(struct line_log_data
*r
)
220 range_set_release(&r
->ranges
);
222 diff_free_filepair(r
->pair
);
225 static void free_line_log_data(struct line_log_data
*r
)
228 struct line_log_data
*next
= r
->next
;
229 line_log_data_clear(r
);
235 static struct line_log_data
*
236 search_line_log_data(struct line_log_data
*list
, const char *path
,
237 struct line_log_data
**insertion_point
)
239 struct line_log_data
*p
= list
;
241 *insertion_point
= NULL
;
243 int cmp
= strcmp(p
->spec
->path
, path
);
246 if (insertion_point
&& cmp
< 0)
247 *insertion_point
= p
;
253 static void line_log_data_insert(struct line_log_data
**list
,
254 struct diff_filespec
*spec
,
255 long begin
, long end
)
257 struct line_log_data
*ip
;
258 struct line_log_data
*p
= search_line_log_data(*list
, spec
->path
, &ip
);
261 range_set_append(&p
->ranges
, begin
, end
);
262 sort_and_merge_range_set(&p
->ranges
);
267 p
= xcalloc(1, sizeof(struct line_log_data
));
269 range_set_append(&p
->ranges
, begin
, end
);
279 struct collect_diff_cbdata
{
280 struct diff_ranges
*diff
;
283 static int collect_diff_cb(long start_a
, long count_a
,
284 long start_b
, long count_b
,
287 struct collect_diff_cbdata
*d
= data
;
290 range_set_append(&d
->diff
->parent
, start_a
, start_a
+ count_a
);
292 range_set_append(&d
->diff
->target
, start_b
, start_b
+ count_b
);
297 static void collect_diff(mmfile_t
*parent
, mmfile_t
*target
, struct diff_ranges
*out
)
299 struct collect_diff_cbdata cbdata
= {NULL
};
304 memset(&xpp
, 0, sizeof(xpp
));
305 memset(&xecfg
, 0, sizeof(xecfg
));
306 xecfg
.ctxlen
= xecfg
.interhunkctxlen
= 0;
309 xecfg
.hunk_func
= collect_diff_cb
;
310 memset(&ecb
, 0, sizeof(ecb
));
312 xdi_diff(parent
, target
, &xpp
, &xecfg
, &ecb
);
316 * These are handy for debugging. Removing them with #if 0 silences
317 * the "unused function" warning.
320 static void dump_range_set(struct range_set
*rs
, const char *desc
)
323 printf("range set %s (%d items):\n", desc
, rs
->nr
);
324 for (i
= 0; i
< rs
->nr
; i
++)
325 printf("\t[%ld,%ld]\n", rs
->ranges
[i
].start
, rs
->ranges
[i
].end
);
328 static void dump_line_log_data(struct line_log_data
*r
)
332 snprintf(buf
, 4096, "file %s\n", r
->spec
->path
);
333 dump_range_set(&r
->ranges
, buf
);
338 static void dump_diff_ranges(struct diff_ranges
*diff
, const char *desc
)
341 assert(diff
->parent
.nr
== diff
->target
.nr
);
342 printf("diff ranges %s (%d items):\n", desc
, diff
->parent
.nr
);
343 printf("\tparent\ttarget\n");
344 for (i
= 0; i
< diff
->parent
.nr
; i
++) {
345 printf("\t[%ld,%ld]\t[%ld,%ld]\n",
346 diff
->parent
.ranges
[i
].start
,
347 diff
->parent
.ranges
[i
].end
,
348 diff
->target
.ranges
[i
].start
,
349 diff
->target
.ranges
[i
].end
);
355 static int ranges_overlap(struct range
*a
, struct range
*b
)
357 return !(a
->end
<= b
->start
|| b
->end
<= a
->start
);
361 * Given a diff and the set of interesting ranges, determine all hunks
362 * of the diff which touch (overlap) at least one of the interesting
363 * ranges in the target.
365 static void diff_ranges_filter_touched(struct diff_ranges
*out
,
366 struct diff_ranges
*diff
,
367 struct range_set
*rs
)
371 assert(out
->target
.nr
== 0);
373 for (i
= 0; i
< diff
->target
.nr
; i
++) {
374 while (diff
->target
.ranges
[i
].start
> rs
->ranges
[j
].end
) {
379 if (ranges_overlap(&diff
->target
.ranges
[i
], &rs
->ranges
[j
])) {
380 range_set_append(&out
->parent
,
381 diff
->parent
.ranges
[i
].start
,
382 diff
->parent
.ranges
[i
].end
);
383 range_set_append(&out
->target
,
384 diff
->target
.ranges
[i
].start
,
385 diff
->target
.ranges
[i
].end
);
391 * Adjust the line counts in 'rs' to account for the lines
392 * added/removed in the diff.
394 static void range_set_shift_diff(struct range_set
*out
,
395 struct range_set
*rs
,
396 struct diff_ranges
*diff
)
400 struct range
*src
= rs
->ranges
;
401 struct range
*target
= diff
->target
.ranges
;
402 struct range
*parent
= diff
->parent
.ranges
;
404 for (i
= 0; i
< rs
->nr
; i
++) {
405 while (j
< diff
->target
.nr
&& src
[i
].start
>= target
[j
].start
) {
406 offset
+= (parent
[j
].end
-parent
[j
].start
)
407 - (target
[j
].end
-target
[j
].start
);
410 range_set_append(out
, src
[i
].start
+offset
, src
[i
].end
+offset
);
415 * Given a diff and the set of interesting ranges, map the ranges
416 * across the diff. That is: observe that the target commit takes
417 * blame for all the + (target-side) ranges. So for every pair of
418 * ranges in the diff that was touched, we remove the latter and add
421 static void range_set_map_across_diff(struct range_set
*out
,
422 struct range_set
*rs
,
423 struct diff_ranges
*diff
,
424 struct diff_ranges
**touched_out
)
426 struct diff_ranges
*touched
= xmalloc(sizeof(*touched
));
427 struct range_set tmp1
= RANGE_SET_INIT
;
428 struct range_set tmp2
= RANGE_SET_INIT
;
430 diff_ranges_init(touched
);
431 diff_ranges_filter_touched(touched
, diff
, rs
);
432 range_set_difference(&tmp1
, rs
, &touched
->target
);
433 range_set_shift_diff(&tmp2
, &tmp1
, diff
);
434 range_set_union(out
, &tmp2
, &touched
->parent
);
435 range_set_release(&tmp1
);
436 range_set_release(&tmp2
);
438 *touched_out
= touched
;
442 static struct commit
*check_single_commit(struct rev_info
*revs
)
444 struct object
*commit
= NULL
;
448 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
449 struct object
*obj
= revs
->pending
.objects
[i
].item
;
450 if (obj
->flags
& UNINTERESTING
)
452 while (obj
->type
== OBJ_TAG
)
453 obj
= deref_tag(obj
, NULL
, 0);
454 if (obj
->type
!= OBJ_COMMIT
)
455 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
457 die("More than one commit to dig from: %s and %s?",
458 revs
->pending
.objects
[i
].name
,
459 revs
->pending
.objects
[found
].name
);
465 die("No commit specified?");
467 return (struct commit
*) commit
;
470 static void fill_blob_sha1(struct commit
*commit
, struct diff_filespec
*spec
)
473 unsigned char sha1
[20];
475 if (get_tree_entry(commit
->object
.sha1
, spec
->path
,
477 die("There is no path %s in the commit", spec
->path
);
478 fill_filespec(spec
, sha1
, 1, mode
);
483 static void fill_line_ends(struct diff_filespec
*spec
, long *lines
,
484 unsigned long **line_ends
)
486 int num
= 0, size
= 50;
488 unsigned long *ends
= NULL
;
491 if (diff_populate_filespec(spec
, 0))
492 die("Cannot read blob %s", sha1_to_hex(spec
->sha1
));
494 ends
= xmalloc(size
* sizeof(*ends
));
497 while (num
< spec
->size
) {
498 if (data
[num
] == '\n' || num
== spec
->size
- 1) {
499 ALLOC_GROW(ends
, (cur
+ 1), size
);
505 /* shrink the array to fit the elements */
506 ends
= xrealloc(ends
, cur
* sizeof(*ends
));
512 struct diff_filespec
*spec
;
514 unsigned long *line_ends
;
517 static const char *nth_line(void *data
, long line
)
519 struct nth_line_cb
*d
= data
;
520 assert(d
&& line
<= d
->lines
);
521 assert(d
->spec
&& d
->spec
->data
);
524 return (char *)d
->spec
->data
;
526 return (char *)d
->spec
->data
+ d
->line_ends
[line
] + 1;
529 static struct line_log_data
*
530 parse_lines(struct commit
*commit
, const char *prefix
, struct string_list
*args
)
533 unsigned long *ends
= NULL
;
534 struct nth_line_cb cb_data
;
535 struct string_list_item
*item
;
536 struct line_log_data
*ranges
= NULL
;
538 for_each_string_list_item(item
, args
) {
539 const char *name_part
, *range_part
;
540 const char *full_name
;
541 struct diff_filespec
*spec
;
542 long begin
= 0, end
= 0;
544 name_part
= skip_range_arg(item
->string
);
545 if (!name_part
|| *name_part
!= ':' || !name_part
[1])
546 die("-L argument '%s' not of the form start,end:file",
548 range_part
= xstrndup(item
->string
, name_part
- item
->string
);
551 full_name
= prefix_path(prefix
, prefix
? strlen(prefix
) : 0,
554 spec
= alloc_filespec(full_name
);
555 fill_blob_sha1(commit
, spec
);
556 fill_line_ends(spec
, &lines
, &ends
);
558 cb_data
.lines
= lines
;
559 cb_data
.line_ends
= ends
;
561 if (parse_range_arg(range_part
, nth_line
, &cb_data
,
562 lines
, &begin
, &end
))
563 die("malformed -L argument '%s'", range_part
);
569 if (lines
< end
|| lines
< begin
)
570 die("file %s has only %ld lines", name_part
, lines
);
571 line_log_data_insert(&ranges
, spec
, begin
, end
);
580 static struct line_log_data
*line_log_data_copy_one(struct line_log_data
*r
)
582 struct line_log_data
*ret
= xmalloc(sizeof(*ret
));
585 line_log_data_init(ret
);
586 range_set_copy(&ret
->ranges
, &r
->ranges
);
595 static struct line_log_data
*
596 line_log_data_copy(struct line_log_data
*r
)
598 struct line_log_data
*ret
= NULL
;
599 struct line_log_data
*tmp
= NULL
, *prev
= NULL
;
602 ret
= tmp
= prev
= line_log_data_copy_one(r
);
605 tmp
= line_log_data_copy_one(r
);
614 /* merge two range sets across files */
615 static struct line_log_data
*line_log_data_merge(struct line_log_data
*a
,
616 struct line_log_data
*b
)
618 struct line_log_data
*head
= NULL
, **pp
= &head
;
621 struct line_log_data
*src
;
622 struct line_log_data
*src2
= NULL
;
623 struct line_log_data
*d
;
630 cmp
= strcmp(a
->spec
->path
, b
->spec
->path
);
634 } else if (cmp
== 0) {
643 d
= xmalloc(sizeof(struct line_log_data
));
644 line_log_data_init(d
);
650 range_set_union(&d
->ranges
, &src
->ranges
, &src2
->ranges
);
652 range_set_copy(&d
->ranges
, &src
->ranges
);
658 static void add_line_range(struct rev_info
*revs
, struct commit
*commit
,
659 struct line_log_data
*range
)
661 struct line_log_data
*old
= NULL
;
662 struct line_log_data
*new = NULL
;
664 old
= lookup_decoration(&revs
->line_log_data
, &commit
->object
);
666 new = line_log_data_merge(old
, range
);
667 free_line_log_data(old
);
669 new = line_log_data_copy(range
);
672 add_decoration(&revs
->line_log_data
, &commit
->object
, new);
675 static void clear_commit_line_range(struct rev_info
*revs
, struct commit
*commit
)
677 struct line_log_data
*r
;
678 r
= lookup_decoration(&revs
->line_log_data
, &commit
->object
);
681 free_line_log_data(r
);
682 add_decoration(&revs
->line_log_data
, &commit
->object
, NULL
);
685 static struct line_log_data
*lookup_line_range(struct rev_info
*revs
,
686 struct commit
*commit
)
688 struct line_log_data
*ret
= NULL
;
690 ret
= lookup_decoration(&revs
->line_log_data
, &commit
->object
);
694 void line_log_init(struct rev_info
*rev
, const char *prefix
, struct string_list
*args
)
696 struct commit
*commit
= NULL
;
697 struct line_log_data
*range
;
699 commit
= check_single_commit(rev
);
700 range
= parse_lines(commit
, prefix
, args
);
701 add_line_range(rev
, commit
, range
);
703 if (!rev
->diffopt
.detect_rename
) {
705 struct line_log_data
*r
= range
;
711 paths
= xmalloc((count
+1)*sizeof(char *));
713 for (i
= 0; i
< count
; i
++) {
714 paths
[i
] = xstrdup(r
->spec
->path
);
718 init_pathspec(&rev
->diffopt
.pathspec
, paths
);
723 static void load_tree_desc(struct tree_desc
*desc
, void **tree
,
724 const unsigned char *sha1
)
727 *tree
= read_object_with_reference(sha1
, tree_type
, &size
, NULL
);
729 die("Unable to read tree (%s)", sha1_to_hex(sha1
));
730 init_tree_desc(desc
, *tree
, size
);
733 static int count_parents(struct commit
*commit
)
735 struct commit_list
*parents
= commit
->parents
;
739 parents
= parents
->next
;
744 static void move_diff_queue(struct diff_queue_struct
*dst
,
745 struct diff_queue_struct
*src
)
748 memcpy(dst
, src
, sizeof(struct diff_queue_struct
));
749 DIFF_QUEUE_CLEAR(src
);
752 static void queue_diffs(struct diff_options
*opt
,
753 struct diff_queue_struct
*queue
,
754 struct commit
*commit
, struct commit
*parent
)
756 void *tree1
= NULL
, *tree2
= NULL
;
757 struct tree_desc desc1
, desc2
;
760 load_tree_desc(&desc2
, &tree2
, commit
->tree
->object
.sha1
);
762 load_tree_desc(&desc1
, &tree1
, parent
->tree
->object
.sha1
);
764 init_tree_desc(&desc1
, "", 0);
766 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
767 diff_tree(&desc1
, &desc2
, "", opt
);
769 move_diff_queue(queue
, &diff_queued_diff
);
777 static char *get_nth_line(long line
, unsigned long *ends
, void *data
)
782 return (char *)data
+ ends
[line
] + 1;
785 static void print_line(const char *prefix
, char first
,
786 long line
, unsigned long *ends
, void *data
,
787 const char *color
, const char *reset
)
789 char *begin
= get_nth_line(line
, ends
, data
);
790 char *end
= get_nth_line(line
+1, ends
, data
);
793 if (end
> begin
&& end
[-1] == '\n') {
798 fputs(prefix
, stdout
);
799 fputs(color
, stdout
);
801 fwrite(begin
, 1, end
-begin
, stdout
);
802 fputs(reset
, stdout
);
805 fputs("\\ No newline at end of file\n", stdout
);
808 static char *output_prefix(struct diff_options
*opt
)
812 if (opt
->output_prefix
) {
813 struct strbuf
*sb
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
820 static void dump_diff_hacky_one(struct rev_info
*rev
, struct line_log_data
*range
)
823 long p_lines
, t_lines
;
824 unsigned long *p_ends
= NULL
, *t_ends
= NULL
;
825 struct diff_filepair
*pair
= range
->pair
;
826 struct diff_ranges
*diff
= &range
->diff
;
828 struct diff_options
*opt
= &rev
->diffopt
;
829 char *prefix
= output_prefix(opt
);
830 const char *c_reset
= diff_get_color(opt
->use_color
, DIFF_RESET
);
831 const char *c_frag
= diff_get_color(opt
->use_color
, DIFF_FRAGINFO
);
832 const char *c_meta
= diff_get_color(opt
->use_color
, DIFF_METAINFO
);
833 const char *c_old
= diff_get_color(opt
->use_color
, DIFF_FILE_OLD
);
834 const char *c_new
= diff_get_color(opt
->use_color
, DIFF_FILE_NEW
);
835 const char *c_plain
= diff_get_color(opt
->use_color
, DIFF_PLAIN
);
840 if (pair
->one
->sha1_valid
)
841 fill_line_ends(pair
->one
, &p_lines
, &p_ends
);
842 fill_line_ends(pair
->two
, &t_lines
, &t_ends
);
844 printf("%s%sdiff --git a/%s b/%s%s\n", prefix
, c_meta
, pair
->one
->path
, pair
->two
->path
, c_reset
);
845 printf("%s%s--- %s%s%s\n", prefix
, c_meta
,
846 pair
->one
->sha1_valid
? "a/" : "",
847 pair
->one
->sha1_valid
? pair
->one
->path
: "/dev/null",
849 printf("%s%s+++ b/%s%s\n", prefix
, c_meta
, pair
->two
->path
, c_reset
);
850 for (i
= 0; i
< range
->ranges
.nr
; i
++) {
852 long t_start
= range
->ranges
.ranges
[i
].start
;
853 long t_end
= range
->ranges
.ranges
[i
].end
;
854 long t_cur
= t_start
;
857 while (j
< diff
->target
.nr
&& diff
->target
.ranges
[j
].end
< t_start
)
859 if (j
== diff
->target
.nr
|| diff
->target
.ranges
[j
].start
> t_end
)
862 /* Scan ahead to determine the last diff that falls in this range */
864 while (j_last
< diff
->target
.nr
&& diff
->target
.ranges
[j_last
].start
< t_end
)
870 * Compute parent hunk headers: we know that the diff
871 * has the correct line numbers (but not all hunks).
872 * So it suffices to shift the start/end according to
873 * the line numbers of the first/last hunk(s) that
874 * fall in this range.
876 if (t_start
< diff
->target
.ranges
[j
].start
)
877 p_start
= diff
->parent
.ranges
[j
].start
- (diff
->target
.ranges
[j
].start
-t_start
);
879 p_start
= diff
->parent
.ranges
[j
].start
;
880 if (t_end
> diff
->target
.ranges
[j_last
].end
)
881 p_end
= diff
->parent
.ranges
[j_last
].end
+ (t_end
-diff
->target
.ranges
[j_last
].end
);
883 p_end
= diff
->parent
.ranges
[j_last
].end
;
885 if (!p_start
&& !p_end
) {
890 /* Now output a diff hunk for this range */
891 printf("%s%s@@ -%ld,%ld +%ld,%ld @@%s\n",
893 p_start
+1, p_end
-p_start
, t_start
+1, t_end
-t_start
,
895 while (j
< diff
->target
.nr
&& diff
->target
.ranges
[j
].start
< t_end
) {
897 for (; t_cur
< diff
->target
.ranges
[j
].start
; t_cur
++)
898 print_line(prefix
, ' ', t_cur
, t_ends
, pair
->two
->data
,
900 for (k
= diff
->parent
.ranges
[j
].start
; k
< diff
->parent
.ranges
[j
].end
; k
++)
901 print_line(prefix
, '-', k
, p_ends
, pair
->one
->data
,
903 for (; t_cur
< diff
->target
.ranges
[j
].end
&& t_cur
< t_end
; t_cur
++)
904 print_line(prefix
, '+', t_cur
, t_ends
, pair
->two
->data
,
908 for (; t_cur
< t_end
; t_cur
++)
909 print_line(prefix
, ' ', t_cur
, t_ends
, pair
->two
->data
,
918 * NEEDSWORK: manually building a diff here is not the Right
919 * Thing(tm). log -L should be built into the diff pipeline.
921 static void dump_diff_hacky(struct rev_info
*rev
, struct line_log_data
*range
)
923 puts(output_prefix(&rev
->diffopt
));
925 dump_diff_hacky_one(rev
, range
);
931 * Unlike most other functions, this destructively operates on
934 static int process_diff_filepair(struct rev_info
*rev
,
935 struct diff_filepair
*pair
,
936 struct line_log_data
*range
,
937 struct diff_ranges
**diff_out
)
939 struct line_log_data
*rg
= range
;
940 struct range_set tmp
;
941 struct diff_ranges diff
;
942 mmfile_t file_parent
, file_target
;
944 assert(pair
->two
->path
);
946 assert(rg
->spec
->path
);
947 if (!strcmp(rg
->spec
->path
, pair
->two
->path
))
954 if (rg
->ranges
.nr
== 0)
957 assert(pair
->two
->sha1_valid
);
958 diff_populate_filespec(pair
->two
, 0);
959 file_target
.ptr
= pair
->two
->data
;
960 file_target
.size
= pair
->two
->size
;
962 if (pair
->one
->sha1_valid
) {
963 diff_populate_filespec(pair
->one
, 0);
964 file_parent
.ptr
= pair
->one
->data
;
965 file_parent
.size
= pair
->one
->size
;
967 file_parent
.ptr
= "";
968 file_parent
.size
= 0;
971 diff_ranges_init(&diff
);
972 collect_diff(&file_parent
, &file_target
, &diff
);
974 /* NEEDSWORK should apply some heuristics to prevent mismatches */
975 rg
->spec
->path
= xstrdup(pair
->one
->path
);
977 range_set_init(&tmp
, 0);
978 range_set_map_across_diff(&tmp
, &rg
->ranges
, &diff
, diff_out
);
979 range_set_release(&rg
->ranges
);
980 range_set_move(&rg
->ranges
, &tmp
);
982 diff_ranges_release(&diff
);
984 return ((*diff_out
)->parent
.nr
> 0);
987 static struct diff_filepair
*diff_filepair_dup(struct diff_filepair
*pair
)
989 struct diff_filepair
*new = xmalloc(sizeof(struct diff_filepair
));
990 new->one
= pair
->one
;
991 new->two
= pair
->two
;
997 static void free_diffqueues(int n
, struct diff_queue_struct
*dq
)
1000 for (i
= 0; i
< n
; i
++)
1001 for (j
= 0; j
< dq
[i
].nr
; j
++)
1002 diff_free_filepair(dq
[i
].queue
[j
]);
1006 static int process_all_files(struct line_log_data
**range_out
,
1007 struct rev_info
*rev
,
1008 struct diff_queue_struct
*queue
,
1009 struct line_log_data
*range
)
1013 *range_out
= line_log_data_copy(range
);
1015 for (i
= 0; i
< queue
->nr
; i
++) {
1016 struct diff_ranges
*pairdiff
= NULL
;
1017 if (process_diff_filepair(rev
, queue
->queue
[i
], *range_out
, &pairdiff
)) {
1018 struct line_log_data
*rg
= range
;
1020 /* NEEDSWORK tramples over data structures not owned here */
1021 while (rg
&& strcmp(rg
->spec
->path
, queue
->queue
[i
]->two
->path
))
1024 rg
->pair
= diff_filepair_dup(queue
->queue
[i
]);
1025 memcpy(&rg
->diff
, pairdiff
, sizeof(struct diff_ranges
));
1032 int line_log_print(struct rev_info
*rev
, struct commit
*commit
)
1034 struct line_log_data
*range
= lookup_line_range(rev
, commit
);
1037 dump_diff_hacky(rev
, range
);
1041 static int process_ranges_ordinary_commit(struct rev_info
*rev
, struct commit
*commit
,
1042 struct line_log_data
*range
)
1044 struct commit
*parent
= NULL
;
1045 struct diff_queue_struct queue
;
1046 struct line_log_data
*parent_range
;
1049 if (commit
->parents
)
1050 parent
= commit
->parents
->item
;
1052 queue_diffs(&rev
->diffopt
, &queue
, commit
, parent
);
1053 changed
= process_all_files(&parent_range
, rev
, &queue
, range
);
1055 add_line_range(rev
, parent
, parent_range
);
1059 static int process_ranges_merge_commit(struct rev_info
*rev
, struct commit
*commit
,
1060 struct line_log_data
*range
)
1062 struct diff_queue_struct
*diffqueues
;
1063 struct line_log_data
**cand
;
1064 struct commit
**parents
;
1065 struct commit_list
*p
;
1067 int nparents
= count_parents(commit
);
1069 diffqueues
= xmalloc(nparents
* sizeof(*diffqueues
));
1070 cand
= xmalloc(nparents
* sizeof(*cand
));
1071 parents
= xmalloc(nparents
* sizeof(*parents
));
1073 p
= commit
->parents
;
1074 for (i
= 0; i
< nparents
; i
++) {
1075 parents
[i
] = p
->item
;
1077 queue_diffs(&rev
->diffopt
, &diffqueues
[i
], commit
, parents
[i
]);
1080 for (i
= 0; i
< nparents
; i
++) {
1083 changed
= process_all_files(&cand
[i
], rev
, &diffqueues
[i
], range
);
1086 * This parent can take all the blame, so we
1087 * don't follow any other path in history
1089 add_line_range(rev
, parents
[i
], cand
[i
]);
1090 clear_commit_line_range(rev
, commit
);
1091 commit
->parents
= xmalloc(sizeof(struct commit_list
));
1092 commit
->parents
->item
= parents
[i
];
1093 commit
->parents
->next
= NULL
;
1096 free_diffqueues(nparents
, diffqueues
);
1097 /* NEEDSWORK leaking like a sieve */
1103 * No single parent took the blame. We add the candidates
1104 * from the above loop to the parents.
1106 for (i
= 0; i
< nparents
; i
++) {
1107 add_line_range(rev
, parents
[i
], cand
[i
]);
1110 clear_commit_line_range(rev
, commit
);
1113 free_diffqueues(nparents
, diffqueues
);
1116 /* NEEDSWORK evil merge detection stuff */
1117 /* NEEDSWORK leaking like a sieve */
1120 static int process_ranges_arbitrary_commit(struct rev_info
*rev
, struct commit
*commit
)
1122 struct line_log_data
*range
= lookup_line_range(rev
, commit
);
1126 if (!commit
->parents
|| !commit
->parents
->next
)
1127 changed
= process_ranges_ordinary_commit(rev
, commit
, range
);
1129 changed
= process_ranges_merge_commit(rev
, commit
, range
);
1133 commit
->object
.flags
|= TREESAME
;
1138 static enum rewrite_result
line_log_rewrite_one(struct rev_info
*rev
, struct commit
**pp
)
1141 struct commit
*p
= *pp
;
1142 if (p
->parents
&& p
->parents
->next
)
1143 return rewrite_one_ok
;
1144 if (p
->object
.flags
& UNINTERESTING
)
1145 return rewrite_one_ok
;
1146 if (!(p
->object
.flags
& TREESAME
))
1147 return rewrite_one_ok
;
1149 return rewrite_one_noparents
;
1150 *pp
= p
->parents
->item
;
1154 int line_log_filter(struct rev_info
*rev
)
1156 struct commit
*commit
;
1157 struct commit_list
*list
= rev
->commits
;
1158 struct commit_list
*out
= NULL
, **pp
= &out
;
1161 struct commit_list
*to_free
= NULL
;
1162 commit
= list
->item
;
1163 if (process_ranges_arbitrary_commit(rev
, commit
)) {
1173 for (list
= out
; list
; list
= list
->next
)
1174 rewrite_parents(rev
, list
->item
, line_log_rewrite_one
);