1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
6 #include "range-diff.h"
7 #include "object-name.h"
8 #include "string-list.h"
9 #include "run-command.h"
12 #include "xdiff-interface.h"
13 #include "linear-assignment.h"
18 #include "repository.h"
24 /* For the search for an exact match */
25 struct hashmap_entry e
;
26 const char *diff
, *patch
;
31 /* the index of the matching item in the other branch, or -1 */
37 * Reads the patches into a string list, with the `util` field being populated
38 * as struct object_id (will need to be free()d).
40 static int read_patches(const char *range
, struct string_list
*list
,
41 const struct strvec
*other_arg
)
43 struct child_process cp
= CHILD_PROCESS_INIT
;
44 struct strbuf buf
= STRBUF_INIT
, contents
= STRBUF_INIT
;
45 struct patch_util
*util
= NULL
;
47 char *line
, *current_filename
= NULL
;
52 strvec_pushl(&cp
.args
, "log", "--no-color", "-p", "--no-merges",
53 "--reverse", "--date-order", "--decorate=no",
54 "--no-prefix", "--submodule=short",
56 * Choose indicators that are not used anywhere
57 * else in diffs, but still look reasonable
58 * (e.g. will not be confusing when debugging)
60 "--output-indicator-new=>",
61 "--output-indicator-old=<",
62 "--output-indicator-context=#",
65 "--show-notes-by-default",
67 strvec_push(&cp
.args
, range
);
69 strvec_pushv(&cp
.args
, other_arg
->v
);
74 if (start_command(&cp
))
75 return error_errno(_("could not start `log`"));
76 if (strbuf_read(&contents
, cp
.out
, 0) < 0) {
77 error_errno(_("could not read `log` output"));
81 if (finish_command(&cp
))
86 for (; size
> 0; size
-= len
, line
+= len
) {
90 eol
= memchr(line
, '\n', size
);
98 if (skip_prefix(line
, "commit ", &p
)) {
100 string_list_append(list
, buf
.buf
)->util
= util
;
103 CALLOC_ARRAY(util
, 1);
104 if (repo_get_oid(the_repository
, p
, &util
->oid
)) {
105 error(_("could not parse commit '%s'"), p
);
107 string_list_clear(list
, 1);
116 error(_("could not parse first line of `log` output: "
117 "did not start with 'commit ': '%s'"),
119 string_list_clear(list
, 1);
123 if (starts_with(line
, "diff --git")) {
124 struct patch patch
= { 0 };
125 struct strbuf root
= STRBUF_INIT
;
130 strbuf_addch(&buf
, '\n');
131 if (!util
->diff_offset
)
132 util
->diff_offset
= buf
.len
;
136 len
= parse_git_diff_header(&root
, &linenr
, 0, line
,
139 error(_("could not parse git header '%.*s'"),
142 string_list_clear(list
, 1);
145 strbuf_addstr(&buf
, " ## ");
146 if (patch
.is_new
> 0)
147 strbuf_addf(&buf
, "%s (new)", patch
.new_name
);
148 else if (patch
.is_delete
> 0)
149 strbuf_addf(&buf
, "%s (deleted)", patch
.old_name
);
150 else if (patch
.is_rename
)
151 strbuf_addf(&buf
, "%s => %s", patch
.old_name
, patch
.new_name
);
153 strbuf_addstr(&buf
, patch
.new_name
);
155 free(current_filename
);
156 if (patch
.is_delete
> 0)
157 current_filename
= xstrdup(patch
.old_name
);
159 current_filename
= xstrdup(patch
.new_name
);
161 if (patch
.new_mode
&& patch
.old_mode
&&
162 patch
.old_mode
!= patch
.new_mode
)
163 strbuf_addf(&buf
, " (mode change %06o => %06o)",
164 patch
.old_mode
, patch
.new_mode
);
166 strbuf_addstr(&buf
, " ##");
167 release_patch(&patch
);
168 } else if (in_header
) {
169 if (starts_with(line
, "Author: ")) {
170 strbuf_addstr(&buf
, " ## Metadata ##\n");
171 strbuf_addstr(&buf
, line
);
172 strbuf_addstr(&buf
, "\n\n");
173 strbuf_addstr(&buf
, " ## Commit message ##\n");
174 } else if (starts_with(line
, "Notes") &&
175 line
[strlen(line
) - 1] == ':') {
176 strbuf_addstr(&buf
, "\n\n");
177 /* strip the trailing colon */
178 strbuf_addf(&buf
, " ## %.*s ##\n",
179 (int)(strlen(line
) - 1), line
);
180 } else if (starts_with(line
, " ")) {
182 while (isspace(*p
) && p
>= line
)
184 strbuf_add(&buf
, line
, p
- line
+ 1);
185 strbuf_addch(&buf
, '\n');
188 } else if (skip_prefix(line
, "@@ ", &p
)) {
190 strbuf_addstr(&buf
, "@@");
191 if (current_filename
&& p
[2])
192 strbuf_addf(&buf
, " %s:", current_filename
);
194 strbuf_addstr(&buf
, p
+ 2);
197 * A completely blank (not ' \n', which is context)
198 * line is not valid in a diff. We skip it
199 * silently, because this neatly handles the blank
200 * separator line between commits in git-log
204 else if (line
[0] == '>') {
205 strbuf_addch(&buf
, '+');
206 strbuf_addstr(&buf
, line
+ 1);
207 } else if (line
[0] == '<') {
208 strbuf_addch(&buf
, '-');
209 strbuf_addstr(&buf
, line
+ 1);
210 } else if (line
[0] == '#') {
211 strbuf_addch(&buf
, ' ');
212 strbuf_addstr(&buf
, line
+ 1);
214 strbuf_addch(&buf
, ' ');
215 strbuf_addstr(&buf
, line
);
218 strbuf_addch(&buf
, '\n');
224 strbuf_release(&contents
);
227 string_list_append(list
, buf
.buf
)->util
= util
;
228 strbuf_release(&buf
);
229 free(current_filename
);
234 static int patch_util_cmp(const void *cmp_data UNUSED
,
235 const struct hashmap_entry
*ha
,
236 const struct hashmap_entry
*hb
,
239 const struct patch_util
240 *a
= container_of(ha
, const struct patch_util
, e
),
241 *b
= container_of(hb
, const struct patch_util
, e
);
242 return strcmp(a
->diff
, keydata
? keydata
: b
->diff
);
245 static void find_exact_matches(struct string_list
*a
, struct string_list
*b
)
247 struct hashmap map
= HASHMAP_INIT(patch_util_cmp
, NULL
);
250 /* First, add the patches of a to a hash map */
251 for (i
= 0; i
< a
->nr
; i
++) {
252 struct patch_util
*util
= a
->items
[i
].util
;
255 util
->patch
= a
->items
[i
].string
;
256 util
->diff
= util
->patch
+ util
->diff_offset
;
257 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
258 hashmap_add(&map
, &util
->e
);
261 /* Now try to find exact matches in b */
262 for (i
= 0; i
< b
->nr
; i
++) {
263 struct patch_util
*util
= b
->items
[i
].util
, *other
;
266 util
->patch
= b
->items
[i
].string
;
267 util
->diff
= util
->patch
+ util
->diff_offset
;
268 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
269 other
= hashmap_remove_entry(&map
, util
, e
, NULL
);
271 if (other
->matching
>= 0)
272 BUG("already assigned!");
275 util
->matching
= other
->i
;
282 static int diffsize_consume(void *data
,
284 unsigned long len UNUSED
)
290 static void diffsize_hunk(void *data
,
291 long ob UNUSED
, long on UNUSED
,
292 long nb UNUSED
, long nn UNUSED
,
293 const char *func UNUSED
, long funclen UNUSED
)
295 diffsize_consume(data
, NULL
, 0);
298 static int diffsize(const char *a
, const char *b
)
300 xpparam_t pp
= { 0 };
301 xdemitconf_t cfg
= { 0 };
306 mf1
.size
= strlen(a
);
308 mf2
.size
= strlen(b
);
311 if (!xdi_diff_outf(&mf1
, &mf2
,
312 diffsize_hunk
, diffsize_consume
, &count
,
316 error(_("failed to generate diff"));
320 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
323 int n
= a
->nr
+ b
->nr
;
324 int *cost
, c
, *a2b
, *b2a
;
327 ALLOC_ARRAY(cost
, st_mult(n
, n
));
331 for (i
= 0; i
< a
->nr
; i
++) {
332 struct patch_util
*a_util
= a
->items
[i
].util
;
334 for (j
= 0; j
< b
->nr
; j
++) {
335 struct patch_util
*b_util
= b
->items
[j
].util
;
337 if (a_util
->matching
== j
)
339 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
340 c
= diffsize(a_util
->diff
, b_util
->diff
);
346 c
= a_util
->matching
< 0 ?
347 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
348 for (j
= b
->nr
; j
< n
; j
++)
352 for (j
= 0; j
< b
->nr
; j
++) {
353 struct patch_util
*util
= b
->items
[j
].util
;
355 c
= util
->matching
< 0 ?
356 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
357 for (i
= a
->nr
; i
< n
; i
++)
361 for (i
= a
->nr
; i
< n
; i
++)
362 for (j
= b
->nr
; j
< n
; j
++)
365 compute_assignment(n
, n
, cost
, a2b
, b2a
);
367 for (i
= 0; i
< a
->nr
; i
++)
368 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
369 struct patch_util
*a_util
= a
->items
[i
].util
;
370 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
372 a_util
->matching
= a2b
[i
];
373 b_util
->matching
= i
;
381 static void output_pair_header(struct diff_options
*diffopt
,
384 struct strbuf
*dashes
,
385 struct patch_util
*a_util
,
386 struct patch_util
*b_util
)
388 struct object_id
*oid
= a_util
? &a_util
->oid
: &b_util
->oid
;
389 struct commit
*commit
;
391 const char *color_reset
= diff_get_color_opt(diffopt
, DIFF_RESET
);
392 const char *color_old
= diff_get_color_opt(diffopt
, DIFF_FILE_OLD
);
393 const char *color_new
= diff_get_color_opt(diffopt
, DIFF_FILE_NEW
);
394 const char *color_commit
= diff_get_color_opt(diffopt
, DIFF_COMMIT
);
396 int abbrev
= diffopt
->abbrev
;
399 abbrev
= DEFAULT_ABBREV
;
402 strbuf_addchars(dashes
, '-',
403 strlen(repo_find_unique_abbrev(the_repository
, oid
, abbrev
)));
408 } else if (!a_util
) {
411 } else if (strcmp(a_util
->patch
, b_util
->patch
)) {
412 color
= color_commit
;
415 color
= color_commit
;
420 strbuf_addstr(buf
, status
== '!' ? color_old
: color
);
422 strbuf_addf(buf
, "%*s: %s ", patch_no_width
, "-", dashes
->buf
);
424 strbuf_addf(buf
, "%*d: %s ", patch_no_width
, a_util
->i
+ 1,
425 repo_find_unique_abbrev(the_repository
, &a_util
->oid
, abbrev
));
428 strbuf_addf(buf
, "%s%s", color_reset
, color
);
429 strbuf_addch(buf
, status
);
431 strbuf_addf(buf
, "%s%s", color_reset
, color_new
);
434 strbuf_addf(buf
, " %*s: %s", patch_no_width
, "-", dashes
->buf
);
436 strbuf_addf(buf
, " %*d: %s", patch_no_width
, b_util
->i
+ 1,
437 repo_find_unique_abbrev(the_repository
, &b_util
->oid
, abbrev
));
439 commit
= lookup_commit_reference(the_repository
, oid
);
442 strbuf_addf(buf
, "%s%s", color_reset
, color
);
444 strbuf_addch(buf
, ' ');
445 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
447 strbuf_addf(buf
, "%s\n", color_reset
);
449 fwrite(buf
->buf
, buf
->len
, 1, diffopt
->file
);
452 static struct userdiff_driver section_headers
= {
454 .pattern
= "^ ## (.*) ##$\n^.?@@ (.*)$",
455 .cflags
= REG_EXTENDED
,
459 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
461 struct diff_filespec
*spec
= alloc_filespec(name
);
463 fill_filespec(spec
, null_oid(), 0, 0100644);
464 spec
->data
= (char *)p
;
465 spec
->size
= strlen(p
);
466 spec
->should_munmap
= 0;
468 spec
->driver
= §ion_headers
;
473 static void patch_diff(const char *a
, const char *b
,
474 struct diff_options
*diffopt
)
476 diff_queue(&diff_queued_diff
,
477 get_filespec("a", a
), get_filespec("b", b
));
479 diffcore_std(diffopt
);
483 static struct strbuf
*output_prefix_cb(struct diff_options
*opt UNUSED
, void *data
)
488 static void output(struct string_list
*a
, struct string_list
*b
,
489 struct range_diff_options
*range_diff_opts
)
491 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
492 int patch_no_width
= decimal_width(1 + (a
->nr
> b
->nr
? a
->nr
: b
->nr
));
494 struct diff_options opts
;
495 struct strbuf indent
= STRBUF_INIT
;
497 if (range_diff_opts
->diffopt
)
498 memcpy(&opts
, range_diff_opts
->diffopt
, sizeof(opts
));
500 repo_diff_setup(the_repository
, &opts
);
503 if (!opts
.output_format
)
504 opts
.output_format
= DIFF_FORMAT_PATCH
;
505 opts
.flags
.suppress_diff_headers
= 1;
506 opts
.flags
.dual_color_diffed_diffs
=
507 range_diff_opts
->dual_color
;
508 opts
.flags
.suppress_hunk_header_line_count
= 1;
509 opts
.output_prefix
= output_prefix_cb
;
510 strbuf_addstr(&indent
, " ");
511 opts
.output_prefix_data
= &indent
;
512 diff_setup_done(&opts
);
515 * We assume the user is really more interested in the second argument
516 * ("newer" version). To that end, we print the output in the order of
517 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
518 * commits that are no longer in the RHS into a good place, we place
519 * them once we have shown all of their predecessors in the LHS.
522 while (i
< a
->nr
|| j
< b
->nr
) {
523 struct patch_util
*a_util
, *b_util
;
524 a_util
= i
< a
->nr
? a
->items
[i
].util
: NULL
;
525 b_util
= j
< b
->nr
? b
->items
[j
].util
: NULL
;
527 /* Skip all the already-shown commits from the LHS. */
528 while (i
< a
->nr
&& a_util
->shown
)
529 a_util
= ++i
< a
->nr
? a
->items
[i
].util
: NULL
;
531 /* Show unmatched LHS commit whose predecessors were shown. */
532 if (i
< a
->nr
&& a_util
->matching
< 0) {
533 if (!range_diff_opts
->right_only
)
534 output_pair_header(&opts
, patch_no_width
,
535 &buf
, &dashes
, a_util
, NULL
);
540 /* Show unmatched RHS commits. */
541 while (j
< b
->nr
&& b_util
->matching
< 0) {
542 if (!range_diff_opts
->left_only
)
543 output_pair_header(&opts
, patch_no_width
,
544 &buf
, &dashes
, NULL
, b_util
);
545 b_util
= ++j
< b
->nr
? b
->items
[j
].util
: NULL
;
548 /* Show matching LHS/RHS pair. */
550 a_util
= a
->items
[b_util
->matching
].util
;
551 output_pair_header(&opts
, patch_no_width
,
552 &buf
, &dashes
, a_util
, b_util
);
553 if (!(opts
.output_format
& DIFF_FORMAT_NO_OUTPUT
))
554 patch_diff(a
->items
[b_util
->matching
].string
,
555 b
->items
[j
].string
, &opts
);
560 strbuf_release(&buf
);
561 strbuf_release(&dashes
);
562 strbuf_release(&indent
);
567 int show_range_diff(const char *range1
, const char *range2
,
568 struct range_diff_options
*range_diff_opts
)
572 struct string_list branch1
= STRING_LIST_INIT_DUP
;
573 struct string_list branch2
= STRING_LIST_INIT_DUP
;
575 if (range_diff_opts
->left_only
&& range_diff_opts
->right_only
)
576 res
= error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
578 if (!res
&& read_patches(range1
, &branch1
, range_diff_opts
->other_arg
))
579 res
= error(_("could not parse log for '%s'"), range1
);
580 if (!res
&& read_patches(range2
, &branch2
, range_diff_opts
->other_arg
))
581 res
= error(_("could not parse log for '%s'"), range2
);
584 find_exact_matches(&branch1
, &branch2
);
585 get_correspondences(&branch1
, &branch2
,
586 range_diff_opts
->creation_factor
);
587 output(&branch1
, &branch2
, range_diff_opts
);
590 string_list_clear(&branch1
, 1);
591 string_list_clear(&branch2
, 1);
596 int is_range_diff_range(const char *arg
)
598 char *copy
= xstrdup(arg
); /* setup_revisions() modifies it */
599 const char *argv
[] = { "", copy
, "--", NULL
};
600 int i
, positive
= 0, negative
= 0;
601 struct rev_info revs
;
603 repo_init_revisions(the_repository
, &revs
, NULL
);
604 if (setup_revisions(3, argv
, &revs
, NULL
) == 1) {
605 for (i
= 0; i
< revs
.pending
.nr
; i
++)
606 if (revs
.pending
.objects
[i
].item
->flags
& UNINTERESTING
)
610 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
611 struct object
*obj
= revs
.pending
.objects
[i
].item
;
613 if (obj
->type
== OBJ_COMMIT
)
614 clear_commit_marks((struct commit
*)obj
,
620 release_revisions(&revs
);
621 return negative
> 0 && positive
> 0;