Skip to content

Commit 909eeb7

Browse files
theiostreamdscho
authored andcommitted
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 <[email protected]> Signed-off-by: Slavica Đukić <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent fefac4d commit 909eeb7

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

diff.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,22 +2492,6 @@ static void pprint_rename(struct strbuf *name, const char *a, const char *b)
24922492
}
24932493
}
24942494

2495-
struct diffstat_t {
2496-
int nr;
2497-
int alloc;
2498-
struct diffstat_file {
2499-
char *from_name;
2500-
char *name;
2501-
char *print_name;
2502-
const char *comments;
2503-
unsigned is_unmerged:1;
2504-
unsigned is_binary:1;
2505-
unsigned is_renamed:1;
2506-
unsigned is_interesting:1;
2507-
uintmax_t added, deleted;
2508-
} **files;
2509-
};
2510-
25112495
static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
25122496
const char *name_a,
25132497
const char *name_b)
@@ -3154,7 +3138,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
31543138
gather_dirstat(options, &dir, changed, "", 0);
31553139
}
31563140

3157-
static void free_diffstat_info(struct diffstat_t *diffstat)
3141+
void free_diffstat_info(struct diffstat_t *diffstat)
31583142
{
31593143
int i;
31603144
for (i = 0; i < diffstat->nr; i++) {
@@ -6278,12 +6262,7 @@ void diff_flush(struct diff_options *options)
62786262
dirstat_by_line) {
62796263
struct diffstat_t diffstat;
62806264

6281-
memset(&diffstat, 0, sizeof(struct diffstat_t));
6282-
for (i = 0; i < q->nr; i++) {
6283-
struct diff_filepair *p = q->queue[i];
6284-
if (check_pair_status(p))
6285-
diff_flush_stat(p, options, &diffstat);
6286-
}
6265+
compute_diffstat(options, &diffstat, q);
62876266
if (output_format & DIFF_FORMAT_NUMSTAT)
62886267
show_numstat(&diffstat, options);
62896268
if (output_format & DIFF_FORMAT_DIFFSTAT)
@@ -6616,6 +6595,20 @@ static int is_submodule_ignored(const char *path, struct diff_options *options)
66166595
return ignored;
66176596
}
66186597

6598+
void compute_diffstat(struct diff_options *options,
6599+
struct diffstat_t *diffstat,
6600+
struct diff_queue_struct *q)
6601+
{
6602+
int i;
6603+
6604+
memset(diffstat, 0, sizeof(struct diffstat_t));
6605+
for (i = 0; i < q->nr; i++) {
6606+
struct diff_filepair *p = q->queue[i];
6607+
if (check_pair_status(p))
6608+
diff_flush_stat(p, options, diffstat);
6609+
}
6610+
}
6611+
66196612
void diff_addremove(struct diff_options *options,
66206613
int addremove, unsigned mode,
66216614
const struct object_id *oid,

diff.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,22 @@ void diff_emit_submodule_error(struct diff_options *o, const char *err);
245245
void diff_emit_submodule_pipethrough(struct diff_options *o,
246246
const char *line, int len);
247247

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+
248264
enum color_diff {
249265
DIFF_RESET = 0,
250266
DIFF_CONTEXT = 1,
@@ -334,6 +350,10 @@ void diff_change(struct diff_options *,
334350

335351
struct diff_filepair *diff_unmerge(struct diff_options *, const char *path);
336352

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+
337357
#define DIFF_SETUP_REVERSE 1
338358
#define DIFF_SETUP_USE_SIZE_CACHE 4
339359

0 commit comments

Comments
 (0)