9 #include "xdiff-interface.h"
17 static void cleanup(struct diff_line_range
*r
)
20 struct diff_line_range
*next
= r
->next
;
21 DIFF_LINE_RANGE_CLEAR(r
);
27 static struct object
*verify_commit(struct rev_info
*revs
)
29 struct object
*commit
= NULL
;
33 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
34 struct object
*obj
= revs
->pending
.objects
[i
].item
;
35 if (obj
->flags
& UNINTERESTING
)
37 while (obj
->type
== OBJ_TAG
)
38 obj
= deref_tag(obj
, NULL
, 0);
39 if (obj
->type
!= OBJ_COMMIT
)
40 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
42 die("More than one commit to dig from: %s and %s?",
43 revs
->pending
.objects
[i
].name
,
44 revs
->pending
.objects
[found
].name
);
50 die("No commit specified?");
55 static void fill_blob_sha1(struct commit
*commit
, struct diff_line_range
*r
)
58 unsigned char sha1
[20];
61 if (get_tree_entry(commit
->object
.sha1
, r
->spec
->path
,
64 fill_filespec(r
->spec
, sha1
, mode
);
70 die("There is no path %s in the commit", r
->spec
->path
);
73 static void fill_line_ends(struct diff_filespec
*spec
, long *lines
,
74 unsigned long **line_ends
)
76 int num
= 0, size
= 50;
78 unsigned long *ends
= NULL
;
81 if (diff_populate_filespec(spec
, 0))
82 die("Cannot read blob %s", sha1_to_hex(spec
->sha1
));
84 ends
= xmalloc(size
* sizeof(*ends
));
87 while (num
< spec
->size
) {
88 if (data
[num
] == '\n' || num
== spec
->size
- 1) {
89 ALLOC_GROW(ends
, (cur
+ 1), size
);
95 /* shrink the array to fit the elements */
96 ends
= xrealloc(ends
, cur
* sizeof(*ends
));
102 struct diff_filespec
*spec
;
104 unsigned long *line_ends
;
107 static const char *nth_line(void *data
, long line
)
109 struct nth_line_cb
*d
= data
;
110 assert(d
&& line
< d
->lines
);
111 assert(d
->spec
&& d
->spec
->data
);
114 return (char *)d
->spec
->data
;
116 return (char *)d
->spec
->data
+ d
->line_ends
[line
] + 1;
120 * Parsing of (comma separated) one item in the -L option
122 const char *parse_loc(const char *spec
, nth_line_fn_t nth_line
,
123 void *data
, long lines
, long begin
, long *ret
)
132 /* Catch the '$' matcher, now it is used to match the last
133 * line of the file. */
134 if (spec
[0] == '$') {
139 /* Allow "-L <something>,+20" to mean starting at <something>
140 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
143 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
144 num
= strtol(spec
+ 1, &term
, 10);
145 if (term
!= spec
+ 1) {
149 *ret
= begin
+ num
- 2;
158 num
= strtol(spec
, &term
, 10);
166 /* it could be a regexp of form /.../ */
167 for (term
= (char *) spec
+ 1; *term
&& *term
!= '/'; term
++) {
174 /* try [spec+1 .. term-1] as regexp */
176 begin
--; /* input is in human terms */
177 line
= nth_line(data
, begin
);
179 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
180 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
181 const char *cp
= line
+ match
[0].rm_so
;
184 while (begin
++ < lines
) {
185 nline
= nth_line(data
, begin
);
186 if (line
<= cp
&& cp
< nline
)
196 regerror(reg_error
, ®exp
, errbuf
, 1024);
197 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
201 static void parse_range(long lines
, unsigned long *line_ends
,
202 struct line_range
*r
, struct diff_filespec
*spec
)
205 struct nth_line_cb data
= {spec
, lines
, line_ends
};
207 term
= parse_loc(r
->arg
, nth_line
, &data
, lines
- 1, 1, &r
->start
);
209 term
= parse_loc(term
+ 1, nth_line
, &data
, lines
- 1,
210 r
->start
+ 1, &r
->end
);
212 die("-L parameter's argument should be <start>,<end>");
216 die("-L parameter's argument should be <start>,<end>");
223 if (r
->start
> r
->end
) {
230 static void parse_lines(struct commit
*commit
, struct diff_line_range
*r
)
233 struct line_range
*old_range
= NULL
;
235 unsigned long *ends
= NULL
;
238 struct diff_filespec
*spec
= r
->spec
;
241 fill_blob_sha1(commit
, r
);
242 old_range
= r
->ranges
;
244 r
->nr
= r
->alloc
= 0;
245 fill_line_ends(spec
, &lines
, &ends
);
246 for (i
= 0; i
< num
; i
++) {
247 parse_range(lines
, ends
, old_range
+ i
, spec
);
248 diff_line_range_insert(r
, old_range
[i
].arg
,
249 old_range
[i
].start
, old_range
[i
].end
);
261 * Insert a new line range into a diff_line_range struct, and keep the
262 * r->ranges sorted by their starting line number.
264 struct line_range
*diff_line_range_insert(struct diff_line_range
*r
,
265 const char *arg
, int start
, int end
)
268 struct line_range
*rs
= r
->ranges
;
269 int left_merge
= 0, right_merge
= 0;
272 assert(start
<= end
);
274 if (r
->nr
== 0 || rs
[r
->nr
- 1].end
< start
- 1) {
276 DIFF_LINE_RANGE_GROW(r
);
280 rs
[num
].start
= start
;
285 for (; i
< r
->nr
; i
++) {
286 if (rs
[i
].end
< start
- 1)
288 if (rs
[i
].end
== start
- 1) {
294 assert(rs
[i
].end
> start
- 1);
295 if (rs
[i
].start
<= start
) {
296 if (rs
[i
].end
< end
) {
301 } else if (rs
[i
].start
<= end
+ 1) {
304 if (rs
[i
].end
< end
) {
311 DIFF_LINE_RANGE_GROW(r
);
313 memmove(rs
+ i
+ 1, rs
+ i
, num
* sizeof(struct line_range
));
325 for (; j
> -1; j
--) {
326 if (rs
[j
].end
>= rs
[i
].start
- 1)
327 if (rs
[j
].start
< rs
[i
].start
)
328 rs
[i
].start
= rs
[j
].start
;
330 memmove(rs
+ j
+ 1, rs
+ i
, (r
->nr
- i
) * sizeof(struct line_range
));
335 for (; j
< r
->nr
; j
++) {
336 if (rs
[j
].start
<= rs
[i
].end
+ 1)
337 if (rs
[j
].end
> rs
[i
].end
)
338 rs
[i
].end
= rs
[j
].end
;
341 memmove(rs
+ i
+ 1, rs
+ j
, (r
->nr
- j
) * sizeof(struct line_range
));
349 void diff_line_range_clear(struct diff_line_range
*r
)
353 for (; i
< r
->nr
; i
++) {
354 struct line_range
*rg
= r
->ranges
+ i
;
360 if (r
->prev
->count
== 1)
362 free_filespec(r
->prev
);
368 if (r
->spec
->count
== 1)
370 free_filespec(r
->spec
);
376 r
->alloc
= r
->nr
= 0;
384 void diff_line_range_append(struct diff_line_range
*r
, const char *arg
)
386 DIFF_LINE_RANGE_GROW(r
);
387 r
->ranges
[r
->nr
- 1].arg
= arg
;
390 struct diff_line_range
*diff_line_range_clone(struct diff_line_range
*r
)
392 struct diff_line_range
*ret
= xmalloc(sizeof(*ret
));
396 DIFF_LINE_RANGE_INIT(ret
);
397 ret
->ranges
= xcalloc(r
->nr
, sizeof(struct line_range
));
398 memcpy(ret
->ranges
, r
->ranges
, sizeof(struct line_range
) * r
->nr
);
400 ret
->alloc
= ret
->nr
= r
->nr
;
402 for (; i
< ret
->nr
; i
++)
403 PRINT_PAIR_INIT(&ret
->ranges
[i
].pair
);
412 struct diff_line_range
*diff_line_range_clone_deeply(struct diff_line_range
*r
)
414 struct diff_line_range
*ret
= NULL
;
415 struct diff_line_range
*tmp
= NULL
, *prev
= NULL
;
418 ret
= tmp
= prev
= diff_line_range_clone(r
);
421 tmp
= diff_line_range_clone(r
);
430 struct diff_line_range
*diff_line_range_merge(struct diff_line_range
*out
,
431 struct diff_line_range
*other
)
433 struct diff_line_range
*one
= out
, *two
= other
;
434 struct diff_line_range
*pone
= NULL
;
437 struct diff_line_range
*ptwo
;
441 if (!strcmp(one
->spec
->path
, two
->spec
->path
)) {
443 for (; i
< two
->nr
; i
++) {
444 diff_line_range_insert(one
, NULL
,
445 two
->ranges
[i
].start
,
451 ptwo
->next
= two
->next
;
452 DIFF_LINE_RANGE_CLEAR(two
);
471 void add_line_range(struct rev_info
*revs
, struct commit
*commit
,
472 struct diff_line_range
*r
)
474 struct diff_line_range
*ret
= NULL
;
476 ret
= lookup_decoration(&revs
->line_range
, &commit
->object
);
477 if (ret
!= NULL
&& r
!= NULL
)
478 diff_line_range_merge(ret
, r
);
480 add_decoration(&revs
->line_range
, &commit
->object
, r
);
483 commit
->object
.flags
|= RANGE_UPDATE
;
486 struct diff_line_range
*lookup_line_range(struct rev_info
*revs
,
487 struct commit
*commit
)
489 struct diff_line_range
*ret
= NULL
;
491 ret
= lookup_decoration(&revs
->line_range
, &commit
->object
);
495 void setup_line(struct rev_info
*rev
, struct diff_line_range
*r
)
497 struct commit
*commit
= NULL
;
498 struct diff_options
*opt
= &rev
->diffopt
;
500 commit
= (struct commit
*)verify_commit(rev
);
501 parse_lines(commit
, r
);
503 add_line_range(rev
, commit
, r
);
505 * Note we support -M/-C to detect file rename
508 diff_tree_release_paths(opt
);
511 struct take_range_cb_data
{
512 struct diff_line_range
*interesting
; /* currently interesting ranges */
513 struct diff_line_range
*range
;
514 /* the ranges corresponds to the interesting ranges of parent commit */
516 /* the last line number of diff hunk */
518 /* whether there is some line changes between the current
519 * commit and its parent */
522 #define SCALE_FACTOR 4
524 * [p_start, p_end] represents the pre-image of current diff hunk,
525 * [t_start, t_end] represents the post-image of the current diff hunk,
526 * [start, end] represents the currently interesting line range in
528 * [o_start, o_end] represents the original line range that coresponds
529 * to current line range.
531 void map_lines(long p_start
, long p_end
, long t_start
, long t_end
,
532 long start
, long end
, long *o_start
, long *o_end
)
535 * Normally, p_start should be less than p_end, so does the
536 * t_start and t_end. But when the line range is added from
537 * scratch, p_start will be greater than p_end. When the line
538 * range is deleted, t_start will be greater than t_end.
540 if (p_start
> p_end
) {
541 *o_start
= *o_end
= 0;
545 if (t_start
> t_end
) {
551 if (start
== t_start
&& end
== t_end
) {
557 if (start
== t_start
) {
559 *o_end
= p_start
+ (end
- start
);
566 *o_start
= p_end
- (end
- start
);
567 if (*o_start
< p_start
)
574 * A heuristic for lines mapping:
576 * When the pre-image is no more than 1/SCALE_FACTOR of the post-image,
577 * there is no effective way to find out which part of pre-image
578 * corresponds to the currently interesting range of post-image.
579 * And we are in the danger of tracking totally useless lines.
580 * So, we just treat all the post-image lines as added from scratch.
582 if (SCALE_FACTOR
* (p_end
- p_start
+ 1) < (t_end
- t_start
+ 1)) {
583 *o_start
= *o_end
= 0;
587 *o_start
= p_start
+ start
- t_start
;
588 *o_end
= p_end
- (t_end
- end
);
590 if (*o_start
> *o_end
) {
596 if (*o_start
< p_start
)
604 * [p_start, p_end] represents the diff hunk line range of pre-image,
605 * [t_start, t_end] represents the diff hunk line range of post-image.
606 * When same == 0, they represent a range of identical lines between
609 * This function find out the corresponding line ranges of currently
610 * interesting ranges which this diff hunk touches.
612 static void map_range(struct take_range_cb_data
*data
, int same
,
613 long p_start
, long p_end
, long t_start
, long t_end
)
615 struct line_range
*ranges
= data
->interesting
->ranges
;
616 long takens
, takene
, start
, end
;
617 int i
= 0, out
= 0, added
= 0;
618 long op_start
= p_start
, op_end
= p_end
, ot_start
= t_start
, ot_end
= t_end
;
620 for (; i
< data
->interesting
->nr
; i
++) {
622 if (t_start
> ranges
[i
].end
)
624 if (t_end
< ranges
[i
].start
)
627 if (t_start
> ranges
[i
].start
) {
630 if (t_end
>= ranges
[i
].end
) {
632 takene
= p_start
+ end
- t_start
;
639 start
= ranges
[i
].start
;
640 takens
= p_start
+ start
- t_start
;
641 if (t_end
>= ranges
[i
].end
) {
643 takene
= p_start
+ end
- t_start
;
652 struct print_pair
*pair
= &ranges
[i
].pair
;
653 struct print_range
*rr
= NULL
;
654 PRINT_PAIR_GROW(pair
);
655 rr
= pair
->ranges
+ pair
->nr
- 1;
656 PRINT_RANGE_INIT(rr
);
659 map_lines(op_start
, op_end
, ot_start
, ot_end
, start
, end
,
661 if (takens
== 0 && takene
== 0) {
668 data
->interesting
->diff
= 1;
672 /* Code movement/copy detect here, now place two dummy statements here */
676 struct line_range
*added_range
= diff_line_range_insert(data
->range
,
677 NULL
, takens
, takene
);
679 ranges
[i
].pstart
= added_range
->start
;
680 ranges
[i
].pend
= added_range
->end
;
684 p_start
= takene
+ 1;
692 * [p_start, p_end] represents the line range of pre-image,
693 * [t_start, t_end] represents the line range of post-image,
694 * and they are identical lines.
696 * This function substracts out the identical lines between current
697 * commit and its parent, from currently interesting ranges.
699 static void take_range(struct take_range_cb_data
*data
,
700 long p_start
, long p_end
, long t_start
, long t_end
)
702 struct line_range
*ranges
= data
->interesting
->ranges
;
703 long takens
, takene
, start
, end
;
704 int i
= 0, out
= 0, added
= 0;
706 for (; i
< data
->interesting
->nr
; i
++) {
708 if (t_start
> ranges
[i
].end
)
710 if (t_end
< ranges
[i
].start
)
713 if (t_start
> ranges
[i
].start
) {
714 long tmp
= ranges
[i
].end
;
715 ranges
[i
].end
= t_start
- 1;
720 takene
= p_start
+ end
- t_start
;
721 p_start
= takene
+ 1;
726 diff_line_range_insert(data
->interesting
, NULL
,
731 start
= ranges
[i
].start
;
732 takens
= p_start
+ start
- t_start
;
733 if (t_end
>= ranges
[i
].end
) {
734 int num
= data
->interesting
->nr
- 1;
736 takene
= p_start
+ end
- t_start
;
738 p_start
= takene
+ 1;
739 memmove(ranges
+ i
, ranges
+ i
+ 1, (num
- i
) * sizeof(*ranges
));
740 data
->interesting
->nr
= num
;
745 ranges
[i
].start
= t_end
+ 1;
750 diff_line_range_insert(data
->range
, NULL
, takens
, takene
);
757 static void take_range_cb(void *data
, long same
, long p_next
, long t_next
)
759 struct take_range_cb_data
*d
= data
;
760 long p_start
= d
->plno
+ 1, t_start
= d
->tlno
+ 1;
761 long p_end
= p_start
+ same
- t_start
, t_end
= same
;
763 /* If one file is added from scratch, we should not bother to call
764 * take_range, since there is nothing to take
766 if (t_end
>= t_start
)
767 take_range(d
, p_start
, p_end
, t_start
, t_end
);
772 static void map_range_cb(void *data
, long same
, long p_next
, long t_next
)
774 struct take_range_cb_data
*d
= data
;
776 long p_start
= d
->plno
+ 1;
777 long t_start
= d
->tlno
+ 1;
778 long p_end
= same
- t_start
+ p_start
;
781 /* Firstly, take the unchanged lines from child */
782 if (t_end
>= t_start
)
783 map_range(d
, 1, p_start
, p_end
, t_start
, t_end
);
785 /* find out which lines to print */
787 p_start
= d
->plno
+ t_start
- d
->tlno
;
788 map_range(d
, 0, p_start
, p_next
, t_start
, t_next
);
795 * We support two kinds of operation in this function:
796 * 1. map == 0, take the same lines from the current commit and assign it
798 * 2. map == 1, in addition to the same lines, we also map the changed lines
799 * from the current commit to the parent according to the
801 * take_range_cb and take_range are used to take same lines from current commit
803 * map_range_cb and map_range are used to map line ranges to the parent.
805 static int assign_range_to_parent(struct rev_info
*rev
, struct commit
*c
,
806 struct commit
*p
, struct diff_line_range
*r
,
807 struct diff_options
*opt
, int map
)
809 struct diff_line_range
*rr
= xmalloc(sizeof(*rr
));
810 struct diff_line_range
*cr
= rr
, *prev_r
= rr
;
811 struct diff_line_range
*rg
= NULL
;
812 struct tree_desc desc1
, desc2
;
813 void *tree1
= NULL
, *tree2
= NULL
;
814 unsigned long size1
, size2
;
815 struct diff_queue_struct
*queue
;
816 struct take_range_cb_data cb
= {NULL
, cr
, 0, 0};
820 xdiff_emit_hunk_consume_fn fn
= map
? map_range_cb
: take_range_cb
;
822 DIFF_LINE_RANGE_INIT(cr
);
823 memset(&xpp
, 0, sizeof(xpp
));
824 memset(&xecfg
, 0, sizeof(xecfg
));
825 xecfg
.ctxlen
= xecfg
.interhunkctxlen
= 0;
828 * Compose up two trees, for root commit, we make up a empty tree.
831 tree2
= read_object_with_reference(c
->tree
->object
.sha1
, "tree",
834 die("Unable to read tree (%s)", sha1_to_hex(c
->tree
->object
.sha1
));
835 init_tree_desc(&desc2
, tree2
, size2
);
837 tree1
= read_object_with_reference(p
->tree
->object
.sha1
,
838 "tree", &size1
, NULL
);
840 die("Unable to read tree (%s)",
841 sha1_to_hex(p
->tree
->object
.sha1
));
842 init_tree_desc(&desc1
, tree1
, size1
);
844 init_tree_desc(&desc1
, "", 0);
847 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
848 diff_tree(&desc1
, &desc2
, "", opt
);
851 queue
= &diff_queued_diff
;
852 for (i
= 0; i
< queue
->nr
; i
++) {
853 struct diff_filepair
*pair
= queue
->queue
[i
];
854 struct diff_line_range
*rg
= r
;
855 mmfile_t file_p
, file_t
;
856 assert(pair
->two
->path
);
858 assert(rg
->spec
->path
);
859 if (!strcmp(rg
->spec
->path
, pair
->two
->path
))
870 rg
->status
= pair
->status
;
871 assert(pair
->two
->sha1_valid
);
872 diff_populate_filespec(pair
->two
, 0);
873 file_t
.ptr
= pair
->two
->data
;
874 file_t
.size
= pair
->two
->size
;
877 free_filespec(rg
->prev
);
878 rg
->prev
= pair
->one
;
880 if (pair
->one
->sha1_valid
) {
881 diff_populate_filespec(pair
->one
, 0);
882 file_p
.ptr
= pair
->one
->data
;
883 file_p
.size
= pair
->one
->size
;
890 struct diff_line_range
*tmp
= xmalloc(sizeof(*tmp
));
895 DIFF_LINE_RANGE_CLEAR(cr
);
897 DIFF_LINE_RANGE_INIT(cr
);
898 if (pair
->one
->sha1_valid
) {
899 cr
->spec
= pair
->one
;
906 cb
.plno
= cb
.tlno
= 0;
907 xdi_diff_hunks(&file_p
, &file_t
, fn
, &cb
, &xpp
, &xecfg
);
911 * The remain part is the same part.
912 * Instead of calculating the true line number of the two files,
913 * use the biggest integer.
916 map_range(&cb
, 1, cb
.plno
+ 1, INT_MAX
, cb
.tlno
+ 1, INT_MAX
);
918 take_range(&cb
, cb
.plno
+ 1, INT_MAX
, cb
.tlno
+ 1, INT_MAX
);
920 opt
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
924 * Collect the untouch ranges, this comes from the files not changed
925 * between two commit.
929 /* clear the touch one to make it usable in next round */
933 struct diff_line_range
*untouch
= diff_line_range_clone(rg
);
934 if (prev_r
== rr
&& rr
->nr
== 0) {
935 rr
= prev_r
= untouch
;
937 prev_r
->next
= untouch
;
945 DIFF_LINE_RANGE_CLEAR(cr
);
958 add_line_range(rev
, p
, rr
);
961 * If there is no new ranges assigned to the parent,
962 * we should mark it as a 'root' commit.
964 if (c
->parents
&& !c
->parents
->next
) {
970 /* and the ranges of current commit c is updated */
971 c
->object
.flags
&= ~RANGE_UPDATE
;
973 c
->object
.flags
|= NEED_PRINT
;
984 static void diff_update_parent_range(struct rev_info
*rev
,
985 struct commit
*commit
)
987 struct diff_line_range
*r
= lookup_line_range(rev
, commit
);
988 struct commit_list
*parents
= commit
->parents
;
989 struct commit
*c
= NULL
;
991 assert(parents
->next
== NULL
);
995 assign_range_to_parent(rev
, commit
, c
, r
, &rev
->diffopt
, 1);
998 struct commit_state
{
999 struct diff_line_range
*range
;
1003 static void assign_parents_range(struct rev_info
*rev
, struct commit
*commit
)
1005 struct commit_list
*parents
= commit
->parents
;
1006 struct diff_line_range
*r
= lookup_line_range(rev
, commit
);
1007 struct diff_line_range
*evil
= NULL
, *range
= NULL
;
1008 struct decoration parents_state
;
1009 struct commit_state
*state
= NULL
;
1012 memset(&parents_state
, 0, sizeof(parents_state
));
1014 * If we are in linear history, update range and flush the patch if
1017 if (parents
== NULL
|| parents
->next
== NULL
)
1018 return diff_update_parent_range(rev
, commit
);
1021 * Loop on the parents and assign the ranges to different
1022 * parents, if there is any range left, this commit must
1025 evil
= diff_line_range_clone_deeply(r
);
1026 parents
= commit
->parents
;
1028 struct commit
*p
= parents
->item
;
1030 struct diff_line_range
*origin_range
= lookup_line_range(rev
, p
);
1032 origin_range
= diff_line_range_clone_deeply(origin_range
);
1034 state
= xmalloc(sizeof(*state
));
1035 state
->range
= origin_range
;
1036 state
->obj
= p
->object
;
1037 add_decoration(&parents_state
, &p
->object
, state
);
1038 diff
= assign_range_to_parent(rev
, commit
, p
, r
, &rev
->diffopt
, 1);
1039 /* Since all the ranges comes from this parent, we can ignore others */
1041 /* restore the state of parents before this one */
1042 parents
= commit
->parents
;
1043 while (parents
->item
!= p
) {
1044 struct commit_list
*list
= parents
;
1045 struct diff_line_range
*line_range
= NULL
;
1046 parents
= parents
->next
;
1047 line_range
= lookup_line_range(rev
, list
->item
);
1048 cleanup(line_range
);
1049 state
= lookup_decoration(&parents_state
, &list
->item
->object
);
1050 add_decoration(&parents_state
, &list
->item
->object
, NULL
);
1051 add_line_range(rev
, list
->item
, state
->range
);
1052 list
->item
->object
= state
->obj
;
1057 commit
->parents
= parents
;
1058 parents
= parents
->next
;
1059 commit
->parents
->next
= NULL
;
1061 /* free the non-use commit_list */
1063 struct commit_list
*list
= parents
;
1064 parents
= parents
->next
;
1069 /* take the ranges from 'commit', try to detect nontrivial merge */
1070 assign_range_to_parent(rev
, commit
, p
, evil
, &rev
->diffopt
, 0);
1071 parents
= parents
->next
;
1074 commit
->object
.flags
|= NONTRIVIAL_MERGE
;
1076 * yes, this must be an evil merge.
1081 commit
->object
.flags
|= EVIL_MERGE
;
1084 range
= range
->next
;
1088 /* Never print out any diff for a merge commit */
1089 commit
->object
.flags
&= ~NEED_PRINT
;
1091 parents
= commit
->parents
;
1093 state
= lookup_decoration(&parents_state
, &parents
->item
->object
);
1095 cleanup(state
->range
);
1098 parents
= parents
->next
;
1102 add_decoration(&rev
->nontrivial_merge
, &commit
->object
, evil
);
1109 const char *one
, *two
;
1110 const char *one_end
, *two_end
;
1111 struct diff_line_range
*range
;
1114 static void flush_lines(struct diff_options
*opt
, const char **ptr
, const char *end
,
1115 int slno
, int elno
, int *lno
, const char *color
, const char heading
)
1117 const char *p
= *ptr
;
1118 struct strbuf buf
= STRBUF_INIT
;
1120 char *line_prefix
= "";
1121 struct strbuf
*msgbuf
;
1123 if (opt
&& opt
->output_prefix
) {
1124 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1125 line_prefix
= msgbuf
->buf
;
1129 reset
= diff_get_color_opt(opt
, DIFF_RESET
);
1133 strbuf_addf(&buf
, "%s%c", color
, heading
);
1134 while (*ptr
< end
&& *lno
< slno
) {
1135 if (**ptr
== '\n') {
1144 assert(*ptr
<= end
);
1147 while (*ptr
< end
&& *lno
<= elno
) {
1148 if (**ptr
== '\n') {
1149 fprintf(opt
->file
, "%s%s", line_prefix
, buf
.buf
);
1151 fwrite(p
, *ptr
- p
, 1, opt
->file
);
1152 fprintf(opt
->file
, "%s\n", reset
);
1159 fprintf(opt
->file
, "%s%s", line_prefix
, buf
.buf
);
1161 fwrite(p
, *ptr
- p
, 1, opt
->file
);
1162 fprintf(opt
->file
, "%s\n", reset
);
1164 strbuf_release(&buf
);
1167 static void diff_flush_range(struct diff_options
*opt
, struct line_chunk
*chunk
,
1168 struct line_range
*range
)
1170 struct print_pair
*pair
= &range
->pair
;
1171 const char *old
= diff_get_color_opt(opt
, DIFF_FILE_OLD
);
1172 const char *new = diff_get_color_opt(opt
, DIFF_FILE_NEW
);
1173 int i
, cur
= range
->start
;
1175 for (i
= 0; i
< pair
->nr
; i
++) {
1176 struct print_range
*pr
= pair
->ranges
+ i
;
1177 if (cur
< pr
->start
)
1178 flush_lines(opt
, &chunk
->two
, chunk
->two_end
,
1179 cur
, pr
->start
- 1, &chunk
->ltwo
, "", ' ');
1181 if (!pr
->line_added
)
1182 flush_lines(opt
, &chunk
->one
, chunk
->one_end
,
1183 pr
->pstart
, pr
->pend
, &chunk
->lone
, old
, '-');
1184 flush_lines(opt
, &chunk
->two
, chunk
->two_end
,
1185 pr
->start
, pr
->end
, &chunk
->ltwo
, new, '+');
1190 if (cur
<= range
->end
) {
1191 flush_lines(opt
, &chunk
->two
, chunk
->two_end
,
1192 cur
, range
->end
, &chunk
->ltwo
, "", ' ');
1196 static void diff_flush_chunks(struct diff_options
*opt
, struct line_chunk
*chunk
)
1198 struct diff_line_range
*range
= chunk
->range
;
1199 const char *set
= diff_get_color_opt(opt
, DIFF_FRAGINFO
);
1200 const char *reset
= diff_get_color_opt(opt
, DIFF_RESET
);
1201 char *line_prefix
= "";
1202 struct strbuf
*msgbuf
;
1205 if (opt
&& opt
->output_prefix
) {
1206 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1207 line_prefix
= msgbuf
->buf
;
1210 for (i
= 0; i
< range
->nr
; i
++) {
1211 struct line_range
*r
= range
->ranges
+ i
;
1212 long lenp
= r
->pend
- r
->pstart
+ 1, pstart
= r
->pstart
;
1213 long len
= r
->end
- r
->start
+ 1;
1217 fprintf(opt
->file
, "%s%s@@ -%ld,%ld +%ld,%ld @@%s\n",
1218 line_prefix
, set
, pstart
, lenp
, r
->start
, len
, reset
);
1220 diff_flush_range(opt
, chunk
, r
);
1224 static void diff_flush_filepair(struct rev_info
*rev
, struct diff_line_range
*range
)
1226 struct diff_options
*opt
= &rev
->diffopt
;
1227 struct diff_filespec
*one
= range
->prev
, *two
= range
->spec
;
1228 struct diff_filepair p
= {one
, two
, range
->status
, 0};
1229 struct strbuf header
= STRBUF_INIT
, meta
= STRBUF_INIT
;
1230 const char *a_prefix
, *b_prefix
;
1231 const char *name_a
, *name_b
, *a_one
, *b_two
;
1233 const char *set
= diff_get_color_opt(opt
, DIFF_METAINFO
);
1234 const char *reset
= diff_get_color_opt(opt
, DIFF_RESET
);
1235 struct line_chunk chunk
;
1236 int must_show_header
;
1237 char *line_prefix
= "";
1238 struct strbuf
*msgbuf
;
1240 if (opt
&& opt
->output_prefix
) {
1241 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1242 line_prefix
= msgbuf
->buf
;
1246 * the ranges that touch no different file, in this case
1247 * the line number will not change, and of course we have
1248 * no sensible range->pair since there is no diff run.
1251 if (rev
->full_line_diff
) {
1252 chunk
.two
= two
->data
;
1253 chunk
.two_end
= (const char *)two
->data
+ two
->size
;
1255 chunk
.range
= range
;
1256 diff_flush_chunks(&rev
->diffopt
, &chunk
);
1261 if (range
->status
== DIFF_STATUS_DELETED
)
1262 die("We are following an nonexistent file, interesting!");
1266 fill_metainfo(&meta
, name_a
, name_b
, one
, two
, opt
, &p
, &must_show_header
,
1267 DIFF_OPT_TST(opt
, COLOR_DIFF
));
1269 diff_set_mnemonic_prefix(opt
, "a/", "b/");
1270 if (DIFF_OPT_TST(opt
, REVERSE_DIFF
)) {
1271 a_prefix
= opt
->b_prefix
;
1272 b_prefix
= opt
->a_prefix
;
1274 a_prefix
= opt
->a_prefix
;
1275 b_prefix
= opt
->b_prefix
;
1278 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
1279 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
1281 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
1282 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
1283 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1284 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1285 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
,
1286 set
, a_one
, b_two
, reset
);
1287 if (lbl
[0][0] == '/') {
1288 strbuf_addf(&header
, "%s%snew file mode %06o%s\n",
1289 line_prefix
, set
, two
->mode
, reset
);
1290 } else if (lbl
[1][0] == '/') {
1291 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n",
1292 line_prefix
, set
, one
->mode
, reset
);
1293 } else if (one
->mode
!= two
->mode
) {
1294 strbuf_addf(&header
, "%s%sold mode %06o%s\n",
1295 line_prefix
, set
, one
->mode
, reset
);
1296 strbuf_addf(&header
, "%s%snew mode %06o%s\n",
1297 line_prefix
, set
, two
->mode
, reset
);
1300 fprintf(opt
->file
, "%s%s", header
.buf
, meta
.buf
);
1301 strbuf_release(&meta
);
1302 strbuf_release(&header
);
1303 fprintf(opt
->file
, "%s%s--- %s%s\n", line_prefix
, set
, lbl
[0], reset
);
1304 fprintf(opt
->file
, "%s%s+++ %s%s\n", line_prefix
, set
, lbl
[1], reset
);
1305 free((void *)a_one
);
1306 free((void *)b_two
);
1308 chunk
.one
= one
->data
;
1309 chunk
.one_end
= (const char *)one
->data
+ one
->size
;
1311 chunk
.two
= two
->data
;
1312 chunk
.two_end
= (const char *)two
->data
+ two
->size
;
1314 chunk
.range
= range
;
1315 diff_flush_chunks(&rev
->diffopt
, &chunk
);
1318 #define EVIL_MERGE_STR "nontrivial merge found"
1319 static void flush_nontrivial_merge(struct rev_info
*rev
,
1320 struct diff_line_range
*range
)
1322 struct diff_options
*opt
= &rev
->diffopt
;
1323 const char *reset
= diff_get_color_opt(opt
, DIFF_RESET
);
1324 const char *frag
= diff_get_color_opt(opt
, DIFF_FRAGINFO
);
1325 const char *meta
= diff_get_color_opt(opt
, DIFF_METAINFO
);
1326 const char *new = diff_get_color_opt(opt
, DIFF_FILE_NEW
);
1327 char *line_prefix
= "";
1328 struct strbuf
*msgbuf
;
1330 struct diff_line_range
*r
= range
;
1332 if (opt
&& opt
->output_prefix
) {
1333 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1334 line_prefix
= msgbuf
->buf
;
1346 fprintf(opt
->file
, "%s%s%s%s\n", line_prefix
, meta
, EVIL_MERGE_STR
, reset
);
1351 const char *ptr
= range
->spec
->data
;
1352 const char *end
= (const char *)range
->spec
->data
+ range
->spec
->size
;
1354 fprintf(opt
->file
, "%s%s%s%s\n", line_prefix
,
1355 meta
, range
->spec
->path
, reset
);
1356 for (; i
< range
->nr
; i
++) {
1357 struct line_range
*r
= range
->ranges
+ i
;
1358 fprintf(opt
->file
, "%s%s@@ %ld,%ld @@%s\n",
1359 line_prefix
, frag
, r
->start
,
1360 r
->end
- r
->start
+ 1, reset
);
1361 flush_lines(opt
, &ptr
, end
, r
->start
, r
->end
,
1364 fprintf(opt
->file
, "%s\n", line_prefix
);
1366 range
= range
->next
;
1370 static void line_log_flush(struct rev_info
*rev
, struct commit
*c
)
1372 struct diff_line_range
*range
= lookup_line_range(rev
, c
);
1373 struct diff_line_range
*nontrivial
= lookup_decoration(&rev
->nontrivial_merge
,
1375 struct log_info log
;
1376 struct diff_options
*opt
= &rev
->diffopt
;
1377 char *line_prefix
= "";
1378 struct strbuf
*msgbuf
;
1380 if (range
== NULL
|| !(c
->object
.flags
& NONTRIVIAL_MERGE
||
1381 c
->object
.flags
& NEED_PRINT
||
1382 rev
->full_line_diff
))
1386 graph_update(rev
->graph
, c
);
1389 rev
->loginfo
= &log
;
1391 rev
->loginfo
= NULL
;
1393 if (opt
&& opt
->output_prefix
) {
1394 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1395 line_prefix
= msgbuf
->buf
;
1397 fprintf(rev
->diffopt
.file
, "%s\n", line_prefix
);
1399 if (c
->object
.flags
& NONTRIVIAL_MERGE
)
1400 flush_nontrivial_merge(rev
, nontrivial
);
1403 if (range
->diff
|| (range
->nr
&& rev
->full_line_diff
))
1404 diff_flush_filepair(rev
, range
);
1405 range
= range
->next
;
1409 while (rev
->graph
&& !graph_is_commit_finished(rev
->graph
)) {
1411 strbuf_init(&sb
, 0);
1412 graph_next_line(rev
->graph
, &sb
);
1413 fputs(sb
.buf
, opt
->file
);
1417 int cmd_line_log_walk(struct rev_info
*rev
)
1419 struct commit
*commit
;
1420 struct commit_list
*list
= NULL
;
1421 struct diff_line_range
*r
= NULL
;
1423 if (prepare_revision_walk(rev
))
1424 die("revision walk prepare failed");
1426 list
= rev
->commits
;
1427 if (list
&& !limited
) {
1428 list
->item
->object
.flags
|= RANGE_UPDATE
;
1431 /* Clear the flags */
1432 while (list
&& !limited
) {
1433 list
->item
->object
.flags
&= ~(RANGE_UPDATE
| NONTRIVIAL_MERGE
|
1434 NEED_PRINT
| EVIL_MERGE
);
1438 list
= rev
->commits
;
1440 struct commit_list
*need_free
= list
;
1441 commit
= list
->item
;
1443 if (commit
->object
.flags
& RANGE_UPDATE
)
1444 assign_parents_range(rev
, commit
);
1446 if (commit
->object
.flags
& NEED_PRINT
||
1447 commit
->object
.flags
& NONTRIVIAL_MERGE
||
1448 rev
->full_line_diff
)
1449 line_log_flush(rev
, commit
);
1451 r
= lookup_line_range(rev
, commit
);
1455 add_line_range(rev
, commit
, r
);
1458 r
= lookup_decoration(&rev
->nontrivial_merge
, &commit
->object
);
1462 add_decoration(&rev
->nontrivial_merge
, &commit
->object
, r
);
1472 static enum rewrite_result
rewrite_one(struct rev_info
*rev
, struct commit
**pp
)
1474 struct diff_line_range
*r
= NULL
;
1478 if (p
->object
.flags
& RANGE_UPDATE
)
1479 assign_parents_range(rev
, p
);
1480 if (p
->object
.flags
& NEED_PRINT
|| p
->object
.flags
& NONTRIVIAL_MERGE
)
1481 return rewrite_one_ok
;
1483 return rewrite_one_noparents
;
1485 r
= lookup_line_range(rev
, p
);
1487 return rewrite_one_noparents
;
1488 *pp
= p
->parents
->item
;
1492 /* The rev->commits must be sorted in topologically order */
1493 void limit_list_line(struct rev_info
*rev
)
1495 struct commit_list
*list
= rev
->commits
;
1496 struct commit_list
*commits
= xmalloc(sizeof(struct commit_list
));
1497 struct commit_list
*out
= commits
, *prev
= commits
;
1499 struct diff_line_range
*r
;
1502 list
->item
->object
.flags
|= RANGE_UPDATE
;
1505 /* Clear the flags */
1507 list
->item
->object
.flags
&= ~(RANGE_UPDATE
| NONTRIVIAL_MERGE
|
1508 NEED_PRINT
| EVIL_MERGE
);
1512 list
= rev
->commits
;
1516 if (c
->object
.flags
& RANGE_UPDATE
)
1517 assign_parents_range(rev
, c
);
1519 if (c
->object
.flags
& NEED_PRINT
|| c
->object
.flags
& NONTRIVIAL_MERGE
) {
1520 if (rewrite_parents(rev
, c
, rewrite_one
))
1521 die("Can't rewrite parent for commit %s",
1522 sha1_to_hex(c
->object
.sha1
));
1524 commits
->next
= xmalloc(sizeof(struct commit_list
));
1526 commits
= commits
->next
;
1528 r
= lookup_line_range(rev
, c
);
1532 add_line_range(rev
, c
, r
);
1542 list
= rev
->commits
;
1544 struct commit_list
*l
= list
;