1 #include "git-compat-util.h"
2 #include "line-range.h"
11 #include "xdiff-interface.h"
18 static void range_set_grow(struct range_set
*rs
, size_t extra
)
20 ALLOC_GROW(rs
->ranges
, rs
->nr
+ extra
, rs
->alloc
);
23 /* Either initialization would be fine */
24 #define RANGE_SET_INIT {0}
26 static void range_set_init(struct range_set
*rs
, size_t prealloc
)
28 rs
->alloc
= rs
->nr
= 0;
31 range_set_grow(rs
, prealloc
);
34 static void range_set_release(struct range_set
*rs
)
37 rs
->alloc
= rs
->nr
= 0;
41 /* dst must be uninitialized! */
42 static void range_set_copy(struct range_set
*dst
, struct range_set
*src
)
44 range_set_init(dst
, src
->nr
);
45 memcpy(dst
->ranges
, src
->ranges
, src
->nr
*sizeof(struct range_set
));
48 static void range_set_move(struct range_set
*dst
, struct range_set
*src
)
50 range_set_release(dst
);
51 dst
->ranges
= src
->ranges
;
53 dst
->alloc
= src
->alloc
;
55 src
->alloc
= src
->nr
= 0;
58 /* tack on a _new_ range _at the end_ */
59 static void range_set_append(struct range_set
*rs
, long a
, long b
)
62 assert(rs
->nr
== 0 || rs
->ranges
[rs
->nr
-1].end
<= a
);
63 range_set_grow(rs
, 1);
64 rs
->ranges
[rs
->nr
].start
= a
;
65 rs
->ranges
[rs
->nr
].end
= b
;
69 static int range_cmp(const void *_r
, const void *_s
)
71 const struct range
*r
= _r
;
72 const struct range
*s
= _s
;
74 /* this could be simply 'return r.start-s.start', but for the types */
75 if (r
->start
== s
->start
)
77 if (r
->start
< s
->start
)
83 * Helper: In-place pass of sorting and merging the ranges in the
84 * range set, to re-establish the invariants after another operation
86 * NEEDSWORK currently not needed
88 static void sort_and_merge_range_set(struct range_set
*rs
)
91 int o
= 1; /* output cursor */
93 qsort(rs
->ranges
, rs
->nr
, sizeof(struct range
), range_cmp
);
95 for (i
= 1; i
< rs
->nr
; i
++) {
96 if (rs
->ranges
[i
].start
<= rs
->ranges
[o
-1].end
) {
97 rs
->ranges
[o
-1].end
= rs
->ranges
[i
].end
;
99 rs
->ranges
[o
].start
= rs
->ranges
[i
].start
;
100 rs
->ranges
[o
].end
= rs
->ranges
[i
].end
;
109 * Union of range sets (i.e., sets of line numbers). Used to merge
110 * them when searches meet at a common ancestor.
112 * This is also where the ranges are consolidated into canonical form:
113 * overlapping and adjacent ranges are merged, and empty ranges are
116 static void range_set_union(struct range_set
*out
,
117 struct range_set
*a
, struct range_set
*b
)
119 int i
= 0, j
= 0, o
= 0;
120 struct range
*ra
= a
->ranges
;
121 struct range
*rb
= b
->ranges
;
122 /* cannot make an alias of out->ranges: it may change during grow */
124 assert(out
->nr
== 0);
125 while (i
< a
->nr
|| j
< b
->nr
) {
127 if (i
< a
->nr
&& j
< b
->nr
) {
128 if (ra
[i
].start
< rb
[j
].start
)
130 else if (ra
[i
].start
> rb
[j
].start
)
132 else if (ra
[i
].end
< rb
[j
].end
)
136 } else if (i
< a
->nr
) /* b exhausted */
138 else /* a exhausted */
140 if (new->start
== new->end
)
142 else if (!o
|| out
->ranges
[o
-1].end
< new->start
) {
143 range_set_grow(out
, 1);
144 out
->ranges
[o
].start
= new->start
;
145 out
->ranges
[o
].end
= new->end
;
147 } else if (out
->ranges
[o
-1].end
< new->end
) {
148 out
->ranges
[o
-1].end
= new->end
;
155 * Difference of range sets (out = a \ b). Pass the "interesting"
156 * ranges as 'a' and the target side of the diff as 'b': it removes
157 * the ranges for which the commit is responsible.
159 static void range_set_difference(struct range_set
*out
,
160 struct range_set
*a
, struct range_set
*b
)
163 for (i
= 0; i
< a
->nr
; i
++) {
164 long start
= a
->ranges
[i
].start
;
165 long end
= a
->ranges
[i
].end
;
166 while (start
< end
) {
167 while (j
< b
->nr
&& start
>= b
->ranges
[j
].end
)
173 if (j
>= b
->nr
|| end
< b
->ranges
[j
].start
) {
179 range_set_append(out
, start
, end
);
182 if (start
>= b
->ranges
[j
].start
) {
187 start
= b
->ranges
[j
].end
;
188 } else if (end
> b
->ranges
[j
].start
) {
193 if (start
< b
->ranges
[j
].start
)
194 range_set_append(out
, start
, b
->ranges
[j
].start
);
195 start
= b
->ranges
[j
].end
;
201 static void diff_ranges_init(struct diff_ranges
*diff
)
203 range_set_init(&diff
->parent
, 0);
204 range_set_init(&diff
->target
, 0);
207 static void diff_ranges_release(struct diff_ranges
*diff
)
209 range_set_release(&diff
->parent
);
210 range_set_release(&diff
->target
);
213 void line_log_data_init(struct line_log_data
*r
)
215 memset(r
, 0, sizeof(struct line_log_data
));
216 range_set_init(&r
->ranges
, 0);
219 static void line_log_data_clear(struct line_log_data
*r
)
221 range_set_release(&r
->ranges
);
223 diff_free_filepair(r
->pair
);
226 static void free_line_log_data(struct line_log_data
*r
)
229 struct line_log_data
*next
= r
->next
;
230 line_log_data_clear(r
);
236 static struct line_log_data
*
237 search_line_log_data(struct line_log_data
*list
, const char *path
,
238 struct line_log_data
**insertion_point
)
240 struct line_log_data
*p
= list
;
242 *insertion_point
= NULL
;
244 int cmp
= strcmp(p
->spec
->path
, path
);
247 if (insertion_point
&& cmp
< 0)
248 *insertion_point
= p
;
254 static void line_log_data_insert(struct line_log_data
**list
,
255 struct diff_filespec
*spec
,
256 long begin
, long end
)
258 struct line_log_data
*ip
;
259 struct line_log_data
*p
= search_line_log_data(*list
, spec
->path
, &ip
);
262 range_set_append(&p
->ranges
, begin
, end
);
263 sort_and_merge_range_set(&p
->ranges
);
268 p
= xcalloc(1, sizeof(struct line_log_data
));
270 range_set_append(&p
->ranges
, begin
, end
);
280 struct collect_diff_cbdata
{
281 struct diff_ranges
*diff
;
284 static int collect_diff_cb(long start_a
, long count_a
,
285 long start_b
, long count_b
,
288 struct collect_diff_cbdata
*d
= data
;
291 range_set_append(&d
->diff
->parent
, start_a
, start_a
+ count_a
);
293 range_set_append(&d
->diff
->target
, start_b
, start_b
+ count_b
);
298 static void collect_diff(mmfile_t
*parent
, mmfile_t
*target
, struct diff_ranges
*out
)
300 struct collect_diff_cbdata cbdata
= {NULL
};
305 memset(&xpp
, 0, sizeof(xpp
));
306 memset(&xecfg
, 0, sizeof(xecfg
));
307 xecfg
.ctxlen
= xecfg
.interhunkctxlen
= 0;
310 xecfg
.hunk_func
= collect_diff_cb
;
311 memset(&ecb
, 0, sizeof(ecb
));
313 xdi_diff(parent
, target
, &xpp
, &xecfg
, &ecb
);
317 * These are handy for debugging. Removing them with #if 0 silences
318 * the "unused function" warning.
321 static void dump_range_set(struct range_set
*rs
, const char *desc
)
324 printf("range set %s (%d items):\n", desc
, rs
->nr
);
325 for (i
= 0; i
< rs
->nr
; i
++)
326 printf("\t[%ld,%ld]\n", rs
->ranges
[i
].start
, rs
->ranges
[i
].end
);
329 static void dump_line_log_data(struct line_log_data
*r
)
333 snprintf(buf
, 4096, "file %s\n", r
->spec
->path
);
334 dump_range_set(&r
->ranges
, buf
);
339 static void dump_diff_ranges(struct diff_ranges
*diff
, const char *desc
)
342 assert(diff
->parent
.nr
== diff
->target
.nr
);
343 printf("diff ranges %s (%d items):\n", desc
, diff
->parent
.nr
);
344 printf("\tparent\ttarget\n");
345 for (i
= 0; i
< diff
->parent
.nr
; i
++) {
346 printf("\t[%ld,%ld]\t[%ld,%ld]\n",
347 diff
->parent
.ranges
[i
].start
,
348 diff
->parent
.ranges
[i
].end
,
349 diff
->target
.ranges
[i
].start
,
350 diff
->target
.ranges
[i
].end
);
356 static int ranges_overlap(struct range
*a
, struct range
*b
)
358 return !(a
->end
<= b
->start
|| b
->end
<= a
->start
);
362 * Given a diff and the set of interesting ranges, determine all hunks
363 * of the diff which touch (overlap) at least one of the interesting
364 * ranges in the target.
366 static void diff_ranges_filter_touched(struct diff_ranges
*out
,
367 struct diff_ranges
*diff
,
368 struct range_set
*rs
)
372 assert(out
->target
.nr
== 0);
374 for (i
= 0; i
< diff
->target
.nr
; i
++) {
375 while (diff
->target
.ranges
[i
].start
> rs
->ranges
[j
].end
) {
380 if (ranges_overlap(&diff
->target
.ranges
[i
], &rs
->ranges
[j
])) {
381 range_set_append(&out
->parent
,
382 diff
->parent
.ranges
[i
].start
,
383 diff
->parent
.ranges
[i
].end
);
384 range_set_append(&out
->target
,
385 diff
->target
.ranges
[i
].start
,
386 diff
->target
.ranges
[i
].end
);
392 * Adjust the line counts in 'rs' to account for the lines
393 * added/removed in the diff.
395 static void range_set_shift_diff(struct range_set
*out
,
396 struct range_set
*rs
,
397 struct diff_ranges
*diff
)
401 struct range
*src
= rs
->ranges
;
402 struct range
*target
= diff
->target
.ranges
;
403 struct range
*parent
= diff
->parent
.ranges
;
405 for (i
= 0; i
< rs
->nr
; i
++) {
406 while (j
< diff
->target
.nr
&& src
[i
].start
>= target
[j
].start
) {
407 offset
+= (parent
[j
].end
-parent
[j
].start
)
408 - (target
[j
].end
-target
[j
].start
);
411 range_set_append(out
, src
[i
].start
+offset
, src
[i
].end
+offset
);
416 * Given a diff and the set of interesting ranges, map the ranges
417 * across the diff. That is: observe that the target commit takes
418 * blame for all the + (target-side) ranges. So for every pair of
419 * ranges in the diff that was touched, we remove the latter and add
422 static void range_set_map_across_diff(struct range_set
*out
,
423 struct range_set
*rs
,
424 struct diff_ranges
*diff
,
425 struct diff_ranges
**touched_out
)
427 struct diff_ranges
*touched
= xmalloc(sizeof(*touched
));
428 struct range_set tmp1
= RANGE_SET_INIT
;
429 struct range_set tmp2
= RANGE_SET_INIT
;
431 diff_ranges_init(touched
);
432 diff_ranges_filter_touched(touched
, diff
, rs
);
433 range_set_difference(&tmp1
, rs
, &touched
->target
);
434 range_set_shift_diff(&tmp2
, &tmp1
, diff
);
435 range_set_union(out
, &tmp2
, &touched
->parent
);
436 range_set_release(&tmp1
);
437 range_set_release(&tmp2
);
439 *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
,
564 die("malformed -L argument '%s'", range_part
);
570 if (lines
< end
|| lines
< begin
)
571 die("file %s has only %ld lines", name_part
, lines
);
572 line_log_data_insert(&ranges
, spec
, begin
, end
);
581 static struct line_log_data
*line_log_data_copy_one(struct line_log_data
*r
)
583 struct line_log_data
*ret
= xmalloc(sizeof(*ret
));
586 line_log_data_init(ret
);
587 range_set_copy(&ret
->ranges
, &r
->ranges
);
596 static struct line_log_data
*
597 line_log_data_copy(struct line_log_data
*r
)
599 struct line_log_data
*ret
= NULL
;
600 struct line_log_data
*tmp
= NULL
, *prev
= NULL
;
603 ret
= tmp
= prev
= line_log_data_copy_one(r
);
606 tmp
= line_log_data_copy_one(r
);
615 /* merge two range sets across files */
616 static struct line_log_data
*line_log_data_merge(struct line_log_data
*a
,
617 struct line_log_data
*b
)
619 struct line_log_data
*head
= NULL
, **pp
= &head
;
622 struct line_log_data
*src
;
623 struct line_log_data
*src2
= NULL
;
624 struct line_log_data
*d
;
631 cmp
= strcmp(a
->spec
->path
, b
->spec
->path
);
635 } else if (cmp
== 0) {
644 d
= xmalloc(sizeof(struct line_log_data
));
645 line_log_data_init(d
);
651 range_set_union(&d
->ranges
, &src
->ranges
, &src2
->ranges
);
653 range_set_copy(&d
->ranges
, &src
->ranges
);
659 static void add_line_range(struct rev_info
*revs
, struct commit
*commit
,
660 struct line_log_data
*range
)
662 struct line_log_data
*old
= NULL
;
663 struct line_log_data
*new = NULL
;
665 old
= lookup_decoration(&revs
->line_log_data
, &commit
->object
);
667 new = line_log_data_merge(old
, range
);
668 free_line_log_data(old
);
670 new = line_log_data_copy(range
);
673 add_decoration(&revs
->line_log_data
, &commit
->object
, new);
676 static void clear_commit_line_range(struct rev_info
*revs
, struct commit
*commit
)
678 struct line_log_data
*r
;
679 r
= lookup_decoration(&revs
->line_log_data
, &commit
->object
);
682 free_line_log_data(r
);
683 add_decoration(&revs
->line_log_data
, &commit
->object
, NULL
);
686 static struct line_log_data
*lookup_line_range(struct rev_info
*revs
,
687 struct commit
*commit
)
689 struct line_log_data
*ret
= NULL
;
691 ret
= lookup_decoration(&revs
->line_log_data
, &commit
->object
);
695 void line_log_init(struct rev_info
*rev
, const char *prefix
, struct string_list
*args
)
697 struct commit
*commit
= NULL
;
698 struct line_log_data
*range
;
700 commit
= check_single_commit(rev
);
701 range
= parse_lines(commit
, prefix
, args
);
702 add_line_range(rev
, commit
, range
);
704 if (!rev
->diffopt
.detect_rename
) {
706 struct line_log_data
*r
= range
;
712 paths
= xmalloc((count
+1)*sizeof(char *));
714 for (i
= 0; i
< count
; i
++) {
715 paths
[i
] = xstrdup(r
->spec
->path
);
719 init_pathspec(&rev
->diffopt
.pathspec
, paths
);
724 static void load_tree_desc(struct tree_desc
*desc
, void **tree
,
725 const unsigned char *sha1
)
728 *tree
= read_object_with_reference(sha1
, tree_type
, &size
, NULL
);
730 die("Unable to read tree (%s)", sha1_to_hex(sha1
));
731 init_tree_desc(desc
, *tree
, size
);
734 static int count_parents(struct commit
*commit
)
736 struct commit_list
*parents
= commit
->parents
;
740 parents
= parents
->next
;
745 static void move_diff_queue(struct diff_queue_struct
*dst
,
746 struct diff_queue_struct
*src
)
749 memcpy(dst
, src
, sizeof(struct diff_queue_struct
));
750 DIFF_QUEUE_CLEAR(src
);
753 static void queue_diffs(struct diff_options
*opt
,
754 struct diff_queue_struct
*queue
,
755 struct commit
*commit
, struct commit
*parent
)
757 void *tree1
= NULL
, *tree2
= NULL
;
758 struct tree_desc desc1
, desc2
;
761 load_tree_desc(&desc2
, &tree2
, commit
->tree
->object
.sha1
);
763 load_tree_desc(&desc1
, &tree1
, parent
->tree
->object
.sha1
);
765 init_tree_desc(&desc1
, "", 0);
767 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
768 diff_tree(&desc1
, &desc2
, "", opt
);
770 move_diff_queue(queue
, &diff_queued_diff
);
778 static char *get_nth_line(long line
, unsigned long *ends
, void *data
)
783 return (char *)data
+ ends
[line
] + 1;
786 static void print_line(const char *prefix
, char first
,
787 long line
, unsigned long *ends
, void *data
,
788 const char *color
, const char *reset
)
790 char *begin
= get_nth_line(line
, ends
, data
);
791 char *end
= get_nth_line(line
+1, ends
, data
);
794 if (end
> begin
&& end
[-1] == '\n') {
799 fputs(prefix
, stdout
);
800 fputs(color
, stdout
);
802 fwrite(begin
, 1, end
-begin
, stdout
);
803 fputs(reset
, stdout
);
806 fputs("\\ No newline at end of file\n", stdout
);
809 static char *output_prefix(struct diff_options
*opt
)
813 if (opt
->output_prefix
) {
814 struct strbuf
*sb
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
821 static void dump_diff_hacky_one(struct rev_info
*rev
, struct line_log_data
*range
)
824 long p_lines
, t_lines
;
825 unsigned long *p_ends
= NULL
, *t_ends
= NULL
;
826 struct diff_filepair
*pair
= range
->pair
;
827 struct diff_ranges
*diff
= &range
->diff
;
829 struct diff_options
*opt
= &rev
->diffopt
;
830 char *prefix
= output_prefix(opt
);
831 const char *c_reset
= diff_get_color(opt
->use_color
, DIFF_RESET
);
832 const char *c_frag
= diff_get_color(opt
->use_color
, DIFF_FRAGINFO
);
833 const char *c_meta
= diff_get_color(opt
->use_color
, DIFF_METAINFO
);
834 const char *c_old
= diff_get_color(opt
->use_color
, DIFF_FILE_OLD
);
835 const char *c_new
= diff_get_color(opt
->use_color
, DIFF_FILE_NEW
);
836 const char *c_plain
= diff_get_color(opt
->use_color
, DIFF_PLAIN
);
841 if (pair
->one
->sha1_valid
)
842 fill_line_ends(pair
->one
, &p_lines
, &p_ends
);
843 fill_line_ends(pair
->two
, &t_lines
, &t_ends
);
845 printf("%s%sdiff --git a/%s b/%s%s\n", prefix
, c_meta
, pair
->one
->path
, pair
->two
->path
, c_reset
);
846 printf("%s%s--- %s%s%s\n", prefix
, c_meta
,
847 pair
->one
->sha1_valid
? "a/" : "",
848 pair
->one
->sha1_valid
? pair
->one
->path
: "/dev/null",
850 printf("%s%s+++ b/%s%s\n", prefix
, c_meta
, pair
->two
->path
, c_reset
);
851 for (i
= 0; i
< range
->ranges
.nr
; i
++) {
853 long t_start
= range
->ranges
.ranges
[i
].start
;
854 long t_end
= range
->ranges
.ranges
[i
].end
;
855 long t_cur
= t_start
;
858 while (j
< diff
->target
.nr
&& diff
->target
.ranges
[j
].end
< t_start
)
860 if (j
== diff
->target
.nr
|| diff
->target
.ranges
[j
].start
> t_end
)
863 /* Scan ahead to determine the last diff that falls in this range */
865 while (j_last
< diff
->target
.nr
&& diff
->target
.ranges
[j_last
].start
< t_end
)
871 * Compute parent hunk headers: we know that the diff
872 * has the correct line numbers (but not all hunks).
873 * So it suffices to shift the start/end according to
874 * the line numbers of the first/last hunk(s) that
875 * fall in this range.
877 if (t_start
< diff
->target
.ranges
[j
].start
)
878 p_start
= diff
->parent
.ranges
[j
].start
- (diff
->target
.ranges
[j
].start
-t_start
);
880 p_start
= diff
->parent
.ranges
[j
].start
;
881 if (t_end
> diff
->target
.ranges
[j_last
].end
)
882 p_end
= diff
->parent
.ranges
[j_last
].end
+ (t_end
-diff
->target
.ranges
[j_last
].end
);
884 p_end
= diff
->parent
.ranges
[j_last
].end
;
886 if (!p_start
&& !p_end
) {
891 /* Now output a diff hunk for this range */
892 printf("%s%s@@ -%ld,%ld +%ld,%ld @@%s\n",
894 p_start
+1, p_end
-p_start
, t_start
+1, t_end
-t_start
,
896 while (j
< diff
->target
.nr
&& diff
->target
.ranges
[j
].start
< t_end
) {
898 for (; t_cur
< diff
->target
.ranges
[j
].start
; t_cur
++)
899 print_line(prefix
, ' ', t_cur
, t_ends
, pair
->two
->data
,
901 for (k
= diff
->parent
.ranges
[j
].start
; k
< diff
->parent
.ranges
[j
].end
; k
++)
902 print_line(prefix
, '-', k
, p_ends
, pair
->one
->data
,
904 for (; t_cur
< diff
->target
.ranges
[j
].end
&& t_cur
< t_end
; t_cur
++)
905 print_line(prefix
, '+', t_cur
, t_ends
, pair
->two
->data
,
909 for (; t_cur
< t_end
; t_cur
++)
910 print_line(prefix
, ' ', t_cur
, t_ends
, pair
->two
->data
,
919 * NEEDSWORK: manually building a diff here is not the Right
920 * Thing(tm). log -L should be built into the diff pipeline.
922 static void dump_diff_hacky(struct rev_info
*rev
, struct line_log_data
*range
)
924 puts(output_prefix(&rev
->diffopt
));
926 dump_diff_hacky_one(rev
, range
);
932 * Unlike most other functions, this destructively operates on
935 static int process_diff_filepair(struct rev_info
*rev
,
936 struct diff_filepair
*pair
,
937 struct line_log_data
*range
,
938 struct diff_ranges
**diff_out
)
940 struct line_log_data
*rg
= range
;
941 struct range_set tmp
;
942 struct diff_ranges diff
;
943 mmfile_t file_parent
, file_target
;
945 assert(pair
->two
->path
);
947 assert(rg
->spec
->path
);
948 if (!strcmp(rg
->spec
->path
, pair
->two
->path
))
955 if (rg
->ranges
.nr
== 0)
958 assert(pair
->two
->sha1_valid
);
959 diff_populate_filespec(pair
->two
, 0);
960 file_target
.ptr
= pair
->two
->data
;
961 file_target
.size
= pair
->two
->size
;
963 if (pair
->one
->sha1_valid
) {
964 diff_populate_filespec(pair
->one
, 0);
965 file_parent
.ptr
= pair
->one
->data
;
966 file_parent
.size
= pair
->one
->size
;
968 file_parent
.ptr
= "";
969 file_parent
.size
= 0;
972 diff_ranges_init(&diff
);
973 collect_diff(&file_parent
, &file_target
, &diff
);
975 /* NEEDSWORK should apply some heuristics to prevent mismatches */
976 rg
->spec
->path
= xstrdup(pair
->one
->path
);
978 range_set_init(&tmp
, 0);
979 range_set_map_across_diff(&tmp
, &rg
->ranges
, &diff
, diff_out
);
980 range_set_release(&rg
->ranges
);
981 range_set_move(&rg
->ranges
, &tmp
);
983 diff_ranges_release(&diff
);
985 return ((*diff_out
)->parent
.nr
> 0);
988 static struct diff_filepair
*diff_filepair_dup(struct diff_filepair
*pair
)
990 struct diff_filepair
*new = xmalloc(sizeof(struct diff_filepair
));
991 new->one
= pair
->one
;
992 new->two
= pair
->two
;
998 static void free_diffqueues(int n
, struct diff_queue_struct
*dq
)
1001 for (i
= 0; i
< n
; i
++)
1002 for (j
= 0; j
< dq
[i
].nr
; j
++)
1003 diff_free_filepair(dq
[i
].queue
[j
]);
1007 static int process_all_files(struct line_log_data
**range_out
,
1008 struct rev_info
*rev
,
1009 struct diff_queue_struct
*queue
,
1010 struct line_log_data
*range
)
1014 *range_out
= line_log_data_copy(range
);
1016 for (i
= 0; i
< queue
->nr
; i
++) {
1017 struct diff_ranges
*pairdiff
= NULL
;
1018 if (process_diff_filepair(rev
, queue
->queue
[i
], *range_out
, &pairdiff
)) {
1019 struct line_log_data
*rg
= range
;
1021 /* NEEDSWORK tramples over data structures not owned here */
1022 while (rg
&& strcmp(rg
->spec
->path
, queue
->queue
[i
]->two
->path
))
1025 rg
->pair
= diff_filepair_dup(queue
->queue
[i
]);
1026 memcpy(&rg
->diff
, pairdiff
, sizeof(struct diff_ranges
));
1033 int line_log_print(struct rev_info
*rev
, struct commit
*commit
)
1035 struct line_log_data
*range
= lookup_line_range(rev
, commit
);
1038 dump_diff_hacky(rev
, range
);
1042 static int process_ranges_ordinary_commit(struct rev_info
*rev
, struct commit
*commit
,
1043 struct line_log_data
*range
)
1045 struct commit
*parent
= NULL
;
1046 struct diff_queue_struct queue
;
1047 struct line_log_data
*parent_range
;
1050 if (commit
->parents
)
1051 parent
= commit
->parents
->item
;
1053 queue_diffs(&rev
->diffopt
, &queue
, commit
, parent
);
1054 changed
= process_all_files(&parent_range
, rev
, &queue
, range
);
1056 add_line_range(rev
, parent
, parent_range
);
1060 static int process_ranges_merge_commit(struct rev_info
*rev
, struct commit
*commit
,
1061 struct line_log_data
*range
)
1063 struct diff_queue_struct
*diffqueues
;
1064 struct line_log_data
**cand
;
1065 struct commit
**parents
;
1066 struct commit_list
*p
;
1068 int nparents
= count_parents(commit
);
1070 diffqueues
= xmalloc(nparents
* sizeof(*diffqueues
));
1071 cand
= xmalloc(nparents
* sizeof(*cand
));
1072 parents
= xmalloc(nparents
* sizeof(*parents
));
1074 p
= commit
->parents
;
1075 for (i
= 0; i
< nparents
; i
++) {
1076 parents
[i
] = p
->item
;
1078 queue_diffs(&rev
->diffopt
, &diffqueues
[i
], commit
, parents
[i
]);
1081 for (i
= 0; i
< nparents
; i
++) {
1084 changed
= process_all_files(&cand
[i
], rev
, &diffqueues
[i
], range
);
1087 * This parent can take all the blame, so we
1088 * don't follow any other path in history
1090 add_line_range(rev
, parents
[i
], cand
[i
]);
1091 clear_commit_line_range(rev
, commit
);
1092 commit
->parents
= xmalloc(sizeof(struct commit_list
));
1093 commit
->parents
->item
= parents
[i
];
1094 commit
->parents
->next
= NULL
;
1097 free_diffqueues(nparents
, diffqueues
);
1098 /* NEEDSWORK leaking like a sieve */
1104 * No single parent took the blame. We add the candidates
1105 * from the above loop to the parents.
1107 for (i
= 0; i
< nparents
; i
++) {
1108 add_line_range(rev
, parents
[i
], cand
[i
]);
1111 clear_commit_line_range(rev
, commit
);
1114 free_diffqueues(nparents
, diffqueues
);
1117 /* NEEDSWORK evil merge detection stuff */
1118 /* NEEDSWORK leaking like a sieve */
1121 static int process_ranges_arbitrary_commit(struct rev_info
*rev
, struct commit
*commit
)
1123 struct line_log_data
*range
= lookup_line_range(rev
, commit
);
1127 if (!commit
->parents
|| !commit
->parents
->next
)
1128 changed
= process_ranges_ordinary_commit(rev
, commit
, range
);
1130 changed
= process_ranges_merge_commit(rev
, commit
, range
);
1134 commit
->object
.flags
|= TREESAME
;
1139 static enum rewrite_result
line_log_rewrite_one(struct rev_info
*rev
, struct commit
**pp
)
1142 struct commit
*p
= *pp
;
1143 if (p
->parents
&& p
->parents
->next
)
1144 return rewrite_one_ok
;
1145 if (p
->object
.flags
& UNINTERESTING
)
1146 return rewrite_one_ok
;
1147 if (!(p
->object
.flags
& TREESAME
))
1148 return rewrite_one_ok
;
1150 return rewrite_one_noparents
;
1151 *pp
= p
->parents
->item
;
1155 int line_log_filter(struct rev_info
*rev
)
1157 struct commit
*commit
;
1158 struct commit_list
*list
= rev
->commits
;
1159 struct commit_list
*out
= NULL
, **pp
= &out
;
1162 struct commit_list
*to_free
= NULL
;
1163 commit
= list
->item
;
1164 if (process_ranges_arbitrary_commit(rev
, commit
)) {
1174 for (list
= out
; list
; list
= list
->next
)
1175 rewrite_parents(rev
, list
->item
, line_log_rewrite_one
);