2 #include "range-diff.h"
3 #include "string-list.h"
4 #include "run-command.h"
7 #include "xdiff-interface.h"
8 #include "linear-assignment.h"
16 /* For the search for an exact match */
17 struct hashmap_entry e
;
18 const char *diff
, *patch
;
23 /* the index of the matching item in the other branch, or -1 */
29 * Reads the patches into a string list, with the `util` field being populated
30 * as struct object_id (will need to be free()d).
32 static int read_patches(const char *range
, struct string_list
*list
,
33 const struct strvec
*other_arg
)
35 struct child_process cp
= CHILD_PROCESS_INIT
;
36 struct strbuf buf
= STRBUF_INIT
, contents
= STRBUF_INIT
;
37 struct patch_util
*util
= NULL
;
39 char *line
, *current_filename
= NULL
;
43 strvec_pushl(&cp
.args
, "log", "--no-color", "-p", "--no-merges",
44 "--reverse", "--date-order", "--decorate=no",
47 * Choose indicators that are not used anywhere
48 * else in diffs, but still look reasonable
49 * (e.g. will not be confusing when debugging)
51 "--output-indicator-new=>",
52 "--output-indicator-old=<",
53 "--output-indicator-context=#",
59 strvec_pushv(&cp
.args
, other_arg
->v
);
60 strvec_push(&cp
.args
, range
);
65 if (start_command(&cp
))
66 return error_errno(_("could not start `log`"));
67 if (strbuf_read(&contents
, cp
.out
, 0) < 0) {
68 error_errno(_("could not read `log` output"));
75 for (; size
> 0; size
-= len
, line
+= len
) {
79 eol
= memchr(line
, '\n', size
);
87 if (skip_prefix(line
, "commit ", &p
)) {
89 string_list_append(list
, buf
.buf
)->util
= util
;
92 util
= xcalloc(sizeof(*util
), 1);
93 if (get_oid(p
, &util
->oid
)) {
94 error(_("could not parse commit '%s'"), p
);
96 string_list_clear(list
, 1);
98 strbuf_release(&contents
);
108 error(_("could not parse first line of `log` output: "
109 "did not start with 'commit ': '%s'"),
111 string_list_clear(list
, 1);
112 strbuf_release(&buf
);
113 strbuf_release(&contents
);
118 if (starts_with(line
, "diff --git")) {
119 struct patch patch
= { 0 };
120 struct strbuf root
= STRBUF_INIT
;
125 strbuf_addch(&buf
, '\n');
126 if (!util
->diff_offset
)
127 util
->diff_offset
= buf
.len
;
131 len
= parse_git_diff_header(&root
, &linenr
, 0, line
,
134 die(_("could not parse git header '%.*s'"),
136 strbuf_addstr(&buf
, " ## ");
137 if (patch
.is_new
> 0)
138 strbuf_addf(&buf
, "%s (new)", patch
.new_name
);
139 else if (patch
.is_delete
> 0)
140 strbuf_addf(&buf
, "%s (deleted)", patch
.old_name
);
141 else if (patch
.is_rename
)
142 strbuf_addf(&buf
, "%s => %s", patch
.old_name
, patch
.new_name
);
144 strbuf_addstr(&buf
, patch
.new_name
);
146 free(current_filename
);
147 if (patch
.is_delete
> 0)
148 current_filename
= xstrdup(patch
.old_name
);
150 current_filename
= xstrdup(patch
.new_name
);
152 if (patch
.new_mode
&& patch
.old_mode
&&
153 patch
.old_mode
!= patch
.new_mode
)
154 strbuf_addf(&buf
, " (mode change %06o => %06o)",
155 patch
.old_mode
, patch
.new_mode
);
157 strbuf_addstr(&buf
, " ##");
158 } else if (in_header
) {
159 if (starts_with(line
, "Author: ")) {
160 strbuf_addstr(&buf
, " ## Metadata ##\n");
161 strbuf_addstr(&buf
, line
);
162 strbuf_addstr(&buf
, "\n\n");
163 strbuf_addstr(&buf
, " ## Commit message ##\n");
164 } else if (starts_with(line
, "Notes") &&
165 line
[strlen(line
) - 1] == ':') {
166 strbuf_addstr(&buf
, "\n\n");
167 /* strip the trailing colon */
168 strbuf_addf(&buf
, " ## %.*s ##\n",
169 (int)(strlen(line
) - 1), line
);
170 } else if (starts_with(line
, " ")) {
172 while (isspace(*p
) && p
>= line
)
174 strbuf_add(&buf
, line
, p
- line
+ 1);
175 strbuf_addch(&buf
, '\n');
178 } else if (skip_prefix(line
, "@@ ", &p
)) {
180 strbuf_addstr(&buf
, "@@");
181 if (current_filename
&& p
[2])
182 strbuf_addf(&buf
, " %s:", current_filename
);
184 strbuf_addstr(&buf
, p
+ 2);
187 * A completely blank (not ' \n', which is context)
188 * line is not valid in a diff. We skip it
189 * silently, because this neatly handles the blank
190 * separator line between commits in git-log
194 else if (line
[0] == '>') {
195 strbuf_addch(&buf
, '+');
196 strbuf_addstr(&buf
, line
+ 1);
197 } else if (line
[0] == '<') {
198 strbuf_addch(&buf
, '-');
199 strbuf_addstr(&buf
, line
+ 1);
200 } else if (line
[0] == '#') {
201 strbuf_addch(&buf
, ' ');
202 strbuf_addstr(&buf
, line
+ 1);
204 strbuf_addch(&buf
, ' ');
205 strbuf_addstr(&buf
, line
);
208 strbuf_addch(&buf
, '\n');
211 strbuf_release(&contents
);
214 string_list_append(list
, buf
.buf
)->util
= util
;
215 strbuf_release(&buf
);
216 free(current_filename
);
218 if (finish_command(&cp
))
224 static int patch_util_cmp(const void *dummy
, const struct patch_util
*a
,
225 const struct patch_util
*b
, const char *keydata
)
227 return strcmp(a
->diff
, keydata
? keydata
: b
->diff
);
230 static void find_exact_matches(struct string_list
*a
, struct string_list
*b
)
232 struct hashmap map
= HASHMAP_INIT((hashmap_cmp_fn
)patch_util_cmp
, NULL
);
235 /* First, add the patches of a to a hash map */
236 for (i
= 0; i
< a
->nr
; i
++) {
237 struct patch_util
*util
= a
->items
[i
].util
;
240 util
->patch
= a
->items
[i
].string
;
241 util
->diff
= util
->patch
+ util
->diff_offset
;
242 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
243 hashmap_add(&map
, &util
->e
);
246 /* Now try to find exact matches in b */
247 for (i
= 0; i
< b
->nr
; i
++) {
248 struct patch_util
*util
= b
->items
[i
].util
, *other
;
251 util
->patch
= b
->items
[i
].string
;
252 util
->diff
= util
->patch
+ util
->diff_offset
;
253 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
254 other
= hashmap_remove_entry(&map
, util
, e
, NULL
);
256 if (other
->matching
>= 0)
257 BUG("already assigned!");
260 util
->matching
= other
->i
;
267 static void diffsize_consume(void *data
, char *line
, unsigned long len
)
272 static void diffsize_hunk(void *data
, long ob
, long on
, long nb
, long nn
,
273 const char *funcline
, long funclen
)
275 diffsize_consume(data
, NULL
, 0);
278 static int diffsize(const char *a
, const char *b
)
280 xpparam_t pp
= { 0 };
281 xdemitconf_t cfg
= { 0 };
286 mf1
.size
= strlen(a
);
288 mf2
.size
= strlen(b
);
291 if (!xdi_diff_outf(&mf1
, &mf2
,
292 diffsize_hunk
, diffsize_consume
, &count
,
296 error(_("failed to generate diff"));
300 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
303 int n
= a
->nr
+ b
->nr
;
304 int *cost
, c
, *a2b
, *b2a
;
307 ALLOC_ARRAY(cost
, st_mult(n
, n
));
311 for (i
= 0; i
< a
->nr
; i
++) {
312 struct patch_util
*a_util
= a
->items
[i
].util
;
314 for (j
= 0; j
< b
->nr
; j
++) {
315 struct patch_util
*b_util
= b
->items
[j
].util
;
317 if (a_util
->matching
== j
)
319 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
320 c
= diffsize(a_util
->diff
, b_util
->diff
);
326 c
= a_util
->matching
< 0 ?
327 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
328 for (j
= b
->nr
; j
< n
; j
++)
332 for (j
= 0; j
< b
->nr
; j
++) {
333 struct patch_util
*util
= b
->items
[j
].util
;
335 c
= util
->matching
< 0 ?
336 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
337 for (i
= a
->nr
; i
< n
; i
++)
341 for (i
= a
->nr
; i
< n
; i
++)
342 for (j
= b
->nr
; j
< n
; j
++)
345 compute_assignment(n
, n
, cost
, a2b
, b2a
);
347 for (i
= 0; i
< a
->nr
; i
++)
348 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
349 struct patch_util
*a_util
= a
->items
[i
].util
;
350 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
352 a_util
->matching
= a2b
[i
];
353 b_util
->matching
= i
;
361 static void output_pair_header(struct diff_options
*diffopt
,
364 struct strbuf
*dashes
,
365 struct patch_util
*a_util
,
366 struct patch_util
*b_util
)
368 struct object_id
*oid
= a_util
? &a_util
->oid
: &b_util
->oid
;
369 struct commit
*commit
;
371 const char *color_reset
= diff_get_color_opt(diffopt
, DIFF_RESET
);
372 const char *color_old
= diff_get_color_opt(diffopt
, DIFF_FILE_OLD
);
373 const char *color_new
= diff_get_color_opt(diffopt
, DIFF_FILE_NEW
);
374 const char *color_commit
= diff_get_color_opt(diffopt
, DIFF_COMMIT
);
378 strbuf_addchars(dashes
, '-',
379 strlen(find_unique_abbrev(oid
,
385 } else if (!a_util
) {
388 } else if (strcmp(a_util
->patch
, b_util
->patch
)) {
389 color
= color_commit
;
392 color
= color_commit
;
397 strbuf_addstr(buf
, status
== '!' ? color_old
: color
);
399 strbuf_addf(buf
, "%*s: %s ", patch_no_width
, "-", dashes
->buf
);
401 strbuf_addf(buf
, "%*d: %s ", patch_no_width
, a_util
->i
+ 1,
402 find_unique_abbrev(&a_util
->oid
, DEFAULT_ABBREV
));
405 strbuf_addf(buf
, "%s%s", color_reset
, color
);
406 strbuf_addch(buf
, status
);
408 strbuf_addf(buf
, "%s%s", color_reset
, color_new
);
411 strbuf_addf(buf
, " %*s: %s", patch_no_width
, "-", dashes
->buf
);
413 strbuf_addf(buf
, " %*d: %s", patch_no_width
, b_util
->i
+ 1,
414 find_unique_abbrev(&b_util
->oid
, DEFAULT_ABBREV
));
416 commit
= lookup_commit_reference(the_repository
, oid
);
419 strbuf_addf(buf
, "%s%s", color_reset
, color
);
421 strbuf_addch(buf
, ' ');
422 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
424 strbuf_addf(buf
, "%s\n", color_reset
);
426 fwrite(buf
->buf
, buf
->len
, 1, diffopt
->file
);
429 static struct userdiff_driver section_headers
= {
430 .funcname
= { "^ ## (.*) ##$\n"
431 "^.?@@ (.*)$", REG_EXTENDED
}
434 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
436 struct diff_filespec
*spec
= alloc_filespec(name
);
438 fill_filespec(spec
, &null_oid
, 0, 0100644);
439 spec
->data
= (char *)p
;
440 spec
->size
= strlen(p
);
441 spec
->should_munmap
= 0;
443 spec
->driver
= §ion_headers
;
448 static void patch_diff(const char *a
, const char *b
,
449 struct diff_options
*diffopt
)
451 diff_queue(&diff_queued_diff
,
452 get_filespec("a", a
), get_filespec("b", b
));
454 diffcore_std(diffopt
);
458 static void output(struct string_list
*a
, struct string_list
*b
,
459 struct diff_options
*diffopt
)
461 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
462 int patch_no_width
= decimal_width(1 + (a
->nr
> b
->nr
? a
->nr
: b
->nr
));
466 * We assume the user is really more interested in the second argument
467 * ("newer" version). To that end, we print the output in the order of
468 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
469 * commits that are no longer in the RHS into a good place, we place
470 * them once we have shown all of their predecessors in the LHS.
473 while (i
< a
->nr
|| j
< b
->nr
) {
474 struct patch_util
*a_util
, *b_util
;
475 a_util
= i
< a
->nr
? a
->items
[i
].util
: NULL
;
476 b_util
= j
< b
->nr
? b
->items
[j
].util
: NULL
;
478 /* Skip all the already-shown commits from the LHS. */
479 while (i
< a
->nr
&& a_util
->shown
)
480 a_util
= ++i
< a
->nr
? a
->items
[i
].util
: NULL
;
482 /* Show unmatched LHS commit whose predecessors were shown. */
483 if (i
< a
->nr
&& a_util
->matching
< 0) {
484 output_pair_header(diffopt
, patch_no_width
,
485 &buf
, &dashes
, a_util
, NULL
);
490 /* Show unmatched RHS commits. */
491 while (j
< b
->nr
&& b_util
->matching
< 0) {
492 output_pair_header(diffopt
, patch_no_width
,
493 &buf
, &dashes
, NULL
, b_util
);
494 b_util
= ++j
< b
->nr
? b
->items
[j
].util
: NULL
;
497 /* Show matching LHS/RHS pair. */
499 a_util
= a
->items
[b_util
->matching
].util
;
500 output_pair_header(diffopt
, patch_no_width
,
501 &buf
, &dashes
, a_util
, b_util
);
502 if (!(diffopt
->output_format
& DIFF_FORMAT_NO_OUTPUT
))
503 patch_diff(a
->items
[b_util
->matching
].string
,
504 b
->items
[j
].string
, diffopt
);
509 strbuf_release(&buf
);
510 strbuf_release(&dashes
);
513 static struct strbuf
*output_prefix_cb(struct diff_options
*opt
, void *data
)
518 int show_range_diff(const char *range1
, const char *range2
,
519 int creation_factor
, int dual_color
,
520 const struct diff_options
*diffopt
,
521 const struct strvec
*other_arg
)
525 struct string_list branch1
= STRING_LIST_INIT_DUP
;
526 struct string_list branch2
= STRING_LIST_INIT_DUP
;
528 if (read_patches(range1
, &branch1
, other_arg
))
529 res
= error(_("could not parse log for '%s'"), range1
);
530 if (!res
&& read_patches(range2
, &branch2
, other_arg
))
531 res
= error(_("could not parse log for '%s'"), range2
);
534 struct diff_options opts
;
535 struct strbuf indent
= STRBUF_INIT
;
538 memcpy(&opts
, diffopt
, sizeof(opts
));
542 if (!opts
.output_format
)
543 opts
.output_format
= DIFF_FORMAT_PATCH
;
544 opts
.flags
.suppress_diff_headers
= 1;
545 opts
.flags
.dual_color_diffed_diffs
= dual_color
;
546 opts
.flags
.suppress_hunk_header_line_count
= 1;
547 opts
.output_prefix
= output_prefix_cb
;
548 strbuf_addstr(&indent
, " ");
549 opts
.output_prefix_data
= &indent
;
550 diff_setup_done(&opts
);
552 find_exact_matches(&branch1
, &branch2
);
553 get_correspondences(&branch1
, &branch2
, creation_factor
);
554 output(&branch1
, &branch2
, &opts
);
556 strbuf_release(&indent
);
559 string_list_clear(&branch1
, 1);
560 string_list_clear(&branch2
, 1);