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 */
28 static size_t find_end_of_line(char *buffer
, unsigned long size
)
30 char *eol
= memchr(buffer
, '\n', size
);
36 return eol
+ 1 - buffer
;
40 * Reads the patches into a string list, with the `util` field being populated
41 * as struct object_id (will need to be free()d).
43 static int read_patches(const char *range
, struct string_list
*list
,
44 const struct strvec
*other_arg
)
46 struct child_process cp
= CHILD_PROCESS_INIT
;
47 struct strbuf buf
= STRBUF_INIT
, contents
= STRBUF_INIT
;
48 struct patch_util
*util
= NULL
;
50 char *line
, *current_filename
= NULL
;
54 strvec_pushl(&cp
.args
, "log", "--no-color", "-p", "--no-merges",
55 "--reverse", "--date-order", "--decorate=no",
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=#",
70 strvec_pushv(&cp
.args
, other_arg
->argv
);
71 strvec_push(&cp
.args
, range
);
76 if (start_command(&cp
))
77 return error_errno(_("could not start `log`"));
78 if (strbuf_read(&contents
, cp
.out
, 0) < 0) {
79 error_errno(_("could not read `log` output"));
86 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
) {
89 len
= find_end_of_line(line
, size
);
91 if (skip_prefix(line
, "commit ", &p
)) {
93 string_list_append(list
, buf
.buf
)->util
= util
;
96 util
= xcalloc(sizeof(*util
), 1);
97 if (get_oid(p
, &util
->oid
)) {
98 error(_("could not parse commit '%s'"), p
);
100 string_list_clear(list
, 1);
101 strbuf_release(&buf
);
102 strbuf_release(&contents
);
112 error(_("could not parse first line of `log` output: "
113 "did not start with 'commit ': '%s'"),
115 string_list_clear(list
, 1);
116 strbuf_release(&buf
);
117 strbuf_release(&contents
);
122 if (starts_with(line
, "diff --git")) {
123 struct patch patch
= { 0 };
124 struct strbuf root
= STRBUF_INIT
;
129 strbuf_addch(&buf
, '\n');
130 if (!util
->diff_offset
)
131 util
->diff_offset
= buf
.len
;
132 line
[len
- 1] = '\n';
134 len
= parse_git_diff_header(&root
, &linenr
, 0, line
,
137 die(_("could not parse git header '%.*s'"),
139 strbuf_addstr(&buf
, " ## ");
140 if (patch
.is_new
> 0)
141 strbuf_addf(&buf
, "%s (new)", patch
.new_name
);
142 else if (patch
.is_delete
> 0)
143 strbuf_addf(&buf
, "%s (deleted)", patch
.old_name
);
144 else if (patch
.is_rename
)
145 strbuf_addf(&buf
, "%s => %s", patch
.old_name
, patch
.new_name
);
147 strbuf_addstr(&buf
, patch
.new_name
);
149 free(current_filename
);
150 if (patch
.is_delete
> 0)
151 current_filename
= xstrdup(patch
.old_name
);
153 current_filename
= xstrdup(patch
.new_name
);
155 if (patch
.new_mode
&& patch
.old_mode
&&
156 patch
.old_mode
!= patch
.new_mode
)
157 strbuf_addf(&buf
, " (mode change %06o => %06o)",
158 patch
.old_mode
, patch
.new_mode
);
160 strbuf_addstr(&buf
, " ##");
161 } else if (in_header
) {
162 if (starts_with(line
, "Author: ")) {
163 strbuf_addstr(&buf
, " ## Metadata ##\n");
164 strbuf_addstr(&buf
, line
);
165 strbuf_addstr(&buf
, "\n\n");
166 strbuf_addstr(&buf
, " ## Commit message ##\n");
167 } else if (starts_with(line
, "Notes") &&
168 line
[strlen(line
) - 1] == ':') {
169 strbuf_addstr(&buf
, "\n\n");
170 /* strip the trailing colon */
171 strbuf_addf(&buf
, " ## %.*s ##\n",
172 (int)(strlen(line
) - 1), line
);
173 } else if (starts_with(line
, " ")) {
175 while (isspace(*p
) && p
>= line
)
177 strbuf_add(&buf
, line
, p
- line
+ 1);
178 strbuf_addch(&buf
, '\n');
181 } else if (skip_prefix(line
, "@@ ", &p
)) {
183 strbuf_addstr(&buf
, "@@");
184 if (current_filename
&& p
[2])
185 strbuf_addf(&buf
, " %s:", current_filename
);
187 strbuf_addstr(&buf
, p
+ 2);
190 * A completely blank (not ' \n', which is context)
191 * line is not valid in a diff. We skip it
192 * silently, because this neatly handles the blank
193 * separator line between commits in git-log
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);
203 } else if (line
[0] == '#') {
204 strbuf_addch(&buf
, ' ');
205 strbuf_addstr(&buf
, line
+ 1);
207 strbuf_addch(&buf
, ' ');
208 strbuf_addstr(&buf
, line
);
211 strbuf_addch(&buf
, '\n');
214 strbuf_release(&contents
);
217 string_list_append(list
, buf
.buf
)->util
= util
;
218 strbuf_release(&buf
);
219 free(current_filename
);
221 if (finish_command(&cp
))
227 static int patch_util_cmp(const void *dummy
, const struct patch_util
*a
,
228 const struct patch_util
*b
, const char *keydata
)
230 return strcmp(a
->diff
, keydata
? keydata
: b
->diff
);
233 static void find_exact_matches(struct string_list
*a
, struct string_list
*b
)
238 hashmap_init(&map
, (hashmap_cmp_fn
)patch_util_cmp
, NULL
, 0);
240 /* First, add the patches of a to a hash map */
241 for (i
= 0; i
< a
->nr
; i
++) {
242 struct patch_util
*util
= a
->items
[i
].util
;
245 util
->patch
= a
->items
[i
].string
;
246 util
->diff
= util
->patch
+ util
->diff_offset
;
247 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
248 hashmap_add(&map
, &util
->e
);
251 /* Now try to find exact matches in b */
252 for (i
= 0; i
< b
->nr
; i
++) {
253 struct patch_util
*util
= b
->items
[i
].util
, *other
;
256 util
->patch
= b
->items
[i
].string
;
257 util
->diff
= util
->patch
+ util
->diff_offset
;
258 hashmap_entry_init(&util
->e
, strhash(util
->diff
));
259 other
= hashmap_remove_entry(&map
, util
, e
, NULL
);
261 if (other
->matching
>= 0)
262 BUG("already assigned!");
265 util
->matching
= other
->i
;
272 static void diffsize_consume(void *data
, char *line
, unsigned long len
)
277 static void diffsize_hunk(void *data
, long ob
, long on
, long nb
, long nn
,
278 const char *funcline
, long funclen
)
280 diffsize_consume(data
, NULL
, 0);
283 static int diffsize(const char *a
, const char *b
)
285 xpparam_t pp
= { 0 };
286 xdemitconf_t cfg
= { 0 };
291 mf1
.size
= strlen(a
);
293 mf2
.size
= strlen(b
);
296 if (!xdi_diff_outf(&mf1
, &mf2
,
297 diffsize_hunk
, diffsize_consume
, &count
,
301 error(_("failed to generate diff"));
305 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
308 int n
= a
->nr
+ b
->nr
;
309 int *cost
, c
, *a2b
, *b2a
;
312 ALLOC_ARRAY(cost
, st_mult(n
, n
));
316 for (i
= 0; i
< a
->nr
; i
++) {
317 struct patch_util
*a_util
= a
->items
[i
].util
;
319 for (j
= 0; j
< b
->nr
; j
++) {
320 struct patch_util
*b_util
= b
->items
[j
].util
;
322 if (a_util
->matching
== j
)
324 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
325 c
= diffsize(a_util
->diff
, b_util
->diff
);
331 c
= a_util
->matching
< 0 ?
332 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
333 for (j
= b
->nr
; j
< n
; j
++)
337 for (j
= 0; j
< b
->nr
; j
++) {
338 struct patch_util
*util
= b
->items
[j
].util
;
340 c
= util
->matching
< 0 ?
341 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
342 for (i
= a
->nr
; i
< n
; i
++)
346 for (i
= a
->nr
; i
< n
; i
++)
347 for (j
= b
->nr
; j
< n
; j
++)
350 compute_assignment(n
, n
, cost
, a2b
, b2a
);
352 for (i
= 0; i
< a
->nr
; i
++)
353 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
354 struct patch_util
*a_util
= a
->items
[i
].util
;
355 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
357 a_util
->matching
= a2b
[i
];
358 b_util
->matching
= i
;
366 static void output_pair_header(struct diff_options
*diffopt
,
369 struct strbuf
*dashes
,
370 struct patch_util
*a_util
,
371 struct patch_util
*b_util
)
373 struct object_id
*oid
= a_util
? &a_util
->oid
: &b_util
->oid
;
374 struct commit
*commit
;
376 const char *color_reset
= diff_get_color_opt(diffopt
, DIFF_RESET
);
377 const char *color_old
= diff_get_color_opt(diffopt
, DIFF_FILE_OLD
);
378 const char *color_new
= diff_get_color_opt(diffopt
, DIFF_FILE_NEW
);
379 const char *color_commit
= diff_get_color_opt(diffopt
, DIFF_COMMIT
);
383 strbuf_addchars(dashes
, '-',
384 strlen(find_unique_abbrev(oid
,
390 } else if (!a_util
) {
393 } else if (strcmp(a_util
->patch
, b_util
->patch
)) {
394 color
= color_commit
;
397 color
= color_commit
;
402 strbuf_addstr(buf
, status
== '!' ? color_old
: color
);
404 strbuf_addf(buf
, "%*s: %s ", patch_no_width
, "-", dashes
->buf
);
406 strbuf_addf(buf
, "%*d: %s ", patch_no_width
, a_util
->i
+ 1,
407 find_unique_abbrev(&a_util
->oid
, DEFAULT_ABBREV
));
410 strbuf_addf(buf
, "%s%s", color_reset
, color
);
411 strbuf_addch(buf
, status
);
413 strbuf_addf(buf
, "%s%s", color_reset
, color_new
);
416 strbuf_addf(buf
, " %*s: %s", patch_no_width
, "-", dashes
->buf
);
418 strbuf_addf(buf
, " %*d: %s", patch_no_width
, b_util
->i
+ 1,
419 find_unique_abbrev(&b_util
->oid
, DEFAULT_ABBREV
));
421 commit
= lookup_commit_reference(the_repository
, oid
);
424 strbuf_addf(buf
, "%s%s", color_reset
, color
);
426 strbuf_addch(buf
, ' ');
427 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
429 strbuf_addf(buf
, "%s\n", color_reset
);
431 fwrite(buf
->buf
, buf
->len
, 1, diffopt
->file
);
434 static struct userdiff_driver section_headers
= {
435 .funcname
= { "^ ## (.*) ##$\n"
436 "^.?@@ (.*)$", REG_EXTENDED
}
439 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
441 struct diff_filespec
*spec
= alloc_filespec(name
);
443 fill_filespec(spec
, &null_oid
, 0, 0100644);
444 spec
->data
= (char *)p
;
445 spec
->size
= strlen(p
);
446 spec
->should_munmap
= 0;
448 spec
->driver
= §ion_headers
;
453 static void patch_diff(const char *a
, const char *b
,
454 struct diff_options
*diffopt
)
456 diff_queue(&diff_queued_diff
,
457 get_filespec("a", a
), get_filespec("b", b
));
459 diffcore_std(diffopt
);
463 static void output(struct string_list
*a
, struct string_list
*b
,
464 struct diff_options
*diffopt
)
466 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
467 int patch_no_width
= decimal_width(1 + (a
->nr
> b
->nr
? a
->nr
: b
->nr
));
471 * We assume the user is really more interested in the second argument
472 * ("newer" version). To that end, we print the output in the order of
473 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
474 * commits that are no longer in the RHS into a good place, we place
475 * them once we have shown all of their predecessors in the LHS.
478 while (i
< a
->nr
|| j
< b
->nr
) {
479 struct patch_util
*a_util
, *b_util
;
480 a_util
= i
< a
->nr
? a
->items
[i
].util
: NULL
;
481 b_util
= j
< b
->nr
? b
->items
[j
].util
: NULL
;
483 /* Skip all the already-shown commits from the LHS. */
484 while (i
< a
->nr
&& a_util
->shown
)
485 a_util
= ++i
< a
->nr
? a
->items
[i
].util
: NULL
;
487 /* Show unmatched LHS commit whose predecessors were shown. */
488 if (i
< a
->nr
&& a_util
->matching
< 0) {
489 output_pair_header(diffopt
, patch_no_width
,
490 &buf
, &dashes
, a_util
, NULL
);
495 /* Show unmatched RHS commits. */
496 while (j
< b
->nr
&& b_util
->matching
< 0) {
497 output_pair_header(diffopt
, patch_no_width
,
498 &buf
, &dashes
, NULL
, b_util
);
499 b_util
= ++j
< b
->nr
? b
->items
[j
].util
: NULL
;
502 /* Show matching LHS/RHS pair. */
504 a_util
= a
->items
[b_util
->matching
].util
;
505 output_pair_header(diffopt
, patch_no_width
,
506 &buf
, &dashes
, a_util
, b_util
);
507 if (!(diffopt
->output_format
& DIFF_FORMAT_NO_OUTPUT
))
508 patch_diff(a
->items
[b_util
->matching
].string
,
509 b
->items
[j
].string
, diffopt
);
514 strbuf_release(&buf
);
515 strbuf_release(&dashes
);
518 static struct strbuf
*output_prefix_cb(struct diff_options
*opt
, void *data
)
523 int show_range_diff(const char *range1
, const char *range2
,
524 int creation_factor
, int dual_color
,
525 const struct diff_options
*diffopt
,
526 const struct strvec
*other_arg
)
530 struct string_list branch1
= STRING_LIST_INIT_DUP
;
531 struct string_list branch2
= STRING_LIST_INIT_DUP
;
533 if (read_patches(range1
, &branch1
, other_arg
))
534 res
= error(_("could not parse log for '%s'"), range1
);
535 if (!res
&& read_patches(range2
, &branch2
, other_arg
))
536 res
= error(_("could not parse log for '%s'"), range2
);
539 struct diff_options opts
;
540 struct strbuf indent
= STRBUF_INIT
;
543 memcpy(&opts
, diffopt
, sizeof(opts
));
547 if (!opts
.output_format
)
548 opts
.output_format
= DIFF_FORMAT_PATCH
;
549 opts
.flags
.suppress_diff_headers
= 1;
550 opts
.flags
.dual_color_diffed_diffs
= dual_color
;
551 opts
.flags
.suppress_hunk_header_line_count
= 1;
552 opts
.output_prefix
= output_prefix_cb
;
553 strbuf_addstr(&indent
, " ");
554 opts
.output_prefix_data
= &indent
;
555 diff_setup_done(&opts
);
557 find_exact_matches(&branch1
, &branch2
);
558 get_correspondences(&branch1
, &branch2
, creation_factor
);
559 output(&branch1
, &branch2
, &opts
);
561 strbuf_release(&indent
);
564 string_list_clear(&branch1
, 1);
565 string_list_clear(&branch2
, 1);