@matheusbsbs / matheus-bagliano / commits / 051308f6e9
Fork 0

binary patch.

This adds "binary patch" to the diff output and teaches apply what to do with them. On the diff generation side, traditionally, we said "Binary files differ\n" without giving anything other than the preimage and postimage object name on the index line. This was good enough for applying a patch generated from your own repository (very useful while rebasing), because the postimage would be available in such a case. However, this was not useful when the recipient of such a patch via e-mail were to apply it, even if the preimage was available. This patch allows the diff to generate "binary" patch when operating under --full-index option. The binary patch follows the usual extended git diff headers, and looks like this: "GIT binary patch\n" <length byte><data>"\n" ... "\n" Each line is prefixed with a "length-byte", whose value is upper or lowercase alphabet that encodes number of bytes that the data on the line decodes to (1..52 -- 'A' means 1, 'B' means 2, ..., 'Z' means 26, 'a' means 27, ...). <data> is 1 or more groups of 5-byte sequence, each of which encodes up to 4 bytes in base85 encoding. Because 52 / 4 * 5 = 65 and we have the length byte, an output line is capped to 66 characters. The payload is the same diff-delta as we use in the packfiles. On the consumption side, git-apply now can decode and apply the binary patch when --allow-binary-replacement is given, the diff was generated with --full-index, and the receiving repository has the preimage blob, which is the same condition as it always required when accepting an "Binary files differ\n" patch. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed May 4, 2006 at 16:51 UTC 051308f6e9cebeb76b8fb4f52b7e9e7ce064445c
4 files changed +275 -68
Makefile
+1 -1
@@ -205,7 +205,7 @@ DIFF_OBJS = \
205 diffcore-delta.o log-tree.o
206
207 LIB_OBJS = \
208 - blob.o commit.o connect.o csum-file.o \
208 + blob.o commit.o connect.o csum-file.o base85.o \
209 date.o diff-delta.o entry.o exec_cmd.o ident.o index.o \
210 object.o pack-check.o patch-delta.o path.o pkt-line.o \
211 quote.o read-cache.o refs.o run-command.o \
apply.c
+172 -60
@@ -10,6 +10,7 @@
10 #include "cache.h"
11 #include "quote.h"
12 #include "blob.h"
13 +#include "delta.h"
14
15 // --check turns on checking that the working tree matches the
16 // files that are being modified, but doesn't apply the patch
@@ -966,6 +967,70 @@ static inline int metadata_changes(struct patch *patch)
967 patch->old_mode != patch->new_mode);
968 }
969
970 +static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
971 +{
972 + /* We have read "GIT binary patch\n"; what follows is a
973 + * sequence of 'length-byte' followed by base-85 encoded
974 + * delta data.
975 + *
976 + * Each 5-byte sequence of base-85 encodes up to 4 bytes,
977 + * and we would limit the patch line to 66 characters,
978 + * so one line can fit up to 13 groups that would decode
979 + * to 52 bytes max. The length byte 'A'-'Z' corresponds
980 + * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
981 + * The end of binary is signalled with an empty line.
982 + */
983 + int llen, used;
984 + struct fragment *fragment;
985 + char *delta = NULL;
986 +
987 + patch->is_binary = 1;
988 + patch->fragments = fragment = xcalloc(1, sizeof(*fragment));
989 + used = 0;
990 + while (1) {
991 + int byte_length, max_byte_length, newsize;
992 + llen = linelen(buffer, size);
993 + used += llen;
994 + linenr++;
995 + if (llen == 1)
996 + break;
997 + /* Minimum line is "A00000\n" which is 7-byte long,
998 + * and the line length must be multiple of 5 plus 2.
999 + */
1000 + if ((llen < 7) || (llen-2) % 5)
1001 + goto corrupt;
1002 + max_byte_length = (llen - 2) / 5 * 4;
1003 + byte_length = *buffer;
1004 + if ('A' <= byte_length && byte_length <= 'Z')
1005 + byte_length = byte_length - 'A' + 1;
1006 + else if ('a' <= byte_length && byte_length <= 'z')
1007 + byte_length = byte_length - 'a' + 27;
1008 + else
1009 + goto corrupt;
1010 + /* if the input length was not multiple of 4, we would
1011 + * have filler at the end but the filler should never
1012 + * exceed 3 bytes
1013 + */
1014 + if (max_byte_length < byte_length ||
1015 + byte_length <= max_byte_length - 4)
1016 + goto corrupt;
1017 + newsize = fragment->size + byte_length;
1018 + delta = xrealloc(delta, newsize);
1019 + if (decode_85(delta + fragment->size,
1020 + buffer + 1,
1021 + byte_length))
1022 + goto corrupt;
1023 + fragment->size = newsize;
1024 + buffer += llen;
1025 + size -= llen;
1026 + }
1027 + fragment->patch = delta;
1028 + return used;
1029 + corrupt:
1030 + return error("corrupt binary patch at line %d: %.*s",
1031 + linenr-1, llen-1, buffer);
1032 +}
1033 +
1034 static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1035 {
1036 int hdrsize, patchsize;
@@ -982,19 +1047,34 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1047 "Files ",
1048 NULL,
1049 };
1050 + static const char git_binary[] = "GIT binary patch\n";
1051 int i;
1052 int hd = hdrsize + offset;
1053 unsigned long llen = linelen(buffer + hd, size - hd);
1054
989 - if (!memcmp(" differ\n", buffer + hd + llen - 8, 8))
1055 + if (llen == sizeof(git_binary) - 1 &&
1056 + !memcmp(git_binary, buffer + hd, llen)) {
1057 + int used;
1058 + linenr++;
1059 + used = parse_binary(buffer + hd + llen,
1060 + size - hd - llen, patch);
1061 + if (used)
1062 + patchsize = used + llen;
1063 + else
1064 + patchsize = 0;
1065 + }
1066 + else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
1067 for (i = 0; binhdr[i]; i++) {
1068 int len = strlen(binhdr[i]);
1069 if (len < size - hd &&
1070 !memcmp(binhdr[i], buffer + hd, len)) {
1071 + linenr++;
1072 patch->is_binary = 1;
1073 + patchsize = llen;
1074 break;
1075 }
1076 }
1077 + }
1078
1079 /* Empty patch cannot be applied if:
1080 * - it is a binary patch and we do not do binary_replace, or
@@ -1345,76 +1425,108 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1425 return offset;
1426 }
1427
1348 -static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1428 +static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1429 {
1350 - struct fragment *frag = patch->fragments;
1430 const char *name = patch->old_name ? patch->old_name : patch->new_name;
1431 + unsigned char sha1[20];
1432 + unsigned char hdr[50];
1433 + int hdrlen;
1434
1353 - if (patch->is_binary) {
1354 - unsigned char sha1[20];
1435 + if (!allow_binary_replacement)
1436 + return error("cannot apply binary patch to '%s' "
1437 + "without --allow-binary-replacement",
1438 + name);
1439
1356 - if (!allow_binary_replacement)
1357 - return error("cannot apply binary patch to '%s' "
1358 - "without --allow-binary-replacement",
1359 - name);
1440 + /* For safety, we require patch index line to contain
1441 + * full 40-byte textual SHA1 for old and new, at least for now.
1442 + */
1443 + if (strlen(patch->old_sha1_prefix) != 40 ||
1444 + strlen(patch->new_sha1_prefix) != 40 ||
1445 + get_sha1_hex(patch->old_sha1_prefix, sha1) ||
1446 + get_sha1_hex(patch->new_sha1_prefix, sha1))
1447 + return error("cannot apply binary patch to '%s' "
1448 + "without full index line", name);
1449
1361 - /* For safety, we require patch index line to contain
1362 - * full 40-byte textual SHA1 for old and new, at least for now.
1450 + if (patch->old_name) {
1451 + /* See if the old one matches what the patch
1452 + * applies to.
1453 */
1364 - if (strlen(patch->old_sha1_prefix) != 40 ||
1365 - strlen(patch->new_sha1_prefix) != 40 ||
1366 - get_sha1_hex(patch->old_sha1_prefix, sha1) ||
1367 - get_sha1_hex(patch->new_sha1_prefix, sha1))
1368 - return error("cannot apply binary patch to '%s' "
1369 - "without full index line", name);
1370 -
1371 - if (patch->old_name) {
1372 - unsigned char hdr[50];
1373 - int hdrlen;
1374 -
1375 - /* See if the old one matches what the patch
1376 - * applies to.
1377 - */
1378 - write_sha1_file_prepare(desc->buffer, desc->size,
1379 - blob_type, sha1, hdr, &hdrlen);
1380 - if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
1381 - return error("the patch applies to '%s' (%s), "
1382 - "which does not match the "
1383 - "current contents.",
1384 - name, sha1_to_hex(sha1));
1385 - }
1386 - else {
1387 - /* Otherwise, the old one must be empty. */
1388 - if (desc->size)
1389 - return error("the patch applies to an empty "
1390 - "'%s' but it is not empty", name);
1391 - }
1454 + write_sha1_file_prepare(desc->buffer, desc->size,
1455 + blob_type, sha1, hdr, &hdrlen);
1456 + if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
1457 + return error("the patch applies to '%s' (%s), "
1458 + "which does not match the "
1459 + "current contents.",
1460 + name, sha1_to_hex(sha1));
1461 + }
1462 + else {
1463 + /* Otherwise, the old one must be empty. */
1464 + if (desc->size)
1465 + return error("the patch applies to an empty "
1466 + "'%s' but it is not empty", name);
1467 + }
1468 +
1469 + if (desc->buffer) {
1470 + free(desc->buffer);
1471 + desc->alloc = desc->size = 0;
1472 + }
1473 + get_sha1_hex(patch->new_sha1_prefix, sha1);
1474 + if (!memcmp(sha1, null_sha1, 20))
1475 + return 0; /* deletion patch */
1476 +
1477 + if (has_sha1_file(sha1)) {
1478 + char type[10];
1479 + unsigned long size;
1480
1393 - /* For now, we do not record post-image data in the patch,
1394 - * and require the object already present in the recipient's
1395 - * object database.
1481 + desc->buffer = read_sha1_file(sha1, type, &size);
1482 + if (!desc->buffer)
1483 + return error("the necessary postimage %s for "
1484 + "'%s' cannot be read",
1485 + patch->new_sha1_prefix, name);
1486 + desc->alloc = desc->size = size;
1487 + }
1488 + else {
1489 + char type[10];
1490 + unsigned long src_size, dst_size;
1491 + void *src;
1492 +
1493 + get_sha1_hex(patch->old_sha1_prefix, sha1);
1494 + src = read_sha1_file(sha1, type, &src_size);
1495 + if (!src)
1496 + return error("the necessary preimage %s for "
1497 + "'%s' cannot be read",
1498 + patch->old_sha1_prefix, name);
1499 +
1500 + /* patch->fragment->patch has the delta data and
1501 + * we should apply it to the preimage.
1502 */
1397 - if (desc->buffer) {
1398 - free(desc->buffer);
1399 - desc->alloc = desc->size = 0;
1400 - }
1401 - get_sha1_hex(patch->new_sha1_prefix, sha1);
1402 -
1403 - if (memcmp(sha1, null_sha1, 20)) {
1404 - char type[10];
1405 - unsigned long size;
1406 -
1407 - desc->buffer = read_sha1_file(sha1, type, &size);
1408 - if (!desc->buffer)
1409 - return error("the necessary postimage %s for "
1410 - "'%s' does not exist",
1411 - patch->new_sha1_prefix, name);
1412 - desc->alloc = desc->size = size;
1413 - }
1503 + desc->buffer = patch_delta(src, src_size,
1504 + (void*) patch->fragments->patch,
1505 + patch->fragments->size,
1506 + &dst_size);
1507 + if (!desc->buffer)
1508 + return error("binary patch does not apply to '%s'",
1509 + name);
1510 + desc->size = desc->alloc = dst_size;
1511
1415 - return 0;
1512 + /* verify that the result matches */
1513 + write_sha1_file_prepare(desc->buffer, desc->size, blob_type,
1514 + sha1, hdr, &hdrlen);
1515 + if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
1516 + return error("binary patch to '%s' creates incorrect result", name);
1517 }
1518
1519 + return 0;
1520 +}
1521 +
1522 +static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1523 +{
1524 + struct fragment *frag = patch->fragments;
1525 + const char *name = patch->old_name ? patch->old_name : patch->new_name;
1526 +
1527 + if (patch->is_binary)
1528 + return apply_binary(desc, patch);
1529 +
1530 while (frag) {
1531 if (apply_one_fragment(desc, frag) < 0)
1532 return error("patch failed: %s:%ld",
cache.h
+3
@@ -363,4 +363,7 @@ extern int receive_keep_pack(int fd[2], const char *me, int quiet);
363 /* pager.c */
364 extern void setup_pager(void);
365
366 +/* base85 */
367 +int decode_85(char *dst, char *line, int linelen);
368 +
369 #endif /* CACHE_H */
diff.c
+99 -7
@@ -8,6 +8,7 @@
8 #include "quote.h"
9 #include "diff.h"
10 #include "diffcore.h"
11 +#include "delta.h"
12 #include "xdiff-interface.h"
13
14 static int use_size_cache;
@@ -391,6 +392,90 @@ static void show_stats(struct diffstat_t* data)
392 total_files, adds, dels);
393 }
394
395 +static void *encode_delta_size(void *data, unsigned long size)
396 +{
397 + unsigned char *cp = data;
398 + *cp++ = size;
399 + size >>= 7;
400 + while (size) {
401 + cp[-1] |= 0x80;
402 + *cp++ = size;
403 + size >>= 7;
404 + }
405 + return cp;
406 +}
407 +
408 +static void *safe_diff_delta(const unsigned char *src, unsigned long src_size,
409 + const unsigned char *dst, unsigned long dst_size,
410 + unsigned long *delta_size)
411 +{
412 + unsigned long bufsize;
413 + unsigned char *data;
414 + unsigned char *cp;
415 +
416 + if (src_size && dst_size)
417 + return diff_delta(src, src_size, dst, dst_size, delta_size, 0);
418 +
419 + /* diff-delta does not like to do delta with empty, so
420 + * we do that by hand here. Sigh...
421 + */
422 +
423 + if (!src_size)
424 + /* literal copy can be done only 127-byte at a time.
425 + */
426 + bufsize = dst_size + (dst_size / 127) + 40;
427 + else
428 + bufsize = 40;
429 + data = xmalloc(bufsize);
430 + cp = encode_delta_size(data, src_size);
431 + cp = encode_delta_size(cp, dst_size);
432 +
433 + if (dst_size) {
434 + /* copy out literally */
435 + while (dst_size) {
436 + int sz = (127 < dst_size) ? 127 : dst_size;
437 + *cp++ = sz;
438 + dst_size -= sz;
439 + while (sz) {
440 + *cp++ = *dst++;
441 + sz--;
442 + }
443 + }
444 + }
445 + *delta_size = (cp - data);
446 + return data;
447 +}
448 +
449 +static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
450 +{
451 + void *delta, *cp;
452 + unsigned long delta_size;
453 +
454 + printf("GIT binary patch\n");
455 + delta = safe_diff_delta(one->ptr, one->size,
456 + two->ptr, two->size,
457 + &delta_size);
458 + if (!delta)
459 + die("unable to generate binary diff");
460 +
461 + /* emit delta encoded in base85 */
462 + cp = delta;
463 + while (delta_size) {
464 + int bytes = (52 < delta_size) ? 52 : delta_size;
465 + char line[70];
466 + delta_size -= bytes;
467 + if (bytes <= 26)
468 + line[0] = bytes + 'A' - 1;
469 + else
470 + line[0] = bytes - 26 + 'a' - 1;
471 + encode_85(line + 1, cp, bytes);
472 + cp += bytes;
473 + puts(line);
474 + }
475 + printf("\n");
476 + free(delta);
477 +}
478 +
479 #define FIRST_FEW_BYTES 8000
480 static int mmfile_is_binary(mmfile_t *mf)
481 {
@@ -407,6 +492,7 @@ static void builtin_diff(const char *name_a,
492 struct diff_filespec *one,
493 struct diff_filespec *two,
494 const char *xfrm_msg,
495 + struct diff_options *o,
496 int complete_rewrite)
497 {
498 mmfile_t mf1, mf2;
@@ -451,8 +537,13 @@ static void builtin_diff(const char *name_a,
537 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
538 die("unable to read files to diff");
539
454 - if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
455 - printf("Binary files %s and %s differ\n", lbl[0], lbl[1]);
540 + if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) {
541 + if (o->full_index)
542 + emit_binary_diff(&mf1, &mf2);
543 + else
544 + printf("Binary files %s and %s differ\n",
545 + lbl[0], lbl[1]);
546 + }
547 else {
548 /* Crazy xdl interfaces.. */
549 const char *diffopts = getenv("GIT_DIFF_OPTS");
@@ -928,6 +1019,7 @@ static void run_diff_cmd(const char *pgm,
1019 struct diff_filespec *one,
1020 struct diff_filespec *two,
1021 const char *xfrm_msg,
1022 + struct diff_options *o,
1023 int complete_rewrite)
1024 {
1025 if (pgm) {
@@ -937,7 +1029,7 @@ static void run_diff_cmd(const char *pgm,
1029 }
1030 if (one && two)
1031 builtin_diff(name, other ? other : name,
940 - one, two, xfrm_msg, complete_rewrite);
1032 + one, two, xfrm_msg, o, complete_rewrite);
1033 else
1034 printf("* Unmerged path %s\n", name);
1035 }
@@ -971,7 +1063,7 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
1063
1064 if (DIFF_PAIR_UNMERGED(p)) {
1065 /* unmerged */
974 - run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, 0);
1066 + run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1067 return;
1068 }
1069
@@ -1041,14 +1133,14 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
1133 * needs to be split into deletion and creation.
1134 */
1135 struct diff_filespec *null = alloc_filespec(two->path);
1044 - run_diff_cmd(NULL, name, other, one, null, xfrm_msg, 0);
1136 + run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1137 free(null);
1138 null = alloc_filespec(one->path);
1047 - run_diff_cmd(NULL, name, other, null, two, xfrm_msg, 0);
1139 + run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1140 free(null);
1141 }
1142 else
1051 - run_diff_cmd(pgm, name, other, one, two, xfrm_msg,
1143 + run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1144 complete_rewrite);
1145
1146 free(name_munged);