@matheusbsbs / matheus-bagliano / commits / e4cb659ebd
Fork 0

diff: export diffstat interface

Make the diffstat interface (namely, the diffstat_t struct and compute_diffstat) no longer be internal to diff.c and allow it to be used by other parts of git. This is helpful for code that may want to easily extract information from files using the diff machinery, while flushing it differently from how the show_* functions used by diff_flush() do it. One example is the builtin implementation of git-add--interactive's status. Signed-off-by: Daniel Ferreira <bnmvco@gmail.com> Signed-off-by: Slavica Đukić <slawica92@hotmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Daniel Ferreira committed Nov 13, 2019 at 12:40 UTC e4cb659ebdd7c21976f9088226cbcc96acaa1dff
2 files changed +36 -23
diff.c
+16 -23
@@ -2495,22 +2495,6 @@ static void pprint_rename(struct strbuf *name, const char *a, const char *b)
2495 }
2496 }
2497
2498 -struct diffstat_t {
2499 - int nr;
2500 - int alloc;
2501 - struct diffstat_file {
2502 - char *from_name;
2503 - char *name;
2504 - char *print_name;
2505 - const char *comments;
2506 - unsigned is_unmerged:1;
2507 - unsigned is_binary:1;
2508 - unsigned is_renamed:1;
2509 - unsigned is_interesting:1;
2510 - uintmax_t added, deleted;
2511 - } **files;
2512 -};
2513 -
2498 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2499 const char *name_a,
2500 const char *name_b)
@@ -3157,7 +3141,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
3141 gather_dirstat(options, &dir, changed, "", 0);
3142 }
3143
3160 -static void free_diffstat_info(struct diffstat_t *diffstat)
3144 +void free_diffstat_info(struct diffstat_t *diffstat)
3145 {
3146 int i;
3147 for (i = 0; i < diffstat->nr; i++) {
@@ -6283,12 +6267,7 @@ void diff_flush(struct diff_options *options)
6267 dirstat_by_line) {
6268 struct diffstat_t diffstat;
6269
6286 - memset(&diffstat, 0, sizeof(struct diffstat_t));
6287 - for (i = 0; i < q->nr; i++) {
6288 - struct diff_filepair *p = q->queue[i];
6289 - if (check_pair_status(p))
6290 - diff_flush_stat(p, options, &diffstat);
6291 - }
6270 + compute_diffstat(options, &diffstat, q);
6271 if (output_format & DIFF_FORMAT_NUMSTAT)
6272 show_numstat(&diffstat, options);
6273 if (output_format & DIFF_FORMAT_DIFFSTAT)
@@ -6621,6 +6600,20 @@ static int is_submodule_ignored(const char *path, struct diff_options *options)
6600 return ignored;
6601 }
6602
6603 +void compute_diffstat(struct diff_options *options,
6604 + struct diffstat_t *diffstat,
6605 + struct diff_queue_struct *q)
6606 +{
6607 + int i;
6608 +
6609 + memset(diffstat, 0, sizeof(struct diffstat_t));
6610 + for (i = 0; i < q->nr; i++) {
6611 + struct diff_filepair *p = q->queue[i];
6612 + if (check_pair_status(p))
6613 + diff_flush_stat(p, options, diffstat);
6614 + }
6615 +}
6616 +
6617 void diff_addremove(struct diff_options *options,
6618 int addremove, unsigned mode,
6619 const struct object_id *oid,
diff.h
+20
@@ -245,6 +245,22 @@ void diff_emit_submodule_error(struct diff_options *o, const char *err);
245 void diff_emit_submodule_pipethrough(struct diff_options *o,
246 const char *line, int len);
247
248 +struct diffstat_t {
249 + int nr;
250 + int alloc;
251 + struct diffstat_file {
252 + char *from_name;
253 + char *name;
254 + char *print_name;
255 + const char *comments;
256 + unsigned is_unmerged:1;
257 + unsigned is_binary:1;
258 + unsigned is_renamed:1;
259 + unsigned is_interesting:1;
260 + uintmax_t added, deleted;
261 + } **files;
262 +};
263 +
264 enum color_diff {
265 DIFF_RESET = 0,
266 DIFF_CONTEXT = 1,
@@ -334,6 +350,10 @@ void diff_change(struct diff_options *,
350
351 struct diff_filepair *diff_unmerge(struct diff_options *, const char *path);
352
353 +void compute_diffstat(struct diff_options *options, struct diffstat_t *diffstat,
354 + struct diff_queue_struct *q);
355 +void free_diffstat_info(struct diffstat_t *diffstat);
356 +
357 #define DIFF_SETUP_REVERSE 1
358 #define DIFF_SETUP_USE_SIZE_CACHE 4
359