Skip to content

Commit f42d7b1

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 5d9962d commit f42d7b1

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
@@ -2495,22 +2495,6 @@ static void pprint_rename(struct strbuf *name, const char *a, const char *b)
24952495
}
24962496
}
24972497

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-
25142498
static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
25152499
const char *name_a,
25162500
const char *name_b)
@@ -3157,7 +3141,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
31573141
gather_dirstat(options, &dir, changed, "", 0);
31583142
}
31593143

3160-
static void free_diffstat_info(struct diffstat_t *diffstat)
3144+
void free_diffstat_info(struct diffstat_t *diffstat)
31613145
{
31623146
int i;
31633147
for (i = 0; i < diffstat->nr; i++) {
@@ -6283,12 +6267,7 @@ void diff_flush(struct diff_options *options)
62836267
dirstat_by_line) {
62846268
struct diffstat_t diffstat;
62856269

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);
62926271
if (output_format & DIFF_FORMAT_NUMSTAT)
62936272
show_numstat(&diffstat, options);
62946273
if (output_format & DIFF_FORMAT_DIFFSTAT)
@@ -6621,6 +6600,20 @@ static int is_submodule_ignored(const char *path, struct diff_options *options)
66216600
return ignored;
66226601
}
66236602

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+
66246617
void diff_addremove(struct diff_options *options,
66256618
int addremove, unsigned mode,
66266619
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)