3 #include "range-diff.h"
4 #include "string-list.h"
5 #include "run-command.h"
8 #include "xdiff-interface.h"
9 #include "linear-assignment.h"
18 /* For the search for an exact match */
19 struct hashmap_entry e
;
20 const char *diff
, *patch
;
25 /* the index of the matching item in the other branch, or -1 */
31 * Reads the patches into a string list, with the `util` field being populated
32 * as struct object_id (will need to be free()d).
34 static int read_patches(const char *range
, struct string_list
*list
,
35 const struct strvec
*other_arg
)
37 struct child_process cp
= CHILD_PROCESS_INIT
;
38 struct strbuf buf
= STRBUF_INIT
, contents
= STRBUF_INIT
;
39 struct patch_util
*util
= NULL
;
41 char *line
, *current_filename
= NULL
;
46 strvec_pushl(&cp
.args
, "log", "--no-color", "-p", "--no-merges",
47 "--reverse", "--date-order", "--decorate=no",
48 "--no-prefix", "--submodule=short",
50 * Choose indicators that are not used anywhere
51 * else in diffs, but still look reasonable
52 * (e.g. will not be confusing when debugging)
54 "--output-indicator-new=>",
55 "--output-indicator-old=<",
56 "--output-indicator-context=#",
61 strvec_push(&cp
.args
, range
);
63 strvec_pushv(&cp
.args
, other_arg
->v
);
68 if (start_command(&cp
))
69 return error_errno(_("could not start `log`"));
70 if (strbuf_read(&contents
, cp
.out
, 0) < 0) {
71 error_errno(_("could not read `log` output"));
75 if (finish_command(&cp
))
80 for (; size
> 0; size
-= len
, line
+= len
) {
84 eol
= memchr(line
, '\n', size
);
92 if (skip_prefix(line
, "commit ", &p
)) {
94 string_list_append(list
, buf
.buf
)->util
= util
;
97 CALLOC_ARRAY(util
, 1);
98 if (get_oid(p
, &util
->oid
)) {
99 error(_("could not parse commit '%s'"), p
);
101 string_list_clear(list
, 1);
110 error(_("could not parse first line of `log` output: "
111 "did not start with 'commit ': '%s'"),
113 string_list_clear(list
, 1);
117 if (starts_with(line
, "diff --git")) {
118 struct patch patch
= { 0 };
119 struct strbuf root
= STRBUF_INIT
;
124 strbuf_addch(&buf
, '\n');
125 if (!util
->diff_offset
)
126 util
->diff_offset
= buf
.len
;
130 len
= parse_git_diff_header(&root
, &linenr
, 0, line
,
133 error(_("could not parse git header '%.*s'"),
136 string_list_clear(list
, 1);
139 strbuf_addstr(&buf
, " ## ");
140 if (patch
.is_new
> 0)
141 strbuf_addf(&buf
, "%s (new)", patch
.new_name
);
142 else if (patch
.is_delete
> 0)
143 strbuf_addf(&buf
, "%s (deleted)", patch
.old_name
);
144 else if (patch
.is_rename
)
145 strbuf_addf(&buf
, "%s => %s", patch
.old_name
, patch
.new_name
);
147 strbuf_addstr(&buf
, patch
.new_name
);
149 free(current_filename
);
150 if (patch
.is_delete
> 0)
151 current_filename
= xstrdup(patch
.old_name
);
153 current_filename
= xstrdup(patch
.new_name
);
155 if (patch
.new_mode
&& patch
.old_mode
&&
156 patch
.old_mode
!= patch
.new_mode
)
157 strbuf_addf(&buf
, " (mode change %06o => %06o)",
158 patch
.old_mode
, patch
.new_mode
);
160 strbuf_addstr(&buf
, " ##");
161 release_patch(&patch
);
162 } else if (in_header
) {
163 if (starts_with(line
, "Author: ")) {
164 strbuf_addstr(&buf
, " ## Metadata ##\n");
165 strbuf_addstr(&buf
, line
);
166 strbuf_addstr(&buf
, "\n\n");
167 strbuf_addstr(&buf
, " ## Commit message ##\n");
168 } else if (starts_with(line
, "Notes") &&
169 line
[strlen(line
) - 1] == ':') {
170 strbuf_addstr(&buf
, "\n\n");
171 /* strip the trailing colon */
172 strbuf_addf(&buf
, " ## %.*s ##\n",
173 (int)(strlen(line
) - 1), line
);
174 } else if (starts_with(line
, " ")) {
176 while (isspace(*p
) && p
>= line
)
178 strbuf_add(&buf
, line
, p
- line
+ 1);
179 strbuf_addch(&buf
, '\n');
182 } else if (skip_prefix(line
, "@@ ", &p
)) {
184 strbuf_addstr(&buf
, "@@");
185 if (current_filename
&& p
[2])
186 strbuf_addf(&buf
, " %s:", current_filename
);
188 strbuf_addstr(&buf
, p
+ 2);
191 * A completely blank (not ' \n', which is context)
192 * line is not valid in a diff. We skip it
193 * silently, because this neatly handles the blank
194 * separator line between commits in git-log
198 else if (line
[0] == '>') {
199 strbuf_addch(&buf
, '+');
200 strbuf_addstr(&buf
, line
+ 1);
201 } else if (line
[0] == '<') {
202 strbuf_addch(&buf
, '-');
203 strbuf_addstr(&buf
, line
+ 1);
204 } else if (line
[0] == '#') {
205 strbuf_addch(&buf
, ' ');
206 strbuf_addstr(&buf
, line
+ 1);
208 strbuf_addch(&buf
, ' ');
209 strbuf_addstr(&buf
, line
);
212 strbuf_addch(&buf
, '\n');
218 strbuf_release(&contents
);
221 string_list_append(list
, buf
.buf
)->util
= util
;
222 strbuf_release(&buf
);
223 free(current_filename
);
228 static int patch_util_cmp(const void *cmp_data UNUSED
,
229 const struct patch_util
*a
,
230 const struct patch_util
*b
,
233 return strcmp(a
->diff
, keydata
? keydata
: b
->diff
);
236 static void find_exact_matches(struct string_list
*a
, struct string_list
*b
)
238 struct hashmap map
= HASHMAP_INIT((hashmap_cmp_fn
)patch_util_cmp
, NULL
);
241 /* First, add the patches of a to a hash map */
242 for (i
= 0; i
< a
->nr
; i
++) {
243 struct patch_util
*util
= a
->items
[i
].util
;
246 util
->patch
= a
->items
[i
].string
;
247 util
->diff
= util
->patch
+ util
->diff_offset
;
248 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
249 hashmap_add(&map
, &util
->e
);
252 /* Now try to find exact matches in b */
253 for (i
= 0; i
< b
->nr
; i
++) {
254 struct patch_util
*util
= b
->items
[i
].util
, *other
;
257 util
->patch
= b
->items
[i
].string
;
258 util
->diff
= util
->patch
+ util
->diff_offset
;
259 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
260 other
= hashmap_remove_entry(&map
, util
, e
, NULL
);
262 if (other
->matching
>= 0)
263 BUG("already assigned!");
266 util
->matching
= other
->i
;
273 static int diffsize_consume(void *data
,
275 unsigned long len UNUSED
)
281 static void diffsize_hunk(void *data
,
282 long ob UNUSED
, long on UNUSED
,
283 long nb UNUSED
, long nn UNUSED
,
284 const char *func UNUSED
, long funclen UNUSED
)
286 diffsize_consume(data
, NULL
, 0);
289 static int diffsize(const char *a
, const char *b
)
291 xpparam_t pp
= { 0 };
292 xdemitconf_t cfg
= { 0 };
297 mf1
.size
= strlen(a
);
299 mf2
.size
= strlen(b
);
302 if (!xdi_diff_outf(&mf1
, &mf2
,
303 diffsize_hunk
, diffsize_consume
, &count
,
307 error(_("failed to generate diff"));
311 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
314 int n
= a
->nr
+ b
->nr
;
315 int *cost
, c
, *a2b
, *b2a
;
318 ALLOC_ARRAY(cost
, st_mult(n
, n
));
322 for (i
= 0; i
< a
->nr
; i
++) {
323 struct patch_util
*a_util
= a
->items
[i
].util
;
325 for (j
= 0; j
< b
->nr
; j
++) {
326 struct patch_util
*b_util
= b
->items
[j
].util
;
328 if (a_util
->matching
== j
)
330 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
331 c
= diffsize(a_util
->diff
, b_util
->diff
);
337 c
= a_util
->matching
< 0 ?
338 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
339 for (j
= b
->nr
; j
< n
; j
++)
343 for (j
= 0; j
< b
->nr
; j
++) {
344 struct patch_util
*util
= b
->items
[j
].util
;
346 c
= util
->matching
< 0 ?
347 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
348 for (i
= a
->nr
; i
< n
; i
++)
352 for (i
= a
->nr
; i
< n
; i
++)
353 for (j
= b
->nr
; j
< n
; j
++)
356 compute_assignment(n
, n
, cost
, a2b
, b2a
);
358 for (i
= 0; i
< a
->nr
; i
++)
359 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
360 struct patch_util
*a_util
= a
->items
[i
].util
;
361 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
363 a_util
->matching
= a2b
[i
];
364 b_util
->matching
= i
;
372 static void output_pair_header(struct diff_options
*diffopt
,
375 struct strbuf
*dashes
,
376 struct patch_util
*a_util
,
377 struct patch_util
*b_util
)
379 struct object_id
*oid
= a_util
? &a_util
->oid
: &b_util
->oid
;
380 struct commit
*commit
;
382 const char *color_reset
= diff_get_color_opt(diffopt
, DIFF_RESET
);
383 const char *color_old
= diff_get_color_opt(diffopt
, DIFF_FILE_OLD
);
384 const char *color_new
= diff_get_color_opt(diffopt
, DIFF_FILE_NEW
);
385 const char *color_commit
= diff_get_color_opt(diffopt
, DIFF_COMMIT
);
387 int abbrev
= diffopt
->abbrev
;
390 abbrev
= DEFAULT_ABBREV
;
393 strbuf_addchars(dashes
, '-',
394 strlen(find_unique_abbrev(oid
, abbrev
)));
399 } else if (!a_util
) {
402 } else if (strcmp(a_util
->patch
, b_util
->patch
)) {
403 color
= color_commit
;
406 color
= color_commit
;
411 strbuf_addstr(buf
, status
== '!' ? color_old
: color
);
413 strbuf_addf(buf
, "%*s: %s ", patch_no_width
, "-", dashes
->buf
);
415 strbuf_addf(buf
, "%*d: %s ", patch_no_width
, a_util
->i
+ 1,
416 find_unique_abbrev(&a_util
->oid
, abbrev
));
419 strbuf_addf(buf
, "%s%s", color_reset
, color
);
420 strbuf_addch(buf
, status
);
422 strbuf_addf(buf
, "%s%s", color_reset
, color_new
);
425 strbuf_addf(buf
, " %*s: %s", patch_no_width
, "-", dashes
->buf
);
427 strbuf_addf(buf
, " %*d: %s", patch_no_width
, b_util
->i
+ 1,
428 find_unique_abbrev(&b_util
->oid
, abbrev
));
430 commit
= lookup_commit_reference(the_repository
, oid
);
433 strbuf_addf(buf
, "%s%s", color_reset
, color
);
435 strbuf_addch(buf
, ' ');
436 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
438 strbuf_addf(buf
, "%s\n", color_reset
);
440 fwrite(buf
->buf
, buf
->len
, 1, diffopt
->file
);
443 static struct userdiff_driver section_headers
= {
444 .funcname
= { "^ ## (.*) ##$\n"
445 "^.?@@ (.*)$", REG_EXTENDED
}
448 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
450 struct diff_filespec
*spec
= alloc_filespec(name
);
452 fill_filespec(spec
, null_oid(), 0, 0100644);
453 spec
->data
= (char *)p
;
454 spec
->size
= strlen(p
);
455 spec
->should_munmap
= 0;
457 spec
->driver
= §ion_headers
;
462 static void patch_diff(const char *a
, const char *b
,
463 struct diff_options
*diffopt
)
465 diff_queue(&diff_queued_diff
,
466 get_filespec("a", a
), get_filespec("b", b
));
468 diffcore_std(diffopt
);
472 static struct strbuf
*output_prefix_cb(struct diff_options
*opt UNUSED
, void *data
)
477 static void output(struct string_list
*a
, struct string_list
*b
,
478 struct range_diff_options
*range_diff_opts
)
480 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
481 int patch_no_width
= decimal_width(1 + (a
->nr
> b
->nr
? a
->nr
: b
->nr
));
483 struct diff_options opts
;
484 struct strbuf indent
= STRBUF_INIT
;
486 if (range_diff_opts
->diffopt
)
487 memcpy(&opts
, range_diff_opts
->diffopt
, sizeof(opts
));
492 if (!opts
.output_format
)
493 opts
.output_format
= DIFF_FORMAT_PATCH
;
494 opts
.flags
.suppress_diff_headers
= 1;
495 opts
.flags
.dual_color_diffed_diffs
=
496 range_diff_opts
->dual_color
;
497 opts
.flags
.suppress_hunk_header_line_count
= 1;
498 opts
.output_prefix
= output_prefix_cb
;
499 strbuf_addstr(&indent
, " ");
500 opts
.output_prefix_data
= &indent
;
501 diff_setup_done(&opts
);
504 * We assume the user is really more interested in the second argument
505 * ("newer" version). To that end, we print the output in the order of
506 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
507 * commits that are no longer in the RHS into a good place, we place
508 * them once we have shown all of their predecessors in the LHS.
511 while (i
< a
->nr
|| j
< b
->nr
) {
512 struct patch_util
*a_util
, *b_util
;
513 a_util
= i
< a
->nr
? a
->items
[i
].util
: NULL
;
514 b_util
= j
< b
->nr
? b
->items
[j
].util
: NULL
;
516 /* Skip all the already-shown commits from the LHS. */
517 while (i
< a
->nr
&& a_util
->shown
)
518 a_util
= ++i
< a
->nr
? a
->items
[i
].util
: NULL
;
520 /* Show unmatched LHS commit whose predecessors were shown. */
521 if (i
< a
->nr
&& a_util
->matching
< 0) {
522 if (!range_diff_opts
->right_only
)
523 output_pair_header(&opts
, patch_no_width
,
524 &buf
, &dashes
, a_util
, NULL
);
529 /* Show unmatched RHS commits. */
530 while (j
< b
->nr
&& b_util
->matching
< 0) {
531 if (!range_diff_opts
->left_only
)
532 output_pair_header(&opts
, patch_no_width
,
533 &buf
, &dashes
, NULL
, b_util
);
534 b_util
= ++j
< b
->nr
? b
->items
[j
].util
: NULL
;
537 /* Show matching LHS/RHS pair. */
539 a_util
= a
->items
[b_util
->matching
].util
;
540 output_pair_header(&opts
, patch_no_width
,
541 &buf
, &dashes
, a_util
, b_util
);
542 if (!(opts
.output_format
& DIFF_FORMAT_NO_OUTPUT
))
543 patch_diff(a
->items
[b_util
->matching
].string
,
544 b
->items
[j
].string
, &opts
);
549 strbuf_release(&buf
);
550 strbuf_release(&dashes
);
551 strbuf_release(&indent
);
556 int show_range_diff(const char *range1
, const char *range2
,
557 struct range_diff_options
*range_diff_opts
)
561 struct string_list branch1
= STRING_LIST_INIT_DUP
;
562 struct string_list branch2
= STRING_LIST_INIT_DUP
;
564 if (range_diff_opts
->left_only
&& range_diff_opts
->right_only
)
565 res
= error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
567 if (!res
&& read_patches(range1
, &branch1
, range_diff_opts
->other_arg
))
568 res
= error(_("could not parse log for '%s'"), range1
);
569 if (!res
&& read_patches(range2
, &branch2
, range_diff_opts
->other_arg
))
570 res
= error(_("could not parse log for '%s'"), range2
);
573 find_exact_matches(&branch1
, &branch2
);
574 get_correspondences(&branch1
, &branch2
,
575 range_diff_opts
->creation_factor
);
576 output(&branch1
, &branch2
, range_diff_opts
);
579 string_list_clear(&branch1
, 1);
580 string_list_clear(&branch2
, 1);
585 int is_range_diff_range(const char *arg
)
587 char *copy
= xstrdup(arg
); /* setup_revisions() modifies it */
588 const char *argv
[] = { "", copy
, "--", NULL
};
589 int i
, positive
= 0, negative
= 0;
590 struct rev_info revs
;
592 init_revisions(&revs
, NULL
);
593 if (setup_revisions(3, argv
, &revs
, NULL
) == 1) {
594 for (i
= 0; i
< revs
.pending
.nr
; i
++)
595 if (revs
.pending
.objects
[i
].item
->flags
& UNINTERESTING
)
599 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
600 struct object
*obj
= revs
.pending
.objects
[i
].item
;
602 if (obj
->type
== OBJ_COMMIT
)
603 clear_commit_marks((struct commit
*)obj
,
609 release_revisions(&revs
);
610 return negative
> 0 && positive
> 0;