Skip to content

Commit c76fcf3

Browse files
committed
Merge branch 'jc/trivial-threeway-binary-merge'
The "git apply -3" code path learned not to bother the lower level merge machinery when the three-way merge can be trivially resolved without the content level merge. * jc/trivial-threeway-binary-merge: apply: resolve trivial merge without hitting ll-merge with "--3way"
2 parents f696272 + 57f183b commit c76fcf3

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

apply.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3468,6 +3468,21 @@ static int load_preimage(struct apply_state *state,
34683468
return 0;
34693469
}
34703470

3471+
static int resolve_to(struct image *image, const struct object_id *result_id)
3472+
{
3473+
unsigned long size;
3474+
enum object_type type;
3475+
3476+
clear_image(image);
3477+
3478+
image->buf = read_object_file(result_id, &type, &size);
3479+
if (!image->buf || type != OBJ_BLOB)
3480+
die("unable to read blob object %s", oid_to_hex(result_id));
3481+
image->len = size;
3482+
3483+
return 0;
3484+
}
3485+
34713486
static int three_way_merge(struct apply_state *state,
34723487
struct image *image,
34733488
char *path,
@@ -3479,6 +3494,12 @@ static int three_way_merge(struct apply_state *state,
34793494
mmbuffer_t result = { NULL };
34803495
int status;
34813496

3497+
/* resolve trivial cases first */
3498+
if (oideq(base, ours))
3499+
return resolve_to(image, theirs);
3500+
else if (oideq(base, theirs) || oideq(ours, theirs))
3501+
return resolve_to(image, ours);
3502+
34823503
read_mmblob(&base_file, base);
34833504
read_mmblob(&our_file, ours);
34843505
read_mmblob(&their_file, theirs);

t/t4108-apply-threeway.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,49 @@ test_expect_success 'apply with --3way --cached and conflicts' '
230230
test_cmp expect.diff actual.diff
231231
'
232232

233+
test_expect_success 'apply binary file patch' '
234+
git reset --hard main &&
235+
cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
236+
git add bin.png &&
237+
git commit -m "add binary file" &&
238+
239+
cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
240+
241+
git diff --binary >bin.diff &&
242+
git reset --hard &&
243+
244+
# Apply must succeed.
245+
git apply bin.diff
246+
'
247+
248+
test_expect_success 'apply binary file patch with 3way' '
249+
git reset --hard main &&
250+
cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
251+
git add bin.png &&
252+
git commit -m "add binary file" &&
253+
254+
cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
255+
256+
git diff --binary >bin.diff &&
257+
git reset --hard &&
258+
259+
# Apply must succeed.
260+
git apply --3way --index bin.diff
261+
'
262+
263+
test_expect_success 'apply full-index patch with 3way' '
264+
git reset --hard main &&
265+
cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
266+
git add bin.png &&
267+
git commit -m "add binary file" &&
268+
269+
cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
270+
271+
git diff --full-index >bin.diff &&
272+
git reset --hard &&
273+
274+
# Apply must succeed.
275+
git apply --3way --index bin.diff
276+
'
277+
233278
test_done

0 commit comments

Comments
 (0)