Skip to content

Commit 4eed0eb

Browse files
stefanbellergitster
authored andcommitted
diff.c: convert emit_binary_diff_body to use emit_diff_symbol
Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f359713 commit 4eed0eb

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

diff.c

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset
561561
}
562562

563563
enum diff_symbol {
564+
DIFF_SYMBOL_BINARY_DIFF_HEADER,
565+
DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
566+
DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
567+
DIFF_SYMBOL_BINARY_DIFF_BODY,
568+
DIFF_SYMBOL_BINARY_DIFF_FOOTER,
564569
DIFF_SYMBOL_SUBMODULE_ADD,
565570
DIFF_SYMBOL_SUBMODULE_DEL,
566571
DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -635,6 +640,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
635640
case DIFF_SYMBOL_SUBMODULE_HEADER:
636641
case DIFF_SYMBOL_SUBMODULE_ERROR:
637642
case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
643+
case DIFF_SYMBOL_BINARY_DIFF_BODY:
638644
case DIFF_SYMBOL_CONTEXT_FRAGINFO:
639645
emit_line(o, "", "", line, len);
640646
break;
@@ -706,6 +712,19 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
706712
case DIFF_SYMBOL_HEADER:
707713
fprintf(o->file, "%s", line);
708714
break;
715+
case DIFF_SYMBOL_BINARY_DIFF_HEADER:
716+
fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
717+
break;
718+
case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
719+
fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
720+
break;
721+
case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
722+
fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
723+
break;
724+
case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
725+
fputs(diff_line_prefix(o), o->file);
726+
fputc('\n', o->file);
727+
break;
709728
case DIFF_SYMBOL_REWRITE_DIFF:
710729
fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
711730
reset = diff_get_color_opt(o, DIFF_RESET);
@@ -2390,8 +2409,8 @@ static unsigned char *deflate_it(char *data,
23902409
return deflated;
23912410
}
23922411

2393-
static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
2394-
const char *prefix)
2412+
static void emit_binary_diff_body(struct diff_options *o,
2413+
mmfile_t *one, mmfile_t *two)
23952414
{
23962415
void *cp;
23972416
void *delta;
@@ -2420,13 +2439,18 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
24202439
}
24212440

24222441
if (delta && delta_size < deflate_size) {
2423-
fprintf(file, "%sdelta %lu\n", prefix, orig_size);
2442+
char *s = xstrfmt("%lu", orig_size);
2443+
emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
2444+
s, strlen(s), 0);
2445+
free(s);
24242446
free(deflated);
24252447
data = delta;
24262448
data_size = delta_size;
2427-
}
2428-
else {
2429-
fprintf(file, "%sliteral %lu\n", prefix, two->size);
2449+
} else {
2450+
char *s = xstrfmt("%lu", two->size);
2451+
emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
2452+
s, strlen(s), 0);
2453+
free(s);
24302454
free(delta);
24312455
data = deflated;
24322456
data_size = deflate_size;
@@ -2435,29 +2459,34 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
24352459
/* emit data encoded in base85 */
24362460
cp = data;
24372461
while (data_size) {
2462+
int len;
24382463
int bytes = (52 < data_size) ? 52 : data_size;
2439-
char line[70];
2464+
char line[71];
24402465
data_size -= bytes;
24412466
if (bytes <= 26)
24422467
line[0] = bytes + 'A' - 1;
24432468
else
24442469
line[0] = bytes - 26 + 'a' - 1;
24452470
encode_85(line + 1, cp, bytes);
24462471
cp = (char *) cp + bytes;
2447-
fprintf(file, "%s", prefix);
2448-
fputs(line, file);
2449-
fputc('\n', file);
2472+
2473+
len = strlen(line);
2474+
line[len++] = '\n';
2475+
line[len] = '\0';
2476+
2477+
emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
2478+
line, len, 0);
24502479
}
2451-
fprintf(file, "%s\n", prefix);
2480+
emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
24522481
free(data);
24532482
}
24542483

2455-
static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two,
2456-
const char *prefix)
2484+
static void emit_binary_diff(struct diff_options *o,
2485+
mmfile_t *one, mmfile_t *two)
24572486
{
2458-
fprintf(file, "%sGIT binary patch\n", prefix);
2459-
emit_binary_diff_body(file, one, two, prefix);
2460-
emit_binary_diff_body(file, two, one, prefix);
2487+
emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
2488+
emit_binary_diff_body(o, one, two);
2489+
emit_binary_diff_body(o, two, one);
24612490
}
24622491

24632492
int diff_filespec_is_binary(struct diff_filespec *one)
@@ -2643,7 +2672,7 @@ static void builtin_diff(const char *name_a,
26432672
emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
26442673
strbuf_reset(&header);
26452674
if (DIFF_OPT_TST(o, BINARY))
2646-
emit_binary_diff(o->file, &mf1, &mf2, line_prefix);
2675+
emit_binary_diff(o, &mf1, &mf2);
26472676
else {
26482677
strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
26492678
diff_line_prefix(o), lbl[0], lbl[1]);

0 commit comments

Comments
 (0)