Skip to content

Commit 259e30d

Browse files
committed
Merge branch 'bc/merge-file-object-input'
"git merge-file" learns a mode to read three contents to be merged from blob objects. * bc/merge-file-object-input: merge-file: add an option to process object IDs git-merge-file doc: drop "-file" from argument placeholders
2 parents 57e216d + e1068f0 commit 259e30d

File tree

3 files changed

+133
-27
lines changed

3 files changed

+133
-27
lines changed

Documentation/git-merge-file.txt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ SYNOPSIS
1111
[verse]
1212
'git merge-file' [-L <current-name> [-L <base-name> [-L <other-name>]]]
1313
[--ours|--theirs|--union] [-p|--stdout] [-q|--quiet] [--marker-size=<n>]
14-
[--[no-]diff3] <current-file> <base-file> <other-file>
14+
[--[no-]diff3] [--object-id] <current> <base> <other>
1515

1616

1717
DESCRIPTION
1818
-----------
19-
'git merge-file' incorporates all changes that lead from the `<base-file>`
20-
to `<other-file>` into `<current-file>`. The result ordinarily goes into
21-
`<current-file>`. 'git merge-file' is useful for combining separate changes
22-
to an original. Suppose `<base-file>` is the original, and both
23-
`<current-file>` and `<other-file>` are modifications of `<base-file>`,
19+
Given three files `<current>`, `<base>` and `<other>`,
20+
'git merge-file' incorporates all changes that lead from `<base>`
21+
to `<other>` into `<current>`. The result ordinarily goes into
22+
`<current>`. 'git merge-file' is useful for combining separate changes
23+
to an original. Suppose `<base>` is the original, and both
24+
`<current>` and `<other>` are modifications of `<base>`,
2425
then 'git merge-file' combines both changes.
2526

26-
A conflict occurs if both `<current-file>` and `<other-file>` have changes
27+
A conflict occurs if both `<current>` and `<other>` have changes
2728
in a common segment of lines. If a conflict is found, 'git merge-file'
2829
normally outputs a warning and brackets the conflict with lines containing
2930
<<<<<<< and >>>>>>> markers. A typical conflict will look like this:
@@ -36,10 +37,14 @@ normally outputs a warning and brackets the conflict with lines containing
3637

3738
If there are conflicts, the user should edit the result and delete one of
3839
the alternatives. When `--ours`, `--theirs`, or `--union` option is in effect,
39-
however, these conflicts are resolved favouring lines from `<current-file>`,
40-
lines from `<other-file>`, or lines from both respectively. The length of the
40+
however, these conflicts are resolved favouring lines from `<current>`,
41+
lines from `<other>`, or lines from both respectively. The length of the
4142
conflict markers can be given with the `--marker-size` option.
4243

44+
If `--object-id` is specified, exactly the same behavior occurs, except that
45+
instead of specifying what to merge as files, it is specified as a list of
46+
object IDs referring to blobs.
47+
4348
The exit value of this program is negative on error, and the number of
4449
conflicts otherwise (truncated to 127 if there are more than that many
4550
conflicts). If the merge was clean, the exit value is 0.
@@ -52,6 +57,14 @@ linkgit:git[1].
5257
OPTIONS
5358
-------
5459

60+
--object-id::
61+
Specify the contents to merge as blobs in the current repository instead of
62+
files. In this case, the operation must take place within a valid repository.
63+
+
64+
If the `-p` option is specified, the merged file (including conflicts, if any)
65+
goes to standard output as normal; otherwise, the merged file is written to the
66+
object store and the object ID of its blob is written to standard output.
67+
5568
-L <label>::
5669
This option may be given up to three times, and
5770
specifies labels to be used in place of the
@@ -62,7 +75,7 @@ OPTIONS
6275

6376
-p::
6477
Send results to standard output instead of overwriting
65-
`<current-file>`.
78+
`<current>`.
6679

6780
-q::
6881
Quiet; do not warn about conflicts.
@@ -93,6 +106,11 @@ EXAMPLES
93106
merges tmp/a123 and tmp/c345 with the base tmp/b234, but uses labels
94107
`a` and `c` instead of `tmp/a123` and `tmp/c345`.
95108

109+
`git merge-file -p --object-id abc1234 def567 890abcd`::
110+
111+
combines the changes of the blob abc1234 and 890abcd since def567,
112+
tries to merge them and writes the result to standard output
113+
96114
GIT
97115
---
98116
Part of the linkgit:git[1] suite

builtin/merge-file.c

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "builtin.h"
22
#include "abspath.h"
3+
#include "hex.h"
4+
#include "object-name.h"
5+
#include "object-store.h"
36
#include "config.h"
47
#include "gettext.h"
58
#include "setup.h"
@@ -31,10 +34,11 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
3134
mmfile_t mmfs[3] = { 0 };
3235
mmbuffer_t result = { 0 };
3336
xmparam_t xmp = { 0 };
34-
int ret = 0, i = 0, to_stdout = 0;
37+
int ret = 0, i = 0, to_stdout = 0, object_id = 0;
3538
int quiet = 0;
3639
struct option options[] = {
3740
OPT_BOOL('p', "stdout", &to_stdout, N_("send results to standard output")),
41+
OPT_BOOL(0, "object-id", &object_id, N_("use object IDs instead of filenames")),
3842
OPT_SET_INT(0, "diff3", &xmp.style, N_("use a diff3 based merge"), XDL_MERGE_DIFF3),
3943
OPT_SET_INT(0, "zdiff3", &xmp.style, N_("use a zealous diff3 based merge"),
4044
XDL_MERGE_ZEALOUS_DIFF3),
@@ -71,21 +75,35 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
7175
return error_errno("failed to redirect stderr to /dev/null");
7276
}
7377

78+
if (object_id)
79+
setup_git_directory();
80+
7481
for (i = 0; i < 3; i++) {
7582
char *fname;
83+
struct object_id oid;
7684
mmfile_t *mmf = mmfs + i;
7785

7886
if (!names[i])
7987
names[i] = argv[i];
8088

8189
fname = prefix_filename(prefix, argv[i]);
8290

83-
if (read_mmfile(mmf, fname))
91+
if (object_id) {
92+
if (repo_get_oid(the_repository, argv[i], &oid))
93+
ret = error(_("object '%s' does not exist"),
94+
argv[i]);
95+
else if (!oideq(&oid, the_hash_algo->empty_blob))
96+
read_mmblob(mmf, &oid);
97+
else
98+
read_mmfile(mmf, "/dev/null");
99+
} else if (read_mmfile(mmf, fname)) {
84100
ret = -1;
85-
else if (mmf->size > MAX_XDIFF_SIZE ||
86-
buffer_is_binary(mmf->ptr, mmf->size))
101+
}
102+
if (ret != -1 && (mmf->size > MAX_XDIFF_SIZE ||
103+
buffer_is_binary(mmf->ptr, mmf->size))) {
87104
ret = error("Cannot merge binary files: %s",
88105
argv[i]);
106+
}
89107

90108
free(fname);
91109
if (ret)
@@ -99,20 +117,32 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
99117
ret = xdl_merge(mmfs + 1, mmfs + 0, mmfs + 2, &xmp, &result);
100118

101119
if (ret >= 0) {
102-
const char *filename = argv[0];
103-
char *fpath = prefix_filename(prefix, argv[0]);
104-
FILE *f = to_stdout ? stdout : fopen(fpath, "wb");
105-
106-
if (!f)
107-
ret = error_errno("Could not open %s for writing",
108-
filename);
109-
else if (result.size &&
110-
fwrite(result.ptr, result.size, 1, f) != 1)
111-
ret = error_errno("Could not write to %s", filename);
112-
else if (fclose(f))
113-
ret = error_errno("Could not close %s", filename);
120+
if (object_id && !to_stdout) {
121+
struct object_id oid;
122+
if (result.size) {
123+
if (write_object_file(result.ptr, result.size, OBJ_BLOB, &oid) < 0)
124+
ret = error(_("Could not write object file"));
125+
} else {
126+
oidcpy(&oid, the_hash_algo->empty_blob);
127+
}
128+
if (ret >= 0)
129+
printf("%s\n", oid_to_hex(&oid));
130+
} else {
131+
const char *filename = argv[0];
132+
char *fpath = prefix_filename(prefix, argv[0]);
133+
FILE *f = to_stdout ? stdout : fopen(fpath, "wb");
134+
135+
if (!f)
136+
ret = error_errno("Could not open %s for writing",
137+
filename);
138+
else if (result.size &&
139+
fwrite(result.ptr, result.size, 1, f) != 1)
140+
ret = error_errno("Could not write to %s", filename);
141+
else if (fclose(f))
142+
ret = error_errno("Could not close %s", filename);
143+
free(fpath);
144+
}
114145
free(result.ptr);
115-
free(fpath);
116146
}
117147

118148
if (ret > 127)

t/t6403-merge-file.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,30 @@ test_expect_success 'merge with no changes' '
6565
test_cmp test.txt orig.txt
6666
'
6767

68+
test_expect_success 'merge with no changes with --object-id' '
69+
git add orig.txt &&
70+
git merge-file -p --object-id :orig.txt :orig.txt :orig.txt >actual &&
71+
test_cmp actual orig.txt
72+
'
73+
6874
test_expect_success "merge without conflict" '
6975
cp new1.txt test.txt &&
7076
git merge-file test.txt orig.txt new2.txt
7177
'
7278

79+
test_expect_success 'merge without conflict with --object-id' '
80+
git add orig.txt new2.txt &&
81+
git merge-file --object-id :orig.txt :orig.txt :new2.txt >actual &&
82+
git rev-parse :new2.txt >expected &&
83+
test_cmp actual expected
84+
'
85+
86+
test_expect_success 'can accept object ID with --object-id' '
87+
git merge-file --object-id $(test_oid empty_blob) $(test_oid empty_blob) :new2.txt >actual &&
88+
git rev-parse :new2.txt >expected &&
89+
test_cmp actual expected
90+
'
91+
7392
test_expect_success 'works in subdirectory' '
7493
mkdir dir &&
7594
cp new1.txt dir/a.txt &&
@@ -138,6 +157,31 @@ test_expect_success "expected conflict markers" '
138157
test_cmp expect.txt test.txt
139158
'
140159

160+
test_expect_success "merge with conflicts with --object-id" '
161+
git add backup.txt orig.txt new3.txt &&
162+
test_must_fail git merge-file -p --object-id :backup.txt :orig.txt :new3.txt >actual &&
163+
sed -e "s/<< test.txt/<< :backup.txt/" \
164+
-e "s/>> new3.txt/>> :new3.txt/" \
165+
expect.txt >expect &&
166+
test_cmp expect actual &&
167+
test_must_fail git merge-file --object-id :backup.txt :orig.txt :new3.txt >oid &&
168+
git cat-file blob "$(cat oid)" >actual &&
169+
test_cmp expect actual
170+
'
171+
172+
test_expect_success "merge with conflicts with --object-id with labels" '
173+
git add backup.txt orig.txt new3.txt &&
174+
test_must_fail git merge-file -p --object-id \
175+
-L test.txt -L orig.txt -L new3.txt \
176+
:backup.txt :orig.txt :new3.txt >actual &&
177+
test_cmp expect.txt actual &&
178+
test_must_fail git merge-file --object-id \
179+
-L test.txt -L orig.txt -L new3.txt \
180+
:backup.txt :orig.txt :new3.txt >oid &&
181+
git cat-file blob "$(cat oid)" >actual &&
182+
test_cmp expect.txt actual
183+
'
184+
141185
test_expect_success "merge conflicting with --ours" '
142186
cp backup.txt test.txt &&
143187
@@ -256,6 +300,14 @@ test_expect_success 'binary files cannot be merged' '
256300
grep "Cannot merge binary files" merge.err
257301
'
258302

303+
test_expect_success 'binary files cannot be merged with --object-id' '
304+
cp "$TEST_DIRECTORY"/test-binary-1.png . &&
305+
git add orig.txt new1.txt test-binary-1.png &&
306+
test_must_fail git merge-file --object-id \
307+
:orig.txt :test-binary-1.png :new1.txt 2> merge.err &&
308+
grep "Cannot merge binary files" merge.err
309+
'
310+
259311
test_expect_success 'MERGE_ZEALOUS simplifies non-conflicts' '
260312
sed -e "s/deerit.\$/deerit;/" -e "s/me;\$/me./" <new5.txt >new6.txt &&
261313
sed -e "s/deerit.\$/deerit,/" -e "s/me;\$/me,/" <new5.txt >new7.txt &&
@@ -389,4 +441,10 @@ test_expect_success 'conflict sections match existing line endings' '
389441
test $(tr "\015" Q <nolf.txt | grep "^[<=>].*Q$" | wc -l) = 0
390442
'
391443

444+
test_expect_success '--object-id fails without repository' '
445+
empty="$(test_oid empty_blob)" &&
446+
nongit test_must_fail git merge-file --object-id $empty $empty $empty 2>err &&
447+
grep "not a git repository" err
448+
'
449+
392450
test_done

0 commit comments

Comments
 (0)