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"
15 /* For the search for an exact match */
16 struct hashmap_entry e
;
17 const char *diff
, *patch
;
22 /* the index of the matching item in the other branch, or -1 */
28 * Reads the patches into a string list, with the `util` field being populated
29 * as struct object_id (will need to be free()d).
31 static int read_patches(const char *range
, struct string_list
*list
)
33 struct child_process cp
= CHILD_PROCESS_INIT
;
35 struct strbuf buf
= STRBUF_INIT
, line
= STRBUF_INIT
;
36 struct patch_util
*util
= NULL
;
39 argv_array_pushl(&cp
.args
, "log", "--no-color", "-p", "--no-merges",
40 "--reverse", "--date-order", "--decorate=no",
42 * Choose indicators that are not used anywhere
43 * else in diffs, but still look reasonable
44 * (e.g. will not be confusing when debugging)
46 "--output-indicator-new=>",
47 "--output-indicator-old=<",
48 "--output-indicator-context=#",
49 "--no-abbrev-commit", range
,
55 if (start_command(&cp
))
56 return error_errno(_("could not start `log`"));
57 in
= fdopen(cp
.out
, "r");
59 error_errno(_("could not read `log` output"));
64 while (strbuf_getline(&line
, in
) != EOF
) {
67 if (skip_prefix(line
.buf
, "commit ", &p
)) {
69 string_list_append(list
, buf
.buf
)->util
= util
;
72 util
= xcalloc(sizeof(*util
), 1);
73 if (get_oid(p
, &util
->oid
)) {
74 error(_("could not parse commit '%s'"), p
);
76 string_list_clear(list
, 1);
78 strbuf_release(&line
);
88 if (starts_with(line
.buf
, "diff --git")) {
90 strbuf_addch(&buf
, '\n');
91 if (!util
->diff_offset
)
92 util
->diff_offset
= buf
.len
;
93 strbuf_addch(&buf
, ' ');
94 strbuf_addbuf(&buf
, &line
);
95 } else if (in_header
) {
96 if (starts_with(line
.buf
, "Author: ")) {
97 strbuf_addbuf(&buf
, &line
);
98 strbuf_addstr(&buf
, "\n\n");
99 } else if (starts_with(line
.buf
, " ")) {
101 strbuf_addbuf(&buf
, &line
);
102 strbuf_addch(&buf
, '\n');
105 } else if (starts_with(line
.buf
, "@@ "))
106 strbuf_addstr(&buf
, "@@");
107 else if (!line
.buf
[0] || starts_with(line
.buf
, "index "))
109 * A completely blank (not ' \n', which is context)
110 * line is not valid in a diff. We skip it
111 * silently, because this neatly handles the blank
112 * separator line between commits in git-log
115 * We also want to ignore the diff's `index` lines
116 * because they contain exact blob hashes in which
117 * we are not interested.
120 else if (line
.buf
[0] == '>') {
121 strbuf_addch(&buf
, '+');
122 strbuf_add(&buf
, line
.buf
+ 1, line
.len
- 1);
123 } else if (line
.buf
[0] == '<') {
124 strbuf_addch(&buf
, '-');
125 strbuf_add(&buf
, line
.buf
+ 1, line
.len
- 1);
126 } else if (line
.buf
[0] == '#') {
127 strbuf_addch(&buf
, ' ');
128 strbuf_add(&buf
, line
.buf
+ 1, line
.len
- 1);
130 strbuf_addch(&buf
, ' ');
131 strbuf_addbuf(&buf
, &line
);
134 strbuf_addch(&buf
, '\n');
138 strbuf_release(&line
);
141 string_list_append(list
, buf
.buf
)->util
= util
;
142 strbuf_release(&buf
);
144 if (finish_command(&cp
))
150 static int patch_util_cmp(const void *dummy
, const struct patch_util
*a
,
151 const struct patch_util
*b
, const char *keydata
)
153 return strcmp(a
->diff
, keydata
? keydata
: b
->diff
);
156 static void find_exact_matches(struct string_list
*a
, struct string_list
*b
)
161 hashmap_init(&map
, (hashmap_cmp_fn
)patch_util_cmp
, NULL
, 0);
163 /* First, add the patches of a to a hash map */
164 for (i
= 0; i
< a
->nr
; i
++) {
165 struct patch_util
*util
= a
->items
[i
].util
;
168 util
->patch
= a
->items
[i
].string
;
169 util
->diff
= util
->patch
+ util
->diff_offset
;
170 hashmap_entry_init(util
, strhash(util
->diff
));
171 hashmap_add(&map
, util
);
174 /* Now try to find exact matches in b */
175 for (i
= 0; i
< b
->nr
; i
++) {
176 struct patch_util
*util
= b
->items
[i
].util
, *other
;
179 util
->patch
= b
->items
[i
].string
;
180 util
->diff
= util
->patch
+ util
->diff_offset
;
181 hashmap_entry_init(util
, strhash(util
->diff
));
182 other
= hashmap_remove(&map
, util
, NULL
);
184 if (other
->matching
>= 0)
185 BUG("already assigned!");
188 util
->matching
= other
->i
;
192 hashmap_free(&map
, 0);
195 static void diffsize_consume(void *data
, char *line
, unsigned long len
)
200 static void diffsize_hunk(void *data
, long ob
, long on
, long nb
, long nn
,
201 const char *funcline
, long funclen
)
203 diffsize_consume(data
, NULL
, 0);
206 static int diffsize(const char *a
, const char *b
)
208 xpparam_t pp
= { 0 };
209 xdemitconf_t cfg
= { 0 };
214 mf1
.size
= strlen(a
);
216 mf2
.size
= strlen(b
);
219 if (!xdi_diff_outf(&mf1
, &mf2
,
220 diffsize_hunk
, diffsize_consume
, &count
,
224 error(_("failed to generate diff"));
228 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
231 int n
= a
->nr
+ b
->nr
;
232 int *cost
, c
, *a2b
, *b2a
;
235 ALLOC_ARRAY(cost
, st_mult(n
, n
));
239 for (i
= 0; i
< a
->nr
; i
++) {
240 struct patch_util
*a_util
= a
->items
[i
].util
;
242 for (j
= 0; j
< b
->nr
; j
++) {
243 struct patch_util
*b_util
= b
->items
[j
].util
;
245 if (a_util
->matching
== j
)
247 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
248 c
= diffsize(a_util
->diff
, b_util
->diff
);
254 c
= a_util
->matching
< 0 ?
255 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
256 for (j
= b
->nr
; j
< n
; j
++)
260 for (j
= 0; j
< b
->nr
; j
++) {
261 struct patch_util
*util
= b
->items
[j
].util
;
263 c
= util
->matching
< 0 ?
264 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
265 for (i
= a
->nr
; i
< n
; i
++)
269 for (i
= a
->nr
; i
< n
; i
++)
270 for (j
= b
->nr
; j
< n
; j
++)
273 compute_assignment(n
, n
, cost
, a2b
, b2a
);
275 for (i
= 0; i
< a
->nr
; i
++)
276 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
277 struct patch_util
*a_util
= a
->items
[i
].util
;
278 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
280 a_util
->matching
= a2b
[i
];
281 b_util
->matching
= i
;
289 static void output_pair_header(struct diff_options
*diffopt
,
292 struct strbuf
*dashes
,
293 struct patch_util
*a_util
,
294 struct patch_util
*b_util
)
296 struct object_id
*oid
= a_util
? &a_util
->oid
: &b_util
->oid
;
297 struct commit
*commit
;
299 const char *color_reset
= diff_get_color_opt(diffopt
, DIFF_RESET
);
300 const char *color_old
= diff_get_color_opt(diffopt
, DIFF_FILE_OLD
);
301 const char *color_new
= diff_get_color_opt(diffopt
, DIFF_FILE_NEW
);
302 const char *color_commit
= diff_get_color_opt(diffopt
, DIFF_COMMIT
);
306 strbuf_addchars(dashes
, '-',
307 strlen(find_unique_abbrev(oid
,
313 } else if (!a_util
) {
316 } else if (strcmp(a_util
->patch
, b_util
->patch
)) {
317 color
= color_commit
;
320 color
= color_commit
;
325 strbuf_addstr(buf
, status
== '!' ? color_old
: color
);
327 strbuf_addf(buf
, "%*s: %s ", patch_no_width
, "-", dashes
->buf
);
329 strbuf_addf(buf
, "%*d: %s ", patch_no_width
, a_util
->i
+ 1,
330 find_unique_abbrev(&a_util
->oid
, DEFAULT_ABBREV
));
333 strbuf_addf(buf
, "%s%s", color_reset
, color
);
334 strbuf_addch(buf
, status
);
336 strbuf_addf(buf
, "%s%s", color_reset
, color_new
);
339 strbuf_addf(buf
, " %*s: %s", patch_no_width
, "-", dashes
->buf
);
341 strbuf_addf(buf
, " %*d: %s", patch_no_width
, b_util
->i
+ 1,
342 find_unique_abbrev(&b_util
->oid
, DEFAULT_ABBREV
));
344 commit
= lookup_commit_reference(the_repository
, oid
);
347 strbuf_addf(buf
, "%s%s", color_reset
, color
);
349 strbuf_addch(buf
, ' ');
350 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
352 strbuf_addf(buf
, "%s\n", color_reset
);
354 fwrite(buf
->buf
, buf
->len
, 1, diffopt
->file
);
357 static struct userdiff_driver no_func_name
= {
358 .funcname
= { "$^", 0 }
361 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
363 struct diff_filespec
*spec
= alloc_filespec(name
);
365 fill_filespec(spec
, &null_oid
, 0, 0100644);
366 spec
->data
= (char *)p
;
367 spec
->size
= strlen(p
);
368 spec
->should_munmap
= 0;
370 spec
->driver
= &no_func_name
;
375 static void patch_diff(const char *a
, const char *b
,
376 struct diff_options
*diffopt
)
378 diff_queue(&diff_queued_diff
,
379 get_filespec("a", a
), get_filespec("b", b
));
381 diffcore_std(diffopt
);
385 static void output(struct string_list
*a
, struct string_list
*b
,
386 struct diff_options
*diffopt
)
388 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
389 int patch_no_width
= decimal_width(1 + (a
->nr
> b
->nr
? a
->nr
: b
->nr
));
393 * We assume the user is really more interested in the second argument
394 * ("newer" version). To that end, we print the output in the order of
395 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
396 * commits that are no longer in the RHS into a good place, we place
397 * them once we have shown all of their predecessors in the LHS.
400 while (i
< a
->nr
|| j
< b
->nr
) {
401 struct patch_util
*a_util
, *b_util
;
402 a_util
= i
< a
->nr
? a
->items
[i
].util
: NULL
;
403 b_util
= j
< b
->nr
? b
->items
[j
].util
: NULL
;
405 /* Skip all the already-shown commits from the LHS. */
406 while (i
< a
->nr
&& a_util
->shown
)
407 a_util
= ++i
< a
->nr
? a
->items
[i
].util
: NULL
;
409 /* Show unmatched LHS commit whose predecessors were shown. */
410 if (i
< a
->nr
&& a_util
->matching
< 0) {
411 output_pair_header(diffopt
, patch_no_width
,
412 &buf
, &dashes
, a_util
, NULL
);
417 /* Show unmatched RHS commits. */
418 while (j
< b
->nr
&& b_util
->matching
< 0) {
419 output_pair_header(diffopt
, patch_no_width
,
420 &buf
, &dashes
, NULL
, b_util
);
421 b_util
= ++j
< b
->nr
? b
->items
[j
].util
: NULL
;
424 /* Show matching LHS/RHS pair. */
426 a_util
= a
->items
[b_util
->matching
].util
;
427 output_pair_header(diffopt
, patch_no_width
,
428 &buf
, &dashes
, a_util
, b_util
);
429 if (!(diffopt
->output_format
& DIFF_FORMAT_NO_OUTPUT
))
430 patch_diff(a
->items
[b_util
->matching
].string
,
431 b
->items
[j
].string
, diffopt
);
436 strbuf_release(&buf
);
437 strbuf_release(&dashes
);
440 static struct strbuf
*output_prefix_cb(struct diff_options
*opt
, void *data
)
445 int show_range_diff(const char *range1
, const char *range2
,
446 int creation_factor
, int dual_color
,
447 struct diff_options
*diffopt
)
451 struct string_list branch1
= STRING_LIST_INIT_DUP
;
452 struct string_list branch2
= STRING_LIST_INIT_DUP
;
454 if (read_patches(range1
, &branch1
))
455 res
= error(_("could not parse log for '%s'"), range1
);
456 if (!res
&& read_patches(range2
, &branch2
))
457 res
= error(_("could not parse log for '%s'"), range2
);
460 struct diff_options opts
;
461 struct strbuf indent
= STRBUF_INIT
;
463 memcpy(&opts
, diffopt
, sizeof(opts
));
464 if (!opts
.output_format
)
465 opts
.output_format
= DIFF_FORMAT_PATCH
;
466 opts
.flags
.suppress_diff_headers
= 1;
467 opts
.flags
.dual_color_diffed_diffs
= dual_color
;
468 opts
.output_prefix
= output_prefix_cb
;
469 strbuf_addstr(&indent
, " ");
470 opts
.output_prefix_data
= &indent
;
471 diff_setup_done(&opts
);
473 find_exact_matches(&branch1
, &branch2
);
474 get_correspondences(&branch1
, &branch2
, creation_factor
);
475 output(&branch1
, &branch2
, &opts
);
477 strbuf_release(&indent
);
480 string_list_clear(&branch1
, 1);
481 string_list_clear(&branch2
, 1);