2 #include "range-diff.h"
3 #include "string-list.h"
4 #include "run-command.h"
7 #include "xdiff-interface.h"
8 #include "linear-assignment.h"
16 /* For the search for an exact match */
17 struct hashmap_entry e
;
18 const char *diff
, *patch
;
23 /* the index of the matching item in the other branch, or -1 */
28 static size_t find_end_of_line(char *buffer
, unsigned long size
)
30 char *eol
= memchr(buffer
, '\n', size
);
36 return eol
+ 1 - buffer
;
40 * Reads the patches into a string list, with the `util` field being populated
41 * as struct object_id (will need to be free()d).
43 static int read_patches(const char *range
, struct string_list
*list
,
44 const struct strvec
*other_arg
)
46 struct child_process cp
= CHILD_PROCESS_INIT
;
47 struct strbuf buf
= STRBUF_INIT
, contents
= STRBUF_INIT
;
48 struct patch_util
*util
= NULL
;
50 char *line
, *current_filename
= NULL
;
54 strvec_pushl(&cp
.args
, "log", "--no-color", "-p", "--no-merges",
55 "--reverse", "--date-order", "--decorate=no",
58 * Choose indicators that are not used anywhere
59 * else in diffs, but still look reasonable
60 * (e.g. will not be confusing when debugging)
62 "--output-indicator-new=>",
63 "--output-indicator-old=<",
64 "--output-indicator-context=#",
70 strvec_pushv(&cp
.args
, other_arg
->v
);
71 strvec_push(&cp
.args
, range
);
76 if (start_command(&cp
))
77 return error_errno(_("could not start `log`"));
78 if (strbuf_read(&contents
, cp
.out
, 0) < 0) {
79 error_errno(_("could not read `log` output"));
83 if (finish_command(&cp
))
88 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
) {
91 len
= find_end_of_line(line
, size
);
93 if (skip_prefix(line
, "commit ", &p
)) {
95 string_list_append(list
, buf
.buf
)->util
= util
;
98 util
= xcalloc(sizeof(*util
), 1);
99 if (get_oid(p
, &util
->oid
)) {
100 error(_("could not parse commit '%s'"), p
);
102 free(current_filename
);
103 string_list_clear(list
, 1);
104 strbuf_release(&buf
);
105 strbuf_release(&contents
);
114 error(_("could not parse first line of `log` output: "
115 "did not start with 'commit ': '%s'"),
117 free(current_filename
);
118 string_list_clear(list
, 1);
119 strbuf_release(&buf
);
120 strbuf_release(&contents
);
124 if (starts_with(line
, "diff --git")) {
125 struct patch patch
= { 0 };
126 struct strbuf root
= STRBUF_INIT
;
131 strbuf_addch(&buf
, '\n');
132 if (!util
->diff_offset
)
133 util
->diff_offset
= buf
.len
;
134 line
[len
- 1] = '\n';
136 len
= parse_git_diff_header(&root
, &linenr
, 0, line
,
139 error(_("could not parse git header '%.*s'"),
142 free(current_filename
);
143 string_list_clear(list
, 1);
144 strbuf_release(&buf
);
145 strbuf_release(&contents
);
148 strbuf_addstr(&buf
, " ## ");
149 if (patch
.is_new
> 0)
150 strbuf_addf(&buf
, "%s (new)", patch
.new_name
);
151 else if (patch
.is_delete
> 0)
152 strbuf_addf(&buf
, "%s (deleted)", patch
.old_name
);
153 else if (patch
.is_rename
)
154 strbuf_addf(&buf
, "%s => %s", patch
.old_name
, patch
.new_name
);
156 strbuf_addstr(&buf
, patch
.new_name
);
158 free(current_filename
);
159 if (patch
.is_delete
> 0)
160 current_filename
= xstrdup(patch
.old_name
);
162 current_filename
= xstrdup(patch
.new_name
);
164 if (patch
.new_mode
&& patch
.old_mode
&&
165 patch
.old_mode
!= patch
.new_mode
)
166 strbuf_addf(&buf
, " (mode change %06o => %06o)",
167 patch
.old_mode
, patch
.new_mode
);
169 strbuf_addstr(&buf
, " ##");
170 } else if (in_header
) {
171 if (starts_with(line
, "Author: ")) {
172 strbuf_addstr(&buf
, " ## Metadata ##\n");
173 strbuf_addstr(&buf
, line
);
174 strbuf_addstr(&buf
, "\n\n");
175 strbuf_addstr(&buf
, " ## Commit message ##\n");
176 } else if (starts_with(line
, "Notes") &&
177 line
[strlen(line
) - 1] == ':') {
178 strbuf_addstr(&buf
, "\n\n");
179 /* strip the trailing colon */
180 strbuf_addf(&buf
, " ## %.*s ##\n",
181 (int)(strlen(line
) - 1), line
);
182 } else if (starts_with(line
, " ")) {
184 while (isspace(*p
) && p
>= line
)
186 strbuf_add(&buf
, line
, p
- line
+ 1);
187 strbuf_addch(&buf
, '\n');
190 } else if (skip_prefix(line
, "@@ ", &p
)) {
192 strbuf_addstr(&buf
, "@@");
193 if (current_filename
&& p
[2])
194 strbuf_addf(&buf
, " %s:", current_filename
);
196 strbuf_addstr(&buf
, p
+ 2);
199 * A completely blank (not ' \n', which is context)
200 * line is not valid in a diff. We skip it
201 * silently, because this neatly handles the blank
202 * separator line between commits in git-log
206 else if (line
[0] == '>') {
207 strbuf_addch(&buf
, '+');
208 strbuf_addstr(&buf
, line
+ 1);
209 } else if (line
[0] == '<') {
210 strbuf_addch(&buf
, '-');
211 strbuf_addstr(&buf
, line
+ 1);
212 } else if (line
[0] == '#') {
213 strbuf_addch(&buf
, ' ');
214 strbuf_addstr(&buf
, line
+ 1);
216 strbuf_addch(&buf
, ' ');
217 strbuf_addstr(&buf
, line
);
220 strbuf_addch(&buf
, '\n');
223 strbuf_release(&contents
);
226 string_list_append(list
, buf
.buf
)->util
= util
;
227 strbuf_release(&buf
);
228 free(current_filename
);
233 static int patch_util_cmp(const void *dummy
, const struct patch_util
*a
,
234 const struct patch_util
*b
, const char *keydata
)
236 return strcmp(a
->diff
, keydata
? keydata
: b
->diff
);
239 static void find_exact_matches(struct string_list
*a
, struct string_list
*b
)
241 struct hashmap map
= HASHMAP_INIT((hashmap_cmp_fn
)patch_util_cmp
, NULL
);
244 /* First, add the patches of a to a hash map */
245 for (i
= 0; i
< a
->nr
; i
++) {
246 struct patch_util
*util
= a
->items
[i
].util
;
249 util
->patch
= a
->items
[i
].string
;
250 util
->diff
= util
->patch
+ util
->diff_offset
;
251 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
252 hashmap_add(&map
, &util
->e
);
255 /* Now try to find exact matches in b */
256 for (i
= 0; i
< b
->nr
; i
++) {
257 struct patch_util
*util
= b
->items
[i
].util
, *other
;
260 util
->patch
= b
->items
[i
].string
;
261 util
->diff
= util
->patch
+ util
->diff_offset
;
262 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
263 other
= hashmap_remove_entry(&map
, util
, e
, NULL
);
265 if (other
->matching
>= 0)
266 BUG("already assigned!");
269 util
->matching
= other
->i
;
276 static void diffsize_consume(void *data
, char *line
, unsigned long len
)
281 static void diffsize_hunk(void *data
, long ob
, long on
, long nb
, long nn
,
282 const char *funcline
, long funclen
)
284 diffsize_consume(data
, NULL
, 0);
287 static int diffsize(const char *a
, const char *b
)
289 xpparam_t pp
= { 0 };
290 xdemitconf_t cfg
= { 0 };
295 mf1
.size
= strlen(a
);
297 mf2
.size
= strlen(b
);
300 if (!xdi_diff_outf(&mf1
, &mf2
,
301 diffsize_hunk
, diffsize_consume
, &count
,
305 error(_("failed to generate diff"));
309 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
312 int n
= a
->nr
+ b
->nr
;
313 int *cost
, c
, *a2b
, *b2a
;
316 ALLOC_ARRAY(cost
, st_mult(n
, n
));
320 for (i
= 0; i
< a
->nr
; i
++) {
321 struct patch_util
*a_util
= a
->items
[i
].util
;
323 for (j
= 0; j
< b
->nr
; j
++) {
324 struct patch_util
*b_util
= b
->items
[j
].util
;
326 if (a_util
->matching
== j
)
328 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
329 c
= diffsize(a_util
->diff
, b_util
->diff
);
335 c
= a_util
->matching
< 0 ?
336 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
337 for (j
= b
->nr
; j
< n
; j
++)
341 for (j
= 0; j
< b
->nr
; j
++) {
342 struct patch_util
*util
= b
->items
[j
].util
;
344 c
= util
->matching
< 0 ?
345 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
346 for (i
= a
->nr
; i
< n
; i
++)
350 for (i
= a
->nr
; i
< n
; i
++)
351 for (j
= b
->nr
; j
< n
; j
++)
354 compute_assignment(n
, n
, cost
, a2b
, b2a
);
356 for (i
= 0; i
< a
->nr
; i
++)
357 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
358 struct patch_util
*a_util
= a
->items
[i
].util
;
359 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
361 a_util
->matching
= a2b
[i
];
362 b_util
->matching
= i
;
370 static void output_pair_header(struct diff_options
*diffopt
,
373 struct strbuf
*dashes
,
374 struct patch_util
*a_util
,
375 struct patch_util
*b_util
)
377 struct object_id
*oid
= a_util
? &a_util
->oid
: &b_util
->oid
;
378 struct commit
*commit
;
380 const char *color_reset
= diff_get_color_opt(diffopt
, DIFF_RESET
);
381 const char *color_old
= diff_get_color_opt(diffopt
, DIFF_FILE_OLD
);
382 const char *color_new
= diff_get_color_opt(diffopt
, DIFF_FILE_NEW
);
383 const char *color_commit
= diff_get_color_opt(diffopt
, DIFF_COMMIT
);
387 strbuf_addchars(dashes
, '-',
388 strlen(find_unique_abbrev(oid
,
394 } else if (!a_util
) {
397 } else if (strcmp(a_util
->patch
, b_util
->patch
)) {
398 color
= color_commit
;
401 color
= color_commit
;
406 strbuf_addstr(buf
, status
== '!' ? color_old
: color
);
408 strbuf_addf(buf
, "%*s: %s ", patch_no_width
, "-", dashes
->buf
);
410 strbuf_addf(buf
, "%*d: %s ", patch_no_width
, a_util
->i
+ 1,
411 find_unique_abbrev(&a_util
->oid
, DEFAULT_ABBREV
));
414 strbuf_addf(buf
, "%s%s", color_reset
, color
);
415 strbuf_addch(buf
, status
);
417 strbuf_addf(buf
, "%s%s", color_reset
, color_new
);
420 strbuf_addf(buf
, " %*s: %s", patch_no_width
, "-", dashes
->buf
);
422 strbuf_addf(buf
, " %*d: %s", patch_no_width
, b_util
->i
+ 1,
423 find_unique_abbrev(&b_util
->oid
, DEFAULT_ABBREV
));
425 commit
= lookup_commit_reference(the_repository
, oid
);
428 strbuf_addf(buf
, "%s%s", color_reset
, color
);
430 strbuf_addch(buf
, ' ');
431 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
433 strbuf_addf(buf
, "%s\n", color_reset
);
435 fwrite(buf
->buf
, buf
->len
, 1, diffopt
->file
);
438 static struct userdiff_driver section_headers
= {
439 .funcname
= { "^ ## (.*) ##$\n"
440 "^.?@@ (.*)$", REG_EXTENDED
}
443 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
445 struct diff_filespec
*spec
= alloc_filespec(name
);
447 fill_filespec(spec
, &null_oid
, 0, 0100644);
448 spec
->data
= (char *)p
;
449 spec
->size
= strlen(p
);
450 spec
->should_munmap
= 0;
452 spec
->driver
= §ion_headers
;
457 static void patch_diff(const char *a
, const char *b
,
458 struct diff_options
*diffopt
)
460 diff_queue(&diff_queued_diff
,
461 get_filespec("a", a
), get_filespec("b", b
));
463 diffcore_std(diffopt
);
467 static struct strbuf
*output_prefix_cb(struct diff_options
*opt
, void *data
)
472 static void output(struct string_list
*a
, struct string_list
*b
,
473 struct range_diff_options
*range_diff_opts
)
475 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
476 int patch_no_width
= decimal_width(1 + (a
->nr
> b
->nr
? a
->nr
: b
->nr
));
478 struct diff_options opts
;
479 struct strbuf indent
= STRBUF_INIT
;
481 if (range_diff_opts
->diffopt
)
482 memcpy(&opts
, range_diff_opts
->diffopt
, sizeof(opts
));
486 if (!opts
.output_format
)
487 opts
.output_format
= DIFF_FORMAT_PATCH
;
488 opts
.flags
.suppress_diff_headers
= 1;
489 opts
.flags
.dual_color_diffed_diffs
=
490 range_diff_opts
->dual_color
;
491 opts
.flags
.suppress_hunk_header_line_count
= 1;
492 opts
.output_prefix
= output_prefix_cb
;
493 strbuf_addstr(&indent
, " ");
494 opts
.output_prefix_data
= &indent
;
495 diff_setup_done(&opts
);
498 * We assume the user is really more interested in the second argument
499 * ("newer" version). To that end, we print the output in the order of
500 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
501 * commits that are no longer in the RHS into a good place, we place
502 * them once we have shown all of their predecessors in the LHS.
505 while (i
< a
->nr
|| j
< b
->nr
) {
506 struct patch_util
*a_util
, *b_util
;
507 a_util
= i
< a
->nr
? a
->items
[i
].util
: NULL
;
508 b_util
= j
< b
->nr
? b
->items
[j
].util
: NULL
;
510 /* Skip all the already-shown commits from the LHS. */
511 while (i
< a
->nr
&& a_util
->shown
)
512 a_util
= ++i
< a
->nr
? a
->items
[i
].util
: NULL
;
514 /* Show unmatched LHS commit whose predecessors were shown. */
515 if (i
< a
->nr
&& a_util
->matching
< 0) {
516 if (!range_diff_opts
->right_only
)
517 output_pair_header(&opts
, patch_no_width
,
518 &buf
, &dashes
, a_util
, NULL
);
523 /* Show unmatched RHS commits. */
524 while (j
< b
->nr
&& b_util
->matching
< 0) {
525 if (!range_diff_opts
->left_only
)
526 output_pair_header(&opts
, patch_no_width
,
527 &buf
, &dashes
, NULL
, b_util
);
528 b_util
= ++j
< b
->nr
? b
->items
[j
].util
: NULL
;
531 /* Show matching LHS/RHS pair. */
533 a_util
= a
->items
[b_util
->matching
].util
;
534 output_pair_header(&opts
, patch_no_width
,
535 &buf
, &dashes
, a_util
, b_util
);
536 if (!(opts
.output_format
& DIFF_FORMAT_NO_OUTPUT
))
537 patch_diff(a
->items
[b_util
->matching
].string
,
538 b
->items
[j
].string
, &opts
);
543 strbuf_release(&buf
);
544 strbuf_release(&dashes
);
545 strbuf_release(&indent
);
548 int show_range_diff(const char *range1
, const char *range2
,
549 struct range_diff_options
*range_diff_opts
)
553 struct string_list branch1
= STRING_LIST_INIT_DUP
;
554 struct string_list branch2
= STRING_LIST_INIT_DUP
;
556 if (range_diff_opts
->left_only
&& range_diff_opts
->right_only
)
557 res
= error(_("--left-only and --right-only are mutually exclusive"));
559 if (!res
&& read_patches(range1
, &branch1
, range_diff_opts
->other_arg
))
560 res
= error(_("could not parse log for '%s'"), range1
);
561 if (!res
&& read_patches(range2
, &branch2
, range_diff_opts
->other_arg
))
562 res
= error(_("could not parse log for '%s'"), range2
);
565 find_exact_matches(&branch1
, &branch2
);
566 get_correspondences(&branch1
, &branch2
,
567 range_diff_opts
->creation_factor
);
568 output(&branch1
, &branch2
, range_diff_opts
);
571 string_list_clear(&branch1
, 1);
572 string_list_clear(&branch2
, 1);