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_unsafe(struct range_set
*rs
, long a
, long b
)
62 range_set_grow(rs
, 1);
63 rs
->ranges
[rs
->nr
].start
= a
;
64 rs
->ranges
[rs
->nr
].end
= b
;
68 static void range_set_append(struct range_set
*rs
, long a
, long b
)
70 assert(rs
->nr
== 0 || rs
->ranges
[rs
->nr
-1].end
<= a
);
71 range_set_append_unsafe(rs
, a
, b
);
74 static int range_cmp(const void *_r
, const void *_s
)
76 const struct range
*r
= _r
;
77 const struct range
*s
= _s
;
79 /* this could be simply 'return r.start-s.start', but for the types */
80 if (r
->start
== s
->start
)
82 if (r
->start
< s
->start
)
88 * Check that the ranges are non-empty, sorted and non-overlapping
90 static void range_set_check_invariants(struct range_set
*rs
)
98 assert(rs
->ranges
[0].start
< rs
->ranges
[0].end
);
100 for (i
= 1; i
< rs
->nr
; i
++) {
101 assert(rs
->ranges
[i
-1].end
< rs
->ranges
[i
].start
);
102 assert(rs
->ranges
[i
].start
< rs
->ranges
[i
].end
);
107 * In-place pass of sorting and merging the ranges in the range set,
108 * to establish the invariants when we get the ranges from the user
110 static void sort_and_merge_range_set(struct range_set
*rs
)
113 int o
= 1; /* output cursor */
115 qsort(rs
->ranges
, rs
->nr
, sizeof(struct range
), range_cmp
);
117 for (i
= 1; i
< rs
->nr
; i
++) {
118 if (rs
->ranges
[i
].start
<= rs
->ranges
[o
-1].end
) {
119 rs
->ranges
[o
-1].end
= rs
->ranges
[i
].end
;
121 rs
->ranges
[o
].start
= rs
->ranges
[i
].start
;
122 rs
->ranges
[o
].end
= rs
->ranges
[i
].end
;
129 range_set_check_invariants(rs
);
133 * Union of range sets (i.e., sets of line numbers). Used to merge
134 * them when searches meet at a common ancestor.
136 * This is also where the ranges are consolidated into canonical form:
137 * overlapping and adjacent ranges are merged, and empty ranges are
140 static void range_set_union(struct range_set
*out
,
141 struct range_set
*a
, struct range_set
*b
)
143 int i
= 0, j
= 0, o
= 0;
144 struct range
*ra
= a
->ranges
;
145 struct range
*rb
= b
->ranges
;
146 /* cannot make an alias of out->ranges: it may change during grow */
148 assert(out
->nr
== 0);
149 while (i
< a
->nr
|| j
< b
->nr
) {
151 if (i
< a
->nr
&& j
< b
->nr
) {
152 if (ra
[i
].start
< rb
[j
].start
)
154 else if (ra
[i
].start
> rb
[j
].start
)
156 else if (ra
[i
].end
< rb
[j
].end
)
160 } else if (i
< a
->nr
) /* b exhausted */
162 else /* a exhausted */
164 if (new->start
== new->end
)
166 else if (!o
|| out
->ranges
[o
-1].end
< new->start
) {
167 range_set_grow(out
, 1);
168 out
->ranges
[o
].start
= new->start
;
169 out
->ranges
[o
].end
= new->end
;
171 } else if (out
->ranges
[o
-1].end
< new->end
) {
172 out
->ranges
[o
-1].end
= new->end
;
179 * Difference of range sets (out = a \ b). Pass the "interesting"
180 * ranges as 'a' and the target side of the diff as 'b': it removes
181 * the ranges for which the commit is responsible.
183 static void range_set_difference(struct range_set
*out
,
184 struct range_set
*a
, struct range_set
*b
)
187 for (i
= 0; i
< a
->nr
; i
++) {
188 long start
= a
->ranges
[i
].start
;
189 long end
= a
->ranges
[i
].end
;
190 while (start
< end
) {
191 while (j
< b
->nr
&& start
>= b
->ranges
[j
].end
)
197 if (j
>= b
->nr
|| end
< b
->ranges
[j
].start
) {
203 range_set_append(out
, start
, end
);
206 if (start
>= b
->ranges
[j
].start
) {
211 start
= b
->ranges
[j
].end
;
212 } else if (end
> b
->ranges
[j
].start
) {
217 if (start
< b
->ranges
[j
].start
)
218 range_set_append(out
, start
, b
->ranges
[j
].start
);
219 start
= b
->ranges
[j
].end
;
225 static void diff_ranges_init(struct diff_ranges
*diff
)
227 range_set_init(&diff
->parent
, 0);
228 range_set_init(&diff
->target
, 0);
231 static void diff_ranges_release(struct diff_ranges
*diff
)
233 range_set_release(&diff
->parent
);
234 range_set_release(&diff
->target
);
237 void line_log_data_init(struct line_log_data
*r
)
239 memset(r
, 0, sizeof(struct line_log_data
));
240 range_set_init(&r
->ranges
, 0);
243 static void line_log_data_clear(struct line_log_data
*r
)
245 range_set_release(&r
->ranges
);
247 diff_free_filepair(r
->pair
);
250 static void free_line_log_data(struct line_log_data
*r
)
253 struct line_log_data
*next
= r
->next
;
254 line_log_data_clear(r
);
260 static struct line_log_data
*
261 search_line_log_data(struct line_log_data
*list
, const char *path
,
262 struct line_log_data
**insertion_point
)
264 struct line_log_data
*p
= list
;
266 *insertion_point
= NULL
;
268 int cmp
= strcmp(p
->path
, path
);
271 if (insertion_point
&& cmp
< 0)
272 *insertion_point
= p
;
279 * Note: takes ownership of 'path', which happens to be what the only
282 static void line_log_data_insert(struct line_log_data
**list
,
284 long begin
, long end
)
286 struct line_log_data
*ip
;
287 struct line_log_data
*p
= search_line_log_data(*list
, path
, &ip
);
290 range_set_append_unsafe(&p
->ranges
, begin
, end
);
291 sort_and_merge_range_set(&p
->ranges
);
296 p
= xcalloc(1, sizeof(struct line_log_data
));
298 range_set_append(&p
->ranges
, begin
, end
);
308 struct collect_diff_cbdata
{
309 struct diff_ranges
*diff
;
312 static int collect_diff_cb(long start_a
, long count_a
,
313 long start_b
, long count_b
,
316 struct collect_diff_cbdata
*d
= data
;
319 range_set_append(&d
->diff
->parent
, start_a
, start_a
+ count_a
);
321 range_set_append(&d
->diff
->target
, start_b
, start_b
+ count_b
);
326 static void collect_diff(mmfile_t
*parent
, mmfile_t
*target
, struct diff_ranges
*out
)
328 struct collect_diff_cbdata cbdata
= {NULL
};
333 memset(&xpp
, 0, sizeof(xpp
));
334 memset(&xecfg
, 0, sizeof(xecfg
));
335 xecfg
.ctxlen
= xecfg
.interhunkctxlen
= 0;
338 xecfg
.hunk_func
= collect_diff_cb
;
339 memset(&ecb
, 0, sizeof(ecb
));
341 xdi_diff(parent
, target
, &xpp
, &xecfg
, &ecb
);
345 * These are handy for debugging. Removing them with #if 0 silences
346 * the "unused function" warning.
349 static void dump_range_set(struct range_set
*rs
, const char *desc
)
352 printf("range set %s (%d items):\n", desc
, rs
->nr
);
353 for (i
= 0; i
< rs
->nr
; i
++)
354 printf("\t[%ld,%ld]\n", rs
->ranges
[i
].start
, rs
->ranges
[i
].end
);
357 static void dump_line_log_data(struct line_log_data
*r
)
361 snprintf(buf
, 4096, "file %s\n", r
->path
);
362 dump_range_set(&r
->ranges
, buf
);
367 static void dump_diff_ranges(struct diff_ranges
*diff
, const char *desc
)
370 assert(diff
->parent
.nr
== diff
->target
.nr
);
371 printf("diff ranges %s (%d items):\n", desc
, diff
->parent
.nr
);
372 printf("\tparent\ttarget\n");
373 for (i
= 0; i
< diff
->parent
.nr
; i
++) {
374 printf("\t[%ld,%ld]\t[%ld,%ld]\n",
375 diff
->parent
.ranges
[i
].start
,
376 diff
->parent
.ranges
[i
].end
,
377 diff
->target
.ranges
[i
].start
,
378 diff
->target
.ranges
[i
].end
);
384 static int ranges_overlap(struct range
*a
, struct range
*b
)
386 return !(a
->end
<= b
->start
|| b
->end
<= a
->start
);
390 * Given a diff and the set of interesting ranges, determine all hunks
391 * of the diff which touch (overlap) at least one of the interesting
392 * ranges in the target.
394 static void diff_ranges_filter_touched(struct diff_ranges
*out
,
395 struct diff_ranges
*diff
,
396 struct range_set
*rs
)
400 assert(out
->target
.nr
== 0);
402 for (i
= 0; i
< diff
->target
.nr
; i
++) {
403 while (diff
->target
.ranges
[i
].start
> rs
->ranges
[j
].end
) {
408 if (ranges_overlap(&diff
->target
.ranges
[i
], &rs
->ranges
[j
])) {
409 range_set_append(&out
->parent
,
410 diff
->parent
.ranges
[i
].start
,
411 diff
->parent
.ranges
[i
].end
);
412 range_set_append(&out
->target
,
413 diff
->target
.ranges
[i
].start
,
414 diff
->target
.ranges
[i
].end
);
420 * Adjust the line counts in 'rs' to account for the lines
421 * added/removed in the diff.
423 static void range_set_shift_diff(struct range_set
*out
,
424 struct range_set
*rs
,
425 struct diff_ranges
*diff
)
429 struct range
*src
= rs
->ranges
;
430 struct range
*target
= diff
->target
.ranges
;
431 struct range
*parent
= diff
->parent
.ranges
;
433 for (i
= 0; i
< rs
->nr
; i
++) {
434 while (j
< diff
->target
.nr
&& src
[i
].start
>= target
[j
].start
) {
435 offset
+= (parent
[j
].end
-parent
[j
].start
)
436 - (target
[j
].end
-target
[j
].start
);
439 range_set_append(out
, src
[i
].start
+offset
, src
[i
].end
+offset
);
444 * Given a diff and the set of interesting ranges, map the ranges
445 * across the diff. That is: observe that the target commit takes
446 * blame for all the + (target-side) ranges. So for every pair of
447 * ranges in the diff that was touched, we remove the latter and add
450 static void range_set_map_across_diff(struct range_set
*out
,
451 struct range_set
*rs
,
452 struct diff_ranges
*diff
,
453 struct diff_ranges
**touched_out
)
455 struct diff_ranges
*touched
= xmalloc(sizeof(*touched
));
456 struct range_set tmp1
= RANGE_SET_INIT
;
457 struct range_set tmp2
= RANGE_SET_INIT
;
459 diff_ranges_init(touched
);
460 diff_ranges_filter_touched(touched
, diff
, rs
);
461 range_set_difference(&tmp1
, rs
, &touched
->target
);
462 range_set_shift_diff(&tmp2
, &tmp1
, diff
);
463 range_set_union(out
, &tmp2
, &touched
->parent
);
464 range_set_release(&tmp1
);
465 range_set_release(&tmp2
);
467 *touched_out
= touched
;
470 static struct commit
*check_single_commit(struct rev_info
*revs
)
472 struct object
*commit
= NULL
;
476 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
477 struct object
*obj
= revs
->pending
.objects
[i
].item
;
478 if (obj
->flags
& UNINTERESTING
)
480 while (obj
->type
== OBJ_TAG
)
481 obj
= deref_tag(obj
, NULL
, 0);
482 if (obj
->type
!= OBJ_COMMIT
)
483 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
485 die("More than one commit to dig from: %s and %s?",
486 revs
->pending
.objects
[i
].name
,
487 revs
->pending
.objects
[found
].name
);
493 die("No commit specified?");
495 return (struct commit
*) commit
;
498 static void fill_blob_sha1(struct commit
*commit
, struct diff_filespec
*spec
)
501 unsigned char sha1
[20];
503 if (get_tree_entry(commit
->object
.sha1
, spec
->path
,
505 die("There is no path %s in the commit", spec
->path
);
506 fill_filespec(spec
, sha1
, 1, mode
);
511 static void fill_line_ends(struct diff_filespec
*spec
, long *lines
,
512 unsigned long **line_ends
)
514 int num
= 0, size
= 50;
516 unsigned long *ends
= NULL
;
519 if (diff_populate_filespec(spec
, 0))
520 die("Cannot read blob %s", sha1_to_hex(spec
->sha1
));
522 ends
= xmalloc(size
* sizeof(*ends
));
525 while (num
< spec
->size
) {
526 if (data
[num
] == '\n' || num
== spec
->size
- 1) {
527 ALLOC_GROW(ends
, (cur
+ 1), size
);
533 /* shrink the array to fit the elements */
534 ends
= xrealloc(ends
, cur
* sizeof(*ends
));
540 struct diff_filespec
*spec
;
542 unsigned long *line_ends
;
545 static const char *nth_line(void *data
, long line
)
547 struct nth_line_cb
*d
= data
;
548 assert(d
&& line
<= d
->lines
);
549 assert(d
->spec
&& d
->spec
->data
);
552 return (char *)d
->spec
->data
;
554 return (char *)d
->spec
->data
+ d
->line_ends
[line
] + 1;
557 static struct line_log_data
*
558 parse_lines(struct commit
*commit
, const char *prefix
, struct string_list
*args
)
561 unsigned long *ends
= NULL
;
562 struct nth_line_cb cb_data
;
563 struct string_list_item
*item
;
564 struct line_log_data
*ranges
= NULL
;
566 for_each_string_list_item(item
, args
) {
567 const char *name_part
, *range_part
;
569 struct diff_filespec
*spec
;
570 long begin
= 0, end
= 0;
572 name_part
= skip_range_arg(item
->string
);
573 if (!name_part
|| *name_part
!= ':' || !name_part
[1])
574 die("-L argument '%s' not of the form start,end:file",
576 range_part
= xstrndup(item
->string
, name_part
- item
->string
);
579 full_name
= prefix_path(prefix
, prefix
? strlen(prefix
) : 0,
582 spec
= alloc_filespec(full_name
);
583 fill_blob_sha1(commit
, spec
);
584 fill_line_ends(spec
, &lines
, &ends
);
586 cb_data
.lines
= lines
;
587 cb_data
.line_ends
= ends
;
589 if (parse_range_arg(range_part
, nth_line
, &cb_data
,
592 die("malformed -L argument '%s'", range_part
);
598 if (lines
< end
|| lines
< begin
)
599 die("file %s has only %ld lines", name_part
, lines
);
600 line_log_data_insert(&ranges
, full_name
, begin
, end
);
610 static struct line_log_data
*line_log_data_copy_one(struct line_log_data
*r
)
612 struct line_log_data
*ret
= xmalloc(sizeof(*ret
));
615 line_log_data_init(ret
);
616 range_set_copy(&ret
->ranges
, &r
->ranges
);
618 ret
->path
= xstrdup(r
->path
);
623 static struct line_log_data
*
624 line_log_data_copy(struct line_log_data
*r
)
626 struct line_log_data
*ret
= NULL
;
627 struct line_log_data
*tmp
= NULL
, *prev
= NULL
;
630 ret
= tmp
= prev
= line_log_data_copy_one(r
);
633 tmp
= line_log_data_copy_one(r
);
642 /* merge two range sets across files */
643 static struct line_log_data
*line_log_data_merge(struct line_log_data
*a
,
644 struct line_log_data
*b
)
646 struct line_log_data
*head
= NULL
, **pp
= &head
;
649 struct line_log_data
*src
;
650 struct line_log_data
*src2
= NULL
;
651 struct line_log_data
*d
;
658 cmp
= strcmp(a
->path
, b
->path
);
662 } else if (cmp
== 0) {
671 d
= xmalloc(sizeof(struct line_log_data
));
672 line_log_data_init(d
);
673 d
->path
= xstrdup(src
->path
);
677 range_set_union(&d
->ranges
, &src
->ranges
, &src2
->ranges
);
679 range_set_copy(&d
->ranges
, &src
->ranges
);
685 static void add_line_range(struct rev_info
*revs
, struct commit
*commit
,
686 struct line_log_data
*range
)
688 struct line_log_data
*old
= NULL
;
689 struct line_log_data
*new = NULL
;
691 old
= lookup_decoration(&revs
->line_log_data
, &commit
->object
);
693 new = line_log_data_merge(old
, range
);
694 free_line_log_data(old
);
696 new = line_log_data_copy(range
);
699 add_decoration(&revs
->line_log_data
, &commit
->object
, new);
702 static void clear_commit_line_range(struct rev_info
*revs
, struct commit
*commit
)
704 struct line_log_data
*r
;
705 r
= lookup_decoration(&revs
->line_log_data
, &commit
->object
);
708 free_line_log_data(r
);
709 add_decoration(&revs
->line_log_data
, &commit
->object
, NULL
);
712 static struct line_log_data
*lookup_line_range(struct rev_info
*revs
,
713 struct commit
*commit
)
715 struct line_log_data
*ret
= NULL
;
716 struct line_log_data
*d
;
718 ret
= lookup_decoration(&revs
->line_log_data
, &commit
->object
);
720 for (d
= ret
; d
; d
= d
->next
)
721 range_set_check_invariants(&d
->ranges
);
726 void line_log_init(struct rev_info
*rev
, const char *prefix
, struct string_list
*args
)
728 struct commit
*commit
= NULL
;
729 struct line_log_data
*range
;
731 commit
= check_single_commit(rev
);
732 range
= parse_lines(commit
, prefix
, args
);
733 add_line_range(rev
, commit
, range
);
735 if (!rev
->diffopt
.detect_rename
) {
737 struct line_log_data
*r
= range
;
743 paths
= xmalloc((count
+1)*sizeof(char *));
745 for (i
= 0; i
< count
; i
++) {
746 paths
[i
] = xstrdup(r
->path
);
750 init_pathspec(&rev
->diffopt
.pathspec
, paths
);
755 static void load_tree_desc(struct tree_desc
*desc
, void **tree
,
756 const unsigned char *sha1
)
759 *tree
= read_object_with_reference(sha1
, tree_type
, &size
, NULL
);
761 die("Unable to read tree (%s)", sha1_to_hex(sha1
));
762 init_tree_desc(desc
, *tree
, size
);
765 static int count_parents(struct commit
*commit
)
767 struct commit_list
*parents
= commit
->parents
;
771 parents
= parents
->next
;
776 static void move_diff_queue(struct diff_queue_struct
*dst
,
777 struct diff_queue_struct
*src
)
780 memcpy(dst
, src
, sizeof(struct diff_queue_struct
));
781 DIFF_QUEUE_CLEAR(src
);
784 static void filter_diffs_for_paths(struct line_log_data
*range
, int keep_deletions
)
787 struct diff_queue_struct outq
;
788 DIFF_QUEUE_CLEAR(&outq
);
790 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
791 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
792 struct line_log_data
*rg
= NULL
;
794 if (!DIFF_FILE_VALID(p
->two
)) {
798 diff_free_filepair(p
);
801 for (rg
= range
; rg
; rg
= rg
->next
) {
802 if (!strcmp(rg
->path
, p
->two
->path
))
808 diff_free_filepair(p
);
810 free(diff_queued_diff
.queue
);
811 diff_queued_diff
= outq
;
814 static inline int diff_might_be_rename(void)
817 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
818 if (!DIFF_FILE_VALID(diff_queued_diff
.queue
[i
]->one
)) {
819 /* fprintf(stderr, "diff_might_be_rename found creation of: %s\n", */
820 /* diff_queued_diff.queue[i]->two->path); */
826 static void queue_diffs(struct line_log_data
*range
,
827 struct diff_options
*opt
,
828 struct diff_queue_struct
*queue
,
829 struct commit
*commit
, struct commit
*parent
)
831 void *tree1
= NULL
, *tree2
= NULL
;
832 struct tree_desc desc1
, desc2
;
835 load_tree_desc(&desc2
, &tree2
, commit
->tree
->object
.sha1
);
837 load_tree_desc(&desc1
, &tree1
, parent
->tree
->object
.sha1
);
839 init_tree_desc(&desc1
, "", 0);
841 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
842 diff_tree(&desc1
, &desc2
, "", opt
);
843 if (opt
->detect_rename
) {
844 filter_diffs_for_paths(range
, 1);
845 if (diff_might_be_rename())
847 filter_diffs_for_paths(range
, 0);
849 move_diff_queue(queue
, &diff_queued_diff
);
857 static char *get_nth_line(long line
, unsigned long *ends
, void *data
)
862 return (char *)data
+ ends
[line
] + 1;
865 static void print_line(const char *prefix
, char first
,
866 long line
, unsigned long *ends
, void *data
,
867 const char *color
, const char *reset
)
869 char *begin
= get_nth_line(line
, ends
, data
);
870 char *end
= get_nth_line(line
+1, ends
, data
);
873 if (end
> begin
&& end
[-1] == '\n') {
878 fputs(prefix
, stdout
);
879 fputs(color
, stdout
);
881 fwrite(begin
, 1, end
-begin
, stdout
);
882 fputs(reset
, stdout
);
885 fputs("\\ No newline at end of file\n", stdout
);
888 static char *output_prefix(struct diff_options
*opt
)
892 if (opt
->output_prefix
) {
893 struct strbuf
*sb
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
900 static void dump_diff_hacky_one(struct rev_info
*rev
, struct line_log_data
*range
)
903 long p_lines
, t_lines
;
904 unsigned long *p_ends
= NULL
, *t_ends
= NULL
;
905 struct diff_filepair
*pair
= range
->pair
;
906 struct diff_ranges
*diff
= &range
->diff
;
908 struct diff_options
*opt
= &rev
->diffopt
;
909 char *prefix
= output_prefix(opt
);
910 const char *c_reset
= diff_get_color(opt
->use_color
, DIFF_RESET
);
911 const char *c_frag
= diff_get_color(opt
->use_color
, DIFF_FRAGINFO
);
912 const char *c_meta
= diff_get_color(opt
->use_color
, DIFF_METAINFO
);
913 const char *c_old
= diff_get_color(opt
->use_color
, DIFF_FILE_OLD
);
914 const char *c_new
= diff_get_color(opt
->use_color
, DIFF_FILE_NEW
);
915 const char *c_plain
= diff_get_color(opt
->use_color
, DIFF_PLAIN
);
920 if (pair
->one
->sha1_valid
)
921 fill_line_ends(pair
->one
, &p_lines
, &p_ends
);
922 fill_line_ends(pair
->two
, &t_lines
, &t_ends
);
924 printf("%s%sdiff --git a/%s b/%s%s\n", prefix
, c_meta
, pair
->one
->path
, pair
->two
->path
, c_reset
);
925 printf("%s%s--- %s%s%s\n", prefix
, c_meta
,
926 pair
->one
->sha1_valid
? "a/" : "",
927 pair
->one
->sha1_valid
? pair
->one
->path
: "/dev/null",
929 printf("%s%s+++ b/%s%s\n", prefix
, c_meta
, pair
->two
->path
, c_reset
);
930 for (i
= 0; i
< range
->ranges
.nr
; i
++) {
932 long t_start
= range
->ranges
.ranges
[i
].start
;
933 long t_end
= range
->ranges
.ranges
[i
].end
;
934 long t_cur
= t_start
;
937 while (j
< diff
->target
.nr
&& diff
->target
.ranges
[j
].end
< t_start
)
939 if (j
== diff
->target
.nr
|| diff
->target
.ranges
[j
].start
> t_end
)
942 /* Scan ahead to determine the last diff that falls in this range */
944 while (j_last
< diff
->target
.nr
&& diff
->target
.ranges
[j_last
].start
< t_end
)
950 * Compute parent hunk headers: we know that the diff
951 * has the correct line numbers (but not all hunks).
952 * So it suffices to shift the start/end according to
953 * the line numbers of the first/last hunk(s) that
954 * fall in this range.
956 if (t_start
< diff
->target
.ranges
[j
].start
)
957 p_start
= diff
->parent
.ranges
[j
].start
- (diff
->target
.ranges
[j
].start
-t_start
);
959 p_start
= diff
->parent
.ranges
[j
].start
;
960 if (t_end
> diff
->target
.ranges
[j_last
].end
)
961 p_end
= diff
->parent
.ranges
[j_last
].end
+ (t_end
-diff
->target
.ranges
[j_last
].end
);
963 p_end
= diff
->parent
.ranges
[j_last
].end
;
965 if (!p_start
&& !p_end
) {
970 /* Now output a diff hunk for this range */
971 printf("%s%s@@ -%ld,%ld +%ld,%ld @@%s\n",
973 p_start
+1, p_end
-p_start
, t_start
+1, t_end
-t_start
,
975 while (j
< diff
->target
.nr
&& diff
->target
.ranges
[j
].start
< t_end
) {
977 for (; t_cur
< diff
->target
.ranges
[j
].start
; t_cur
++)
978 print_line(prefix
, ' ', t_cur
, t_ends
, pair
->two
->data
,
980 for (k
= diff
->parent
.ranges
[j
].start
; k
< diff
->parent
.ranges
[j
].end
; k
++)
981 print_line(prefix
, '-', k
, p_ends
, pair
->one
->data
,
983 for (; t_cur
< diff
->target
.ranges
[j
].end
&& t_cur
< t_end
; t_cur
++)
984 print_line(prefix
, '+', t_cur
, t_ends
, pair
->two
->data
,
988 for (; t_cur
< t_end
; t_cur
++)
989 print_line(prefix
, ' ', t_cur
, t_ends
, pair
->two
->data
,
998 * NEEDSWORK: manually building a diff here is not the Right
999 * Thing(tm). log -L should be built into the diff pipeline.
1001 static void dump_diff_hacky(struct rev_info
*rev
, struct line_log_data
*range
)
1003 puts(output_prefix(&rev
->diffopt
));
1005 dump_diff_hacky_one(rev
, range
);
1006 range
= range
->next
;
1011 * Unlike most other functions, this destructively operates on
1014 static int process_diff_filepair(struct rev_info
*rev
,
1015 struct diff_filepair
*pair
,
1016 struct line_log_data
*range
,
1017 struct diff_ranges
**diff_out
)
1019 struct line_log_data
*rg
= range
;
1020 struct range_set tmp
;
1021 struct diff_ranges diff
;
1022 mmfile_t file_parent
, file_target
;
1024 assert(pair
->two
->path
);
1027 if (!strcmp(rg
->path
, pair
->two
->path
))
1034 if (rg
->ranges
.nr
== 0)
1037 assert(pair
->two
->sha1_valid
);
1038 diff_populate_filespec(pair
->two
, 0);
1039 file_target
.ptr
= pair
->two
->data
;
1040 file_target
.size
= pair
->two
->size
;
1042 if (pair
->one
->sha1_valid
) {
1043 diff_populate_filespec(pair
->one
, 0);
1044 file_parent
.ptr
= pair
->one
->data
;
1045 file_parent
.size
= pair
->one
->size
;
1047 file_parent
.ptr
= "";
1048 file_parent
.size
= 0;
1051 diff_ranges_init(&diff
);
1052 collect_diff(&file_parent
, &file_target
, &diff
);
1054 /* NEEDSWORK should apply some heuristics to prevent mismatches */
1056 rg
->path
= xstrdup(pair
->one
->path
);
1058 range_set_init(&tmp
, 0);
1059 range_set_map_across_diff(&tmp
, &rg
->ranges
, &diff
, diff_out
);
1060 range_set_release(&rg
->ranges
);
1061 range_set_move(&rg
->ranges
, &tmp
);
1063 diff_ranges_release(&diff
);
1065 return ((*diff_out
)->parent
.nr
> 0);
1068 static struct diff_filepair
*diff_filepair_dup(struct diff_filepair
*pair
)
1070 struct diff_filepair
*new = xmalloc(sizeof(struct diff_filepair
));
1071 new->one
= pair
->one
;
1072 new->two
= pair
->two
;
1078 static void free_diffqueues(int n
, struct diff_queue_struct
*dq
)
1081 for (i
= 0; i
< n
; i
++)
1082 for (j
= 0; j
< dq
[i
].nr
; j
++)
1083 diff_free_filepair(dq
[i
].queue
[j
]);
1087 static int process_all_files(struct line_log_data
**range_out
,
1088 struct rev_info
*rev
,
1089 struct diff_queue_struct
*queue
,
1090 struct line_log_data
*range
)
1094 *range_out
= line_log_data_copy(range
);
1096 for (i
= 0; i
< queue
->nr
; i
++) {
1097 struct diff_ranges
*pairdiff
= NULL
;
1098 struct diff_filepair
*pair
= queue
->queue
[i
];
1099 if (process_diff_filepair(rev
, pair
, *range_out
, &pairdiff
)) {
1101 * Store away the diff for later output. We
1102 * tuck it in the ranges we got as _input_,
1103 * since that's the commit that caused the
1106 * NEEDSWORK not enough when we get around to
1107 * doing something interesting with merges;
1108 * currently each invocation on a merge parent
1109 * trashes the previous one's diff.
1111 * NEEDSWORK tramples over data structures not owned here
1113 struct line_log_data
*rg
= range
;
1115 while (rg
&& strcmp(rg
->path
, pair
->two
->path
))
1118 rg
->pair
= diff_filepair_dup(queue
->queue
[i
]);
1119 memcpy(&rg
->diff
, pairdiff
, sizeof(struct diff_ranges
));
1126 int line_log_print(struct rev_info
*rev
, struct commit
*commit
)
1128 struct line_log_data
*range
= lookup_line_range(rev
, commit
);
1131 dump_diff_hacky(rev
, range
);
1135 static int process_ranges_ordinary_commit(struct rev_info
*rev
, struct commit
*commit
,
1136 struct line_log_data
*range
)
1138 struct commit
*parent
= NULL
;
1139 struct diff_queue_struct queue
;
1140 struct line_log_data
*parent_range
;
1143 if (commit
->parents
)
1144 parent
= commit
->parents
->item
;
1146 queue_diffs(range
, &rev
->diffopt
, &queue
, commit
, parent
);
1147 changed
= process_all_files(&parent_range
, rev
, &queue
, range
);
1149 add_line_range(rev
, parent
, parent_range
);
1153 static int process_ranges_merge_commit(struct rev_info
*rev
, struct commit
*commit
,
1154 struct line_log_data
*range
)
1156 struct diff_queue_struct
*diffqueues
;
1157 struct line_log_data
**cand
;
1158 struct commit
**parents
;
1159 struct commit_list
*p
;
1161 int nparents
= count_parents(commit
);
1163 diffqueues
= xmalloc(nparents
* sizeof(*diffqueues
));
1164 cand
= xmalloc(nparents
* sizeof(*cand
));
1165 parents
= xmalloc(nparents
* sizeof(*parents
));
1167 p
= commit
->parents
;
1168 for (i
= 0; i
< nparents
; i
++) {
1169 parents
[i
] = p
->item
;
1171 queue_diffs(range
, &rev
->diffopt
, &diffqueues
[i
], commit
, parents
[i
]);
1174 for (i
= 0; i
< nparents
; i
++) {
1177 changed
= process_all_files(&cand
[i
], rev
, &diffqueues
[i
], range
);
1180 * This parent can take all the blame, so we
1181 * don't follow any other path in history
1183 add_line_range(rev
, parents
[i
], cand
[i
]);
1184 clear_commit_line_range(rev
, commit
);
1185 commit
->parents
= xmalloc(sizeof(struct commit_list
));
1186 commit
->parents
->item
= parents
[i
];
1187 commit
->parents
->next
= NULL
;
1190 free_diffqueues(nparents
, diffqueues
);
1191 /* NEEDSWORK leaking like a sieve */
1197 * No single parent took the blame. We add the candidates
1198 * from the above loop to the parents.
1200 for (i
= 0; i
< nparents
; i
++) {
1201 add_line_range(rev
, parents
[i
], cand
[i
]);
1204 clear_commit_line_range(rev
, commit
);
1207 free_diffqueues(nparents
, diffqueues
);
1210 /* NEEDSWORK evil merge detection stuff */
1211 /* NEEDSWORK leaking like a sieve */
1214 static int process_ranges_arbitrary_commit(struct rev_info
*rev
, struct commit
*commit
)
1216 struct line_log_data
*range
= lookup_line_range(rev
, commit
);
1220 if (!commit
->parents
|| !commit
->parents
->next
)
1221 changed
= process_ranges_ordinary_commit(rev
, commit
, range
);
1223 changed
= process_ranges_merge_commit(rev
, commit
, range
);
1227 commit
->object
.flags
|= TREESAME
;
1232 static enum rewrite_result
line_log_rewrite_one(struct rev_info
*rev
, struct commit
**pp
)
1235 struct commit
*p
= *pp
;
1236 if (p
->parents
&& p
->parents
->next
)
1237 return rewrite_one_ok
;
1238 if (p
->object
.flags
& UNINTERESTING
)
1239 return rewrite_one_ok
;
1240 if (!(p
->object
.flags
& TREESAME
))
1241 return rewrite_one_ok
;
1243 return rewrite_one_noparents
;
1244 *pp
= p
->parents
->item
;
1248 int line_log_filter(struct rev_info
*rev
)
1250 struct commit
*commit
;
1251 struct commit_list
*list
= rev
->commits
;
1252 struct commit_list
*out
= NULL
, **pp
= &out
;
1255 struct commit_list
*to_free
= NULL
;
1256 commit
= list
->item
;
1257 if (process_ranges_arbitrary_commit(rev
, commit
)) {
1267 for (list
= out
; list
; list
= list
->next
)
1268 rewrite_parents(rev
, list
->item
, line_log_rewrite_one
);