]>
git.ipfire.org Git - thirdparty/git.git/blob - range-diff.c
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"
14 /* For the search for an exact match */
15 struct hashmap_entry e
;
16 const char *diff
, *patch
;
21 /* the index of the matching item in the other branch, or -1 */
27 * Reads the patches into a string list, with the `util` field being populated
28 * as struct object_id (will need to be free()d).
30 static int read_patches(const char *range
, struct string_list
*list
)
32 struct child_process cp
= CHILD_PROCESS_INIT
;
34 struct strbuf buf
= STRBUF_INIT
, line
= STRBUF_INIT
;
35 struct patch_util
*util
= NULL
;
38 argv_array_pushl(&cp
.args
, "log", "--no-color", "-p", "--no-merges",
39 "--reverse", "--date-order", "--decorate=no",
40 "--no-abbrev-commit", range
,
46 if (start_command(&cp
))
47 return error_errno(_("could not start `log`"));
48 in
= fdopen(cp
.out
, "r");
50 error_errno(_("could not read `log` output"));
55 while (strbuf_getline(&line
, in
) != EOF
) {
58 if (skip_prefix(line
.buf
, "commit ", &p
)) {
60 string_list_append(list
, buf
.buf
)->util
= util
;
63 util
= xcalloc(sizeof(*util
), 1);
64 if (get_oid(p
, &util
->oid
)) {
65 error(_("could not parse commit '%s'"), p
);
67 string_list_clear(list
, 1);
69 strbuf_release(&line
);
79 if (starts_with(line
.buf
, "diff --git")) {
81 strbuf_addch(&buf
, '\n');
82 if (!util
->diff_offset
)
83 util
->diff_offset
= buf
.len
;
84 strbuf_addbuf(&buf
, &line
);
85 } else if (in_header
) {
86 if (starts_with(line
.buf
, "Author: ")) {
87 strbuf_addbuf(&buf
, &line
);
88 strbuf_addstr(&buf
, "\n\n");
89 } else if (starts_with(line
.buf
, " ")) {
91 strbuf_addbuf(&buf
, &line
);
92 strbuf_addch(&buf
, '\n');
95 } else if (starts_with(line
.buf
, "@@ "))
96 strbuf_addstr(&buf
, "@@");
97 else if (!line
.buf
[0] || starts_with(line
.buf
, "index "))
99 * A completely blank (not ' \n', which is context)
100 * line is not valid in a diff. We skip it
101 * silently, because this neatly handles the blank
102 * separator line between commits in git-log
105 * We also want to ignore the diff's `index` lines
106 * because they contain exact blob hashes in which
107 * we are not interested.
111 strbuf_addbuf(&buf
, &line
);
113 strbuf_addch(&buf
, '\n');
117 strbuf_release(&line
);
120 string_list_append(list
, buf
.buf
)->util
= util
;
121 strbuf_release(&buf
);
123 if (finish_command(&cp
))
129 static int patch_util_cmp(const void *dummy
, const struct patch_util
*a
,
130 const struct patch_util
*b
, const char *keydata
)
132 return strcmp(a
->diff
, keydata
? keydata
: b
->diff
);
135 static void find_exact_matches(struct string_list
*a
, struct string_list
*b
)
140 hashmap_init(&map
, (hashmap_cmp_fn
)patch_util_cmp
, NULL
, 0);
142 /* First, add the patches of a to a hash map */
143 for (i
= 0; i
< a
->nr
; i
++) {
144 struct patch_util
*util
= a
->items
[i
].util
;
147 util
->patch
= a
->items
[i
].string
;
148 util
->diff
= util
->patch
+ util
->diff_offset
;
149 hashmap_entry_init(util
, strhash(util
->diff
));
150 hashmap_add(&map
, util
);
153 /* Now try to find exact matches in b */
154 for (i
= 0; i
< b
->nr
; i
++) {
155 struct patch_util
*util
= b
->items
[i
].util
, *other
;
158 util
->patch
= b
->items
[i
].string
;
159 util
->diff
= util
->patch
+ util
->diff_offset
;
160 hashmap_entry_init(util
, strhash(util
->diff
));
161 other
= hashmap_remove(&map
, util
, NULL
);
163 if (other
->matching
>= 0)
164 BUG("already assigned!");
167 util
->matching
= other
->i
;
171 hashmap_free(&map
, 0);
174 static void diffsize_consume(void *data
, char *line
, unsigned long len
)
179 static int diffsize(const char *a
, const char *b
)
181 xpparam_t pp
= { 0 };
182 xdemitconf_t cfg
= { 0 };
187 mf1
.size
= strlen(a
);
189 mf2
.size
= strlen(b
);
192 if (!xdi_diff_outf(&mf1
, &mf2
, diffsize_consume
, &count
, &pp
, &cfg
))
195 error(_("failed to generate diff"));
199 static void get_correspondences(struct string_list
*a
, struct string_list
*b
,
202 int n
= a
->nr
+ b
->nr
;
203 int *cost
, c
, *a2b
, *b2a
;
206 ALLOC_ARRAY(cost
, st_mult(n
, n
));
210 for (i
= 0; i
< a
->nr
; i
++) {
211 struct patch_util
*a_util
= a
->items
[i
].util
;
213 for (j
= 0; j
< b
->nr
; j
++) {
214 struct patch_util
*b_util
= b
->items
[j
].util
;
216 if (a_util
->matching
== j
)
218 else if (a_util
->matching
< 0 && b_util
->matching
< 0)
219 c
= diffsize(a_util
->diff
, b_util
->diff
);
225 c
= a_util
->matching
< 0 ?
226 a_util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
227 for (j
= b
->nr
; j
< n
; j
++)
231 for (j
= 0; j
< b
->nr
; j
++) {
232 struct patch_util
*util
= b
->items
[j
].util
;
234 c
= util
->matching
< 0 ?
235 util
->diffsize
* creation_factor
/ 100 : COST_MAX
;
236 for (i
= a
->nr
; i
< n
; i
++)
240 for (i
= a
->nr
; i
< n
; i
++)
241 for (j
= b
->nr
; j
< n
; j
++)
244 compute_assignment(n
, n
, cost
, a2b
, b2a
);
246 for (i
= 0; i
< a
->nr
; i
++)
247 if (a2b
[i
] >= 0 && a2b
[i
] < b
->nr
) {
248 struct patch_util
*a_util
= a
->items
[i
].util
;
249 struct patch_util
*b_util
= b
->items
[a2b
[i
]].util
;
251 a_util
->matching
= a2b
[i
];
252 b_util
->matching
= i
;
260 static void output_pair_header(struct strbuf
*buf
,
261 struct strbuf
*dashes
,
262 struct patch_util
*a_util
,
263 struct patch_util
*b_util
)
265 struct object_id
*oid
= a_util
? &a_util
->oid
: &b_util
->oid
;
266 struct commit
*commit
;
269 strbuf_addchars(dashes
, '-',
270 strlen(find_unique_abbrev(oid
,
275 strbuf_addf(buf
, "-: %s ", dashes
->buf
);
277 strbuf_addf(buf
, "%d: %s ", a_util
->i
+ 1,
278 find_unique_abbrev(&a_util
->oid
, DEFAULT_ABBREV
));
281 strbuf_addch(buf
, '>');
283 strbuf_addch(buf
, '<');
284 else if (strcmp(a_util
->patch
, b_util
->patch
))
285 strbuf_addch(buf
, '!');
287 strbuf_addch(buf
, '=');
290 strbuf_addf(buf
, " -: %s", dashes
->buf
);
292 strbuf_addf(buf
, " %d: %s", b_util
->i
+ 1,
293 find_unique_abbrev(&b_util
->oid
, DEFAULT_ABBREV
));
295 commit
= lookup_commit_reference(the_repository
, oid
);
297 strbuf_addch(buf
, ' ');
298 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, buf
);
300 strbuf_addch(buf
, '\n');
302 fwrite(buf
->buf
, buf
->len
, 1, stdout
);
305 static struct diff_filespec
*get_filespec(const char *name
, const char *p
)
307 struct diff_filespec
*spec
= alloc_filespec(name
);
309 fill_filespec(spec
, &null_oid
, 0, 0644);
310 spec
->data
= (char *)p
;
311 spec
->size
= strlen(p
);
312 spec
->should_munmap
= 0;
318 static void patch_diff(const char *a
, const char *b
,
319 struct diff_options
*diffopt
)
321 diff_queue(&diff_queued_diff
,
322 get_filespec("a", a
), get_filespec("b", b
));
324 diffcore_std(diffopt
);
328 static void output(struct string_list
*a
, struct string_list
*b
,
329 struct diff_options
*diffopt
)
331 struct strbuf buf
= STRBUF_INIT
, dashes
= STRBUF_INIT
;
335 * We assume the user is really more interested in the second argument
336 * ("newer" version). To that end, we print the output in the order of
337 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
338 * commits that are no longer in the RHS into a good place, we place
339 * them once we have shown all of their predecessors in the LHS.
342 while (i
< a
->nr
|| j
< b
->nr
) {
343 struct patch_util
*a_util
, *b_util
;
344 a_util
= i
< a
->nr
? a
->items
[i
].util
: NULL
;
345 b_util
= j
< b
->nr
? b
->items
[j
].util
: NULL
;
347 /* Skip all the already-shown commits from the LHS. */
348 while (i
< a
->nr
&& a_util
->shown
)
349 a_util
= ++i
< a
->nr
? a
->items
[i
].util
: NULL
;
351 /* Show unmatched LHS commit whose predecessors were shown. */
352 if (i
< a
->nr
&& a_util
->matching
< 0) {
353 output_pair_header(&buf
, &dashes
, a_util
, NULL
);
358 /* Show unmatched RHS commits. */
359 while (j
< b
->nr
&& b_util
->matching
< 0) {
360 output_pair_header(&buf
, &dashes
, NULL
, b_util
);
361 b_util
= ++j
< b
->nr
? b
->items
[j
].util
: NULL
;
364 /* Show matching LHS/RHS pair. */
366 a_util
= a
->items
[b_util
->matching
].util
;
367 output_pair_header(&buf
, &dashes
, a_util
, b_util
);
368 if (!(diffopt
->output_format
& DIFF_FORMAT_NO_OUTPUT
))
369 patch_diff(a
->items
[b_util
->matching
].string
,
370 b
->items
[j
].string
, diffopt
);
375 strbuf_release(&buf
);
376 strbuf_release(&dashes
);
379 int show_range_diff(const char *range1
, const char *range2
,
380 int creation_factor
, struct diff_options
*diffopt
)
384 struct string_list branch1
= STRING_LIST_INIT_DUP
;
385 struct string_list branch2
= STRING_LIST_INIT_DUP
;
387 if (read_patches(range1
, &branch1
))
388 res
= error(_("could not parse log for '%s'"), range1
);
389 if (!res
&& read_patches(range2
, &branch2
))
390 res
= error(_("could not parse log for '%s'"), range2
);
393 find_exact_matches(&branch1
, &branch2
);
394 get_correspondences(&branch1
, &branch2
, creation_factor
);
395 output(&branch1
, &branch2
, diffopt
);
398 string_list_clear(&branch1
, 1);
399 string_list_clear(&branch2
, 1);