binary patch.
PATCH 1/1 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.
---
Makefile | 2 - apply.c | 232 ++++++++++++++++++++++++++++++++++++++++++++++---------------- cache.h | 3 + diff.c | 106 ++++++++++++++++++++++++++-- 4 files changed, 275 insertions(+), 68 deletions(-)
Makefile +1 -1
@@ -205,7 +205,7 @@ DIFF_OBJS = \ diffcore-delta.o log-tree.o LIB_OBJS = \ - blob.o commit.o connect.o csum-file.o \ + blob.o commit.o connect.o csum-file.o base85.o \ date.o diff-delta.o entry.o exec_cmd.o ident.o index.o \ object.o pack-check.o patch-delta.o path.o pkt-line.o \ quote.o read-cache.o refs.o run-command.o \
apply.c +172 -60
279 lines not on this page · raw message
cache.h +3 -0
@@ -363,4 +363,7 @@ extern int receive_keep_pack(int fd[2],/* pager.c */ extern void setup_pager (void ); +/* base85 */ +int decode_85 (char *dst,char *line,int linelen); +#endif /* CACHE_H */
diff.c +99 -7
159 lines not on this page · raw message
Signature · 1 line
1.3.1.g25a9
Quoted 2 lines · Junio C Hamano
This adds "binary patch" to the diff output and teaches apply what to do with them.
This is nice.
However I'd deflate the delta data before encoding it with base85.
Nicolas
Quoted 8 lines · Nicolas Pitre
Quoted 2 lines · Junio C Hamano
This adds "binary patch" to the diff output and teaches apply what to do with them.
This is nice.
However I'd deflate the delta data before encoding it with base85.
Yeah, things still to be done are deflating both delta and optionally perhaps use just deflate without delta for "new file" codepath.
And testsuite.
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Nicolas Pitre
This is nice.
However I'd deflate the delta data before encoding it with base85.
Yeah, things still to be done are deflating both delta and optionally perhaps use just deflate without delta for "new file" codepath.
And testsuite.
-- >8 --
[PATCH] binary diff: further updates.
This updates the user interface and generated diff data format.
* "diff --binary" is used to signal that we want an e-mailable binary patch. It implies --full-index and -p.
* "apply --allow-binary-replacement" acquired a short synonym "apply --binary".
* After the "GIT binary patch\n" header line there is a token to record which binary patch mechanism was used, so that we can extend it later. Currently there are two mechanisms
defined: "literal" and "delta". The former records the deflated postimage and the latter records the deflated delta from the preimage to postimage.
For purely implementation convenience, I added the deflated length after these "literal/delta" tokens (otherwise the decoding side needs to guess and reallocate the buffer while inflating). Improvement patches are very welcomed.
---
* Have done only very minimum testing, and unlike somebody else ;-) my code is not always perfect, so this will still stay out of "next" for a while.
apply.c | 130 +++++++++++++++++++++++++++++++++++++++++++++---------------- cache.h | 1 diff.c | 135 +++++++++++++++++++++++++++++++++------------------------------ diff.h | 1 4 files changed, 169 insertions(+), 98 deletions(-)
apply.c +97 -33
@@ -114,6 +114,9 @@ struct patch {char *new_name, *old_name, *def_name;unsigned int old_mode, new_mode;int is_rename, is_copy, is_new, is_delete, is_binary; +#define BINARY_DELTA_DEFLATED 1 +#define BINARY_LITERAL_DEFLATED 2 +unsigned long deflate_origlen;int lines_added, lines_deleted;int score;struct fragment *fragments;
@@ -969,9 +972,11 @@ static inline int metadata_changes(strucstatic int parse_binary (char *buffer,unsigned long size,struct patch *patch) { -/* We have read "GIT binary patch\n"; what follows is a -* sequence of 'length-byte' followed by base-85 encoded -* delta data. +/* We have read "GIT binary patch\n"; what follows is a line +* that says the patch method (currently, either "deflated +* literal" or "deflated delta") and the length of data before +* deflating; a sequence of 'length-byte' followed by base-85 +* encoded data follows. * * Each 5-byte sequence of base-85 encodes up to 4 bytes, * and we would limit the patch line to 66 characters,
@@ -982,11 +987,27 @@ static int parse_binary(char *buffer, un*/ int llen, used;struct fragment *fragment; -char *delta =NULL ; +char *data =NULL ; - patch->is_binary =1 ; patch->fragments = fragment =xcalloc (1 ,sizeof (*fragment)); - used =0 ; + +/* Grab the type of patch */ + llen =linelen (buffer, size); + used = llen; + linenr++; + +if (!strncmp (buffer,"delta " ,6 )) { + patch->is_binary =BINARY_DELTA_DEFLATED ; + patch->deflate_origlen =strtoul (buffer +6 ,NULL ,10 ); + } +else if (!strncmp (buffer,"literal " ,8 )) { + patch->is_binary =BINARY_LITERAL_DEFLATED ; + patch->deflate_origlen =strtoul (buffer +8 ,NULL ,10 ); + } +else +return error ("unrecognized binary patch at line %d: %.*s" , + linenr-1 , llen-1 , buffer); + buffer += llen;while (1 ) {int byte_length, max_byte_length, newsize; llen =linelen (buffer, size);
@@ -1015,8 +1036,8 @@ static int parse_binary(char *buffer, un byte_length <= max_byte_length - 4) goto corrupt; newsize = fragment->size + byte_length; - delta =xrealloc (delta, newsize); -if (decode_85 (delta + fragment->size, + data =xrealloc (data, newsize); +if (decode_85 (data + fragment->size, buffer +1 , byte_length)) goto corrupt;
@@ -1024,7 +1045,7 @@ static int parse_binary(char *buffer, un buffer += llen; size -= llen; } - fragment->patch = delta; + fragment->patch = data;return used; corrupt: return error("corrupt binary patch at line %d: %.*s",
@@ -1425,6 +1446,61 @@ #endifreturn offset; } +static char *inflate_it (const void *data,unsigned long size, +unsigned long inflated_size) +{ +z_stream stream; +void *out; +int st; + +memset (&stream,0 ,sizeof (stream)); + + stream.next_in = (unsigned char *)data; + stream.avail_in = size; + stream.next_out = out =xmalloc (inflated_size); + stream.avail_out = inflated_size; +inflateInit (&stream); + st =inflate (&stream,Z_FINISH ); +if ((st !=Z_STREAM_END ) || stream.total_out != inflated_size) { +free (out); +return NULL ; + } +return out; +} + +static int apply_binary_fragment (struct buffer_desc *desc,struct patch *patch) +{ +unsigned long dst_size; +struct fragment *fragment = patch->fragments; +void *data; +void *result; + + data =inflate_it (fragment->patch, fragment->size, + patch->deflate_origlen); +if (!data) +return error ("corrupt patch data" ); +switch (patch->is_binary) { +case BINARY_DELTA_DEFLATED : + result =patch_delta (desc->buffer, desc->size, + data, + patch->deflate_origlen, + &dst_size); +free (desc->buffer); + desc->buffer = result; +free (data); +break ; +case BINARY_LITERAL_DEFLATED : +free (desc->buffer); + desc->buffer = data; + dst_size = patch->deflate_origlen; +break ; + } +if (!desc->buffer) +return -1 ; + desc->size = desc->alloc = dst_size; +return 0 ; +} +static int apply_binary (struct buffer_desc *desc,struct patch *patch) {const char *name = patch->old_name ? patch->old_name : patch->new_name;
@@ -1466,18 +1542,20 @@ static int apply_binary(struct buffer_de "'%s' but it is not empty", name); } -if (desc->buffer) { +get_sha1_hex (patch->new_sha1_prefix, sha1); +if (!memcmp (sha1, null_sha1,20 )) {free (desc->buffer); desc->alloc = desc->size =0 ; - } -get_sha1_hex (patch->new_sha1_prefix, sha1); -if (!memcmp (sha1, null_sha1,20 )) + desc->buffer =NULL ;return 0 ;/* deletion patch */ + }if (has_sha1_file (sha1)) { +/* We already have the postimage */ char type[10 ];unsigned long size; +free (desc->buffer); desc->buffer =read_sha1_file (sha1, type, &size);if (!desc->buffer) return error("the necessary postimage %s for "
@@ -1486,28 +1564,13 @@ static int apply_binary(struct buffer_de desc->alloc = desc->size = size; }else { -char type[10 ]; -unsigned long src_size, dst_size; -void *src; - -get_sha1_hex (patch->old_sha1_prefix, sha1); - src =read_sha1_file (sha1, type, &src_size); -if (!src) -return error ("the necessary preimage %s for " -"'%s' cannot be read" , - patch->old_sha1_prefix, name); - -/* patch->fragment->patch has the delta data and -* we should apply it to the preimage. +/* We have verified desc matches the preimage; +* apply the patch data to it, which is stored +* in the patch->fragments->{patch,size}. */ - desc->buffer =patch_delta (src, src_size, - (void *) patch->fragments->patch, - patch->fragments->size, - &dst_size); -if (!desc->buffer) +if (apply_binary_fragment (desc, patch))return error ("binary patch does not apply to '%s'" , name); - desc->size = desc->alloc = dst_size;/* verify that the result matches */ write_sha1_file_prepare(desc->buffer, desc->size, blob_type,
@@ -2102,7 +2165,8 @@ int main(int argc, char **argv) diffstat =1 ;continue ; } -if (!strcmp (arg,"--allow-binary-replacement" )) { +if (!strcmp (arg,"--allow-binary-replacement" ) || + !strcmp (arg,"--binary" )) { allow_binary_replacement =1 ;continue ; }
cache.h +1 -0
@@ -365,5 +365,6 @@ extern void setup_pager(void);/* base85 */ int decode_85 (char *dst,char *line,int linelen); +void encode_85 (char *buf,unsigned char *data,int bytes);#endif /* CACHE_H */
diff.c +70 -65
168 lines not on this page · raw message
diff.h +1 -0
@@ -28,6 +28,7 @@ struct diff_options { with_raw:1, with_stat:1, tree_in_recursive:1, + binary:1, full_index:1, silent_on_remove:1, find_copies_harder:1;
Signature · 1 line
1.3.1.g25a9
Quoted 9 lines · Junio C Hamano
Quoted 5 lines
Yeah, things still to be done are deflating both delta and optionally perhaps use just deflate without delta for "new file" codepath.
And testsuite.
-- >8 --
[PATCH] binary diff: further updates.
...
Quoted 7 lines
Signed-off-by Junio C Hamano <[email protected]>---
* Have done only very minimum testing, and unlike somebody else ;-) my code is not always perfect, so this will still stay out of "next" for a while.
OK, now it passes the testsuite I wrote, so I'll push this out in "next". I was not drunk while doing the testsuite so the code now must be perfect ;-).
diff.c static void show_stats(struct diffstat_t · line 439
Quoted 13 lines · Junio C Hamano
Quoted 3 lines
Yeah, things still to be done are deflating both delta and optionally perhaps use just deflate without delta for "new file" codepath.
diff.c +9
-0+/* We could do deflated delta, or we could do just deflated two, +* whichever is smaller. +*/ + delta =NULL ; + deflated =deflate_it (two->ptr, two->size, &deflate_size); +if (one->size && two->size) { + delta =diff_delta (one->ptr, one->size, + two->ptr, two->size, + &delta_size, deflate_size);
Here you probably want to use deflate_size-1 (deflate_size can't be 0).
diff.c static void show_stats(struct diffstat_t · line 442
Quoted 3 lines
diff.c +3
-0+ orig_size = delta_size; +if (delta) + delta =deflate_it (delta, delta_size, &delta_size);
Here you're leaking the original delta buffer memory.
Nicolas
Quoted 8 lines · Nicolas Pitre
diff.c static void show_stats(struct diffstat_t · line 439
Quoted 6 lines
diff.c +6
-0+ delta =NULL ; + deflated =deflate_it (two->ptr, two->size, &deflate_size); +if (one->size && two->size) { + delta =diff_delta (one->ptr, one->size, + two->ptr, two->size, + &delta_size, deflate_size);Here you probably want to use deflate_size-1 (
deflate_sizecan't be 0).
I am not sure if -1 is worth here.
The delta is going to be deflated and hopefully gets a bit smaller, so if we really care that level of detail, it might be worth to do (deflate_size*3/2) or something like that here, use delta with or without deflate whichever is smaller, and mark the uncompressed delta with a different tag ("uncompressed delta"?). And for symmetry, to deal with uncompressible data, we may want to have "uncompressed literal" as well.
Quoted 5 lines
diff.c static void show_stats(struct diffstat_t · line 442
Quoted 3 lines
diff.c +3
-0+ orig_size = delta_size; +if (delta) + delta =deflate_it (delta, delta_size, &delta_size);Here you're leaking the original delta buffer memory.
Indeed. Thanks.
Quoted 13 lines · Junio C Hamano
Quoted 8 lines · Nicolas Pitre
diff.c static void show_stats(struct diffstat_t · line 439
Quoted 6 lines
diff.c +6
-0+ delta =NULL ; + deflated =deflate_it (two->ptr, two->size, &deflate_size); +if (one->size && two->size) { + delta =diff_delta (one->ptr, one->size, + two->ptr, two->size, + &delta_size, deflate_size);Here you probably want to use deflate_size-1 (
deflate_sizecan't be 0).I am not sure if -1 is worth here.
Quoted 7 lines
The delta is going to be deflated and hopefully gets a bit smaller, so if we really care that level of detail, it might be worth to do (deflate_size*3/2) or something like that here, use delta with or without deflate whichever is smaller, and mark the uncompressed delta with a different tag ("uncompressed delta"?). And for symmetry, to deal with uncompressible data, we may want to have "uncompressed literal" as well.
Nah... Please just forget that. ;-)
Nicolas
Quoted 11 lines · Nicolas Pitre
Quoted 7 lines · Junio C Hamano
The delta is going to be deflated and hopefully gets a bit smaller, so if we really care that level of detail, it might be worth to do (deflate_size*3/2) or something like that here, use delta with or without deflate whichever is smaller, and mark the uncompressed delta with a different tag ("uncompressed delta"?). And for symmetry, to deal with uncompressible data, we may want to have "uncompressed literal" as well.
Nah... Please just forget that. ;-)
I was serious about the above actually.
BTW, this "binary patch" opens a different can of worms.
Currently, the diff uses a heuristic borrowed from GNU diff (I did not look at the code when I did it, but it is described in its documentation) to decide if a file is binary (look at the first few bytes and find NUL). I am sure people will want to have a way to say "that heuristic fails but this _is_ a binary file and please treat it as such".
There are two, both valid, I think, ways to do it.
- give an option to "diff" that says "treat this path as binary for this invocation of the program".
- give an attribute to blob object that says "this blob is binary and should be treated as such".
The latter is probably the right way to go in the longer term.
A blob being binary or not is a property of the content and does not depend on where it sits in the history, so unlike "recording renames as a hint in commit objects", the attribute is at the blob level, not at the commit nor the tree that points at the blob.
But "binaryness" affects only certain operations that extract the data (e.g. diff and grep) and not others (e.g. fetch). Also, it makes sense to being able to retroactively mark a blob, which was not marked as such originally, is a binary. So I do not think it should be recorded in the object header.
Which suggests that we may perhaps want to have notes that can be attached to existing objects to augment them without changing the contents of the data, and have tools notice these notes when they are available. Another example is to associate correct MIME types to blobs so, gitweb _blob_ links can do sensible things to them.
These external notes are purely for Porcelains (in the context of this sentence "diff" and "grep" are Porcelain), but we would also want a way to propagate them across repositories somehow. In a sense, "grafts" information is similar to the external notes in that it augments existing commit objects, but its effect is a bit more intrusive; it affects the way the core operates.
Quoted 12 lines · Junio C Hamano
Quoted 10 lines · Nicolas Pitre
Quoted 7 lines · Junio C Hamano
The delta is going to be deflated and hopefully gets a bit smaller, so if we really care that level of detail, it might be worth to do (deflate_size*3/2) or something like that here, use delta with or without deflate whichever is smaller, and mark the uncompressed delta with a different tag ("uncompressed delta"?). And for symmetry, to deal with uncompressible data, we may want to have "uncompressed literal" as well.
Quoted 3 lines
Quoted 1 line
Nah... Please just forget that. ;-)
I was serious about the above actually.
And I think this is overkill.
First, if a deflated delta is to be _larger_ than its inflated version this is because the delta data is really really short, most probably shorter than a single base85 line. Same for literal data.
So I truely think the pretty special and rare case where not deflating might be smaller is simply not worth the added complexity.
Quoted 11 lines
BTW, this "binary patch" opens a different can of worms.
Currently, the diff uses a heuristic borrowed from GNU diff (I did not look at the code when I did it, but it is described in its documentation) to decide if a file is binary (look at the first few bytes and find NUL). I am sure people will want to have a way to say "that heuristic fails but this _is_ a binary file and please treat it as such".
There are two, both valid, I think, ways to do it.
Quoted 7 lines
- give an option to "diff" that says "treat this path as binary for this invocation of the program".
- give an attribute to blob object that says "this blob is binary and should be treated as such".
The latter is probably the right way to go in the longer term.
I'm not sure I agree.
Quoted 5 lines
A blob being binary or not is a property of the content and does not depend on where it sits in the history, so unlike "recording renames as a hint in commit objects", the attribute is at the blob level, not at the commit nor the tree that points at the blob.
Well, sort of.
Quoted 5 lines
But "binaryness" affects only certain operations that extract the data (e.g. diff and grep) and not others (e.g. fetch). Also, it makes sense to being able to retroactively mark a blob, which was not marked as such originally, is a binary. So I do not think it should be recorded in the object header.
Agreed.
Quoted 6 lines
Which suggests that we may perhaps want to have notes that can be attached to existing objects to augment them without changing the contents of the data, and have tools notice these notes when they are available. Another example is to associate correct MIME types to blobs so, gitweb _blob_ links can do sensible things to them.
I think blobs are the wrong level to attach such notes. If you go that path you'll have to add as many entries for the number of blobs many revisions of the same file might have.
Instead I think it should be attached to files. After all being a binary or not is a file attribute regardless of its revision. And implementation wise I'd do it as a .gitbin file listing all names of files that should be considered as binaries, with path globing and all, just like .gitignore currently lists files that should be ignored.
And the advantage is that those .gitbin files can be distributed and revision-controlled just like the .gitignore files.
And in addition to those files you could have a section in the repo config file listing default name patterns for files that are considered binaries. Or even a section, if present, that lists patterns for files that are _not_ binaries since that list might certainly be shorter. There could be a corresponding .gittext as well.
And in the absence of any of those then the default automatic euristic applies.
Nicolas
Quoted 5 lines · Junio C Hamano
But "binaryness" affects only certain operations that extract the data (e.g. diff and grep) and not others (e.g. fetch). Also, it makes sense to being able to retroactively mark a blob, which was not marked as such originally, is a binary. So I do not think it should be recorded in the object header.
Why do you think it makes sense to retroactively mark a blob with things like binariness or MIME type? To the extent that the information is not possible to extract from the blob contents, it seems to me to be a permanent aspect of the blob. And I could see having blobs with the same content but different type information (that one is a ZIP archive, while this one is a OpenDocument file), and tools may care how they were specified, and the user would want to be able to track how they had historically been marked, if the system allows them to be marked at all.
Of course, there's still the issue of how this info is generated for a new blob; I think it should live in the index for tracked files and come from a .gitignore-style file for new files. (For that matter, there could be a .gitmetadata file, which would handle "ignore" as well as binary and whatever other info you want to produce about your not-previously-tracked files.)
-Daniel
*This .sig left intentionally blank*
Quoted 17 lines · Daniel Barkalow
Quoted 5 lines · Junio C Hamano
But "binaryness" affects only certain operations that extract the data (e.g. diff and grep) and not others (e.g. fetch). Also, it makes sense to being able to retroactively mark a blob, which was not marked as such originally, is a binary. So I do not think it should be recorded in the object header.
Why do you think it makes sense to retroactively mark a blob with things like binariness or MIME type? To the extent that the information is not possible to extract from the blob contents, it seems to me to be a permanent aspect of the blob. And I could see having blobs with the same content but different type information (that one is a ZIP archive, while this one is a OpenDocument file), and tools may care how they were specified, and the user would want to be able to track how they had historically been marked, if the system allows them to be marked at all.
Quoted 6 lines
Of course, there's still the issue of how this info is generated for a new blob; I think it should live in the index for tracked files and come from a .gitignore-style file for new files. (For that matter, there could be a .gitmetadata file, which would handle "ignore" as well as binary and whatever other info you want to produce about your not-previously-tracked files.)
I think Nico's solution (compromise?) is the right and most practical one.