merge-file: add an option to process object IDs
git merge-file knows how to merge files on the file system already. It would be helpful, however, to allow it to also merge single blobs. Teach it an `--object-id` option which means that its arguments are object IDs and not files to allow it to do so. We handle the empty blob specially since read_mmblob doesn't read it directly and otherwise users cannot specify an empty ancestor. Signed-off-by: brian m. carlson <bk2204@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Nov 1, 2023 at 19:24 UTC
e1068f0ad40f8e724513885e5a9ebb8548885e02
3 files changed
+123
-18
Documentation/git-merge-file.txt
+18
-1
@@ -11,7 +11,7 @@ SYNOPSIS
11
[verse]
12
'git merge-file' [-L <current-name> [-L <base-name> [-L <other-name>]]]
13
[--ours|--theirs|--union] [-p|--stdout] [-q|--quiet] [--marker-size=<n>]
14
- [--[no-]diff3] <current> <base> <other>
14
+ [--[no-]diff3] [--object-id] <current> <base> <other>
15
16
17
DESCRIPTION
@@ -41,6 +41,10 @@ however, these conflicts are resolved favouring lines from `<current>`,
41
lines from `<other>`, or lines from both respectively. The length of the
42
conflict markers can be given with the `--marker-size` option.
43
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
+
48
The exit value of this program is negative on error, and the number of
49
conflicts otherwise (truncated to 127 if there are more than that many
50
conflicts). If the merge was clean, the exit value is 0.
@@ -53,6 +57,14 @@ linkgit:git[1].
57
OPTIONS
58
-------
59
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
+
68
-L <label>::
69
This option may be given up to three times, and
70
specifies labels to be used in place of the
@@ -94,6 +106,11 @@ EXAMPLES
106
merges tmp/a123 and tmp/c345 with the base tmp/b234, but uses labels
107
`a` and `c` instead of `tmp/a123` and `tmp/c345`.
108
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
+
114
GIT
115
---
116
Part of the linkgit:git[1] suite
builtin/merge-file.c
+47
-17
@@ -1,5 +1,8 @@
1
#include "builtin.h"
2
#include "abspath.h"
3
+#include "hex.h"
4
+#include "object-name.h"
5
+#include "object-store.h"
6
#include "config.h"
7
#include "gettext.h"
8
#include "setup.h"
@@ -31,10 +34,11 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
34
mmfile_t mmfs[3] = { 0 };
35
mmbuffer_t result = { 0 };
36
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;
38
int quiet = 0;
39
struct option options[] = {
40
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")),
42
OPT_SET_INT(0, "diff3", &xmp.style, N_("use a diff3 based merge"), XDL_MERGE_DIFF3),
43
OPT_SET_INT(0, "zdiff3", &xmp.style, N_("use a zealous diff3 based merge"),
44
XDL_MERGE_ZEALOUS_DIFF3),
@@ -71,8 +75,12 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
75
return error_errno("failed to redirect stderr to /dev/null");
76
}
77
78
+ if (object_id)
79
+ setup_git_directory();
80
+
81
for (i = 0; i < 3; i++) {
82
char *fname;
83
+ struct object_id oid;
84
mmfile_t *mmf = mmfs + i;
85
86
if (!names[i])
@@ -80,12 +88,22 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
88
89
fname = prefix_filename(prefix, argv[i]);
90
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)) {
100
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))) {
104
ret = error("Cannot merge binary files: %s",
105
argv[i]);
106
+ }
107
108
free(fname);
109
if (ret)
@@ -99,20 +117,32 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
117
ret = xdl_merge(mmfs + 1, mmfs + 0, mmfs + 2, &xmp, &result);
118
119
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
+ }
145
free(result.ptr);
115
- free(fpath);
146
}
147
148
if (ret > 127)
t/t6403-merge-file.sh
+58
@@ -65,11 +65,30 @@ test_expect_success 'merge with no changes' '
65
test_cmp test.txt orig.txt
66
'
67
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
+
74
test_expect_success "merge without conflict" '
75
cp new1.txt test.txt &&
76
git merge-file test.txt orig.txt new2.txt
77
'
78
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
+
92
test_expect_success 'works in subdirectory' '
93
mkdir dir &&
94
cp new1.txt dir/a.txt &&
@@ -138,6 +157,31 @@ test_expect_success "expected conflict markers" '
157
test_cmp expect.txt test.txt
158
'
159
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
+
185
test_expect_success "merge conflicting with --ours" '
186
cp backup.txt test.txt &&
187
@@ -256,6 +300,14 @@ test_expect_success 'binary files cannot be merged' '
300
grep "Cannot merge binary files" merge.err
301
'
302
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
+
311
test_expect_success 'MERGE_ZEALOUS simplifies non-conflicts' '
312
sed -e "s/deerit.\$/deerit;/" -e "s/me;\$/me./" <new5.txt >new6.txt &&
313
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' '
441
test $(tr "\015" Q <nolf.txt | grep "^[<=>].*Q$" | wc -l) = 0
442
'
443
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
+
450
test_done