1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "environment.h"
7 #include "range-diff.h"
8 #include "object-name.h"
9 #include "string-list.h"
10 #include "run-command.h"
13 #include "xdiff-interface.h"
14 #include "linear-assignment.h"
19 #include "repository.h"
25 /* For the search for an exact match */
26 struct hashmap_entry e
;
27 const char *diff
, *patch
;
32 /* the index of the matching item in the other branch, or -1 */
38 * Reads the patches into a string list, with the `util` field being populated
39 * as struct object_id (will need to be free()d).
41 static int read_patches(const char *range
, struct string_list
*list
,
42 const struct strvec
*other_arg
,
43 unsigned int include_merges
)
45 struct child_process cp
= CHILD_PROCESS_INIT
;
46 struct strbuf buf
= STRBUF_INIT
, contents
= STRBUF_INIT
;
47 struct patch_util
*util
= NULL
;
49 char *line
, *current_filename
= NULL
;
54 strvec_pushl(&cp
.args
, "log", "--no-color", "-p",
55 "--reverse", "--date-order", "--decorate=no",
56 "--no-prefix", "--submodule=short",
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=#",
67 "--show-notes-by-default",
70 strvec_push(&cp
.args
, "--no-merges");
71 strvec_push(&cp
.args
, range
);
73 strvec_pushv(&cp
.args
, other_arg
->v
);
78 if (start_command(&cp
))
79 return error_errno(_("could not start `log`"));
80 if (strbuf_read(&contents
, cp
.out
, 0) < 0) {
81 error_errno(_("could not read `log` output"));
85 if (finish_command(&cp
))
90 for (; size
> 0; size
-= len
, line
+= len
) {
94 eol
= memchr(line
, '\n', size
);
102 if (skip_prefix(line
, "commit ", &p
)) {
105 string_list_append(list
, buf
.buf
)->util
= util
;
108 CALLOC_ARRAY(util
, 1);
109 if (include_merges
&& (q
= strstr(p
, " (from ")))
111 if (repo_get_oid(the_repository
, p
, &util
->oid
)) {
112 error(_("could not parse commit '%s'"), p
);
114 string_list_clear(list
, 1);
123 error(_("could not parse first line of `log` output: "
124 "did not start with 'commit ': '%s'"),
126 string_list_clear(list
, 1);
130 if (starts_with(line
, "diff --git")) {
131 struct patch patch
= { 0 };
132 struct strbuf root
= STRBUF_INIT
;
137 strbuf_addch(&buf
, '\n');
138 if (!util
->diff_offset
)
139 util
->diff_offset
= buf
.len
;
143 len
= parse_git_diff_header(&root
, &linenr
, 0, line
,
146 error(_("could not parse git header '%.*s'"),
149 string_list_clear(list
, 1);
152 strbuf_addstr(&buf
, " ## ");
153 if (patch
.is_new
> 0)
154 strbuf_addf(&buf
, "%s (new)", patch
.new_name
);
155 else if (patch
.is_delete
> 0)
156 strbuf_addf(&buf
, "%s (deleted)", patch
.old_name
);
157 else if (patch
.is_rename
)
158 strbuf_addf(&buf
, "%s => %s", patch
.old_name
, patch
.new_name
);
160 strbuf_addstr(&buf
, patch
.new_name
);
162 free(current_filename
);
163 if (patch
.is_delete
> 0)
164 current_filename
= xstrdup(patch
.old_name
);
166 current_filename
= xstrdup(patch
.new_name
);
168 if (patch
.new_mode
&& patch
.old_mode
&&
169 patch
.old_mode
!= patch
.new_mode
)
170 strbuf_addf(&buf
, " (mode change %06o => %06o)",
171 patch
.old_mode
, patch
.new_mode
);
173 strbuf_addstr(&buf
, " ##");
174 release_patch(&patch
);
175 } else if (in_header
) {
176 if (starts_with(line
, "Author: ")) {
177 strbuf_addstr(&buf
, " ## Metadata ##\n");
178 strbuf_addstr(&buf
, line
);
179 strbuf_addstr(&buf
, "\n\n");
180 strbuf_addstr(&buf
, " ## Commit message ##\n");
181 } else if (starts_with(line
, "Notes") &&
182 line
[strlen(line
) - 1] == ':') {
183 strbuf_addstr(&buf
, "\n\n");
184 /* strip the trailing colon */
185 strbuf_addf(&buf
, " ## %.*s ##\n",
186 (int)(strlen(line
) - 1), line
);
187 } else if (starts_with(line
, " ")) {
189 while (isspace(*p
) && p
>= line
)
191 strbuf_add(&buf
, line
, p
- line
+ 1);
192 strbuf_addch(&buf
, '\n');
195 } else if (skip_prefix(line
, "@@ ", &p
)) {
197 strbuf_addstr(&buf
, "@@");
198 if (current_filename
&& p
[2])
199 strbuf_addf(&buf
, " %s:", current_filename
);
201 strbuf_addstr(&buf
, p
+ 2);
204 * A completely blank (not ' \n', which is context)
205 * line is not valid in a diff. We skip it
206 * silently, because this neatly handles the blank
207 * separator line between commits in git-log
211 else if (line
[0] == '>') {
212 strbuf_addch(&buf
, '+');
213 strbuf_addstr(&buf
, line
+ 1);
214 } else if (line
[0] == '<') {
215 strbuf_addch(&buf
, '-');
216 strbuf_addstr(&buf
, line
+ 1);
217 } else if (line
[0] == '#') {
218 strbuf_addch(&buf
, ' ');
219 strbuf_addstr(&buf
, line
+ 1);
221 strbuf_addch(&buf
, ' ');
222 strbuf_addstr(&buf
, line
);
225 strbuf_addch(&buf
, '\n');
231 strbuf_release(&contents
);
234 string_list_append(list
, buf
.buf
)->util
= util
;
235 strbuf_release(&buf
);
236 free(current_filename
);
241 static int patch_util_cmp(const void *cmp_data UNUSED
,
242 const struct hashmap_entry
*ha
,
243 const struct hashmap_entry
*hb
,
246 const struct patch_util
247 *a
= container_of(ha
, const struct patch_util
, e
),
248 *b
= container_of(hb
, const struct patch_util
, e
);
249 return strcmp(a
->diff
, keydata
? keydata
: b
->diff
);
252 static void find_exact_matches(struct string_list
*a
, struct string_list
*b
)
254 struct hashmap map
= HASHMAP_INIT(patch_util_cmp
, NULL
);
257 /* First, add the patches of a to a hash map */
258 for (i
= 0; i
< a
->nr
; i
++) {
259 struct patch_util
*util
= a
->items
[i
].util
;
262 util
->patch
= a
->items
[i
].string
;
263 util
->diff
= util
->patch
+ util
->diff_offset
;
264 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
265 hashmap_add(&map
, &util
->e
);
268 /* Now try to find exact matches in b */
269 for (i
= 0; i
< b
->nr
; i
++) {
270 struct patch_util
*util
= b
->items
[i
].util
, *other
;
273 util
->patch
= b
->items
[i
].string
;
274 util
->diff
= util
->patch
+ util
->diff_offset
;
275 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
276 other
= hashmap_remove_entry(&map
, util
, e
, NULL
);
278 if (other
->matching
>= 0)
279 BUG("already assigned!");
282 util
->matching
= other
->i
;
289 static int diffsize_consume(void *data
,
291 unsigned long len UNUSED
)
297 static void diffsize_hunk(void *data
,
298 long ob UNUSED
, long on UNUSED
,
299 long nb UNUSED
, long nn UNUSED
,
300 const char *func UNUSED
, long funclen UNUSED
)
302 diffsize_consume(data
, NULL
, 0);
305 static int diffsize(const char *a
, const char *b
)
307 xpparam_t pp
= { 0 };
308 xdemitconf_t cfg
= { 0 };
313 mf1
.size
= strlen(a
);
315 mf2
.size
= strlen(b
);
318 if (!xdi_diff_outf(&mf1
, &mf2
,
319 diffsize_hunk
, diffsize_consume
, &count
,
323 error(_("failed to generate diff"));
327 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
330 int n
= a
->nr
+ b
->nr
;
331 int *cost
, c
, *a2b
, *b2a
;
334 ALLOC_ARRAY(cost
, st_mult(n
, n
));
338 for (i
= 0; i
< a
->nr
; i
++) {
339 struct patch_util
*a_util
= a
->items
[i
].util
;
341 for (j
= 0; j
< b
->nr
; j
++) {
342 struct patch_util
*b_util
= b
->items
[j
].util
;
344 if (a_util
->matching
== j
)
346 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
347 c
= diffsize(a_util
->diff
, b_util
->diff
);
353 c
= a_util
->matching
< 0 ?
354 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
355 for (j
= b
->nr
; j
< n
; j
++)
359 for (j
= 0; j
< b
->nr
; j
++) {
360 struct patch_util
*util
= b
->items
[j
].util
;
362 c
= util
->matching
< 0 ?
363 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
364 for (i
= a
->nr
; i
< n
; i
++)
368 for (i
= a
->nr
; i
< n
; i
++)
369 for (j
= b
->nr
; j
< n
; j
++)
372 compute_assignment(n
, n
, cost
, a2b
, b2a
);
374 for (i
= 0; i
< a
->nr
; i
++)
375 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
376 struct patch_util
*a_util
= a
->items
[i
].util
;
377 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
379 a_util
->matching
= a2b
[i
];
380 b_util
->matching
= i
;
388 static void output_pair_header(struct diff_options
*diffopt
,
391 struct strbuf
*dashes
,
392 struct patch_util
*a_util
,
393 struct patch_util
*b_util
)
395 struct object_id
*oid
= a_util
? &a_util
->oid
: &b_util
->oid
;
396 struct commit
*commit
;
398 const char *color_reset
= diff_get_color_opt(diffopt
, DIFF_RESET
);
399 const char *color_old
= diff_get_color_opt(diffopt
, DIFF_FILE_OLD
);
400 const char *color_new
= diff_get_color_opt(diffopt
, DIFF_FILE_NEW
);
401 const char *color_commit
= diff_get_color_opt(diffopt
, DIFF_COMMIT
);
403 int abbrev
= diffopt
->abbrev
;
406 abbrev
= DEFAULT_ABBREV
;
409 strbuf_addchars(dashes
, '-',
410 strlen(repo_find_unique_abbrev(the_repository
, oid
, abbrev
)));
415 } else if (!a_util
) {
418 } else if (strcmp(a_util
->patch
, b_util
->patch
)) {
419 color
= color_commit
;
422 color
= color_commit
;
427 strbuf_addstr(buf
, status
== '!' ? color_old
: color
);
429 strbuf_addf(buf
, "%*s: %s ", patch_no_width
, "-", dashes
->buf
);
431 strbuf_addf(buf
, "%*d: %s ", patch_no_width
, a_util
->i
+ 1,
432 repo_find_unique_abbrev(the_repository
, &a_util
->oid
, abbrev
));
435 strbuf_addf(buf
, "%s%s", color_reset
, color
);
436 strbuf_addch(buf
, status
);
438 strbuf_addf(buf
, "%s%s", color_reset
, color_new
);
441 strbuf_addf(buf
, " %*s: %s", patch_no_width
, "-", dashes
->buf
);
443 strbuf_addf(buf
, " %*d: %s", patch_no_width
, b_util
->i
+ 1,
444 repo_find_unique_abbrev(the_repository
, &b_util
->oid
, abbrev
));
446 commit
= lookup_commit_reference(the_repository
, oid
);
449 strbuf_addf(buf
, "%s%s", color_reset
, color
);
451 strbuf_addch(buf
, ' ');
452 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
454 strbuf_addf(buf
, "%s\n", color_reset
);
456 fwrite(buf
->buf
, buf
->len
, 1, diffopt
->file
);
459 static struct userdiff_driver section_headers
= {
461 .pattern
= "^ ## (.*) ##$\n^.?@@ (.*)$",
462 .cflags
= REG_EXTENDED
,
466 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
468 struct diff_filespec
*spec
= alloc_filespec(name
);
470 fill_filespec(spec
, null_oid(the_hash_algo
), 0, 0100644);
471 spec
->data
= (char *)p
;
472 spec
->size
= strlen(p
);
473 spec
->should_munmap
= 0;
475 spec
->driver
= §ion_headers
;
480 static void patch_diff(const char *a
, const char *b
,
481 struct diff_options
*diffopt
)
483 diff_queue(&diff_queued_diff
,
484 get_filespec("a", a
), get_filespec("b", b
));
486 diffcore_std(diffopt
);
490 static const char *output_prefix_cb(struct diff_options
*opt UNUSED
, void *data
)
495 static void output(struct string_list
*a
, struct string_list
*b
,
496 struct range_diff_options
*range_diff_opts
)
498 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
499 int patch_no_width
= decimal_width(1 + (a
->nr
> b
->nr
? a
->nr
: b
->nr
));
501 struct diff_options opts
;
502 struct strbuf indent
= STRBUF_INIT
;
504 if (range_diff_opts
->diffopt
)
505 memcpy(&opts
, range_diff_opts
->diffopt
, sizeof(opts
));
507 repo_diff_setup(the_repository
, &opts
);
510 if (!opts
.output_format
)
511 opts
.output_format
= DIFF_FORMAT_PATCH
;
512 opts
.flags
.suppress_diff_headers
= 1;
513 opts
.flags
.dual_color_diffed_diffs
=
514 range_diff_opts
->dual_color
;
515 opts
.flags
.suppress_hunk_header_line_count
= 1;
516 opts
.output_prefix
= output_prefix_cb
;
517 strbuf_addstr(&indent
, " ");
518 opts
.output_prefix_data
= indent
.buf
;
519 diff_setup_done(&opts
);
522 * We assume the user is really more interested in the second argument
523 * ("newer" version). To that end, we print the output in the order of
524 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
525 * commits that are no longer in the RHS into a good place, we place
526 * them once we have shown all of their predecessors in the LHS.
529 while (i
< a
->nr
|| j
< b
->nr
) {
530 struct patch_util
*a_util
, *b_util
;
531 a_util
= i
< a
->nr
? a
->items
[i
].util
: NULL
;
532 b_util
= j
< b
->nr
? b
->items
[j
].util
: NULL
;
534 /* Skip all the already-shown commits from the LHS. */
535 while (i
< a
->nr
&& a_util
->shown
)
536 a_util
= ++i
< a
->nr
? a
->items
[i
].util
: NULL
;
538 /* Show unmatched LHS commit whose predecessors were shown. */
539 if (i
< a
->nr
&& a_util
->matching
< 0) {
540 if (!range_diff_opts
->right_only
)
541 output_pair_header(&opts
, patch_no_width
,
542 &buf
, &dashes
, a_util
, NULL
);
547 /* Show unmatched RHS commits. */
548 while (j
< b
->nr
&& b_util
->matching
< 0) {
549 if (!range_diff_opts
->left_only
)
550 output_pair_header(&opts
, patch_no_width
,
551 &buf
, &dashes
, NULL
, b_util
);
552 b_util
= ++j
< b
->nr
? b
->items
[j
].util
: NULL
;
555 /* Show matching LHS/RHS pair. */
557 a_util
= a
->items
[b_util
->matching
].util
;
558 output_pair_header(&opts
, patch_no_width
,
559 &buf
, &dashes
, a_util
, b_util
);
560 if (!(opts
.output_format
& DIFF_FORMAT_NO_OUTPUT
))
561 patch_diff(a
->items
[b_util
->matching
].string
,
562 b
->items
[j
].string
, &opts
);
567 strbuf_release(&buf
);
568 strbuf_release(&dashes
);
569 strbuf_release(&indent
);
574 int show_range_diff(const char *range1
, const char *range2
,
575 struct range_diff_options
*range_diff_opts
)
579 struct string_list branch1
= STRING_LIST_INIT_DUP
;
580 struct string_list branch2
= STRING_LIST_INIT_DUP
;
581 unsigned int include_merges
= range_diff_opts
->include_merges
;
583 if (range_diff_opts
->left_only
&& range_diff_opts
->right_only
)
584 res
= error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
586 if (!res
&& read_patches(range1
, &branch1
, range_diff_opts
->other_arg
, include_merges
))
587 res
= error(_("could not parse log for '%s'"), range1
);
588 if (!res
&& read_patches(range2
, &branch2
, range_diff_opts
->other_arg
, include_merges
))
589 res
= error(_("could not parse log for '%s'"), range2
);
592 find_exact_matches(&branch1
, &branch2
);
593 get_correspondences(&branch1
, &branch2
,
594 range_diff_opts
->creation_factor
);
595 output(&branch1
, &branch2
, range_diff_opts
);
598 string_list_clear(&branch1
, 1);
599 string_list_clear(&branch2
, 1);
604 int is_range_diff_range(const char *arg
)
606 char *copy
= xstrdup(arg
); /* setup_revisions() modifies it */
607 const char *argv
[] = { "", copy
, "--", NULL
};
608 int i
, positive
= 0, negative
= 0;
609 struct rev_info revs
;
611 repo_init_revisions(the_repository
, &revs
, NULL
);
612 if (setup_revisions(3, argv
, &revs
, NULL
) == 1) {
613 for (i
= 0; i
< revs
.pending
.nr
; i
++)
614 if (revs
.pending
.objects
[i
].item
->flags
& UNINTERESTING
)
618 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
619 struct object
*obj
= revs
.pending
.objects
[i
].item
;
621 if (obj
->type
== OBJ_COMMIT
)
622 clear_commit_marks((struct commit
*)obj
,
628 release_revisions(&revs
);
629 return negative
> 0 && positive
> 0;