Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5407709

Browse files
committed
diff: determine DIFFABLE-ness for binaries
Always set `GIT_DIFF_PATCH_DIFFABLE` for all files, regardless of binary-ness, so that the binary callback is invoked to either show the binary contents, or just print the standard "Binary files differ" message. We may need to do deeper inspection for binary files where we have avoided loading the contents into a file map.
1 parent ba8fb7c commit 5407709

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/diff_patch.c

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ GIT_INLINE(bool) should_skip_binary(git_patch *patch, git_diff_file *file)
121121
return (file->flags & GIT_DIFF_FLAG_BINARY) != 0;
122122
}
123123

124+
static bool diff_patch_diffable(git_patch *patch)
125+
{
126+
size_t olen, nlen;
127+
128+
if (patch->delta->status == GIT_DELTA_UNMODIFIED)
129+
return false;
130+
131+
/* if we've determined this to be binary (and we are not showing binary
132+
* data) then we have skipped loading the map data. instead, query the
133+
* file data itself.
134+
*/
135+
if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0 &&
136+
(patch->diff_opts.flags & GIT_DIFF_SHOW_BINARY) == 0) {
137+
olen = (size_t)patch->ofile.file->size;
138+
nlen = (size_t)patch->nfile.file->size;
139+
} else {
140+
olen = patch->ofile.map.len;
141+
nlen = patch->nfile.map.len;
142+
}
143+
144+
/* if both sides are empty, files are identical */
145+
if (!olen && !nlen)
146+
return false;
147+
148+
/* otherwise, check the file sizes and the oid */
149+
return (olen != nlen ||
150+
!git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id));
151+
}
152+
124153
static int diff_patch_load(git_patch *patch, git_diff_output *output)
125154
{
126155
int error = 0;
@@ -186,18 +215,7 @@ static int diff_patch_load(git_patch *patch, git_diff_output *output)
186215
diff_patch_update_binary(patch);
187216

188217
if (!error) {
189-
bool skip_binary =
190-
(patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0 &&
191-
(patch->diff_opts.flags & GIT_DIFF_SHOW_BINARY) == 0;
192-
193-
/* patch is diffable only for non-binary, modified files where
194-
* at least one side has data and the data actually changed
195-
*/
196-
if (!skip_binary &&
197-
patch->delta->status != GIT_DELTA_UNMODIFIED &&
198-
(patch->ofile.map.len || patch->nfile.map.len) &&
199-
(patch->ofile.map.len != patch->nfile.map.len ||
200-
!git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id)))
218+
if (diff_patch_diffable(patch))
201219
patch->flags |= GIT_DIFF_PATCH_DIFFABLE;
202220

203221
patch->flags |= GIT_DIFF_PATCH_LOADED;

src/diff_patch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ enum {
2424
GIT_DIFF_PATCH_ALLOCATED = (1 << 0),
2525
GIT_DIFF_PATCH_INITIALIZED = (1 << 1),
2626
GIT_DIFF_PATCH_LOADED = (1 << 2),
27+
/* the two sides are different */
2728
GIT_DIFF_PATCH_DIFFABLE = (1 << 3),
29+
/* the difference between the two sides has been computed */
2830
GIT_DIFF_PATCH_DIFFED = (1 << 4),
2931
GIT_DIFF_PATCH_FLATTENED = (1 << 5),
3032
};

0 commit comments

Comments
 (0)