1 #include "git-compat-util.h"
2 #include "environment.h"
4 #include "range-diff.h"
5 #include "object-name.h"
6 #include "string-list.h"
7 #include "run-command.h"
10 #include "xdiff-interface.h"
11 #include "linear-assignment.h"
16 #include "repository.h"
22 /* For the search for an exact match */
23 struct hashmap_entry e
;
24 const char *diff
, *patch
;
29 /* the index of the matching item in the other branch, or -1 */
35 * Reads the patches into a string list, with the `util` field being populated
36 * as struct object_id (will need to be free()d).
38 static int read_patches(const char *range
, struct string_list
*list
,
39 const struct strvec
*other_arg
)
41 struct child_process cp
= CHILD_PROCESS_INIT
;
42 struct strbuf buf
= STRBUF_INIT
, contents
= STRBUF_INIT
;
43 struct patch_util
*util
= NULL
;
45 char *line
, *current_filename
= NULL
;
50 strvec_pushl(&cp
.args
, "log", "--no-color", "-p", "--no-merges",
51 "--reverse", "--date-order", "--decorate=no",
52 "--no-prefix", "--submodule=short",
54 * Choose indicators that are not used anywhere
55 * else in diffs, but still look reasonable
56 * (e.g. will not be confusing when debugging)
58 "--output-indicator-new=>",
59 "--output-indicator-old=<",
60 "--output-indicator-context=#",
63 "--show-notes-by-default",
65 strvec_push(&cp
.args
, range
);
67 strvec_pushv(&cp
.args
, other_arg
->v
);
72 if (start_command(&cp
))
73 return error_errno(_("could not start `log`"));
74 if (strbuf_read(&contents
, cp
.out
, 0) < 0) {
75 error_errno(_("could not read `log` output"));
79 if (finish_command(&cp
))
84 for (; size
> 0; size
-= len
, line
+= len
) {
88 eol
= memchr(line
, '\n', size
);
96 if (skip_prefix(line
, "commit ", &p
)) {
98 string_list_append(list
, buf
.buf
)->util
= util
;
101 CALLOC_ARRAY(util
, 1);
102 if (repo_get_oid(the_repository
, p
, &util
->oid
)) {
103 error(_("could not parse commit '%s'"), p
);
105 string_list_clear(list
, 1);
114 error(_("could not parse first line of `log` output: "
115 "did not start with 'commit ': '%s'"),
117 string_list_clear(list
, 1);
121 if (starts_with(line
, "diff --git")) {
122 struct patch patch
= { 0 };
123 struct strbuf root
= STRBUF_INIT
;
128 strbuf_addch(&buf
, '\n');
129 if (!util
->diff_offset
)
130 util
->diff_offset
= buf
.len
;
134 len
= parse_git_diff_header(&root
, &linenr
, 0, line
,
137 error(_("could not parse git header '%.*s'"),
140 string_list_clear(list
, 1);
143 strbuf_addstr(&buf
, " ## ");
144 if (patch
.is_new
> 0)
145 strbuf_addf(&buf
, "%s (new)", patch
.new_name
);
146 else if (patch
.is_delete
> 0)
147 strbuf_addf(&buf
, "%s (deleted)", patch
.old_name
);
148 else if (patch
.is_rename
)
149 strbuf_addf(&buf
, "%s => %s", patch
.old_name
, patch
.new_name
);
151 strbuf_addstr(&buf
, patch
.new_name
);
153 free(current_filename
);
154 if (patch
.is_delete
> 0)
155 current_filename
= xstrdup(patch
.old_name
);
157 current_filename
= xstrdup(patch
.new_name
);
159 if (patch
.new_mode
&& patch
.old_mode
&&
160 patch
.old_mode
!= patch
.new_mode
)
161 strbuf_addf(&buf
, " (mode change %06o => %06o)",
162 patch
.old_mode
, patch
.new_mode
);
164 strbuf_addstr(&buf
, " ##");
165 release_patch(&patch
);
166 } else if (in_header
) {
167 if (starts_with(line
, "Author: ")) {
168 strbuf_addstr(&buf
, " ## Metadata ##\n");
169 strbuf_addstr(&buf
, line
);
170 strbuf_addstr(&buf
, "\n\n");
171 strbuf_addstr(&buf
, " ## Commit message ##\n");
172 } else if (starts_with(line
, "Notes") &&
173 line
[strlen(line
) - 1] == ':') {
174 strbuf_addstr(&buf
, "\n\n");
175 /* strip the trailing colon */
176 strbuf_addf(&buf
, " ## %.*s ##\n",
177 (int)(strlen(line
) - 1), line
);
178 } else if (starts_with(line
, " ")) {
180 while (isspace(*p
) && p
>= line
)
182 strbuf_add(&buf
, line
, p
- line
+ 1);
183 strbuf_addch(&buf
, '\n');
186 } else if (skip_prefix(line
, "@@ ", &p
)) {
188 strbuf_addstr(&buf
, "@@");
189 if (current_filename
&& p
[2])
190 strbuf_addf(&buf
, " %s:", current_filename
);
192 strbuf_addstr(&buf
, p
+ 2);
195 * A completely blank (not ' \n', which is context)
196 * line is not valid in a diff. We skip it
197 * silently, because this neatly handles the blank
198 * separator line between commits in git-log
202 else if (line
[0] == '>') {
203 strbuf_addch(&buf
, '+');
204 strbuf_addstr(&buf
, line
+ 1);
205 } else if (line
[0] == '<') {
206 strbuf_addch(&buf
, '-');
207 strbuf_addstr(&buf
, line
+ 1);
208 } else if (line
[0] == '#') {
209 strbuf_addch(&buf
, ' ');
210 strbuf_addstr(&buf
, line
+ 1);
212 strbuf_addch(&buf
, ' ');
213 strbuf_addstr(&buf
, line
);
216 strbuf_addch(&buf
, '\n');
222 strbuf_release(&contents
);
225 string_list_append(list
, buf
.buf
)->util
= util
;
226 strbuf_release(&buf
);
227 free(current_filename
);
232 static int patch_util_cmp(const void *cmp_data UNUSED
,
233 const struct hashmap_entry
*ha
,
234 const struct hashmap_entry
*hb
,
237 const struct patch_util
238 *a
= container_of(ha
, const struct patch_util
, e
),
239 *b
= container_of(hb
, const struct patch_util
, e
);
240 return strcmp(a
->diff
, keydata
? keydata
: b
->diff
);
243 static void find_exact_matches(struct string_list
*a
, struct string_list
*b
)
245 struct hashmap map
= HASHMAP_INIT(patch_util_cmp
, NULL
);
248 /* First, add the patches of a to a hash map */
249 for (i
= 0; i
< a
->nr
; i
++) {
250 struct patch_util
*util
= a
->items
[i
].util
;
253 util
->patch
= a
->items
[i
].string
;
254 util
->diff
= util
->patch
+ util
->diff_offset
;
255 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
256 hashmap_add(&map
, &util
->e
);
259 /* Now try to find exact matches in b */
260 for (i
= 0; i
< b
->nr
; i
++) {
261 struct patch_util
*util
= b
->items
[i
].util
, *other
;
264 util
->patch
= b
->items
[i
].string
;
265 util
->diff
= util
->patch
+ util
->diff_offset
;
266 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
267 other
= hashmap_remove_entry(&map
, util
, e
, NULL
);
269 if (other
->matching
>= 0)
270 BUG("already assigned!");
273 util
->matching
= other
->i
;
280 static int diffsize_consume(void *data
,
282 unsigned long len UNUSED
)
288 static void diffsize_hunk(void *data
,
289 long ob UNUSED
, long on UNUSED
,
290 long nb UNUSED
, long nn UNUSED
,
291 const char *func UNUSED
, long funclen UNUSED
)
293 diffsize_consume(data
, NULL
, 0);
296 static int diffsize(const char *a
, const char *b
)
298 xpparam_t pp
= { 0 };
299 xdemitconf_t cfg
= { 0 };
304 mf1
.size
= strlen(a
);
306 mf2
.size
= strlen(b
);
309 if (!xdi_diff_outf(&mf1
, &mf2
,
310 diffsize_hunk
, diffsize_consume
, &count
,
314 error(_("failed to generate diff"));
318 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
321 int n
= a
->nr
+ b
->nr
;
322 int *cost
, c
, *a2b
, *b2a
;
325 ALLOC_ARRAY(cost
, st_mult(n
, n
));
329 for (i
= 0; i
< a
->nr
; i
++) {
330 struct patch_util
*a_util
= a
->items
[i
].util
;
332 for (j
= 0; j
< b
->nr
; j
++) {
333 struct patch_util
*b_util
= b
->items
[j
].util
;
335 if (a_util
->matching
== j
)
337 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
338 c
= diffsize(a_util
->diff
, b_util
->diff
);
344 c
= a_util
->matching
< 0 ?
345 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
346 for (j
= b
->nr
; j
< n
; j
++)
350 for (j
= 0; j
< b
->nr
; j
++) {
351 struct patch_util
*util
= b
->items
[j
].util
;
353 c
= util
->matching
< 0 ?
354 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
355 for (i
= a
->nr
; i
< n
; i
++)
359 for (i
= a
->nr
; i
< n
; i
++)
360 for (j
= b
->nr
; j
< n
; j
++)
363 compute_assignment(n
, n
, cost
, a2b
, b2a
);
365 for (i
= 0; i
< a
->nr
; i
++)
366 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
367 struct patch_util
*a_util
= a
->items
[i
].util
;
368 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
370 a_util
->matching
= a2b
[i
];
371 b_util
->matching
= i
;
379 static void output_pair_header(struct diff_options
*diffopt
,
382 struct strbuf
*dashes
,
383 struct patch_util
*a_util
,
384 struct patch_util
*b_util
)
386 struct object_id
*oid
= a_util
? &a_util
->oid
: &b_util
->oid
;
387 struct commit
*commit
;
389 const char *color_reset
= diff_get_color_opt(diffopt
, DIFF_RESET
);
390 const char *color_old
= diff_get_color_opt(diffopt
, DIFF_FILE_OLD
);
391 const char *color_new
= diff_get_color_opt(diffopt
, DIFF_FILE_NEW
);
392 const char *color_commit
= diff_get_color_opt(diffopt
, DIFF_COMMIT
);
394 int abbrev
= diffopt
->abbrev
;
397 abbrev
= DEFAULT_ABBREV
;
400 strbuf_addchars(dashes
, '-',
401 strlen(repo_find_unique_abbrev(the_repository
, oid
, abbrev
)));
406 } else if (!a_util
) {
409 } else if (strcmp(a_util
->patch
, b_util
->patch
)) {
410 color
= color_commit
;
413 color
= color_commit
;
418 strbuf_addstr(buf
, status
== '!' ? color_old
: color
);
420 strbuf_addf(buf
, "%*s: %s ", patch_no_width
, "-", dashes
->buf
);
422 strbuf_addf(buf
, "%*d: %s ", patch_no_width
, a_util
->i
+ 1,
423 repo_find_unique_abbrev(the_repository
, &a_util
->oid
, abbrev
));
426 strbuf_addf(buf
, "%s%s", color_reset
, color
);
427 strbuf_addch(buf
, status
);
429 strbuf_addf(buf
, "%s%s", color_reset
, color_new
);
432 strbuf_addf(buf
, " %*s: %s", patch_no_width
, "-", dashes
->buf
);
434 strbuf_addf(buf
, " %*d: %s", patch_no_width
, b_util
->i
+ 1,
435 repo_find_unique_abbrev(the_repository
, &b_util
->oid
, abbrev
));
437 commit
= lookup_commit_reference(the_repository
, oid
);
440 strbuf_addf(buf
, "%s%s", color_reset
, color
);
442 strbuf_addch(buf
, ' ');
443 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
445 strbuf_addf(buf
, "%s\n", color_reset
);
447 fwrite(buf
->buf
, buf
->len
, 1, diffopt
->file
);
450 static struct userdiff_driver section_headers
= {
451 .funcname
= { "^ ## (.*) ##$\n"
452 "^.?@@ (.*)$", REG_EXTENDED
}
455 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
457 struct diff_filespec
*spec
= alloc_filespec(name
);
459 fill_filespec(spec
, null_oid(), 0, 0100644);
460 spec
->data
= (char *)p
;
461 spec
->size
= strlen(p
);
462 spec
->should_munmap
= 0;
464 spec
->driver
= §ion_headers
;
469 static void patch_diff(const char *a
, const char *b
,
470 struct diff_options
*diffopt
)
472 diff_queue(&diff_queued_diff
,
473 get_filespec("a", a
), get_filespec("b", b
));
475 diffcore_std(diffopt
);
479 static struct strbuf
*output_prefix_cb(struct diff_options
*opt UNUSED
, void *data
)
484 static void output(struct string_list
*a
, struct string_list
*b
,
485 struct range_diff_options
*range_diff_opts
)
487 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
488 int patch_no_width
= decimal_width(1 + (a
->nr
> b
->nr
? a
->nr
: b
->nr
));
490 struct diff_options opts
;
491 struct strbuf indent
= STRBUF_INIT
;
493 if (range_diff_opts
->diffopt
)
494 memcpy(&opts
, range_diff_opts
->diffopt
, sizeof(opts
));
496 repo_diff_setup(the_repository
, &opts
);
499 if (!opts
.output_format
)
500 opts
.output_format
= DIFF_FORMAT_PATCH
;
501 opts
.flags
.suppress_diff_headers
= 1;
502 opts
.flags
.dual_color_diffed_diffs
=
503 range_diff_opts
->dual_color
;
504 opts
.flags
.suppress_hunk_header_line_count
= 1;
505 opts
.output_prefix
= output_prefix_cb
;
506 strbuf_addstr(&indent
, " ");
507 opts
.output_prefix_data
= &indent
;
508 diff_setup_done(&opts
);
511 * We assume the user is really more interested in the second argument
512 * ("newer" version). To that end, we print the output in the order of
513 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
514 * commits that are no longer in the RHS into a good place, we place
515 * them once we have shown all of their predecessors in the LHS.
518 while (i
< a
->nr
|| j
< b
->nr
) {
519 struct patch_util
*a_util
, *b_util
;
520 a_util
= i
< a
->nr
? a
->items
[i
].util
: NULL
;
521 b_util
= j
< b
->nr
? b
->items
[j
].util
: NULL
;
523 /* Skip all the already-shown commits from the LHS. */
524 while (i
< a
->nr
&& a_util
->shown
)
525 a_util
= ++i
< a
->nr
? a
->items
[i
].util
: NULL
;
527 /* Show unmatched LHS commit whose predecessors were shown. */
528 if (i
< a
->nr
&& a_util
->matching
< 0) {
529 if (!range_diff_opts
->right_only
)
530 output_pair_header(&opts
, patch_no_width
,
531 &buf
, &dashes
, a_util
, NULL
);
536 /* Show unmatched RHS commits. */
537 while (j
< b
->nr
&& b_util
->matching
< 0) {
538 if (!range_diff_opts
->left_only
)
539 output_pair_header(&opts
, patch_no_width
,
540 &buf
, &dashes
, NULL
, b_util
);
541 b_util
= ++j
< b
->nr
? b
->items
[j
].util
: NULL
;
544 /* Show matching LHS/RHS pair. */
546 a_util
= a
->items
[b_util
->matching
].util
;
547 output_pair_header(&opts
, patch_no_width
,
548 &buf
, &dashes
, a_util
, b_util
);
549 if (!(opts
.output_format
& DIFF_FORMAT_NO_OUTPUT
))
550 patch_diff(a
->items
[b_util
->matching
].string
,
551 b
->items
[j
].string
, &opts
);
556 strbuf_release(&buf
);
557 strbuf_release(&dashes
);
558 strbuf_release(&indent
);
563 int show_range_diff(const char *range1
, const char *range2
,
564 struct range_diff_options
*range_diff_opts
)
568 struct string_list branch1
= STRING_LIST_INIT_DUP
;
569 struct string_list branch2
= STRING_LIST_INIT_DUP
;
571 if (range_diff_opts
->left_only
&& range_diff_opts
->right_only
)
572 res
= error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
574 if (!res
&& read_patches(range1
, &branch1
, range_diff_opts
->other_arg
))
575 res
= error(_("could not parse log for '%s'"), range1
);
576 if (!res
&& read_patches(range2
, &branch2
, range_diff_opts
->other_arg
))
577 res
= error(_("could not parse log for '%s'"), range2
);
580 find_exact_matches(&branch1
, &branch2
);
581 get_correspondences(&branch1
, &branch2
,
582 range_diff_opts
->creation_factor
);
583 output(&branch1
, &branch2
, range_diff_opts
);
586 string_list_clear(&branch1
, 1);
587 string_list_clear(&branch2
, 1);
592 int is_range_diff_range(const char *arg
)
594 char *copy
= xstrdup(arg
); /* setup_revisions() modifies it */
595 const char *argv
[] = { "", copy
, "--", NULL
};
596 int i
, positive
= 0, negative
= 0;
597 struct rev_info revs
;
599 repo_init_revisions(the_repository
, &revs
, NULL
);
600 if (setup_revisions(3, argv
, &revs
, NULL
) == 1) {
601 for (i
= 0; i
< revs
.pending
.nr
; i
++)
602 if (revs
.pending
.objects
[i
].item
->flags
& UNINTERESTING
)
606 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
607 struct object
*obj
= revs
.pending
.objects
[i
].item
;
609 if (obj
->type
== OBJ_COMMIT
)
610 clear_commit_marks((struct commit
*)obj
,
616 release_revisions(&revs
);
617 return negative
> 0 && positive
> 0;