Skip to contentSkip to conversation

Avoid run_command() for recursive in builtin-merge

PATCH In review Miklos Vajna 5 participants 38 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Avoid run_command() for recursive in builtin-merge

Hi,

The idea (by Dscho) is simple: builtin-merge-recursive already has a merge_recursive() function that is _almost_ ready to be called from builtins without fork+exec, so make use of it.

And yes, of course this is not for 1.6.0, I just wanted to send this out before the "suggested 'pencils down' date" of GSoC.

Miklos Vajna (2):
merge-recursive: prepare merge_recursive() to be called from builtins
builtin-merge: avoid run_command_v_opt() for recursive

 builtin-merge-recursive.c |   19 +++++++---
 builtin-merge.c           |   82 +++++++++++++++++++++++++++++++--------------
 2 files changed, 70 insertions(+), 31 deletions(-)
#1 lore raw

PATCH 1/2 merge-recursive: prepare merge_recursive() to be called from builtins

When other builtins call merge_recursive(), they would have to handle the GIT_MERGE_VERBOSITY environment variable, causing a code duplication. Same story for the git_config() call. It's better to do it when merge_recursive() is called the first time.

---
 builtin-merge-recursive.c |   19 +++++++++++++------
 1 files changed, 13 insertions(+), 6 deletions(-)
builtin-merge-recursive.c +13 -6
index 43e55bf..09aa830 100644
@@ -1218,6 +1218,8 @@ static struct commit_list *reverse_commit_list(struct commit_list *list)
 	return next;
 }
 
+static int merge_config(const char *var, const char *value, void *cb);
+
 /*
  * Merge the commits h1 and h2, return the resulting virtual
  * commit object and a flag indicating the cleanness of the merge.
@@ -1233,6 +1235,17 @@ int merge_recursive(struct commit *h1,
 	struct commit *merged_common_ancestors;
 	struct tree *mrtree = mrtree;
 	int clean;
+	static int initial = 1;
+
+	if (initial) {
+		git_config(merge_config, NULL);
+		if (getenv("GIT_MERGE_VERBOSITY"))
+			verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
+		if (verbosity >= 5)
+			buffer_output = 0;
+
+		initial = 0;
+	}
 
 	if (show(4)) {
 		output(4, "Merging:");
@@ -1369,10 +1382,6 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
 			subtree_merge = 1;
 	}
 
-	git_config(merge_config, NULL);
-	if (getenv("GIT_MERGE_VERBOSITY"))
-		verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
-
 	if (argc < 4)
 		die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
 
@@ -1384,8 +1393,6 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
 	}
 	if (argc - i != 3) /* "--" "<head>" "<remote>" */
 		die("Not handling anything other than two heads merge.");
-	if (verbosity >= 5)
-		buffer_output = 0;
 
 	branch1 = argv[++i];
 	branch2 = argv[++i];
Signature · 1 line
1.6.0.rc0.14.g95f8.dirty
#2 lore raw

PATCH 2/2 builtin-merge: avoid run_command_v_opt() for recursive

The try_merge_strategy() function always ran the strategy in a separate process, though this is not always necessary. The recursive strategy can be called without a fork(). This patch adds a check, and calls recursive in the same process without wasting resources.

---

Adding the signoff of Dscho as well, as he wrote the last hunk.

 builtin-merge.c |   82 ++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 57 insertions(+), 25 deletions(-)
builtin-merge.c +57 -25
index dde0c7e..2789a78 100644
@@ -22,6 +22,7 @@
 #include "log-tree.h"
 #include "color.h"
 #include "rerere.h"
+#include "merge-recursive.h"
 
 #define DEFAULT_TWOHEAD (1<<0)
 #define DEFAULT_OCTOPUS (1<<1)
@@ -511,28 +512,55 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
 	struct commit_list *j;
 	struct strbuf buf;
 
-	args = xmalloc((4 + commit_list_count(common) +
-			commit_list_count(remoteheads)) * sizeof(char *));
-	strbuf_init(&buf, 0);
-	strbuf_addf(&buf, "merge-%s", strategy);
-	args[i++] = buf.buf;
-	for (j = common; j; j = j->next)
-		args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
-	args[i++] = "--";
-	args[i++] = head_arg;
-	for (j = remoteheads; j; j = j->next)
-		args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
-	args[i] = NULL;
-	ret = run_command_v_opt(args, RUN_GIT_CMD);
-	strbuf_release(&buf);
-	i = 1;
-	for (j = common; j; j = j->next)
-		free((void *)args[i++]);
-	i += 2;
-	for (j = remoteheads; j; j = j->next)
-		free((void *)args[i++]);
-	free(args);
-	return -ret;
+	if (!strcmp(strategy, "recursive")) {
+		int clean;
+		struct commit *result;
+		struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
+		int index_fd;
+		struct commit_list *reversed = NULL;
+
+		if (remoteheads->next) {
+			error("Not handling anything other than two heads merge.");
+			return 2;
+		}
+
+		for (j = common; j; j = j->next)
+			commit_list_insert(j->item, &reversed);
+
+		index_fd = hold_locked_index(lock, 1);
+		clean = merge_recursive(lookup_commit(head),
+				remoteheads->item, head_arg,
+				(const char *)remoteheads->item->util,
+				reversed, &result);
+		if (active_cache_changed &&
+				(write_cache(index_fd, active_cache, active_nr) ||
+				 commit_locked_index(lock)))
+			die ("unable to write %s", get_index_file());
+		return clean ? 0 : 1;
+	} else {
+		args = xmalloc((4 + commit_list_count(common) +
+					commit_list_count(remoteheads)) * sizeof(char *));
+		strbuf_init(&buf, 0);
+		strbuf_addf(&buf, "merge-%s", strategy);
+		args[i++] = buf.buf;
+		for (j = common; j; j = j->next)
+			args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
+		args[i++] = "--";
+		args[i++] = head_arg;
+		for (j = remoteheads; j; j = j->next)
+			args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
+		args[i] = NULL;
+		ret = run_command_v_opt(args, RUN_GIT_CMD);
+		strbuf_release(&buf);
+		i = 1;
+		for (j = common; j; j = j->next)
+			free((void *)args[i++]);
+		i += 2;
+		for (j = remoteheads; j; j = j->next)
+			free((void *)args[i++]);
+		free(args);
+		return -ret;
+	}
 }
 
 static void count_diff_files(struct diff_queue_struct *q,
@@ -670,7 +698,9 @@ static int finish_automerge(struct commit_list *common,
 	struct strbuf buf = STRBUF_INIT;
 	unsigned char result_commit[20];
 
-	free_commit_list(common);
+	if (strcmp(wt_strategy, "recursive"))
+		/* recursive already freed it */
+		free_commit_list(common);
 	if (allow_fast_forward) {
 		parents = remoteheads;
 		commit_list_insert(lookup_commit(head), &parents);
@@ -873,12 +903,14 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 
 	for (i = 0; i < argc; i++) {
 		struct object *o;
+		struct commit *commit;
 
 		o = peel_to_type(argv[i], 0, NULL, OBJ_COMMIT);
 		if (!o)
 			die("%s - not something we can merge", argv[i]);
-		remotes = &commit_list_insert(lookup_commit(o->sha1),
-			remotes)->next;
+		commit = lookup_commit(o->sha1);
+		commit->util = (void *)argv[i];
+		remotes = &commit_list_insert(commit, remotes)->next;
 
 		strbuf_addf(&buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
 		setenv(buf.buf, argv[i], 1);
Signature · 1 line
1.6.0.rc0.14.g95f8.dirty
#3 lore raw

Re: [PATCH 2/2] builtin-merge: avoid run_command_v_opt() for recursive

Quoted 4 lines · Miklos Vajna

The try_merge_strategy() function always ran the strategy in a separate process, though this is not always necessary. The recursive strategy can be called without a fork(). This patch adds a check, and calls recursive in the same process without wasting resources.

Yes, it saves a fork, but is this really worth it in the bigger picture?

Doesn't the current code structure have benefit of allowing git-merge itself do necessary clean-up action when merge-recursive calls any of the die() it has in many places?

#4 lore raw

Re: [PATCH 2/2] builtin-merge: avoid run_command_v_opt() for recursive

Quoted 5 lines · Junio C Hamano

Yes, it saves a fork, but is this really worth it in the bigger picture?

Doesn't the current code structure have benefit of allowing git-merge itself do necessary clean-up action when merge-recursive calls any of the die() it has in many places?

As far as I see in most cases merge-recursive does not call die().

Cases when it does are like:

- broken snprintf
- cache_tree_fully_valid() or cache_tree_update() fails
- diff_setup_done() fails
- flush_buffer() fails
- read_sha1_file() fails
- ll_merge() fails

etc.

In short, I think there are two cases when a die() would be problematic inside merge-recursive when calling it from builtin-merge:

- merge-recursive can't handle a merge, but an other strategy could do. This is the case when doing an octopus merge but in that case merge_recursive() is not called at all.

- merge-recursive results in conflicts, but an other strategy could handle the merge without conflicts. In this case die() isn't used either, so this will not be a problem.

So I don't think there is a case when a die() inside merge-recursive would occur, but an other strategy would handle the merge properly.

Unless I missed something. ;-)

#5 lore raw

Re: [PATCH 2/2] builtin-merge: avoid run_command_v_opt() for recursive

Quoted 2 lines · Miklos Vajna

So I don't think there is a case when a die() inside merge-recursive would occur, but an other strategy would handle the merge properly.

Normal case is fine.

I was not worried too much about "the other strategy will" case, but isn't the general structure of git-merge driver be:

- do some set-up computation, and leave info in .git/
- call strategy
- depending on how it exits, perform further operation (such as writing out tree out of index and updating HEAD) using the info in .git it left before it called strategy, and clean up whatever was done in the first step (things like "drop the index lock"?).

Dying in the strategy and not allowing the clean-up was what I was worried about. If you can guarantee that you are not going to leave the repository in a wedged state, calling merge-recursive internally is perfectly fine, but the codepaths involved need to be carefully vetted for that.

#6 lore raw

Re: [PATCH 2/2] builtin-merge: avoid run_command_v_opt() for recursive

Quoted 9 lines · Junio C Hamano

I was not worried too much about "the other strategy will" case, but isn't the general structure of git-merge driver be:

- do some set-up computation, and leave info in .git/
- call strategy
- depending on how it exits, perform further operation (such as writing out tree out of index and updating HEAD) using the info in .git it left before it called strategy, and clean up whatever was done in the first step (things like "drop the index lock"?).

atexit() will take care of this. I'll send a testcase for this in a bit, I just wrote it because I was not exactly sure about this.

Quoted 5 lines

Dying in the strategy and not allowing the clean-up was what I was worried about. If you can guarantee that you are not going to leave the repository in a wedged state, calling merge-recursive internally is perfectly fine, but the codepaths involved need to be carefully vetted for that.

As far as I see there are no such codepaths, but I may be wrong; in that case new tests will be needed as well, since the current 'make test' output is happy. ;-)

#7 lore raw

PATCH 1/1 Add a new test to ensure merging a submodule is handled properly.

In this test we cause a directory / submodule conflict then we check if the index is unlocked properly.

---

I'm no exactly sure if this should be included or not, I just wrote it to make sure that a die() insure merge-recursive will not cause an unlocked index at the end.

 t/t7607-merge-submodule.sh |   40 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 40 insertions(+), 0 deletions(-)
 create mode 100755 t/t7607-merge-submodule.sh
t/t7607-merge-submodule.sh new file +40 -0
new file mode 100755
index 0000000..552930a
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+test_description='git-merge
+
+Testing if a directory / submodule conflict is handled properly.'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	mkdir lib &&
+	cd lib &&
+	git init &&
+	echo c1 >c1.c &&
+	git add c1.c &&
+	git commit -m "submodule init" &&
+	mkdir ../main &&
+	cd ../main &&
+	git init &&
+	echo main >main.c &&
+	git add main.c &&
+	git commit -m "c0: main init" &&
+	git tag c0 &&
+	mkdir lib &&
+	echo lib >lib/c1.c &&
+	git add lib/c1.c &&
+	git commit -m "c1: lib init" &&
+	git tag c1 &&
+	git reset --hard c0 &&
+	git submodule add "`pwd`/../lib" lib &&
+	git commit -m "c2: add submodule" &&
+	git tag c2
+'
+
+test_expect_success 'dir/submodule conflict' '
+	git reset --hard c1 &&
+	test_must_fail git merge c2 &&
+	test ! -f .git/index.lock
+'
+
+test_done
Signature · 1 line
1.6.0.rc0.14.g95f8.dirty
#8 lore raw

Re: [PATCH 1/2] merge-recursive: prepare merge_recursive() to be called from builtins

In reply to Miklos Vajna

Hi,

Quoted 4 lines · Miklos Vajna

When other builtins call merge_recursive(), they would have to handle the GIT_MERGE_VERBOSITY environment variable, causing a code duplication. Same story for the git_config() call. It's better to do it when merge_recursive() is called the first time.

Hmm, I have the long-run vision that we have a nice libgit some day, with merge_recursive() being part of it. And I'm a little unsure if libified functions should rely on environment variables.
So I'm wondering if the verbosity should be set in the caller functions of merge_recursive(), i.e. that cmd_merge_recursive() and cmd_merge() (or another part of builtin-merge.c) does the
getenv("GIT_MERGE_VERBOSITY")
stuff and a verbosity value could be a new argument to merge_recursive(). Then other merge_recursive() users don't need to

	setenv("GIT_MERGE_VERBOSITY", "3", 1)

or something, they just pass 3 as the verbosity value.

Just a thought,
Stephan

PS: Your patch looks fine to me.

Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
#9 lore raw

Re: [PATCH 1/2] merge-recursive: prepare merge_recursive() to be called from builtins

Quoted 1 line · Stephan Beyer

Hmm, I have the long-run vision that we have a nice libgit some day,

That would be nice, but in that case I would start avoiding die() which is an awful amount of work... (There were multiple threads on the list previously.)

Quoted 2 lines

with merge_recursive() being part of it. And I'm a little unsure if libified functions should rely on environment variables.

Well, many libs do this. Random example: ld.so relies on LD_LIBRARY_PATH as well.

Anyway sure, using function parameters instead of env vars is more elegant.

#10 lore raw

Re: [PATCH 1/2] merge-recursive: prepare merge_recursive() to be called from builtins

In reply to Stephan Beyer
Quoted 3 lines · Stephan Beyer

Hmm, I have the long-run vision that we have a nice libgit some day, with merge_recursive() being part of it. And I'm a little unsure if libified functions should rely on environment variables.

I think the environment variable is the least of your worries.

I do not think anybody has vetted if it is safe to call merge_recursive() more than once in a single run of a process. Things to watch out for are the use of static variables (e.g. current_{file,directory}_set that are used for its (semi-broken) D/F conflict detection), its liberal use of die(), leaking of "virtual commit", to name a few. They are all perfectly fine programming constructs when we assume this is a one-shot "run and clean up with exit" program, but whoever wants to libify it needs to arrange them to be cleaned up inside the "library" without making the code too ugly nor one-shot use too expensive.

But such a clean-up may not be too bad as I initially feared, I suppose. After a cursory look in builtin-merge-recursive.c, at least it does not seem to mark objects with random object flags, expecting that nobody else will look at them after it exits --- which would have been very expensive to clean up after the fact.

#11 lore raw

Re: [PATCH 1/2] merge-recursive: prepare merge_recursive() to be called from builtins

Hi,

Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Stephan Beyer

Hmm, I have the long-run vision that we have a nice libgit some day, with merge_recursive() being part of it. And I'm a little unsure if libified functions should rely on environment variables.

I think the environment variable is the least of your worries.

I do not think anybody has vetted if it is safe to call merge_recursive() more than once in a single run of a process.

I use it in builtin-sequencer.c, without yet spending much effort in libifying it. For the tests, it works. (Each "pick" calls a merge_recursive(), each threeway-merge needing "patch" does.) But I should either spend some effort in improving the libification or, if this gets too time-consuming before Aug 17, I revert the commits that make use of merge_recursive() instead of running git-merge-recursive.

Quoted 1 line

leaking of "virtual commit",

Apropos, I think I have some tiny leak fixes lying around here. Would such patches go into 1.6.0 or is it too dangerous? (A free() that fixes a leak in one place could cause a segmentation fault in another place. Of course I can try to explain in the commit messages, why those are leaks in every case.)

Quoted 1 line

But such a clean-up may not be too bad as I initially feared, I suppose.

I suppose, too.

Regards.

Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
#12 lore raw

PATCH 1/1 builtin-revert.c: Make use of merge_recursive()

In reply to Miklos Vajna

Cherry-pick and revert always ran the merging in a separate process. This patch makes cherry-pick/revert call merge_recursive() instead of running git-merge-recursive.

---

	Hi,
	I wonder if this patch fits in line.
 builtin-merge-recursive.c |    2 +-
 builtin-revert.c          |   41 ++++++++++++++++++++++-------------------
 merge-recursive.h         |    1 +
 3 files changed, 24 insertions(+), 20 deletions(-)
builtin-merge-recursive.c +1 -1
index 09aa830..d8bd21f 100644
@@ -1327,7 +1327,7 @@ static const char *better_branch_name(const char *branch)
 	return name ? name : branch;
 }
 
-static struct commit *get_ref(const char *ref)
+struct commit *get_ref(const char *ref)
 {
 	unsigned char sha1[20];
 	struct object *object;
builtin-revert.c +22 -19

68 lines not on this page · raw message

merge-recursive.h +1 -0
index f37630a..40f329b 100644
@@ -1,6 +1,7 @@
 #ifndef MERGE_RECURSIVE_H
 #define MERGE_RECURSIVE_H
 
+struct commit *get_ref(const char *ref);
 int merge_recursive(struct commit *h1,
 		    struct commit *h2,
 		    const char *branch1,
Signature · 1 line
1.6.0.rc2.264.g563b6
#13 lore raw

Re: [PATCH] builtin-revert.c: Make use of merge_recursive()

Hi,

builtin-merge-recursive.c static const char *better_branch_name(const char *branch) · line 1330

Quoted 10 lines · Stephan Beyer
builtin-merge-recursive.c +1 -1
index 09aa830..d8bd21f 100644
@@ -1327,7 +1327,7 @@ static const char *better_branch_name(const char *branch)
 	return name ? name : branch;
 }
 
-static struct commit *get_ref(const char *ref)
+struct commit *get_ref(const char *ref)

The name get_ref() is way too generic to be non-static. But I have a hunch that peel_to_type() does a lot of what we want here, if not all of it.

Quoted 16 lines
builtin-revert.c +0 -8
index 27881e9..c54cf8a 100644
@@ -219,15 +216,21 @@ static int merge_recursive(const char *base_sha1,
 	 * and $prev on top of us (when reverting), or the change between
 	 * $prev and $commit on top of us (when cherry-picking or replaying).
 	 */
-	argv[i++] = "merge-recursive";
-	if (base_sha1)
-		argv[i++] = base_sha1;
-	argv[i++] = "--";
-	argv[i++] = head_sha1;
-	argv[i++] = next_sha1;
-	argv[i++] = NULL;
-

builtin-revert.c static int merge_recursive(const char *base_sha1, · line 227

Quoted 10 lines
builtin-revert.c +9 -1
-	return run_command_v_opt(argv, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD);
+	if (base_sha1) {
+		struct commit *base = get_ref(base_sha1);
+		commit_list_insert(base, &ca);
+	}
+	h1 = get_ref(head_sha1);
+	h2 = get_ref(next_sha1);
+
+	index_fd = hold_locked_index(lock, 1);
+	clean = merge_recursive(h1, h2, head_name, next_name, ca, &result);

h1 and h2 are not expressive. head_commit and next_commit would be.

Rest looks good to me -- even if I had to spend too much time (therefore being not really thorough in the end) verifying that merge_recursive() does not lock the index itself, and that GITHEAD_* definitions are not necessary anymore, since merge_recursive() takes the arguments directly; you might want to make it easier for this reviewer in the future, if you want this reviewer to review your patches, that is.

Ciao,
Dscho

#14 lore raw

Re: [PATCH] builtin-revert.c: Make use of merge_recursive()

Hi,

Quoted 12 lines · Johannes Schindelin

Hi,

Quoted 8 lines · Stephan Beyer
builtin-merge-recursive.c +0 -0
index 09aa830..d8bd21f 100644
@@ -1327,7 +1327,7 @@ static const char *better_branch_name(const char *branch)
 	return name ? name : branch;
 }
 
Quoted 4 lines

builtin-merge-recursive.c static const char *better_branch_name(const char *branch) · line 1330

Quoted 2 lines
builtin-merge-recursive.c +1 -1
-static struct commit *get_ref(const char *ref)
+struct commit *get_ref(const char *ref)

The name get_ref() is way too generic to be non-static.

That's right.

Quoted 2 lines

But I have a hunch that peel_to_type() does a lot of what we want here, if not all of it.

get_ref() has a big advantage over peel_to_type(): it can handle trees, via "virtual commits" (make_virtual_commit()).
If you wonder where we need to handle trees on cherry-pick/revert:
With the -n (no commit) option you are allowed to have a dirty index. So the recursive merge is not done using the HEAD commit but using the uncommitted tree of the index.

Well, a good alternative could be to just make the really cool make_virtual_commit() function non-static.
The name could be generic enough. Is it? :-)
Or perhaps: make_virtual_commit_from_tree().

Btw I also need get_ref() (or make_virtual_commit()) for threeway fallback of the sequencer "patch -3" instruction ("git am -3"). ;)

Quoted 7 lines

builtin-merge-recursive.c static const char *better_branch_name(const char *branch)

Quoted 5 lines
builtin-merge-recursive.c +5 -0
+	h1 = get_ref(head_sha1);
+	h2 = get_ref(next_sha1);
+
+	index_fd = hold_locked_index(lock, 1);
+	clean = merge_recursive(h1, h2, head_name, next_name, ca, &result);

h1 and h2 are not expressive. head_commit and next_commit would be.

This is also quite true.
Those names, also "ca", were taken from cmd_merge_recursive(). (This is no excuse, just an explanation.)

Quoted 3 lines

Rest looks good to me -- even if I had to spend too much time (therefore being not really thorough in the end) verifying that merge_recursive() does not lock the index itself,

I can't help here. Miklos has the same change in patch v2/2 and I wonder if you really expect that I don't test my patches, because a double lock wouldn't have worked.

Quoted 2 lines

and that GITHEAD_* definitions are not necessary anymore, since merge_recursive() takes the arguments directly;

Ok, I hoped that would've been clear by using head_name/next_name directly in the merge_recursive() arguments, but nevertheless thanks for your comment, ...because: using get_ref() the GITHEAD_* definitions *are* still needed, because it takes the GITHEAD_* name for the virtual commits...

Under this additional circumstance, I really tend to make make_virtual_commit() non-static.

Kind regards,
Stephan

Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
#15 lore raw

Re: [PATCH] builtin-revert.c: Make use of merge_recursive()

Quoted 4 lines · Stephan Beyer

Well, a good alternative could be to just make the really cool make_virtual_commit() function non-static.
The name could be generic enough. Is it? :-)
Or perhaps: make_virtual_commit_from_tree().

Given that you can't make virtual commits from commits, tags or blobs, I think the name "as is" generic enough.

#16 lore raw

PATCH 1/1 builtin-revert: Make use of merge_recursive()

Cherry-pick and revert always ran the merging in a separate process. This patch makes cherry-pick/revert call merge_recursive() instead of running git-merge-recursive.

To be able to cherry-pick/revert -n (without committing) on a dirty index, make_virtual_commit() is needed and thus declared non-static.

Also the GITHEAD_* environment definitions are not needed anymore, since the names are direct arguments to make_virtual_commit() and merge_recursive().

---

	Hi,
	so I give it a new try.
 builtin-merge-recursive.c |    2 +-
 builtin-revert.c          |   56 +++++++++++++++++++++++++-------------------
 merge-recursive.h         |    2 +
 3 files changed, 35 insertions(+), 25 deletions(-)
builtin-merge-recursive.c +1 -1
index 09aa830..395bdf8 100644
@@ -42,7 +42,7 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two)
  * - *(int *)commit->object.sha1 set to the virtual id.
  */
 
-static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
+struct commit *make_virtual_commit(struct tree *tree, const char *comment)
 {
 	struct commit *commit = xcalloc(1, sizeof(struct commit));
 	static unsigned virtual_id = 1;
builtin-revert.c +32 -24

81 lines not on this page · raw message

merge-recursive.h +2 -0
index f37630a..a9eead3 100644
@@ -1,6 +1,8 @@
 #ifndef MERGE_RECURSIVE_H
 #define MERGE_RECURSIVE_H
 
+extern struct commit *make_virtual_commit(struct tree *tree,
+					  const char *comment);
 int merge_recursive(struct commit *h1,
 		    struct commit *h2,
 		    const char *branch1,
Signature · 1 line
1.6.0.rc2.267.g02e66a
#17 lore raw

Re: [PATCH] builtin-revert: Make use of merge_recursive()

Sorry, I forgot to change this to [PATCH v2] or something.

And...

merge-recursive.h line 8

Quoted 13 lines · Stephan Beyer
merge-recursive.h +2 -0
index f37630a..a9eead3 100644
@@ -1,6 +1,8 @@
 #ifndef MERGE_RECURSIVE_H
 #define MERGE_RECURSIVE_H
 
+extern struct commit *make_virtual_commit(struct tree *tree,
+					  const char *comment);
 int merge_recursive(struct commit *h1,
 		    struct commit *h2,
 		    const char *branch1,

Is this a mistake that some forward declarations in header files are not declared "extern"?

Regards,
Stephan

Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
#18 lore raw

Re: [PATCH] builtin-revert: Make use of merge_recursive()

Quoted 13 lines · Stephan Beyer

Sorry, I forgot to change this to [PATCH v2] or something.

And...

Quoted 8 lines · Stephan Beyer
merge-recursive.h +0 -0
index f37630a..a9eead3 100644
@@ -1,6 +1,8 @@
 #ifndef MERGE_RECURSIVE_H
 #define MERGE_RECURSIVE_H
 
Quoted 8 lines

merge-recursive.h line 8

Quoted 5 lines
merge-recursive.h +2 -0
+extern struct commit *make_virtual_commit(struct tree *tree,
+					  const char *comment);
 int merge_recursive(struct commit *h1,
 		    struct commit *h2,
 		    const char *branch1,

Is this a mistake that some forward declarations in header files are not declared "extern"?

Yup, that looks old fashioned.

#19 lore raw

Re: [PATCH] builtin-revert: Make use of merge_recursive()

In reply to Stephan Beyer
Quoted 7 lines · Stephan Beyer

Cherry-pick and revert always ran the merging in a separate process. This patch makes cherry-pick/revert call merge_recursive() instead of running git-merge-recursive.

To be able to cherry-pick/revert -n (without committing) on a dirty index, make_virtual_commit() is needed and thus declared non-static.

Quoted 8 lines

Also the GITHEAD_* environment definitions are not needed anymore, since the names are direct arguments to make_virtual_commit() and merge_recursive().

---

	Hi,
	so I give it a new try.

Looks good from a cursory reading.

I am not absolutely sure if the phoney commit-looking object that has nonsense SHA-1 created by make_virtual_commit() would have any unintended side effects to the rest of the system, but it does not look like it is even inserted into the global object hash table, so this should be Ok.

That was the last piece of worry coming from me regarding this "call recursive internally" theme.

Would we need to consolidate this and Miklos's "call recursive internally from git-merge wrapper" by making them into three patches? I.e.

(1) move bulk of code from builtin-merge-recursive.c to a new file merge-recursive.c and introduce merge_recursive_helper() in there so that both of you and cmd_merge_recursive() itself can call it;

(2) make revert.c use merge_recursive_helper();

(3) make builtin-merge.c use merge_recursive_helper().

#20 lore raw

PATCH 1/1 Split out merge_recursive() to merge-recursive.c

Move most of the of code from builtin-merge-recursive.c to a new file merge-recursive.c and introduce merge_recursive_setup() in there so that builtin-merge-recursive and other builtins call it.

---

Quoted 4 lines · Junio C Hamano

(1) move bulk of code from builtin-merge-recursive.c to a new file merge-recursive.c and introduce merge_recursive_helper() in there so
that both of you and cmd_merge_recursive() itself can call it;

Something like this?

The function is actually called merge_recursive_setup(), as I think that name is more intuitive, but I can rename it back to _helper() if that's really a problem.

 Makefile                                       |    1 +
 builtin-merge-recursive.c                      | 1327 +-----------------------
 builtin-merge-recursive.c => merge-recursive.c |  107 +--
 merge-recursive.h                              |    6 +-
 4 files changed, 21 insertions(+), 1420 deletions(-)
 copy builtin-merge-recursive.c => merge-recursive.c (92%)
Makefile +1 -0
index 90c5a13..1e6be75 100644
@@ -437,6 +437,7 @@ LIB_OBJS += log-tree.o
 LIB_OBJS += mailmap.o
 LIB_OBJS += match-trees.o
 LIB_OBJS += merge-file.o
+LIB_OBJS += merge-recursive.o
 LIB_OBJS += name-hash.o
 LIB_OBJS += object.o
 LIB_OBJS += pack-check.o
builtin-merge-recursive.c +4 -1323

1,365 lines not on this page · raw message

merge-recursive.c renamed from builtin-merge-recursive.c +11 -96

140 lines not on this page · raw message

merge-recursive.h +5 -1
index f37630a..73e4413 100644
@@ -14,7 +14,11 @@ int merge_trees(struct tree *head,
 		const char *branch1,
 		const char *branch2,
 		struct tree **result);
-
+struct commit *make_virtual_commit(struct tree *tree, const char *comment);
+int merge_recursive_config(const char *var, const char *value, void *cb);
+void merge_recursive_setup(int is_subtree_merge);
 struct tree *write_tree_from_memory(void);
 
+extern int merge_recursive_verbosity;
+
 #endif
Signature · 1 line
1.6.0.rc0.14.g95f8.dirty
#21 lore raw

PATCH 1/1 merge-recursive.c: Add more generic merge_recursive_generic()

merge_recursive_generic() takes, in comparison to to merge_recursive(), no commit ("struct commit *") arguments but SHA ids ("unsigned char *"), and no commit list of bases but an array of refs ("const char **").

This makes it more generic in the case that it can also take the SHA of a tree to merge trees without commits, for the bases, the head and the remote.

merge_recursive_generic() also handles locking and updating of the index, which is a common use case of merge_recursive().

This patch also rewrites builtin-merge-recursive.c to make use of merge_recursive_generic(). By doing this, I stumbled over the limitation of 20 bases and I've added a warning if this limitation is exceeded.

This patch qualifies make_virtual_commit() as static again because this function is not needed anymore outside merge-recursive.c.

---
Hi,

so this patch is marked as (1b) since it is based on Miklos' patch that I called (1a) in my mind :-)
I'm unsure if it's good to squash both, but that's the decision of the maintainer - if taken at all.

Some further remark that I thought about when writig the commit message:
1. It could be cleaner to choose a "struct string_list" for the base_list in merge_recursive_generic()
2. If we do so, we've had lifted the limitation to 20 bases automatically.
2. Since the patchset that is in work in this thread is for post 1.6.0, I wonder if there should be a patch for the 20 bases limitation in git-merge-recursive for 1.6.0. Either lifting it (ALLOC_GROW foo bar) or printing a warning.

Regards,
Stephan

 builtin-merge-recursive.c |   64 +++++++++++++-------------------------------
 merge-recursive.c         |   53 +++++++++++++++++++++++++++++++++++++
 merge-recursive.h         |    4 ++-
 3 files changed, 75 insertions(+), 46 deletions(-)
builtin-merge-recursive.c +19 -45
index 8bf2fa5..25f540b 100644
@@ -15,36 +15,13 @@ static const char *better_branch_name(const char *branch)
 	return name ? name : branch;
 }
 
-static struct commit *get_ref(const char *ref)
-{
-	unsigned char sha1[20];
-	struct object *object;
-
-	if (get_sha1(ref, sha1))
-		die("Could not resolve ref '%s'", ref);
-	object = deref_tag(parse_object(sha1), ref, strlen(ref));
-	if (!object)
-		return NULL;
-	if (object->type == OBJ_TREE)
-		return make_virtual_commit((struct tree*)object,
-			better_branch_name(ref));
-	if (object->type != OBJ_COMMIT)
-		return NULL;
-	if (parse_commit((struct commit *)object))
-		die("Could not parse commit '%s'", sha1_to_hex(object->sha1));
-	return (struct commit *)object;
-}
-
 int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
 {
-	static const char *bases[20];
-	static unsigned bases_count = 0;
-	int i, clean;
+	const char *bases[21];
+	unsigned bases_count = 0;
+	int i, failed;
 	const char *branch1, *branch2;
-	struct commit *result, *h1, *h2;
-	struct commit_list *ca = NULL;
-	struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
-	int index_fd;
+	unsigned char h1[20], h2[20];
 	int subtree_merge = 0;
 
 	if (argv[0]) {
@@ -60,10 +37,15 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
 		die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
 
 	for (i = 1; i < argc; ++i) {
-		if (!strcmp(argv[i], "--"))
+		if (!strcmp(argv[i], "--")) {
+			bases[bases_count] = NULL;
 			break;
-		if (bases_count < sizeof(bases)/sizeof(*bases))
+		}
+		if (bases_count < ARRAY_SIZE(bases)-1)
 			bases[bases_count++] = argv[i];
+		else
+			warning("Cannot handle more than %zu bases. "
+				"Ignoring %s.", ARRAY_SIZE(bases)-1, argv[i]);
 	}
 	if (argc - i != 3) /* "--" "<head>" "<remote>" */
 		die("Not handling anything other than two heads merge.");
@@ -71,8 +53,10 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
 	branch1 = argv[++i];
 	branch2 = argv[++i];
 
-	h1 = get_ref(branch1);
-	h2 = get_ref(branch2);
+	if (get_sha1(branch1, h1))
+		die("Could not resolve ref '%s'", branch1);
+	if (get_sha1(branch2, h2))
+		die("Could not resolve ref '%s'", branch2);
 
 	branch1 = better_branch_name(branch1);
 	branch2 = better_branch_name(branch2);
@@ -80,18 +64,8 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
 	if (merge_recursive_verbosity >= 3)
 		printf("Merging %s with %s\n", branch1, branch2);
 
-	index_fd = hold_locked_index(lock, 1);
-
-	for (i = 0; i < bases_count; i++) {
-		struct commit *ancestor = get_ref(bases[i]);
-		ca = commit_list_insert(ancestor, &ca);
-	}
-	clean = merge_recursive(h1, h2, branch1, branch2, ca, &result);
-
-	if (active_cache_changed &&
-	    (write_cache(index_fd, active_cache, active_nr) ||
-	     commit_locked_index(lock)))
-			die ("unable to write %s", get_index_file());
-
-	return clean ? 0: 1;
+	failed = merge_recursive_generic(bases, h1, branch1, h2, branch2);
+	if (failed < 0)
+		return 128; /* die() error code */
+	return failed;
 }
merge-recursive.c +53 -0

59 lines not on this page · raw message

merge-recursive.h +3 -1
index 73e4413..4dd6476 100644
@@ -14,7 +14,9 @@ int merge_trees(struct tree *head,
 		const char *branch1,
 		const char *branch2,
 		struct tree **result);
-struct commit *make_virtual_commit(struct tree *tree, const char *comment);
+extern int merge_recursive_generic(const char **base_list,
+		const unsigned char *head_sha1, const char *head_name,
+		const unsigned char *next_sha1, const char *next_name);
 int merge_recursive_config(const char *var, const char *value, void *cb);
 void merge_recursive_setup(int is_subtree_merge);
 struct tree *write_tree_from_memory(void);
Signature · 1 line
1.6.0.rc2.281.g6f6cf
#22 lore raw

PATCH 1/1 Make builtin-revert.c use merge_recursive_generic()

Cherry-pick and revert always ran the merging in a separate process. This patch makes cherry-pick/revert call merge_recursive_generic() instead of running a git-merge-recursive process.

The GITHEAD_* environment definitions are not needed anymore, since the names are direct arguments to merge_recursive_generic().

---
Hi,
So this is now based on the new merge-recursive.c based on (1a) and (1b).
The number of lines deleted and inserted tells me that we're doing something right here.

 builtin-revert.c |   50 +++++++++++++-------------------------------------
 1 files changed, 13 insertions(+), 37 deletions(-)
builtin-revert.c +13 -37
index 27881e9..2a724ca 100644
@@ -11,6 +11,7 @@
 #include "cache-tree.h"
 #include "diff.h"
 #include "revision.h"
+#include "merge-recursive.h"
 
 /*
  * This implements the builtins revert and cherry-pick.
@@ -200,36 +201,6 @@ static void set_author_ident_env(const char *message)
 			sha1_to_hex(commit->object.sha1));
 }
 
-static int merge_recursive(const char *base_sha1,
-		const char *head_sha1, const char *head_name,
-		const char *next_sha1, const char *next_name)
-{
-	char buffer[256];
-	const char *argv[6];
-	int i = 0;
-
-	sprintf(buffer, "GITHEAD_%s", head_sha1);
-	setenv(buffer, head_name, 1);
-	sprintf(buffer, "GITHEAD_%s", next_sha1);
-	setenv(buffer, next_name, 1);
-
-	/*
-	 * This three way merge is an interesting one.  We are at
-	 * $head, and would want to apply the change between $commit
-	 * and $prev on top of us (when reverting), or the change between
-	 * $prev and $commit on top of us (when cherry-picking or replaying).
-	 */
-	argv[i++] = "merge-recursive";
-	if (base_sha1)
-		argv[i++] = base_sha1;
-	argv[i++] = "--";
-	argv[i++] = head_sha1;
-	argv[i++] = next_sha1;
-	argv[i++] = NULL;
-
-	return run_command_v_opt(argv, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD);
-}
-
 static char *help_msg(const unsigned char *sha1)
 {
 	static char helpbuf[1024];
@@ -266,12 +237,14 @@ static int revert_or_cherry_pick(int argc, const char **argv)
 {
 	unsigned char head[20];
 	struct commit *base, *next, *parent;
-	int i;
+	int i, fail;
 	char *oneline, *reencoded_message = NULL;
+	const char *base_list[] = { NULL, NULL };
 	const char *message, *encoding;
 	const char *defmsg = xstrdup(git_path("MERGE_MSG"));
 
-	git_config(git_default_config, NULL);
+	git_config(merge_recursive_config, NULL);
+	merge_recursive_setup(0);
 	me = action == REVERT ? "revert" : "cherry-pick";
 	setenv(GIT_REFLOG_ACTION, me, 0);
 	parse_args(argc, argv);
@@ -373,11 +346,14 @@ static int revert_or_cherry_pick(int argc, const char **argv)
 		}
 	}
 
-	if (merge_recursive(base == NULL ?
-				NULL : sha1_to_hex(base->object.sha1),
-				sha1_to_hex(head), "HEAD",
-				sha1_to_hex(next->object.sha1), oneline) ||
-			write_cache_as_tree(head, 0, NULL)) {
+	if (base)
+		base_list[0] = sha1_to_hex(base->object.sha1);
+	fail = merge_recursive_generic(base_list, head, "HEAD",
+						  next->object.sha1, oneline);
+	if (fail < 0)
+		exit(1);
+
+	if (fail || write_cache_as_tree(head, 0, NULL)) {
 		add_to_msg("\nConflicts:\n\n");
 		read_cache();
 		for (i = 0; i < active_nr;) {
Signature · 1 line
1.6.0.rc2.281.g6f6cf
#23 lore raw

Re: [PATCH (1b)] merge-recursive.c: Add more generic merge_recursive_generic()

In reply to Stephan Beyer
Quoted 10 lines · Stephan Beyer

merge_recursive_generic() takes, in comparison to to merge_recursive(), no commit ("struct commit *") arguments but SHA ids ("unsigned char *"), and no commit list of bases but an array of refs ("const char **").

This makes it more generic in the case that it can also take the SHA of a tree to merge trees without commits, for the bases, the head and the remote.

merge_recursive_generic() also handles locking and updating of the index, which is a common use case of merge_recursive().

Then what about adding an extra parameter to merge_recursive_generic() so that merge_recursive_setup() could be a static function?

Or is there any reason to keep them separated?

Thanks.

#33 lore raw

Re: [PATCH] Split out merge_recursive() to merge-recursive.c

In reply to Miklos Vajna

Hi,

Quoted 3 lines · Miklos Vajna

Move most of the of code from builtin-merge-recursive.c to a new file merge-recursive.c and introduce merge_recursive_setup() in there so that builtin-merge-recursive and other builtins call it.

So, according to that change my "revert" patch is the same plus this interdiff:

--8<--

builtin-revert.c +2 -1
index dcee181..941b875 100644
@@ -281,7 +281,8 @@ static int revert_or_cherry_pick(int argc, const char **argv)
 	const char *message, *encoding;
 	const char *defmsg = xstrdup(git_path("MERGE_MSG"));
 
-	git_config(git_default_config, NULL);
+	git_config(merge_recursive_config, NULL);
+	merge_recursive_setup(0);
 	me = action == REVERT ? "revert" : "cherry-pick";
 	setenv(GIT_REFLOG_ACTION, me, 0);
 	parse_args(argc, argv);

-->8--

I'm fine with that, but my hope(?) was that we could have some more generic function that takes SHAs ("unsigned char *") instead of commits. I don't know if this is bad for builtin-merge, but from the "revert" and "sequencer" point of view this is all I need.

Hmm, I think it takes less time to implement it based on your patch than explaining :-)

One further comment:

Quoted 6 lines
(quoted diff) +2 -2
-static int merge_config(const char *var, const char *value, void *cb)
+int merge_recursive_config(const char *var, const char *value, void *cb)
 {
 	if (!strcasecmp(var, "merge.verbosity")) {
-		verbosity = git_config_int(var, value);
+		merge_recursive_verbosity = git_config_int(var, value);

[...]

Quoted 6 lines
merge-recursive.h +0 -0
index f37630a..73e4413 100644
@@ -14,7 +14,11 @@ int merge_trees(struct tree *head,
 		const char *branch1,

merge-recursive.h int merge_trees(struct tree *head, · line 23

Quoted 10 lines
merge-recursive.h +4 -1
 		const char *branch2,
 		struct tree **result);
-
+struct commit *make_virtual_commit(struct tree *tree, const char *comment);
+int merge_recursive_config(const char *var, const char *value, void *cb);
+void merge_recursive_setup(int is_subtree_merge);
 struct tree *write_tree_from_memory(void);
 
 
+extern int merge_recursive_verbosity;

Why this?

So we have:
1. "merge.verbosity" config value
2. GIT_MERGE_VERBOSITY environment
3. merge_recursive_verbosity variable

I wonder if 3 is really necessary.

Kind regards,
Stephan

Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
#34 lore raw

Re: [PATCH] Split out merge_recursive() to merge-recursive.c

Quoted 10 lines · Stephan Beyer
Quoted 1 line

+extern int merge_recursive_verbosity;

Why this?

So we have:
1. "merge.verbosity" config value
2. GIT_MERGE_VERBOSITY environment
3. merge_recursive_verbosity variable

I wonder if 3 is really necessary.

Yes, IMHO it is. The idea is that a

git_config(merge_recursive_config, NULL);
merge_recursive_setup(0);

will take merge.verbosity and GIT_MERGE_VERBOSITY into account, finally you can always read merge_recursive_verbosity, and you don't have to deal with the details (if it is set based on GIT_MERGE_VERBOSITY or based on merge.verbosity).

#36 lore raw

Re: [PATCH] builtin-revert: Make use of merge_recursive()

In reply to Junio C Hamano

Hi,

Quoted 4 lines · Junio C Hamano

I am not absolutely sure if the phoney commit-looking object that has nonsense SHA-1 created by make_virtual_commit() would have any unintended side effects to the rest of the system, but it does not look like it is even inserted into the global object hash table, so this should be Ok.

Hmm, the git-merge-recursive process that was run before, calls get_ref() and that also used make_virtual_commit() and thus this virtual_id. So if there is some danger regarding this, then it has also been there before my patch :-)

Quoted 2 lines

That was the last piece of worry coming from me regarding this "call recursive internally" theme.

Well, for simple cherry-pick/revert your other worries can be ignored, because it only runs merge_recursive() _once_ and the die()s do not hurt. Wrong?

Quoted 7 lines

Would we need to consolidate this and Miklos's "call recursive internally from git-merge wrapper" by making them into three patches? I.e.

(1) move bulk of code from builtin-merge-recursive.c to a new file merge-recursive.c and introduce merge_recursive_helper() in there so that both of you and cmd_merge_recursive() itself can call it;

I'd like to see a "libified" merge-recursive.c, but I wouldn't call the interesting function merge_recursive_helper(), I'd just take merge_recursive().
Of course those index locking could be done in it.

Looking at my sequencer code, I'd also be satisfied, if it takes SHAs as argument and no "struct commit *".
But then this should be more generic, i.e. OBJ_TAG has to be handled correctly (builtin-revert does that at the beginning at parse-args()).

Hmm, then step (1) is ok. :-)

Quoted 3 lines

(2) make revert.c use merge_recursive_helper();

(3) make builtin-merge.c use merge_recursive_helper().

This is ok then.

Thanks and regards,
Stephan

Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
#37 lore raw

Re: [PATCH] builtin-revert: Make use of merge_recursive()

Quoted 18 lines · Stephan Beyer
Quoted 7 lines

Would we need to consolidate this and Miklos's "call recursive internally from git-merge wrapper" by making them into three patches? I.e.

(1) move bulk of code from builtin-merge-recursive.c to a new file merge-recursive.c and introduce merge_recursive_helper() in there so that both of you and cmd_merge_recursive() itself can call it;

I'd like to see a "libified" merge-recursive.c, but I wouldn't call the interesting function merge_recursive_helper(), I'd just take merge_recursive().
Of course those index locking could be done in it.

Looking at my sequencer code, I'd also be satisfied, if it takes SHAs as argument and no "struct commit *".
But then this should be more generic, i.e. OBJ_TAG has to be handled correctly (builtin-revert does that at the beginning at parse-args()).

Quoted 7 lines

Hmm, then step (1) is ok. :-)

Quoted 3 lines

(2) make revert.c use merge_recursive_helper();

(3) make builtin-merge.c use merge_recursive_helper().

This is ok then.

Thanks.

Let me try to be a bit more explicit to avoid confusion.

I won't queue this round of patch (neither this nor Miklos's), as we are not in a hurry during the 1.6.0-rc period anyway, but expect (1) to happen first, Ok?

#24 lore raw

Re: [PATCH (1b)] merge-recursive.c: Add more generic merge_recursive_generic()

In reply to Miklos Vajna

Hi,

Quoted 9 lines · Miklos Vajna
Quoted 8 lines · Stephan Beyer

merge_recursive_generic() takes, in comparison to to merge_recursive(), no commit ("struct commit *") arguments but SHA ids ("unsigned char *"), and no commit list of bases but an array of refs ("const char **").

This makes it more generic in the case that it can also take the SHA of a tree to merge trees without commits, for the bases, the head and the remote.

Quoted 5 lines
Quoted 2 lines

merge_recursive_generic() also handles locking and updating of the index, which is a common use case of merge_recursive().

Then what about adding an extra parameter to merge_recursive_generic() so that merge_recursive_setup() could be a static function?

Could it?

I did not intend to replace merge_recursive() by merge_recursive_generic(), because merge_recursive() may be the better choice in cases where the caller only deals with commit objects and never with tree objects directly. Or if the caller does not want to lock the index or do some other stuff with the index...
In that case merge_recursive_setup() is still needed, isn't it?

Regards,
Stephan

Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
#25 lore raw

Re: [PATCH (1b)] merge-recursive.c: Add more generic merge_recursive_generic()

Quoted 6 lines · Stephan Beyer

I did not intend to replace merge_recursive() by merge_recursive_generic(), because merge_recursive() may be the better choice in cases where the caller only deals with commit objects and never with tree objects directly. Or if the caller does not want to lock the index or do some other stuff with the index...
In that case merge_recursive_setup() is still needed, isn't it?

Hm, OK. I was not aware pure merge_recursive() will be interesting for other builtins even after merge_recursive_generic().

#26 lore raw

Re: [PATCH (1b)] merge-recursive.c: Add more generic merge_recursive_generic()

In reply to Stephan Beyer
Quoted 11 lines · Stephan Beyer

merge_recursive_generic() takes, in comparison to to merge_recursive(), no commit ("struct commit *") arguments but SHA ids ("unsigned char *"), and no commit list of bases but an array of refs ("const char **").

This makes it more generic in the case that it can also take the SHA of a tree to merge trees without commits, for the bases, the head and the remote.

merge_recursive_generic() also handles locking and updating of the index, which is a common use case of merge_recursive().

Quoted 7 lines

This patch also rewrites builtin-merge-recursive.c to make use of merge_recursive_generic(). By doing this, I stumbled over the limitation of 20 bases and I've added a warning if this limitation is exceeded.

This patch qualifies make_virtual_commit() as static again because this function is not needed anymore outside merge-recursive.c.

You might look at builtin-checkout and see if merge_working_tree() could be made cleaner with this API, or if some other API could accomodate both cases more nicely.

(I'm not sure either way, but it would be a good confirmation that the API is properly convenient if that additional case could use it.)

-Daniel
*This .sig left intentionally blank*

#27 lore raw

Re: [PATCH (1b)] merge-recursive.c: Add more generic merge_recursive_generic()

Hi,

Quoted 3 lines · Daniel Barkalow

You might look at builtin-checkout and see if merge_working_tree() could be made cleaner with this API, or if some other API could accomodate both cases more nicely.

Puhh, I've not dug into merging stuff that deep, but for me it does not look that this can be done in a useful way, i.e. merge_working_tree() does not do a recursive merge.

Regards.

Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
#28 lore raw

Re: [PATCH (1b)] merge-recursive.c: Add more generic merge_recursive_generic()

Quoted 10 lines · Stephan Beyer

Hi,

Quoted 3 lines · Daniel Barkalow

You might look at builtin-checkout and see if merge_working_tree() could be made cleaner with this API, or if some other API could accomodate both cases more nicely.

Puhh, I've not dug into merging stuff that deep, but for me it does not look that this can be done in a useful way, i.e. merge_working_tree() does not do a recursive merge.

Ah, true. It's actually doing a single merge in the way that merge_recursive would do a single merge. I think it ought to be doing a recursive merge, but that's probably a change for later, anyway. (This is for -m, which essentially picks the uncommited changes versus the old branch, applied to the new branch uncommitted)

-Daniel
*This .sig left intentionally blank*

#29 lore raw

Re: [PATCH (1b)] merge-recursive.c: Add more generic merge_recursive_generic()

Quoted 9 lines · Daniel Barkalow
Quoted 3 lines

Puhh, I've not dug into merging stuff that deep, but for me it does not look that this can be done in a useful way, i.e. merge_working_tree() does not do a recursive merge.

Ah, true. It's actually doing a single merge in the way that merge_recursive would do a single merge. I think it ought to be doing a recursive merge, but that's probably a change for later, anyway. (This is for -m, which essentially picks the uncommited changes versus the old branch, applied to the new branch uncommitted)

Why would you think it should be doing a recursive merge? It shouldn't.

Think of builtin-merge-recursive.c::merge_trees() as a fancier version of 3-tree variant of "unpack_trees()", with -m and -u option.

When you want to perform an exact three-way merge (i.e. you have two states O and B, and you want to apply changes between O and B to your state A, and you _precisely_ know what O is) that's the interface you would want to use, not the recursive one. The recursive behaviour is desirable only when you have A and B and need to infer where O should be, and/or there are multiple O's to deal with (i.e. running "git-merge B" when you are at A).

In all the potential users of merge-recursive machinery, namely, "revert", "cherry-pick", "stash apply", "am -3", and "checkout -m", you know what single common tree to use for your three-way merge. These operations, when done with direct C call into merge machinery, should NOT be using the "recursive" one.

When you switch branches from A to B with checkout, and you have local changes A', then you would want an exact three-way merge that modifies B by applying changes from A to A'.

When you cherry-pick commit C on top of your current HEAD, you want an exact three-way merge that modifies your HEAD by applying changes from C^ to C, and you do not want the merge machinery to take ancestry relation (and criss cross merges) between HEAD and C into account at all.

The scripted version of revert/cherry-pick used git-merge-recursive because that is the Porcelain API available, and the current C-rewrite uses it as well, but if we are rewriting it to call merge-recursive machinery directly, it should be making a single merge request to merge_trees(), not "recursive" one.

#30 lore raw

Re: [PATCH (1b)] merge-recursive.c: Add more generic merge_recursive_generic()

Hi,

Quoted 5 lines · Junio C Hamano

The scripted version of revert/cherry-pick used git-merge-recursive because that is the Porcelain API available, and the current C-rewrite uses it as well, but if we are rewriting it to call merge-recursive machinery directly, it should be making a single merge request to merge_trees(), not "recursive" one.

This is an interesting point.
So perhaps we don't need stuff like my regarding merge_recursive_generic(), at least not for revert/cherry-pick.

Regards,
Stephan

Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
#31 lore raw

Re: [PATCH (1b)] merge-recursive.c: Add more generic merge_recursive_generic()

In reply to Junio C Hamano
Quoted 17 lines · Junio C Hamano
Quoted 9 lines · Daniel Barkalow
Quoted 3 lines

Puhh, I've not dug into merging stuff that deep, but for me it does not look that this can be done in a useful way, i.e. merge_working_tree() does not do a recursive merge.

Ah, true. It's actually doing a single merge in the way that merge_recursive would do a single merge. I think it ought to be doing a recursive merge, but that's probably a change for later, anyway. (This is for -m, which essentially picks the uncommited changes versus the old branch, applied to the new branch uncommitted)

Why would you think it should be doing a recursive merge? It shouldn't.

Think of builtin-merge-recursive.c::merge_trees() as a fancier version of 3-tree variant of "unpack_trees()", with -m and -u option.

Quoted 7 lines

When you want to perform an exact three-way merge (i.e. you have two states O and B, and you want to apply changes between O and B to your state A, and you _precisely_ know what O is) that's the interface you would want to use, not the recursive one. The recursive behaviour is desirable only when you have A and B and need to infer where O should be, and/or there are multiple O's to deal with (i.e. running "git-merge B" when you are at A).

I understand that you know exactly what the change is, but I'm not convinced that you don't want to consider how O is related to A in determining who to apply that change to A. My naive impression was:

- the difference between O and B is to change X to Y, and we can determine this exactly
- we need to find X' in A to change it to Y'
- we can more accurately find X' in A if we find Z in the common ancestors of O and A such that X in O corresponds to Z in the ancestors, and we get similar benefits in determining the right Y'

That is, we care about the common ancestor not only for finding the relevant change in the near side to apply, but also for finding how to apply it to the far side.

Quoted 5 lines

In all the potential users of merge-recursive machinery, namely, "revert", "cherry-pick", "stash apply", "am -3", and "checkout -m", you know what single common tree to use for your three-way merge. These operations, when done with direct C call into merge machinery, should NOT be using the "recursive" one.

I agree that they should all be using the same mechanism, but I'm not sure it shouldn't be recursive or take history into account.

And I'd certainly believe that just running our merge-recursive won't actually do anything clever with the rest of the history, and that merge_trees() is the most suitable method currently available (which is essentially why I ended up using it in checkout -m, not entirely realizing that I'd removed the "recursive" aspect in the process of skipping parts of merge_recursive that weren't relevant).

Maybe merge_trees should get the two sides as structs with a struct tree * and a char * branch name, and the struct could someday get optional struct commit *s for the lineage of the side if somebody comes up with a method for merging that makes use of the component changes.

-Daniel
*This .sig left intentionally blank*

#32 lore raw

Re: [PATCH (1b)] merge-recursive.c: Add more generic merge_recursive_generic()

Quoted 4 lines · Daniel Barkalow

Maybe merge_trees should get the two sides as structs with a struct tree * and a char * branch name, and the struct could someday get optional struct commit *s for the lineage of the side if somebody comes up with a method for merging that makes use of the component changes.

I do agree with you that with some ancestry-based hinting the "find renames" part of merge_trees() postprocessing can do a better job than the current code. Contrary to a widespread misconception we often hear on this list and on #git, merge-recursive detects renames solely by looking at the three endpoints. Some people incorrectly recommend "commit a pure rename, then commit modifications to the renamed path"; such an artificial split would not at all help merging histories with renames if the modification made after renames are too great.

Suppose you have this history, where upper branch renames a path and modifies the contents in the renamed path in commits X, Y and Z. You would want to merge the history leading to B to your HEAD, A, to create a merge M:

            X---Y---Z---B              X---Y---Z---B  
           /                ===>      /             \
       ---O---o---o---A           ---O---o---o---A---M    

Maybe the rename done between O and X were pure enough that "diff-tree -M O X" would have found it, but commits Y, Z, or B changed the contents of the renamed path too greatly for "diff-tree -M O B" to notice the rename anymore. "git merge B" when you are at A will not find such a rename, but if we allowed rename detection stepwise to look at "diff-tree -M" for (O,X), (X,Y), (Y,Z), (Z,B), (and the same for history between O and A), we might be able to find renames better [*1*].

Suppose instead of merging the whole history leading to B, you would want to apply the small fix made with Y on top of A:

            X---Y---Z---B              X---Y---Z---B   
           /                ===>      /                
       ---O---o---o---A           ---O---o---o---A---Y'

You would "checkout A && cherry-pick Y" which amounts to three-way merge using X as "common", A as "mine" and Y as "his", which is the moral equivalent of:

read-tree -m -u X A Y

but with rename detection. merge_trees(A, Y, X) could traverse ancestry between X and A to find O and figure out where in A the paths that are affected in diff(X,Y) appear by making pairwise "diff-tree -M" to go from X back to O and then forward to A to find out that where the paths touched by Y were originally in O and where they are in A.

But you are assuming that "common" and "ours" have any ancestry relationship. You generally cannot.

The most extreme case is "am -3" where both "common" and "theirs" are pure trees without any ancestry relationship to anything else. They are built by looking at the "index" lines contained in the patch and contain only those paths that are affected by the patch, and the merge machinery merges that change into arbitrary "ours".

You can cherry-pick a change C from history that does not have any common ancestor with your history leading to "ours" ("common"=C^ and "theirs"=C in this case), and the same applies to revert of C ("common"=C and "theirs"=C^). "rebase --onto A C^ C" works the same way.

So this "extra ancestry information to help rename discovery" can at most be a hint.

More importantly, all of the above does not have anything to do with the "recursiveness" of merge-recursive (which is the difference between merge_trees() and merge_recursive()) at all. So I am still correct to say that "cherry-pick", "revert", "checkout -m", "am -3" and "stash apply" should use the merge_trees() interface, not the recursive one.

[Footnote]

*1* But that would be quite more expensive than what we currently do, and it only deals with an uninteresting special case of wholesale file rename.

I suspect if we ever do the stepwise thing, we would be better off doing not "diff-tree -M" but "blame -C" to really find where the lines affected between O and B ended up in A, and apply the change there.

#35 lore raw

Re: [PATCH] Split out merge_recursive() to merge-recursive.c

In reply to Miklos Vajna
Quoted 7 lines · Miklos Vajna

Move most of the of code from builtin-merge-recursive.c to a new file merge-recursive.c and introduce merge_recursive_setup() in there so that builtin-merge-recursive and other builtins call it.

---

Quoted 7 lines
Quoted 4 lines · Junio C Hamano

(1) move bulk of code from builtin-merge-recursive.c to a new file merge-recursive.c and introduce merge_recursive_helper() in there so
that both of you and cmd_merge_recursive() itself can call it;

Something like this?

Hmm, I think callers except "git merge" implementation should not be even using the "recursive" variant, so in that sense, I do not think you would need to expose anything but merge_trees(). But the new file is called merge-recursive.c not merge-trees.c, and in any case, builtin-merge.c needs to link to the recursive one, so this split is Ok. Exposure of make_virtual_commit() somewhat feels dirty, and I tend to agree that the pure "three-tree merge with rename detection" (aka merge_trees()) should operate on three trees, not commits, though.