git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
d61288572300d125375dc847a505cfa00fc75fff blob 7170 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
 
/*
 * Builtin "git merge-one-file"
 *
 * Copyright (c) 2020 Alban Gruin
 *
 * Based on git-merge-one-file.sh, written by Linus Torvalds.
 *
 * This is the git per-file merge script, called with
 *
 *   $1 - original file SHA1 (or empty)
 *   $2 - file in branch1 SHA1 (or empty)
 *   $3 - file in branch2 SHA1 (or empty)
 *   $4 - pathname in repository
 *   $5 - original file mode (or empty)
 *   $6 - file in branch1 mode (or empty)
 *   $7 - file in branch2 mode (or empty)
 *
 * Handle some trivial cases.. The _really_ trivial cases have
 * been handled already by git read-tree, but that one doesn't
 * do any merges that might change the tree layout.
 */

#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h"
#include "builtin.h"
#include "commit.h"
#include "dir.h"
#include "lockfile.h"
#include "object-store.h"
#include "xdiff-interface.h"

static int add_to_index_cacheinfo(unsigned int mode,
				  const struct object_id *oid, const char *path)
{
	struct cache_entry *ce;
	int len, option;

	if (!verify_path(path, mode))
		return error("Invalid path '%s'", path);

	len = strlen(path);
	ce = make_empty_cache_entry(&the_index, len);

	oidcpy(&ce->oid, oid);
	memcpy(ce->name, path, len);
	ce->ce_flags = create_ce_flags(0);
	ce->ce_namelen = len;
	ce->ce_mode = create_ce_mode(mode);
	if (assume_unchanged)
		ce->ce_flags |= CE_VALID;
	option = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
	if (add_cache_entry(ce, option))
		return error("%s: cannot add to the index", path);

	return 0;
}

static int checkout_from_index(const char *path)
{
	struct checkout state;
	struct cache_entry *ce;

	state.istate = &the_index;
	state.force = 1;
	state.base_dir = "";
	state.base_dir_len = 0;

	ce = cache_file_exists(path, strlen(path), 0);
	if (checkout_entry(ce, &state, NULL, NULL) < 0)
		return error("%s: cannot checkout file", path);
	return 0;
}

static int merge_one_file_deleted(const struct object_id *orig_blob,
				  const struct object_id *our_blob,
				  const struct object_id *their_blob, const char *path,
				  unsigned int orig_mode, unsigned int our_mode, unsigned int their_mode)
{
	if ((our_blob && orig_mode != our_mode) ||
	    (their_blob && orig_mode != their_mode))
		return error(_("File %s deleted on one branch but had its "
			       "permissions changed on the other."), path);

	if (our_blob) {
		printf("Removing %s\n", path);

		if (file_exists(path))
			remove_path(path);
	}

	if (remove_file_from_cache(path))
		return error("%s: cannot remove from the index", path);
	return 0;
}

static int do_merge_one_file(const struct object_id *orig_blob,
			     const struct object_id *our_blob,
			     const struct object_id *their_blob, const char *path,
			     unsigned int orig_mode, unsigned int our_mode, unsigned int their_mode)
{
	int ret, i, dest;
	mmbuffer_t result = {NULL, 0};
	mmfile_t mmfs[3];
	xmparam_t xmp = {{0}};
	struct cache_entry *ce;

	if (our_mode == S_IFLNK || their_mode == S_IFLNK)
		return error(_("%s: Not merging symbolic link changes."), path);
	else if (our_mode == S_IFGITLINK || their_mode == S_IFGITLINK)
		return error(_("%s: Not merging conflicting submodule changes."), path);

	read_mmblob(mmfs + 0, our_blob);
	read_mmblob(mmfs + 2, their_blob);

	if (orig_blob) {
		printf("Auto-merging %s\n", path);
		read_mmblob(mmfs + 1, orig_blob);
	} else {
		printf("Added %s in both, but differently.\n", path);
		read_mmblob(mmfs + 1, the_hash_algo->empty_blob);
	}

	xmp.level = XDL_MERGE_ZEALOUS_ALNUM;
	xmp.style = 0;
	xmp.favor = 0;

	ret = xdl_merge(mmfs + 1, mmfs + 0, mmfs + 2, &xmp, &result);

	for (i = 0; i < 3; i++)
		free(mmfs[i].ptr);

	if (ret > 127)
		ret = 1;

	ce = cache_file_exists(path, strlen(path), 0);
	if (!ce)
		BUG("file is not present in the cache?");

	unlink(path);
	dest = open(path, O_WRONLY | O_CREAT, ce->ce_mode);
	write_in_full(dest, result.ptr, result.size);
	close(dest);

	free(result.ptr);

	if (ret) {
		if (!orig_blob)
			error(_("content conflict in %s"), path);
		if (our_mode != their_mode)
			error(_("permission conflict: %o->%o,%o in %s"),
			      orig_mode, our_mode, their_mode, path);

		return 1;
	}

	return add_file_to_cache(path, 0);
}

static int merge_one_file(const struct object_id *orig_blob,
			  const struct object_id *our_blob,
			  const struct object_id *their_blob, const char *path,
			  unsigned int orig_mode, unsigned int our_mode, unsigned int their_mode)
{
	if (orig_blob &&
	    ((our_blob && oideq(orig_blob, our_blob)) ||
	     (their_blob && oideq(orig_blob, their_blob))))
		return merge_one_file_deleted(orig_blob, our_blob, their_blob, path,
					      orig_mode, our_mode, their_mode);
	else if (!orig_blob && our_blob && !their_blob) {
		return add_to_index_cacheinfo(our_mode, our_blob, path);
	} else if (!orig_blob && !our_blob && their_blob) {
		printf("Adding %s\n", path);

		if (file_exists(path))
			return error(_("untracked %s is overwritten by the merge."), path);

		if (add_to_index_cacheinfo(their_mode, their_blob, path))
			return 1;
		return checkout_from_index(path);
	} else if (!orig_blob && our_blob && their_blob &&
		   oideq(our_blob, their_blob)) {
		if (our_mode != their_mode)
			return error(_("File %s added identically in both branches, "
				       "but permissions conflict %o->%o."),
				     path, our_mode, their_mode);

		printf("Adding %s\n", path);

		if (add_to_index_cacheinfo(our_mode, our_blob, path))
			return 1;
		return checkout_from_index(path);
	} else if (our_blob && their_blob)
		return do_merge_one_file(orig_blob, our_blob, their_blob, path,
					 orig_mode, our_mode, their_mode);
	else {
		char *orig_hex = "", *our_hex = "", *their_hex = "";

		if (orig_blob)
			orig_hex = oid_to_hex(orig_blob);
		if (our_blob)
			our_hex = oid_to_hex(our_blob);
		if (their_blob)
			their_hex = oid_to_hex(their_blob);

		return error(_("%s: Not handling case %s -> %s -> %s"),
			path, orig_hex, our_hex, their_hex);
	}

	return 0;
}

static const char builtin_merge_one_file_usage[] =
	"git merge-one-file <orig blob> <our blob> <their blob> <path> "
	"<orig mode> <our mode> <their mode>\n\n"
	"Blob ids and modes should be empty for missing files.";

int cmd_merge_one_file(int argc, const char **argv, const char *prefix)
{
	struct object_id orig_blob, our_blob, their_blob,
		*p_orig_blob = NULL, *p_our_blob = NULL, *p_their_blob = NULL;
	unsigned int orig_mode = 0, our_mode = 0, their_mode = 0, ret;
	struct lock_file lock = LOCK_INIT;

	if (argc != 8)
		usage(builtin_merge_one_file_usage);

	if (read_cache() < 0)
		die("invalid index");

	hold_locked_index(&lock, LOCK_DIE_ON_ERROR);

	if (!get_oid(argv[1], &orig_blob)) {
		p_orig_blob = &orig_blob;
		orig_mode = strtol(argv[5], NULL, 8);
	}

	if (!get_oid(argv[2], &our_blob)) {
		p_our_blob = &our_blob;
		our_mode = strtol(argv[6], NULL, 8);
	}

	if (!get_oid(argv[3], &their_blob)) {
		p_their_blob = &their_blob;
		their_mode = strtol(argv[7], NULL, 8);
	}

	ret = merge_one_file(p_orig_blob, p_our_blob, p_their_blob, argv[4],
			     orig_mode, our_mode, their_mode);

	if (ret) {
		rollback_lock_file(&lock);
		return ret;
	}

	return write_locked_index(&the_index, &lock, COMMIT_LOCK);
}
debug log:

solving d612885723 ...
found d612885723 in https://public-inbox.org/git/20200625121953.16991-5-alban.gruin@gmail.com/
found d9ebd820cb in https://public-inbox.org/git/20200625121953.16991-4-alban.gruin@gmail.com/
found 4992a6cd30 in https://public-inbox.org/git/20200625121953.16991-3-alban.gruin@gmail.com/

applying [1/3] https://public-inbox.org/git/20200625121953.16991-3-alban.gruin@gmail.com/
diff --git a/builtin/merge-one-file.c b/builtin/merge-one-file.c
new file mode 100644
index 0000000000..4992a6cd30


applying [2/3] https://public-inbox.org/git/20200625121953.16991-4-alban.gruin@gmail.com/
diff --git a/builtin/merge-one-file.c b/builtin/merge-one-file.c
index 4992a6cd30..d9ebd820cb 100644


applying [3/3] https://public-inbox.org/git/20200625121953.16991-5-alban.gruin@gmail.com/
diff --git a/builtin/merge-one-file.c b/builtin/merge-one-file.c
index d9ebd820cb..d612885723 100644

Checking patch builtin/merge-one-file.c...
Applied patch builtin/merge-one-file.c cleanly.
Checking patch builtin/merge-one-file.c...
Applied patch builtin/merge-one-file.c cleanly.
Checking patch builtin/merge-one-file.c...
Applied patch builtin/merge-one-file.c cleanly.

index at:
100644 d61288572300d125375dc847a505cfa00fc75fff	builtin/merge-one-file.c

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).