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"
21 /* For the search for an exact match */
22 struct hashmap_entry e
;
23 const char *diff
, *patch
;
28 /* the index of the matching item in the other branch, or -1 */
34 * Reads the patches into a string list, with the `util` field being populated
35 * as struct object_id (will need to be free()d).
37 static int read_patches(const char *range
, struct string_list
*list
,
38 const struct strvec
*other_arg
)
40 struct child_process cp
= CHILD_PROCESS_INIT
;
41 struct strbuf buf
= STRBUF_INIT
, contents
= STRBUF_INIT
;
42 struct patch_util
*util
= NULL
;
44 char *line
, *current_filename
= NULL
;
49 strvec_pushl(&cp
.args
, "log", "--no-color", "-p", "--no-merges",
50 "--reverse", "--date-order", "--decorate=no",
51 "--no-prefix", "--submodule=short",
53 * Choose indicators that are not used anywhere
54 * else in diffs, but still look reasonable
55 * (e.g. will not be confusing when debugging)
57 "--output-indicator-new=>",
58 "--output-indicator-old=<",
59 "--output-indicator-context=#",
64 strvec_push(&cp
.args
, range
);
66 strvec_pushv(&cp
.args
, other_arg
->v
);
71 if (start_command(&cp
))
72 return error_errno(_("could not start `log`"));
73 if (strbuf_read(&contents
, cp
.out
, 0) < 0) {
74 error_errno(_("could not read `log` output"));
78 if (finish_command(&cp
))
83 for (; size
> 0; size
-= len
, line
+= len
) {
87 eol
= memchr(line
, '\n', size
);
95 if (skip_prefix(line
, "commit ", &p
)) {
97 string_list_append(list
, buf
.buf
)->util
= util
;
100 CALLOC_ARRAY(util
, 1);
101 if (repo_get_oid(the_repository
, p
, &util
->oid
)) {
102 error(_("could not parse commit '%s'"), p
);
104 string_list_clear(list
, 1);
113 error(_("could not parse first line of `log` output: "
114 "did not start with 'commit ': '%s'"),
116 string_list_clear(list
, 1);
120 if (starts_with(line
, "diff --git")) {
121 struct patch patch
= { 0 };
122 struct strbuf root
= STRBUF_INIT
;
127 strbuf_addch(&buf
, '\n');
128 if (!util
->diff_offset
)
129 util
->diff_offset
= buf
.len
;
133 len
= parse_git_diff_header(&root
, &linenr
, 0, line
,
136 error(_("could not parse git header '%.*s'"),
139 string_list_clear(list
, 1);
142 strbuf_addstr(&buf
, " ## ");
143 if (patch
.is_new
> 0)
144 strbuf_addf(&buf
, "%s (new)", patch
.new_name
);
145 else if (patch
.is_delete
> 0)
146 strbuf_addf(&buf
, "%s (deleted)", patch
.old_name
);
147 else if (patch
.is_rename
)
148 strbuf_addf(&buf
, "%s => %s", patch
.old_name
, patch
.new_name
);
150 strbuf_addstr(&buf
, patch
.new_name
);
152 free(current_filename
);
153 if (patch
.is_delete
> 0)
154 current_filename
= xstrdup(patch
.old_name
);
156 current_filename
= xstrdup(patch
.new_name
);
158 if (patch
.new_mode
&& patch
.old_mode
&&
159 patch
.old_mode
!= patch
.new_mode
)
160 strbuf_addf(&buf
, " (mode change %06o => %06o)",
161 patch
.old_mode
, patch
.new_mode
);
163 strbuf_addstr(&buf
, " ##");
164 release_patch(&patch
);
165 } else if (in_header
) {
166 if (starts_with(line
, "Author: ")) {
167 strbuf_addstr(&buf
, " ## Metadata ##\n");
168 strbuf_addstr(&buf
, line
);
169 strbuf_addstr(&buf
, "\n\n");
170 strbuf_addstr(&buf
, " ## Commit message ##\n");
171 } else if (starts_with(line
, "Notes") &&
172 line
[strlen(line
) - 1] == ':') {
173 strbuf_addstr(&buf
, "\n\n");
174 /* strip the trailing colon */
175 strbuf_addf(&buf
, " ## %.*s ##\n",
176 (int)(strlen(line
) - 1), line
);
177 } else if (starts_with(line
, " ")) {
179 while (isspace(*p
) && p
>= line
)
181 strbuf_add(&buf
, line
, p
- line
+ 1);
182 strbuf_addch(&buf
, '\n');
185 } else if (skip_prefix(line
, "@@ ", &p
)) {
187 strbuf_addstr(&buf
, "@@");
188 if (current_filename
&& p
[2])
189 strbuf_addf(&buf
, " %s:", current_filename
);
191 strbuf_addstr(&buf
, p
+ 2);
194 * A completely blank (not ' \n', which is context)
195 * line is not valid in a diff. We skip it
196 * silently, because this neatly handles the blank
197 * separator line between commits in git-log
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);
207 } else if (line
[0] == '#') {
208 strbuf_addch(&buf
, ' ');
209 strbuf_addstr(&buf
, line
+ 1);
211 strbuf_addch(&buf
, ' ');
212 strbuf_addstr(&buf
, line
);
215 strbuf_addch(&buf
, '\n');
221 strbuf_release(&contents
);
224 string_list_append(list
, buf
.buf
)->util
= util
;
225 strbuf_release(&buf
);
226 free(current_filename
);
231 static int patch_util_cmp(const void *cmp_data UNUSED
,
232 const struct patch_util
*a
,
233 const struct patch_util
*b
,
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 int diffsize_consume(void *data
,
278 unsigned long len UNUSED
)
284 static void diffsize_hunk(void *data
,
285 long ob UNUSED
, long on UNUSED
,
286 long nb UNUSED
, long nn UNUSED
,
287 const char *func UNUSED
, long funclen UNUSED
)
289 diffsize_consume(data
, NULL
, 0);
292 static int diffsize(const char *a
, const char *b
)
294 xpparam_t pp
= { 0 };
295 xdemitconf_t cfg
= { 0 };
300 mf1
.size
= strlen(a
);
302 mf2
.size
= strlen(b
);
305 if (!xdi_diff_outf(&mf1
, &mf2
,
306 diffsize_hunk
, diffsize_consume
, &count
,
310 error(_("failed to generate diff"));
314 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
317 int n
= a
->nr
+ b
->nr
;
318 int *cost
, c
, *a2b
, *b2a
;
321 ALLOC_ARRAY(cost
, st_mult(n
, n
));
325 for (i
= 0; i
< a
->nr
; i
++) {
326 struct patch_util
*a_util
= a
->items
[i
].util
;
328 for (j
= 0; j
< b
->nr
; j
++) {
329 struct patch_util
*b_util
= b
->items
[j
].util
;
331 if (a_util
->matching
== j
)
333 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
334 c
= diffsize(a_util
->diff
, b_util
->diff
);
340 c
= a_util
->matching
< 0 ?
341 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
342 for (j
= b
->nr
; j
< n
; j
++)
346 for (j
= 0; j
< b
->nr
; j
++) {
347 struct patch_util
*util
= b
->items
[j
].util
;
349 c
= util
->matching
< 0 ?
350 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
351 for (i
= a
->nr
; i
< n
; i
++)
355 for (i
= a
->nr
; i
< n
; i
++)
356 for (j
= b
->nr
; j
< n
; j
++)
359 compute_assignment(n
, n
, cost
, a2b
, b2a
);
361 for (i
= 0; i
< a
->nr
; i
++)
362 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
363 struct patch_util
*a_util
= a
->items
[i
].util
;
364 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
366 a_util
->matching
= a2b
[i
];
367 b_util
->matching
= i
;
375 static void output_pair_header(struct diff_options
*diffopt
,
378 struct strbuf
*dashes
,
379 struct patch_util
*a_util
,
380 struct patch_util
*b_util
)
382 struct object_id
*oid
= a_util
? &a_util
->oid
: &b_util
->oid
;
383 struct commit
*commit
;
385 const char *color_reset
= diff_get_color_opt(diffopt
, DIFF_RESET
);
386 const char *color_old
= diff_get_color_opt(diffopt
, DIFF_FILE_OLD
);
387 const char *color_new
= diff_get_color_opt(diffopt
, DIFF_FILE_NEW
);
388 const char *color_commit
= diff_get_color_opt(diffopt
, DIFF_COMMIT
);
390 int abbrev
= diffopt
->abbrev
;
393 abbrev
= DEFAULT_ABBREV
;
396 strbuf_addchars(dashes
, '-',
397 strlen(repo_find_unique_abbrev(the_repository
, oid
, abbrev
)));
402 } else if (!a_util
) {
405 } else if (strcmp(a_util
->patch
, b_util
->patch
)) {
406 color
= color_commit
;
409 color
= color_commit
;
414 strbuf_addstr(buf
, status
== '!' ? color_old
: color
);
416 strbuf_addf(buf
, "%*s: %s ", patch_no_width
, "-", dashes
->buf
);
418 strbuf_addf(buf
, "%*d: %s ", patch_no_width
, a_util
->i
+ 1,
419 repo_find_unique_abbrev(the_repository
, &a_util
->oid
, abbrev
));
422 strbuf_addf(buf
, "%s%s", color_reset
, color
);
423 strbuf_addch(buf
, status
);
425 strbuf_addf(buf
, "%s%s", color_reset
, color_new
);
428 strbuf_addf(buf
, " %*s: %s", patch_no_width
, "-", dashes
->buf
);
430 strbuf_addf(buf
, " %*d: %s", patch_no_width
, b_util
->i
+ 1,
431 repo_find_unique_abbrev(the_repository
, &b_util
->oid
, abbrev
));
433 commit
= lookup_commit_reference(the_repository
, oid
);
436 strbuf_addf(buf
, "%s%s", color_reset
, color
);
438 strbuf_addch(buf
, ' ');
439 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
441 strbuf_addf(buf
, "%s\n", color_reset
);
443 fwrite(buf
->buf
, buf
->len
, 1, diffopt
->file
);
446 static struct userdiff_driver section_headers
= {
447 .funcname
= { "^ ## (.*) ##$\n"
448 "^.?@@ (.*)$", REG_EXTENDED
}
451 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
453 struct diff_filespec
*spec
= alloc_filespec(name
);
455 fill_filespec(spec
, null_oid(), 0, 0100644);
456 spec
->data
= (char *)p
;
457 spec
->size
= strlen(p
);
458 spec
->should_munmap
= 0;
460 spec
->driver
= §ion_headers
;
465 static void patch_diff(const char *a
, const char *b
,
466 struct diff_options
*diffopt
)
468 diff_queue(&diff_queued_diff
,
469 get_filespec("a", a
), get_filespec("b", b
));
471 diffcore_std(diffopt
);
475 static struct strbuf
*output_prefix_cb(struct diff_options
*opt UNUSED
, void *data
)
480 static void output(struct string_list
*a
, struct string_list
*b
,
481 struct range_diff_options
*range_diff_opts
)
483 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
484 int patch_no_width
= decimal_width(1 + (a
->nr
> b
->nr
? a
->nr
: b
->nr
));
486 struct diff_options opts
;
487 struct strbuf indent
= STRBUF_INIT
;
489 if (range_diff_opts
->diffopt
)
490 memcpy(&opts
, range_diff_opts
->diffopt
, sizeof(opts
));
492 repo_diff_setup(the_repository
, &opts
);
495 if (!opts
.output_format
)
496 opts
.output_format
= DIFF_FORMAT_PATCH
;
497 opts
.flags
.suppress_diff_headers
= 1;
498 opts
.flags
.dual_color_diffed_diffs
=
499 range_diff_opts
->dual_color
;
500 opts
.flags
.suppress_hunk_header_line_count
= 1;
501 opts
.output_prefix
= output_prefix_cb
;
502 strbuf_addstr(&indent
, " ");
503 opts
.output_prefix_data
= &indent
;
504 diff_setup_done(&opts
);
507 * We assume the user is really more interested in the second argument
508 * ("newer" version). To that end, we print the output in the order of
509 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
510 * commits that are no longer in the RHS into a good place, we place
511 * them once we have shown all of their predecessors in the LHS.
514 while (i
< a
->nr
|| j
< b
->nr
) {
515 struct patch_util
*a_util
, *b_util
;
516 a_util
= i
< a
->nr
? a
->items
[i
].util
: NULL
;
517 b_util
= j
< b
->nr
? b
->items
[j
].util
: NULL
;
519 /* Skip all the already-shown commits from the LHS. */
520 while (i
< a
->nr
&& a_util
->shown
)
521 a_util
= ++i
< a
->nr
? a
->items
[i
].util
: NULL
;
523 /* Show unmatched LHS commit whose predecessors were shown. */
524 if (i
< a
->nr
&& a_util
->matching
< 0) {
525 if (!range_diff_opts
->right_only
)
526 output_pair_header(&opts
, patch_no_width
,
527 &buf
, &dashes
, a_util
, NULL
);
532 /* Show unmatched RHS commits. */
533 while (j
< b
->nr
&& b_util
->matching
< 0) {
534 if (!range_diff_opts
->left_only
)
535 output_pair_header(&opts
, patch_no_width
,
536 &buf
, &dashes
, NULL
, b_util
);
537 b_util
= ++j
< b
->nr
? b
->items
[j
].util
: NULL
;
540 /* Show matching LHS/RHS pair. */
542 a_util
= a
->items
[b_util
->matching
].util
;
543 output_pair_header(&opts
, patch_no_width
,
544 &buf
, &dashes
, a_util
, b_util
);
545 if (!(opts
.output_format
& DIFF_FORMAT_NO_OUTPUT
))
546 patch_diff(a
->items
[b_util
->matching
].string
,
547 b
->items
[j
].string
, &opts
);
552 strbuf_release(&buf
);
553 strbuf_release(&dashes
);
554 strbuf_release(&indent
);
559 int show_range_diff(const char *range1
, const char *range2
,
560 struct range_diff_options
*range_diff_opts
)
564 struct string_list branch1
= STRING_LIST_INIT_DUP
;
565 struct string_list branch2
= STRING_LIST_INIT_DUP
;
567 if (range_diff_opts
->left_only
&& range_diff_opts
->right_only
)
568 res
= error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
570 if (!res
&& read_patches(range1
, &branch1
, range_diff_opts
->other_arg
))
571 res
= error(_("could not parse log for '%s'"), range1
);
572 if (!res
&& read_patches(range2
, &branch2
, range_diff_opts
->other_arg
))
573 res
= error(_("could not parse log for '%s'"), range2
);
576 find_exact_matches(&branch1
, &branch2
);
577 get_correspondences(&branch1
, &branch2
,
578 range_diff_opts
->creation_factor
);
579 output(&branch1
, &branch2
, range_diff_opts
);
582 string_list_clear(&branch1
, 1);
583 string_list_clear(&branch2
, 1);
588 int is_range_diff_range(const char *arg
)
590 char *copy
= xstrdup(arg
); /* setup_revisions() modifies it */
591 const char *argv
[] = { "", copy
, "--", NULL
};
592 int i
, positive
= 0, negative
= 0;
593 struct rev_info revs
;
595 repo_init_revisions(the_repository
, &revs
, NULL
);
596 if (setup_revisions(3, argv
, &revs
, NULL
) == 1) {
597 for (i
= 0; i
< revs
.pending
.nr
; i
++)
598 if (revs
.pending
.objects
[i
].item
->flags
& UNINTERESTING
)
602 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
603 struct object
*obj
= revs
.pending
.objects
[i
].item
;
605 if (obj
->type
== OBJ_COMMIT
)
606 clear_commit_marks((struct commit
*)obj
,
612 release_revisions(&revs
);
613 return negative
> 0 && positive
> 0;