2 #include "range-diff.h"
3 #include "string-list.h"
4 #include "run-command.h"
5 #include "argv-array.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
)
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
;
53 argv_array_pushl(&cp
.args
, "log", "--no-color", "-p", "--no-merges",
54 "--reverse", "--date-order", "--decorate=no",
56 * Choose indicators that are not used anywhere
57 * else in diffs, but still look reasonable
58 * (e.g. will not be confusing when debugging)
60 "--output-indicator-new=>",
61 "--output-indicator-old=<",
62 "--output-indicator-context=#",
63 "--no-abbrev-commit", range
,
69 if (start_command(&cp
))
70 return error_errno(_("could not start `log`"));
71 if (strbuf_read(&contents
, cp
.out
, 0) < 0) {
72 error_errno(_("could not read `log` output"));
79 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
) {
82 len
= find_end_of_line(line
, size
);
84 if (skip_prefix(line
, "commit ", &p
)) {
86 string_list_append(list
, buf
.buf
)->util
= util
;
89 util
= xcalloc(sizeof(*util
), 1);
90 if (get_oid(p
, &util
->oid
)) {
91 error(_("could not parse commit '%s'"), p
);
93 string_list_clear(list
, 1);
95 strbuf_release(&contents
);
104 if (starts_with(line
, "diff --git")) {
105 struct patch patch
= { 0 };
106 struct strbuf root
= STRBUF_INIT
;
110 strbuf_addch(&buf
, '\n');
111 if (!util
->diff_offset
)
112 util
->diff_offset
= buf
.len
;
113 line
[len
- 1] = '\n';
114 len
= parse_git_diff_header(&root
, &linenr
, 1, line
,
117 die(_("could not parse git header '%.*s'"), (int)len
, line
);
118 strbuf_addstr(&buf
, " ## ");
119 if (patch
.is_new
> 0)
120 strbuf_addf(&buf
, "%s (new)", patch
.new_name
);
121 else if (patch
.is_delete
> 0)
122 strbuf_addf(&buf
, "%s (deleted)", patch
.old_name
);
123 else if (patch
.is_rename
)
124 strbuf_addf(&buf
, "%s => %s", patch
.old_name
, patch
.new_name
);
126 strbuf_addstr(&buf
, patch
.new_name
);
128 free(current_filename
);
129 if (patch
.is_delete
> 0)
130 current_filename
= xstrdup(patch
.old_name
);
132 current_filename
= xstrdup(patch
.new_name
);
134 if (patch
.new_mode
&& patch
.old_mode
&&
135 patch
.old_mode
!= patch
.new_mode
)
136 strbuf_addf(&buf
, " (mode change %06o => %06o)",
137 patch
.old_mode
, patch
.new_mode
);
139 strbuf_addstr(&buf
, " ##");
140 } else if (in_header
) {
141 if (starts_with(line
, "Author: ")) {
142 strbuf_addstr(&buf
, line
);
143 strbuf_addstr(&buf
, "\n\n");
144 } else if (starts_with(line
, " ")) {
146 while (isspace(*p
) && p
>= line
)
148 strbuf_add(&buf
, line
, p
- line
+ 1);
149 strbuf_addch(&buf
, '\n');
152 } else if (skip_prefix(line
, "@@ ", &p
)) {
154 strbuf_addstr(&buf
, "@@");
155 if (current_filename
&& p
[2])
156 strbuf_addf(&buf
, " %s:", current_filename
);
158 strbuf_addstr(&buf
, p
+ 2);
161 * A completely blank (not ' \n', which is context)
162 * line is not valid in a diff. We skip it
163 * silently, because this neatly handles the blank
164 * separator line between commits in git-log
168 else if (line
[0] == '>') {
169 strbuf_addch(&buf
, '+');
170 strbuf_addstr(&buf
, line
+ 1);
171 } else if (line
[0] == '<') {
172 strbuf_addch(&buf
, '-');
173 strbuf_addstr(&buf
, line
+ 1);
174 } else if (line
[0] == '#') {
175 strbuf_addch(&buf
, ' ');
176 strbuf_addstr(&buf
, line
+ 1);
178 strbuf_addch(&buf
, ' ');
179 strbuf_addstr(&buf
, line
);
182 strbuf_addch(&buf
, '\n');
185 strbuf_release(&contents
);
188 string_list_append(list
, buf
.buf
)->util
= util
;
189 strbuf_release(&buf
);
190 free(current_filename
);
192 if (finish_command(&cp
))
198 static int patch_util_cmp(const void *dummy
, const struct patch_util
*a
,
199 const struct patch_util
*b
, const char *keydata
)
201 return strcmp(a
->diff
, keydata ? keydata
: b
->diff
);
204 static void find_exact_matches(struct string_list
*a
, struct string_list
*b
)
209 hashmap_init(&map
, (hashmap_cmp_fn
)patch_util_cmp
, NULL
, 0);
211 /* First, add the patches of a to a hash map */
212 for (i
= 0; i
< a
->nr
; i
++) {
213 struct patch_util
*util
= a
->items
[i
].util
;
216 util
->patch
= a
->items
[i
].string
;
217 util
->diff
= util
->patch
+ util
->diff_offset
;
218 hashmap_entry_init(util
, strhash(util
->diff
));
219 hashmap_add(&map
, util
);
222 /* Now try to find exact matches in b */
223 for (i
= 0; i
< b
->nr
; i
++) {
224 struct patch_util
*util
= b
->items
[i
].util
, *other
;
227 util
->patch
= b
->items
[i
].string
;
228 util
->diff
= util
->patch
+ util
->diff_offset
;
229 hashmap_entry_init(util
, strhash(util
->diff
));
230 other
= hashmap_remove(&map
, util
, NULL
);
232 if (other
->matching
>= 0)
233 BUG("already assigned!");
236 util
->matching
= other
->i
;
240 hashmap_free(&map
, 0);
243 static void diffsize_consume(void *data
, char *line
, unsigned long len
)
248 static void diffsize_hunk(void *data
, long ob
, long on
, long nb
, long nn
,
249 const char *funcline
, long funclen
)
251 diffsize_consume(data
, NULL
, 0);
254 static int diffsize(const char *a
, const char *b
)
256 xpparam_t pp
= { 0 };
257 xdemitconf_t cfg
= { 0 };
262 mf1
.size
= strlen(a
);
264 mf2
.size
= strlen(b
);
267 if (!xdi_diff_outf(&mf1
, &mf2
,
268 diffsize_hunk
, diffsize_consume
, &count
,
272 error(_("failed to generate diff"));
276 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
279 int n
= a
->nr
+ b
->nr
;
280 int *cost
, c
, *a2b
, *b2a
;
283 ALLOC_ARRAY(cost
, st_mult(n
, n
));
287 for (i
= 0; i
< a
->nr
; i
++) {
288 struct patch_util
*a_util
= a
->items
[i
].util
;
290 for (j
= 0; j
< b
->nr
; j
++) {
291 struct patch_util
*b_util
= b
->items
[j
].util
;
293 if (a_util
->matching
== j
)
295 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
296 c
= diffsize(a_util
->diff
, b_util
->diff
);
302 c
= a_util
->matching
< 0 ?
303 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
304 for (j
= b
->nr
; j
< n
; j
++)
308 for (j
= 0; j
< b
->nr
; j
++) {
309 struct patch_util
*util
= b
->items
[j
].util
;
311 c
= util
->matching
< 0 ?
312 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
313 for (i
= a
->nr
; i
< n
; i
++)
317 for (i
= a
->nr
; i
< n
; i
++)
318 for (j
= b
->nr
; j
< n
; j
++)
321 compute_assignment(n
, n
, cost
, a2b
, b2a
);
323 for (i
= 0; i
< a
->nr
; i
++)
324 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
325 struct patch_util
*a_util
= a
->items
[i
].util
;
326 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
328 a_util
->matching
= a2b
[i
];
329 b_util
->matching
= i
;
337 static void output_pair_header(struct diff_options
*diffopt
,
340 struct strbuf
*dashes
,
341 struct patch_util
*a_util
,
342 struct patch_util
*b_util
)
344 struct object_id
*oid
= a_util ?
&a_util
->oid
: &b_util
->oid
;
345 struct commit
*commit
;
347 const char *color_reset
= diff_get_color_opt(diffopt
, DIFF_RESET
);
348 const char *color_old
= diff_get_color_opt(diffopt
, DIFF_FILE_OLD
);
349 const char *color_new
= diff_get_color_opt(diffopt
, DIFF_FILE_NEW
);
350 const char *color_commit
= diff_get_color_opt(diffopt
, DIFF_COMMIT
);
354 strbuf_addchars(dashes
, '-',
355 strlen(find_unique_abbrev(oid
,
361 } else if (!a_util
) {
364 } else if (strcmp(a_util
->patch
, b_util
->patch
)) {
365 color
= color_commit
;
368 color
= color_commit
;
373 strbuf_addstr(buf
, status
== '!' ? color_old
: color
);
375 strbuf_addf(buf
, "%*s: %s ", patch_no_width
, "-", dashes
->buf
);
377 strbuf_addf(buf
, "%*d: %s ", patch_no_width
, a_util
->i
+ 1,
378 find_unique_abbrev(&a_util
->oid
, DEFAULT_ABBREV
));
381 strbuf_addf(buf
, "%s%s", color_reset
, color
);
382 strbuf_addch(buf
, status
);
384 strbuf_addf(buf
, "%s%s", color_reset
, color_new
);
387 strbuf_addf(buf
, " %*s: %s", patch_no_width
, "-", dashes
->buf
);
389 strbuf_addf(buf
, " %*d: %s", patch_no_width
, b_util
->i
+ 1,
390 find_unique_abbrev(&b_util
->oid
, DEFAULT_ABBREV
));
392 commit
= lookup_commit_reference(the_repository
, oid
);
395 strbuf_addf(buf
, "%s%s", color_reset
, color
);
397 strbuf_addch(buf
, ' ');
398 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
400 strbuf_addf(buf
, "%s\n", color_reset
);
402 fwrite(buf
->buf
, buf
->len
, 1, diffopt
->file
);
405 static struct userdiff_driver no_func_name
= {
406 .funcname
= { "$^", 0 }
409 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
411 struct diff_filespec
*spec
= alloc_filespec(name
);
413 fill_filespec(spec
, &null_oid
, 0, 0100644);
414 spec
->data
= (char *)p
;
415 spec
->size
= strlen(p
);
416 spec
->should_munmap
= 0;
418 spec
->driver
= &no_func_name
;
423 static void patch_diff(const char *a
, const char *b
,
424 struct diff_options
*diffopt
)
426 diff_queue(&diff_queued_diff
,
427 get_filespec("a", a
), get_filespec("b", b
));
429 diffcore_std(diffopt
);
433 static void output(struct string_list
*a
, struct string_list
*b
,
434 struct diff_options
*diffopt
)
436 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
437 int patch_no_width
= decimal_width(1 + (a
->nr
> b
->nr ? a
->nr
: b
->nr
));
441 * We assume the user is really more interested in the second argument
442 * ("newer" version). To that end, we print the output in the order of
443 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
444 * commits that are no longer in the RHS into a good place, we place
445 * them once we have shown all of their predecessors in the LHS.
448 while (i
< a
->nr
|| j
< b
->nr
) {
449 struct patch_util
*a_util
, *b_util
;
450 a_util
= i
< a
->nr ? a
->items
[i
].util
: NULL
;
451 b_util
= j
< b
->nr ? b
->items
[j
].util
: NULL
;
453 /* Skip all the already-shown commits from the LHS. */
454 while (i
< a
->nr
&& a_util
->shown
)
455 a_util
= ++i
< a
->nr ? a
->items
[i
].util
: NULL
;
457 /* Show unmatched LHS commit whose predecessors were shown. */
458 if (i
< a
->nr
&& a_util
->matching
< 0) {
459 output_pair_header(diffopt
, patch_no_width
,
460 &buf
, &dashes
, a_util
, NULL
);
465 /* Show unmatched RHS commits. */
466 while (j
< b
->nr
&& b_util
->matching
< 0) {
467 output_pair_header(diffopt
, patch_no_width
,
468 &buf
, &dashes
, NULL
, b_util
);
469 b_util
= ++j
< b
->nr ? b
->items
[j
].util
: NULL
;
472 /* Show matching LHS/RHS pair. */
474 a_util
= a
->items
[b_util
->matching
].util
;
475 output_pair_header(diffopt
, patch_no_width
,
476 &buf
, &dashes
, a_util
, b_util
);
477 if (!(diffopt
->output_format
& DIFF_FORMAT_NO_OUTPUT
))
478 patch_diff(a
->items
[b_util
->matching
].string
,
479 b
->items
[j
].string
, diffopt
);
484 strbuf_release(&buf
);
485 strbuf_release(&dashes
);
488 static struct strbuf
*output_prefix_cb(struct diff_options
*opt
, void *data
)
493 int show_range_diff(const char *range1
, const char *range2
,
494 int creation_factor
, int dual_color
,
495 struct diff_options
*diffopt
)
499 struct string_list branch1
= STRING_LIST_INIT_DUP
;
500 struct string_list branch2
= STRING_LIST_INIT_DUP
;
502 if (read_patches(range1
, &branch1
))
503 res
= error(_("could not parse log for '%s'"), range1
);
504 if (!res
&& read_patches(range2
, &branch2
))
505 res
= error(_("could not parse log for '%s'"), range2
);
508 struct diff_options opts
;
509 struct strbuf indent
= STRBUF_INIT
;
512 memcpy(&opts
, diffopt
, sizeof(opts
));
516 if (!opts
.output_format
)
517 opts
.output_format
= DIFF_FORMAT_PATCH
;
518 opts
.flags
.suppress_diff_headers
= 1;
519 opts
.flags
.dual_color_diffed_diffs
= dual_color
;
520 opts
.flags
.suppress_hunk_header_line_count
= 1;
521 opts
.output_prefix
= output_prefix_cb
;
522 strbuf_addstr(&indent
, " ");
523 opts
.output_prefix_data
= &indent
;
524 diff_setup_done(&opts
);
526 find_exact_matches(&branch1
, &branch2
);
527 get_correspondences(&branch1
, &branch2
, creation_factor
);
528 output(&branch1
, &branch2
, &opts
);
530 strbuf_release(&indent
);
533 string_list_clear(&branch1
, 1);
534 string_list_clear(&branch2
, 1);