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 filter_diffs_for_paths(struct line_log_data
*range
, int keep_deletions
)
756 struct diff_queue_struct outq
;
757 DIFF_QUEUE_CLEAR(&outq
);
759 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
760 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
761 struct line_log_data
*rg
= NULL
;
763 if (!DIFF_FILE_VALID(p
->two
)) {
767 diff_free_filepair(p
);
770 for (rg
= range
; rg
; rg
= rg
->next
) {
771 if (!strcmp(rg
->spec
->path
, p
->two
->path
))
777 diff_free_filepair(p
);
779 free(diff_queued_diff
.queue
);
780 diff_queued_diff
= outq
;
783 static inline int diff_might_be_rename(void)
786 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
787 if (!DIFF_FILE_VALID(diff_queued_diff
.queue
[i
]->one
)) {
788 /* fprintf(stderr, "diff_might_be_rename found creation of: %s\n", */
789 /* diff_queued_diff.queue[i]->two->path); */
795 static void queue_diffs(struct line_log_data
*range
,
796 struct diff_options
*opt
,
797 struct diff_queue_struct
*queue
,
798 struct commit
*commit
, struct commit
*parent
)
800 void *tree1
= NULL
, *tree2
= NULL
;
801 struct tree_desc desc1
, desc2
;
804 load_tree_desc(&desc2
, &tree2
, commit
->tree
->object
.sha1
);
806 load_tree_desc(&desc1
, &tree1
, parent
->tree
->object
.sha1
);
808 init_tree_desc(&desc1
, "", 0);
810 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
811 diff_tree(&desc1
, &desc2
, "", opt
);
812 if (opt
->detect_rename
) {
813 filter_diffs_for_paths(range
, 1);
814 if (diff_might_be_rename())
816 filter_diffs_for_paths(range
, 0);
818 move_diff_queue(queue
, &diff_queued_diff
);
826 static char *get_nth_line(long line
, unsigned long *ends
, void *data
)
831 return (char *)data
+ ends
[line
] + 1;
834 static void print_line(const char *prefix
, char first
,
835 long line
, unsigned long *ends
, void *data
,
836 const char *color
, const char *reset
)
838 char *begin
= get_nth_line(line
, ends
, data
);
839 char *end
= get_nth_line(line
+1, ends
, data
);
842 if (end
> begin
&& end
[-1] == '\n') {
847 fputs(prefix
, stdout
);
848 fputs(color
, stdout
);
850 fwrite(begin
, 1, end
-begin
, stdout
);
851 fputs(reset
, stdout
);
854 fputs("\\ No newline at end of file\n", stdout
);
857 static char *output_prefix(struct diff_options
*opt
)
861 if (opt
->output_prefix
) {
862 struct strbuf
*sb
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
869 static void dump_diff_hacky_one(struct rev_info
*rev
, struct line_log_data
*range
)
872 long p_lines
, t_lines
;
873 unsigned long *p_ends
= NULL
, *t_ends
= NULL
;
874 struct diff_filepair
*pair
= range
->pair
;
875 struct diff_ranges
*diff
= &range
->diff
;
877 struct diff_options
*opt
= &rev
->diffopt
;
878 char *prefix
= output_prefix(opt
);
879 const char *c_reset
= diff_get_color(opt
->use_color
, DIFF_RESET
);
880 const char *c_frag
= diff_get_color(opt
->use_color
, DIFF_FRAGINFO
);
881 const char *c_meta
= diff_get_color(opt
->use_color
, DIFF_METAINFO
);
882 const char *c_old
= diff_get_color(opt
->use_color
, DIFF_FILE_OLD
);
883 const char *c_new
= diff_get_color(opt
->use_color
, DIFF_FILE_NEW
);
884 const char *c_plain
= diff_get_color(opt
->use_color
, DIFF_PLAIN
);
889 if (pair
->one
->sha1_valid
)
890 fill_line_ends(pair
->one
, &p_lines
, &p_ends
);
891 fill_line_ends(pair
->two
, &t_lines
, &t_ends
);
893 printf("%s%sdiff --git a/%s b/%s%s\n", prefix
, c_meta
, pair
->one
->path
, pair
->two
->path
, c_reset
);
894 printf("%s%s--- %s%s%s\n", prefix
, c_meta
,
895 pair
->one
->sha1_valid
? "a/" : "",
896 pair
->one
->sha1_valid
? pair
->one
->path
: "/dev/null",
898 printf("%s%s+++ b/%s%s\n", prefix
, c_meta
, pair
->two
->path
, c_reset
);
899 for (i
= 0; i
< range
->ranges
.nr
; i
++) {
901 long t_start
= range
->ranges
.ranges
[i
].start
;
902 long t_end
= range
->ranges
.ranges
[i
].end
;
903 long t_cur
= t_start
;
906 while (j
< diff
->target
.nr
&& diff
->target
.ranges
[j
].end
< t_start
)
908 if (j
== diff
->target
.nr
|| diff
->target
.ranges
[j
].start
> t_end
)
911 /* Scan ahead to determine the last diff that falls in this range */
913 while (j_last
< diff
->target
.nr
&& diff
->target
.ranges
[j_last
].start
< t_end
)
919 * Compute parent hunk headers: we know that the diff
920 * has the correct line numbers (but not all hunks).
921 * So it suffices to shift the start/end according to
922 * the line numbers of the first/last hunk(s) that
923 * fall in this range.
925 if (t_start
< diff
->target
.ranges
[j
].start
)
926 p_start
= diff
->parent
.ranges
[j
].start
- (diff
->target
.ranges
[j
].start
-t_start
);
928 p_start
= diff
->parent
.ranges
[j
].start
;
929 if (t_end
> diff
->target
.ranges
[j_last
].end
)
930 p_end
= diff
->parent
.ranges
[j_last
].end
+ (t_end
-diff
->target
.ranges
[j_last
].end
);
932 p_end
= diff
->parent
.ranges
[j_last
].end
;
934 if (!p_start
&& !p_end
) {
939 /* Now output a diff hunk for this range */
940 printf("%s%s@@ -%ld,%ld +%ld,%ld @@%s\n",
942 p_start
+1, p_end
-p_start
, t_start
+1, t_end
-t_start
,
944 while (j
< diff
->target
.nr
&& diff
->target
.ranges
[j
].start
< t_end
) {
946 for (; t_cur
< diff
->target
.ranges
[j
].start
; t_cur
++)
947 print_line(prefix
, ' ', t_cur
, t_ends
, pair
->two
->data
,
949 for (k
= diff
->parent
.ranges
[j
].start
; k
< diff
->parent
.ranges
[j
].end
; k
++)
950 print_line(prefix
, '-', k
, p_ends
, pair
->one
->data
,
952 for (; t_cur
< diff
->target
.ranges
[j
].end
&& t_cur
< t_end
; t_cur
++)
953 print_line(prefix
, '+', t_cur
, t_ends
, pair
->two
->data
,
957 for (; t_cur
< t_end
; t_cur
++)
958 print_line(prefix
, ' ', t_cur
, t_ends
, pair
->two
->data
,
967 * NEEDSWORK: manually building a diff here is not the Right
968 * Thing(tm). log -L should be built into the diff pipeline.
970 static void dump_diff_hacky(struct rev_info
*rev
, struct line_log_data
*range
)
972 puts(output_prefix(&rev
->diffopt
));
974 dump_diff_hacky_one(rev
, range
);
980 * Unlike most other functions, this destructively operates on
983 static int process_diff_filepair(struct rev_info
*rev
,
984 struct diff_filepair
*pair
,
985 struct line_log_data
*range
,
986 struct diff_ranges
**diff_out
)
988 struct line_log_data
*rg
= range
;
989 struct range_set tmp
;
990 struct diff_ranges diff
;
991 mmfile_t file_parent
, file_target
;
993 assert(pair
->two
->path
);
995 assert(rg
->spec
->path
);
996 if (!strcmp(rg
->spec
->path
, pair
->two
->path
))
1003 if (rg
->ranges
.nr
== 0)
1006 assert(pair
->two
->sha1_valid
);
1007 diff_populate_filespec(pair
->two
, 0);
1008 file_target
.ptr
= pair
->two
->data
;
1009 file_target
.size
= pair
->two
->size
;
1011 if (pair
->one
->sha1_valid
) {
1012 diff_populate_filespec(pair
->one
, 0);
1013 file_parent
.ptr
= pair
->one
->data
;
1014 file_parent
.size
= pair
->one
->size
;
1016 file_parent
.ptr
= "";
1017 file_parent
.size
= 0;
1020 diff_ranges_init(&diff
);
1021 collect_diff(&file_parent
, &file_target
, &diff
);
1023 /* NEEDSWORK should apply some heuristics to prevent mismatches */
1024 rg
->spec
->path
= xstrdup(pair
->one
->path
);
1026 range_set_init(&tmp
, 0);
1027 range_set_map_across_diff(&tmp
, &rg
->ranges
, &diff
, diff_out
);
1028 range_set_release(&rg
->ranges
);
1029 range_set_move(&rg
->ranges
, &tmp
);
1031 diff_ranges_release(&diff
);
1033 return ((*diff_out
)->parent
.nr
> 0);
1036 static struct diff_filepair
*diff_filepair_dup(struct diff_filepair
*pair
)
1038 struct diff_filepair
*new = xmalloc(sizeof(struct diff_filepair
));
1039 new->one
= pair
->one
;
1040 new->two
= pair
->two
;
1046 static void free_diffqueues(int n
, struct diff_queue_struct
*dq
)
1049 for (i
= 0; i
< n
; i
++)
1050 for (j
= 0; j
< dq
[i
].nr
; j
++)
1051 diff_free_filepair(dq
[i
].queue
[j
]);
1055 static int process_all_files(struct line_log_data
**range_out
,
1056 struct rev_info
*rev
,
1057 struct diff_queue_struct
*queue
,
1058 struct line_log_data
*range
)
1062 *range_out
= line_log_data_copy(range
);
1064 for (i
= 0; i
< queue
->nr
; i
++) {
1065 struct diff_ranges
*pairdiff
= NULL
;
1066 if (process_diff_filepair(rev
, queue
->queue
[i
], *range_out
, &pairdiff
)) {
1067 struct line_log_data
*rg
= range
;
1069 /* NEEDSWORK tramples over data structures not owned here */
1070 while (rg
&& strcmp(rg
->spec
->path
, queue
->queue
[i
]->two
->path
))
1073 rg
->pair
= diff_filepair_dup(queue
->queue
[i
]);
1074 memcpy(&rg
->diff
, pairdiff
, sizeof(struct diff_ranges
));
1081 int line_log_print(struct rev_info
*rev
, struct commit
*commit
)
1083 struct line_log_data
*range
= lookup_line_range(rev
, commit
);
1086 dump_diff_hacky(rev
, range
);
1090 static int process_ranges_ordinary_commit(struct rev_info
*rev
, struct commit
*commit
,
1091 struct line_log_data
*range
)
1093 struct commit
*parent
= NULL
;
1094 struct diff_queue_struct queue
;
1095 struct line_log_data
*parent_range
;
1098 if (commit
->parents
)
1099 parent
= commit
->parents
->item
;
1101 queue_diffs(range
, &rev
->diffopt
, &queue
, commit
, parent
);
1102 changed
= process_all_files(&parent_range
, rev
, &queue
, range
);
1104 add_line_range(rev
, parent
, parent_range
);
1108 static int process_ranges_merge_commit(struct rev_info
*rev
, struct commit
*commit
,
1109 struct line_log_data
*range
)
1111 struct diff_queue_struct
*diffqueues
;
1112 struct line_log_data
**cand
;
1113 struct commit
**parents
;
1114 struct commit_list
*p
;
1116 int nparents
= count_parents(commit
);
1118 diffqueues
= xmalloc(nparents
* sizeof(*diffqueues
));
1119 cand
= xmalloc(nparents
* sizeof(*cand
));
1120 parents
= xmalloc(nparents
* sizeof(*parents
));
1122 p
= commit
->parents
;
1123 for (i
= 0; i
< nparents
; i
++) {
1124 parents
[i
] = p
->item
;
1126 queue_diffs(range
, &rev
->diffopt
, &diffqueues
[i
], commit
, parents
[i
]);
1129 for (i
= 0; i
< nparents
; i
++) {
1132 changed
= process_all_files(&cand
[i
], rev
, &diffqueues
[i
], range
);
1135 * This parent can take all the blame, so we
1136 * don't follow any other path in history
1138 add_line_range(rev
, parents
[i
], cand
[i
]);
1139 clear_commit_line_range(rev
, commit
);
1140 commit
->parents
= xmalloc(sizeof(struct commit_list
));
1141 commit
->parents
->item
= parents
[i
];
1142 commit
->parents
->next
= NULL
;
1145 free_diffqueues(nparents
, diffqueues
);
1146 /* NEEDSWORK leaking like a sieve */
1152 * No single parent took the blame. We add the candidates
1153 * from the above loop to the parents.
1155 for (i
= 0; i
< nparents
; i
++) {
1156 add_line_range(rev
, parents
[i
], cand
[i
]);
1159 clear_commit_line_range(rev
, commit
);
1162 free_diffqueues(nparents
, diffqueues
);
1165 /* NEEDSWORK evil merge detection stuff */
1166 /* NEEDSWORK leaking like a sieve */
1169 static int process_ranges_arbitrary_commit(struct rev_info
*rev
, struct commit
*commit
)
1171 struct line_log_data
*range
= lookup_line_range(rev
, commit
);
1175 if (!commit
->parents
|| !commit
->parents
->next
)
1176 changed
= process_ranges_ordinary_commit(rev
, commit
, range
);
1178 changed
= process_ranges_merge_commit(rev
, commit
, range
);
1182 commit
->object
.flags
|= TREESAME
;
1187 static enum rewrite_result
line_log_rewrite_one(struct rev_info
*rev
, struct commit
**pp
)
1190 struct commit
*p
= *pp
;
1191 if (p
->parents
&& p
->parents
->next
)
1192 return rewrite_one_ok
;
1193 if (p
->object
.flags
& UNINTERESTING
)
1194 return rewrite_one_ok
;
1195 if (!(p
->object
.flags
& TREESAME
))
1196 return rewrite_one_ok
;
1198 return rewrite_one_noparents
;
1199 *pp
= p
->parents
->item
;
1203 int line_log_filter(struct rev_info
*rev
)
1205 struct commit
*commit
;
1206 struct commit_list
*list
= rev
->commits
;
1207 struct commit_list
*out
= NULL
, **pp
= &out
;
1210 struct commit_list
*to_free
= NULL
;
1211 commit
= list
->item
;
1212 if (process_ranges_arbitrary_commit(rev
, commit
)) {
1222 for (list
= out
; list
; list
= list
->next
)
1223 rewrite_parents(rev
, list
->item
, line_log_rewrite_one
);