Skip to contentSkip to conversation

implement automatic submodule merge strategy when possible

PATCH Superseded Heiko Voigt 2 participants 7 messages Jun 15, 2016

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover implement automatic submodule merge strategy when possible

This series implements a semi-automatic submodule merge as was discussed in this thread:

http://thread.gmane.org/gmane.comp.version-control.git/148942

In short:

Suppose we merge two submodule hashes A and B. If either A->B or B->A is a fast-forward the automatic result is the one containing both.

If there is not direct relation between A and B but we find a merge on any ref that contains both we suggest this as a possible resolution to the user. If there are more merges we output a list of them.

The suggestion tells the user to use update-index because checkout_submodule() on which Jens is working is not finished yet. This is a temporary solution and we can automatically checkout a submodule later when this is implemented.

Note: As I am not that familiar with the git codebase yet another pair of eyes would be really appreciated.

The newest version of this series can always be found on

git://github.com/hvoigt/git.git

Branch: submodule_merge_v2

Heiko Voigt (4):
add missing && to submodule-merge testcase teach ref iteration module about submodules extent setup_revisions() so it works with submodules implement automatic fast forward merge for submodules

 cache.h                    |    3 +
 merge-recursive.c          |    9 ++-
 path.c                     |   38 +++++++++++
 refs.c                     |  130 +++++++++++++++++++++++++++++--------
 refs.h                     |    8 ++
 revision.c                 |   32 +++++----
 revision.h                 |    1 +
 submodule.c                |  154 ++++++++++++++++++++++++++++++++++++++++++++
 submodule.h                |    2 +
 t/t7405-submodule-merge.sh |  124 +++++++++++++++++++++++++++++++++--
 10 files changed, 450 insertions(+), 51 deletions(-)
#1 lore raw

PATCH 1/4 add missing && to submodule-merge testcase

---
 t/t7405-submodule-merge.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
t/t7405-submodule-merge.sh +1 -1
index 9a21f78..4a7b893 100755
@@ -45,7 +45,7 @@ test_expect_success setup '
 	 git commit -m sub-b) &&
 	git add sub &&
 	test_tick &&
-	git commit -m b
+	git commit -m b &&
 
 	git checkout -b c a &&
 	git merge -s ours b &&
Signature · 1 line
1.7.1.528.gb3958.dirty
#2 lore raw

PATCH 2/4 teach ref iteration module about submodules

In reply to Heiko Voigt

We will use this in a later patch to extend setup_revisions() to load revisions directly from a submodule.

---
 cache.h |    3 ++
 path.c  |   38 ++++++++++++++++++++++++
 refs.c  |   99 +++++++++++++++++++++++++++++++++++++++++++++-----------------
 3 files changed, 113 insertions(+), 27 deletions(-)
cache.h +3 -0
index ff4a7c2..d9e1152 100644
@@ -641,6 +641,9 @@ extern char *git_pathdup(const char *fmt, ...)
 /* Return a statically allocated filename matching the sha1 signature */
 extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
 extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
+extern char *git_path_submodule(const char *path, const char *fmt, ...)
+	__attribute__((format (printf, 2, 3)));
+
 extern char *sha1_file_name(const unsigned char *sha1);
 extern char *sha1_pack_name(const unsigned char *sha1);
 extern char *sha1_pack_index_name(const unsigned char *sha1);
path.c +38 -0
index b4c8d91..fde959f 100644
@@ -122,6 +122,44 @@ char *git_path(const char *fmt, ...)
 	return cleanup_path(pathname);
 }
 
+char *git_path_submodule(const char *path, const char *fmt, ...)
+{
+	char *pathname = get_pathname();
+	struct strbuf buf = STRBUF_INIT;
+	const char *git_dir;
+	va_list args;
+	unsigned len;
+
+	len = strlen(path);
+	if (len > PATH_MAX-100)
+		return bad_path;
+
+	strbuf_addstr(&buf, path);
+	if (len && path[len-1] != '/')
+		strbuf_addch(&buf, '/');
+	strbuf_addstr(&buf, ".git");
+
+	git_dir = read_gitfile_gently(buf.buf);
+	if (git_dir) {
+		strbuf_reset(&buf);
+		strbuf_addstr(&buf, git_dir);
+	}
+	strbuf_addch(&buf, '/');
+
+	strncpy(pathname, buf.buf, PATH_MAX);
+	if (pathname[PATH_MAX-1] != '\0')
+		return bad_path;
+
+	strbuf_release(&buf);
+	len = strlen(pathname);
+
+	va_start(args, fmt);
+	len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
+	va_end(args);
+	if (len >= PATH_MAX)
+		return bad_path;
+	return cleanup_path(pathname);
+}
 
 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
 int git_mkstemp(char *path, size_t len, const char *template)
refs.c +72 -27

218 lines not on this page · raw message

Signature · 1 line
1.7.1.528.gb3958.dirty
#3 lore raw

Re: [PATCH 2/4] teach ref iteration module about submodules

path.c char *git_path(const char *fmt, ...) · line 151

Quoted 8 lines · Heiko Voigt
path.c +8 -0
+char *git_path_submodule(const char *path, const char *fmt, ...)
+{
+...
+	strbuf_addch(&buf, '/');
+
+	strncpy(pathname, buf.buf, PATH_MAX);
+	if (pathname[PATH_MAX-1] != '\0')
+		return bad_path;

This may not be wrong per-se, but having strncpy() NUL-pad the remainder of the buffer only because you want to check overlong path by inspecting pathname[PATH_MAX-1] sounds somewhat stupid, no? Your buf.len knows how long the path is already at this point, doesn't it?

refs.c void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) · line 357

Quoted 10 lines
refs.c +2 -3
@@ -322,11 +352,12 @@ void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
 	for_each_rawref(warn_if_dangling_symref, &data);
 }
 
-static struct ref_list *get_loose_refs(void)
+static struct ref_list *get_loose_refs(const char *submodule)
 {
-	if (!cached_refs.did_loose) {
-		cached_refs.loose = get_ref_dir("refs", NULL);
+	if (!cached_refs.did_loose || cached_refs.submodule != submodule) {

Do you really mean "!=" here? I do not see anywhere that you are "intern"-ing (a la Lisp implementations) names of submodules to make address comparison work as a cheap equality check.

refs.c void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) · line 358

Quoted 1 line
refs.c +1 -0
+		cached_refs.loose = get_ref_dir(submodule, "refs", NULL);

What happened to the old ref_list that had the refs from the toplevel project (or the last submodule you visited) if your "did_loose" is true? Leakage?

refs.c void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) · line 363

Quoted 5 lines
refs.c +1 -0
 		cached_refs.did_loose = 1;
+		cached_refs.submodule = submodule;
 	}
 	return cached_refs.loose;
 }

Once you grabbed loose refs for _any_ repository, you will have did_loose set, so the flag has now became pretty much useless.

More importantly, I wonder if you would instead want to have cached_refs structure for each submodule separately, or at least not nuke the cached_refs structure for the top-level project, only because you wanted to peek into one submodule. While your for_each_ref() is walking the refs of top-level project, its callback may stomp on the cached_refs by asking about submodule refs, and there is nothing in this code structure to help catching such a bug, is there?

#4 lore raw

Re: Re: [PATCH 2/4] teach ref iteration module about submodules

Hi,

Quoted 15 lines · Junio C Hamano

path.c char *git_path(const char *fmt, ...) · line 151

Quoted 8 lines · Heiko Voigt
path.c +8 -0
+char *git_path_submodule(const char *path, const char *fmt, ...)
+{
+...
+	strbuf_addch(&buf, '/');
+
+	strncpy(pathname, buf.buf, PATH_MAX);
+	if (pathname[PATH_MAX-1] != '\0')
+		return bad_path;

This may not be wrong per-se, but having strncpy() NUL-pad the remainder of the buffer only because you want to check overlong path by inspecting pathname[PATH_MAX-1] sounds somewhat stupid, no? Your buf.len knows how long the path is already at this point, doesn't it?

Well I needed to copy it to pathname anyway so I thought this would be the simplest check. But you are right about the unnecessary padding. What do you think about rephrasing this as

	if (buf.len >= PATH_MAX)
		return bad_path;

	memcpy(pathname, buf.buf, buf.len);

?

Quoted 4 lines
Quoted 4 lines
refs.c +0 -0
@@ -322,11 +352,12 @@ void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
 	for_each_rawref(warn_if_dangling_symref, &data);
 }
 
Quoted 10 lines

refs.c void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) · line 357

Quoted 6 lines
refs.c +2 -3
-static struct ref_list *get_loose_refs(void)
+static struct ref_list *get_loose_refs(const char *submodule)
 {
-	if (!cached_refs.did_loose) {
-		cached_refs.loose = get_ref_dir("refs", NULL);
+	if (!cached_refs.did_loose || cached_refs.submodule != submodule) {

Do you really mean "!=" here? I do not see anywhere that you are "intern"-ing (a la Lisp implementations) names of submodules to make address comparison work as a cheap equality check.

Well it was a quick and dirty way of getting the cache overwritten in case the pointer is different. See further below for the correction.

Quoted 16 lines

refs.c void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) · line 358

Quoted 2 lines
refs.c +1 -0
+		cached_refs.loose = get_ref_dir(submodule, "refs", NULL);

What happened to the old ref_list that had the refs from the toplevel project (or the last submodule you visited) if your "did_loose" is true? Leakage?

refs.c void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) · line 363

Quoted 5 lines
refs.c +1 -0
 		cached_refs.did_loose = 1;
+		cached_refs.submodule = submodule;
 	}
 	return cached_refs.loose;
 }

Once you grabbed loose refs for _any_ repository, you will have did_loose set, so the flag has now became pretty much useless.

Quoted 7 lines

More importantly, I wonder if you would instead want to have cached_refs structure for each submodule separately, or at least not nuke the cached_refs structure for the top-level project, only because you wanted to peek into one submodule. While your for_each_ref() is walking the refs of top-level project, its callback may stomp on the cached_refs by asking about submodule refs, and there is nothing in this code structure to help catching such a bug, is there?

Thanks for spotting this. I forgot that this part which was still pretty much in its quick and dirty state from the first implementation. I think currently it does not make much sense to apply the caching logic for submodule refs. In the merge case we iterate over them once so caching does not gain us anything here. I will drop it and write it like this:

Quoted 14 lines

static struct ref_list *get_loose_refs(const char *submodule)

{
	if (submodule) {
		free_ref_list(submodule_refs.loose);
		submodule_refs.loose = get_ref_dir(submodule, "refs", NULL);
		return submodule_refs.loose;
	}

	if (!cached_refs.did_loose) {
		cached_refs.loose = get_ref_dir(NULL, "refs", NULL);
		cached_refs.did_loose = 1;
	}
	return cached_refs.loose;
}

I think the same applies to the caching of packed refs:

Quoted 17 lines

static struct ref_list *get_packed_refs(const char *submodule)

{
	const char *packed_refs_file;
	struct cached_refs *refs;

	if (submodule) {
		packed_refs_file = git_path_submodule(submodule, "packed-refs");
		refs = &submodule_refs;
		free_ref_list(refs->packed);
	} else {
		packed_refs_file = git_path("packed-refs");
		refs = &cached_refs;
	}

	if (!refs->did_packed || submodule) {
		FILE *f = fopen(packed_refs_file, "r");
		refs->packed = NULL;
Quoted 8 lines
		if (f) {
			read_packed_refs(f, refs);
			fclose(f);
		}
		refs->did_packed = 1;
	}
	return refs->packed;
}

I also realized that I forgot to use the --ancestry-path option for setup_revisions() in this series. It will be added in the next one.

Anything else? I will send another iteration with corrected patches shortly.

cheers Heiko

#5 lore raw

PATCH 3/4 extent setup_revisions() so it works with submodules

In reply to Heiko Voigt

---

What I do not like about this patch is that I have to duplicate a whole set of functions to extend them for submodules. Anyone an idea how to solve this more elegantly?

 refs.c     |   31 +++++++++++++++++++++++++++++++
 refs.h     |    8 ++++++++
 revision.c |   32 ++++++++++++++++++--------------
 revision.h |    1 +
 4 files changed, 58 insertions(+), 14 deletions(-)
refs.c +31 -0
index 5a461d2..c2c1850 100644
@@ -712,31 +712,62 @@ int head_ref(each_ref_fn fn, void *cb_data)
 	return do_head_ref(NULL, fn, cb_data);
 }
 
+int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+{
+	return do_head_ref(submodule, fn, cb_data);
+}
+
 int for_each_ref(each_ref_fn fn, void *cb_data)
 {
 	return do_for_each_ref(NULL, "refs/", fn, 0, 0, cb_data);
 }
 
+int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+{
+	return do_for_each_ref(submodule, "refs/", fn, 0, 0, cb_data);
+}
+
 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
 {
 	return do_for_each_ref(NULL, prefix, fn, strlen(prefix), 0, cb_data);
 }
 
+int for_each_ref_in_submodule(const char *submodule, const char *prefix,
+		each_ref_fn fn, void *cb_data)
+{
+	return do_for_each_ref(submodule, prefix, fn, strlen(prefix), 0, cb_data);
+}
+
 int for_each_tag_ref(each_ref_fn fn, void *cb_data)
 {
 	return for_each_ref_in("refs/tags/", fn, cb_data);
 }
 
+int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+{
+	return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data);
+}
+
 int for_each_branch_ref(each_ref_fn fn, void *cb_data)
 {
 	return for_each_ref_in("refs/heads/", fn, cb_data);
 }
 
+int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+{
+	return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data);
+}
+
 int for_each_remote_ref(each_ref_fn fn, void *cb_data)
 {
 	return for_each_ref_in("refs/remotes/", fn, cb_data);
 }
 
+int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+{
+	return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data);
+}
+
 int for_each_replace_ref(each_ref_fn fn, void *cb_data)
 {
 	return do_for_each_ref(NULL, "refs/replace/", fn, 13, 0, cb_data);
refs.h +8 -0
index 762ce50..5e7a9a5 100644
@@ -28,6 +28,14 @@ extern int for_each_replace_ref(each_ref_fn, void *);
 extern int for_each_glob_ref(each_ref_fn, const char *pattern, void *);
 extern int for_each_glob_ref_in(each_ref_fn, const char *pattern, const char* prefix, void *);
 
+extern int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
+extern int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
+extern int for_each_ref_in_submodule(const char *submodule, const char *prefix,
+		each_ref_fn fn, void *cb_data);
+extern int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
+extern int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
+extern int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
+
 static inline const char *has_glob_specials(const char *pattern)
 {
 	return strpbrk(pattern, "?*[");
revision.c +18 -14
index b209d49..0a5d8f9 100644
@@ -721,12 +721,12 @@ static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
 	cb->all_flags = flags;
 }
 
-static void handle_refs(struct rev_info *revs, unsigned flags,
-		int (*for_each)(each_ref_fn, void *))
+static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
+		int (*for_each)(const char *, each_ref_fn, void *))
 {
 	struct all_refs_cb cb;
 	init_all_refs_cb(&cb, revs, flags);
-	for_each(handle_one_ref, &cb);
+	for_each(submodule, handle_one_ref, &cb);
 }
 
 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
@@ -1308,14 +1308,14 @@ void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
 	ctx->argc -= n;
 }
 
-static int for_each_bad_bisect_ref(each_ref_fn fn, void *cb_data)
+static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
 {
-	return for_each_ref_in("refs/bisect/bad", fn, cb_data);
+	return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
 }
 
-static int for_each_good_bisect_ref(each_ref_fn fn, void *cb_data)
+static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
 {
-	return for_each_ref_in("refs/bisect/good", fn, cb_data);
+	return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
 }
 
 static void append_prune_data(const char ***prune_data, const char **av)
@@ -1357,6 +1357,10 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
 {
 	int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0;
 	const char **prune_data = NULL;
+	const char *submodule = NULL;
+
+	if (opt)
+		submodule = opt->submodule;
 
 	/* First, search for "--" */
 	seen_dashdash = 0;
@@ -1381,26 +1385,26 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
 			int opts;
 
 			if (!strcmp(arg, "--all")) {
-				handle_refs(revs, flags, for_each_ref);
-				handle_refs(revs, flags, head_ref);
+				handle_refs(submodule, revs, flags, for_each_ref_submodule);
+				handle_refs(submodule, revs, flags, head_ref_submodule);
 				continue;
 			}
 			if (!strcmp(arg, "--branches")) {
-				handle_refs(revs, flags, for_each_branch_ref);
+				handle_refs(submodule, revs, flags, for_each_branch_ref_submodule);
 				continue;
 			}
 			if (!strcmp(arg, "--bisect")) {
-				handle_refs(revs, flags, for_each_bad_bisect_ref);
-				handle_refs(revs, flags ^ UNINTERESTING, for_each_good_bisect_ref);
+				handle_refs(submodule, revs, flags, for_each_bad_bisect_ref);
+				handle_refs(submodule, revs, flags ^ UNINTERESTING, for_each_good_bisect_ref);
 				revs->bisect = 1;
 				continue;
 			}
 			if (!strcmp(arg, "--tags")) {
-				handle_refs(revs, flags, for_each_tag_ref);
+				handle_refs(submodule, revs, flags, for_each_tag_ref_submodule);
 				continue;
 			}
 			if (!strcmp(arg, "--remotes")) {
-				handle_refs(revs, flags, for_each_remote_ref);
+				handle_refs(submodule, revs, flags, for_each_remote_ref_submodule);
 				continue;
 			}
 			if (!prefixcmp(arg, "--glob=")) {
revision.h +1 -0
index 568f1c9..0fe4322 100644
@@ -145,6 +145,7 @@ extern volatile show_early_output_fn_t show_early_output;
 struct setup_revision_opt {
 	const char *def;
 	void (*tweak)(struct rev_info *, struct setup_revision_opt *);
+	const char *submodule;
 };
 
 extern void init_revisions(struct rev_info *revs, const char *prefix);
Signature · 1 line
1.7.1.528.gb3958.dirty
#6 lore raw

PATCH 4/4 implement automatic fast forward merge for submodules

In reply to Heiko Voigt

This implements a simple merge strategy for submodule hashes. We check whether one side of the merge candidates is already contained in the other and then merge automatically.

If both sides contain changes we search for a merge in the submodule. In case a single one exists we check that out and suggest it as the merge resolution. A list of candidates is returned when we find multiple merges that contain both sides of the changes.

This is useful for a workflow in which the developers can publish topic branches in submodules and a seperate maintainer merges them. In case the developers always wait until their branch gets merged before tracking them in the superproject all merges of branches that contain submodule changes will be resolved automatically. If developers choose to track their feature branch the maintainer might get a conflict but git will search the submodule for a merge and suggest it/them as a resolution.

---
 merge-recursive.c          |    9 ++-
 submodule.c                |  154 ++++++++++++++++++++++++++++++++++++++++++++
 submodule.h                |    2 +
 t/t7405-submodule-merge.sh |  122 +++++++++++++++++++++++++++++++++--
 4 files changed, 278 insertions(+), 9 deletions(-)
merge-recursive.c +6 -3
index 206c103..a032a8b 100644
@@ -20,6 +20,7 @@
 #include "attr.h"
 #include "merge-recursive.h"
 #include "dir.h"
+#include "submodule.h"
 
 static struct tree *shift_tree_object(struct tree *one, struct tree *two,
 				      const char *subtree_shift)
@@ -525,13 +526,15 @@ static void update_file_flags(struct merge_options *o,
 		void *buf;
 		unsigned long size;
 
-		if (S_ISGITLINK(mode))
+		if (S_ISGITLINK(mode)) {
 			/*
 			 * We may later decide to recursively descend into
 			 * the submodule directory and update its index
 			 * and/or work tree, but we do not do that now.
 			 */
+			update_wd = 0;
 			goto update_index;
+		}
 
 		buf = read_sha1_file(sha, &type, &size);
 		if (!buf)
@@ -716,8 +719,8 @@ static struct merge_file_info merge_file(struct merge_options *o,
 			free(result_buf.ptr);
 			result.clean = (merge_status == 0);
 		} else if (S_ISGITLINK(a->mode)) {
-			result.clean = 0;
-			hashcpy(result.sha, a->sha1);
+			result.clean = merge_submodule(result.sha, one->path, one->sha1,
+						       a->sha1, b->sha1);
 		} else if (S_ISLNK(a->mode)) {
 			hashcpy(result.sha, a->sha1);
 
submodule.c +154 -0

163 lines not on this page · raw message

submodule.h +2 -0
index dbda270..b75a704 100644
@@ -6,5 +6,7 @@ void show_submodule_summary(FILE *f, const char *path,
 		unsigned dirty_submodule,
 		const char *del, const char *add, const char *reset);
 unsigned is_submodule_modified(const char *path, int ignore_untracked);
+int merge_submodule(unsigned char result[20], const char *path, const unsigned char base[20],
+		    const unsigned char a[20], const unsigned char b[20]);
 
 #endif
t/t7405-submodule-merge.sh +116 -6

129 lines not on this page · raw message

Signature · 1 line
1.7.1.528.gb3958.dirty