All of lore.kernel.org
 help / color / mirror / Atom feed
* [WIP/RFC 00/13] git notes merge
@ 2010-07-23 10:14 Johan Herland
  2010-07-23 10:14 ` [WIP/RFC 01/13] (trivial) notes.h: Minor documentation fixes to copy_notes() Johan Herland
                   ` (13 more replies)
  0 siblings, 14 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:14 UTC (permalink / raw)
  To: git; +Cc: johan

Hi,

Here are some rough patches implementing most of the 'git notes merge'
functionality. There are still missing pieces in the area of manual
conflict resolution, but what's here should be enough to start
experimenting with automatic notes merge resolution.

Patches #1 - #8 + #10 are a combination of general cleanup and
preparations for 'git notes merge'. Patch #9 implements handling
of trivial notes merges ("already up-to-date" and "fast-forward").
Patch #11 implements object-level merging of notes, using regular
three-way rules. Patch #12 adds a few automatic conflict resolvers
that can be used instead of manual conflict resolution (which has
yet to be implemented). Patch #13 adds some preliminary docs.

For now, this is probably only ready for those with a special
interest in notes.


Have fun! :)

...Johan


Johan Herland (13):
  (trivial) notes.h: Minor documentation fixes to copy_notes()
  notes.h: Make default_notes_ref() available in notes API
  notes.h/c: Clarify the handling of notes objects that are == null_sha1
  notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond
  (trivial) t3303: Indent with tabs instead of spaces for consistency
  notes.c: Use two newlines (instead of one) when concatenating notes
  (trivial) git notes prune: Accept --verbose in addition to -v
  builtin/notes.c: Split notes ref DWIMmery into a separate function
  git notes merge: Initial implementation handling trivial merges only
  builtin/notes.c: Refactor creation of notes commits.
  git notes merge: Handle real, non-conflicting notes merges
  git notes merge: Add automatic conflict resolvers (ours, theirs, union)
  Documentation: Preliminary docs on 'git notes merge'

 Documentation/git-notes.txt         |   30 ++-
 Makefile                            |    2 +
 builtin.h                           |    2 +-
 builtin/notes.c                     |  130 ++++++--
 notes-cache.c                       |    3 +-
 notes-merge.c                       |  474 +++++++++++++++++++++++++
 notes-merge.h                       |   59 +++
 notes.c                             |  196 ++++++-----
 notes.h                             |   46 +++-
 t/t3301-notes.sh                    |    4 +
 t/t3303-notes-subtrees.sh           |   19 +-
 t/t3308-notes-merge.sh              |  669 +++++++++++++++++++++++++++++++++++
 t/t3309-notes-merge-auto-resolve.sh |  512 +++++++++++++++++++++++++++
 t/t3404-rebase-interactive.sh       |    1 +
 t/t9301-fast-import-notes.sh        |    5 +
 15 files changed, 2014 insertions(+), 138 deletions(-)
 create mode 100644 notes-merge.c
 create mode 100644 notes-merge.h
 create mode 100755 t/t3308-notes-merge.sh
 create mode 100755 t/t3309-notes-merge-auto-resolve.sh

--
1.7.2.220.gea1d3

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [WIP/RFC 01/13] (trivial) notes.h: Minor documentation fixes to copy_notes()
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
@ 2010-07-23 10:14 ` Johan Herland
  2010-07-23 10:14 ` [WIP/RFC 02/13] notes.h: Make default_notes_ref() available in notes API Johan Herland
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:14 UTC (permalink / raw)
  To: git; +Cc: johan

Signed-off-by: Johan Herland <johan@herland.net>
---
 notes.h |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/notes.h b/notes.h
index 65fc3a6..c0288b0 100644
--- a/notes.h
+++ b/notes.h
@@ -104,6 +104,10 @@ const unsigned char *get_note(struct notes_tree *t,
  * Copy a note from one object to another in the given notes_tree.
  *
  * Fails if the to_obj already has a note unless 'force' is true.
+ *
+ * IMPORTANT: The changes made by copy_note() to the given notes_tree structure
+ * are not persistent until a subsequent call to write_notes_tree() returns
+ * zero.
  */
 int copy_note(struct notes_tree *t,
 	      const unsigned char *from_obj, const unsigned char *to_obj,
@@ -149,6 +153,7 @@ int copy_note(struct notes_tree *t,
  * notes tree) from within the callback:
  * - add_note()
  * - remove_note()
+ * - copy_note()
  * - free_notes()
  */
 typedef int each_note_fn(const unsigned char *object_sha1,
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 02/13] notes.h: Make default_notes_ref() available in notes API
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
  2010-07-23 10:14 ` [WIP/RFC 01/13] (trivial) notes.h: Minor documentation fixes to copy_notes() Johan Herland
@ 2010-07-23 10:14 ` Johan Herland
  2010-07-23 10:14 ` [WIP/RFC 03/13] notes.h/c: Clarify the handling of notes objects that are == null_sha1 Johan Herland
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:14 UTC (permalink / raw)
  To: git; +Cc: johan

Signed-off-by: Johan Herland <johan@herland.net>
---
 notes.c |    2 +-
 notes.h |   14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletions(-)

diff --git a/notes.c b/notes.c
index 1978244..79fd850 100644
--- a/notes.c
+++ b/notes.c
@@ -898,7 +898,7 @@ static int notes_display_config(const char *k, const char *v, void *cb)
 	return 0;
 }
 
-static const char *default_notes_ref(void)
+const char *default_notes_ref(void)
 {
 	const char *notes_ref = NULL;
 	if (!notes_ref)
diff --git a/notes.h b/notes.h
index c0288b0..20db42f 100644
--- a/notes.h
+++ b/notes.h
@@ -44,6 +44,20 @@ extern struct notes_tree {
 } default_notes_tree;
 
 /*
+ * Return the default notes ref.
+ *
+ * The default notes ref is the notes ref that is used when notes_ref == NULL
+ * is passed to init_notes().
+ *
+ * This the first of the following to be defined:
+ * 1. The '--ref' option to 'git notes', if given
+ * 2. The $GIT_NOTES_REF environment variable, if set
+ * 3. The value of the core.notesRef config variable, if set
+ * 4. GIT_NOTES_DEFAULT_REF (i.e. "refs/notes/commits")
+ */
+const char *default_notes_ref(void);
+
+/*
  * Flags controlling behaviour of notes tree initialization
  *
  * Default behaviour is to initialize the notes tree from the tree object
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 03/13] notes.h/c: Clarify the handling of notes objects that are == null_sha1
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
  2010-07-23 10:14 ` [WIP/RFC 01/13] (trivial) notes.h: Minor documentation fixes to copy_notes() Johan Herland
  2010-07-23 10:14 ` [WIP/RFC 02/13] notes.h: Make default_notes_ref() available in notes API Johan Herland
@ 2010-07-23 10:14 ` Johan Herland
  2010-07-23 10:14 ` [WIP/RFC 04/13] notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond Johan Herland
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:14 UTC (permalink / raw)
  To: git; +Cc: johan

Clearly specify how combine_notes functions are expected to handle null_sha1
in input, and how they may set the result to null_sha1 in order to cause
the removal of a note.

Also document that passing note_sha1 == null_sha1 to add_note() is usually
a no-op, except in cases where combining it with an existing note yields a
new/changed result.

The only functional changes in this patch concern the handling of null_sha1
in notes_tree_insert(). Otherwise the patch consists solely of reordering
functions in notes.c to avoid use-before-declaration, and adding
documentation to notes.h.

Signed-off-by: Johan Herland <johan@herland.net>
---
 notes.c |  158 +++++++++++++++++++++++++++++++++-----------------------------
 notes.h |   16 ++++++-
 2 files changed, 99 insertions(+), 75 deletions(-)

diff --git a/notes.c b/notes.c
index 79fd850..9e92021 100644
--- a/notes.c
+++ b/notes.c
@@ -150,6 +150,79 @@ static struct leaf_node *note_tree_find(struct notes_tree *t,
 }
 
 /*
+ * How to consolidate an int_node:
+ * If there are > 1 non-NULL entries, give up and return non-zero.
+ * Otherwise replace the int_node at the given index in the given parent node
+ * with the only entry (or a NULL entry if no entries) from the given tree,
+ * and return 0.
+ */
+static int note_tree_consolidate(struct int_node *tree,
+	struct int_node *parent, unsigned char index)
+{
+	unsigned int i;
+	void *p = NULL;
+
+	assert(tree && parent);
+	assert(CLR_PTR_TYPE(parent->a[index]) == tree);
+
+	for (i = 0; i < 16; i++) {
+		if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
+			if (p) /* more than one entry */
+				return -2;
+			p = tree->a[i];
+		}
+	}
+
+	/* replace tree with p in parent[index] */
+	parent->a[index] = p;
+	free(tree);
+	return 0;
+}
+
+/*
+ * To remove a leaf_node:
+ * Search to the tree location appropriate for the given leaf_node's key:
+ * - If location does not hold a matching entry, abort and do nothing.
+ * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
+ * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
+ */
+static void note_tree_remove(struct notes_tree *t, struct int_node *tree,
+		unsigned char n, struct leaf_node *entry)
+{
+	struct leaf_node *l;
+	struct int_node *parent_stack[20];
+	unsigned char i, j;
+	void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
+
+	assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
+	if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
+		return; /* type mismatch, nothing to remove */
+	l = (struct leaf_node *) CLR_PTR_TYPE(*p);
+	if (hashcmp(l->key_sha1, entry->key_sha1))
+		return; /* key mismatch, nothing to remove */
+
+	/* we have found a matching entry */
+	free(l);
+	*p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
+
+	/* consolidate this tree level, and parent levels, if possible */
+	if (!n)
+		return; /* cannot consolidate top level */
+	/* first, build stack of ancestors between root and current node */
+	parent_stack[0] = t->root;
+	for (i = 0; i < n; i++) {
+		j = GET_NIBBLE(i, entry->key_sha1);
+		parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
+	}
+	assert(i == n && parent_stack[i] == tree);
+	/* next, unwind stack until note_tree_consolidate() is done */
+	while (i > 0 &&
+	       !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
+				      GET_NIBBLE(i - 1, entry->key_sha1)))
+		i--;
+}
+
+/*
  * To insert a leaf_node:
  * Search to the tree location appropriate for the given leaf_node's key:
  * - If location is unused (NULL), store the tweaked pointer directly there
@@ -175,7 +248,10 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
 	switch (GET_PTR_TYPE(*p)) {
 	case PTR_TYPE_NULL:
 		assert(!*p);
-		*p = SET_PTR_TYPE(entry, type);
+		if (is_null_sha1(entry->val_sha1))
+			free(entry);
+		else
+			*p = SET_PTR_TYPE(entry, type);
 		return;
 	case PTR_TYPE_NOTE:
 		switch (type) {
@@ -191,6 +267,9 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
 					    sha1_to_hex(l->val_sha1),
 					    sha1_to_hex(entry->val_sha1),
 					    sha1_to_hex(l->key_sha1));
+
+				if (is_null_sha1(l->val_sha1))
+					note_tree_remove(t, tree, n, entry);
 				free(entry);
 				return;
 			}
@@ -222,6 +301,10 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
 	/* non-matching leaf_node */
 	assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
 	       GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
+	if (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */
+		free(entry);
+		return;
+	}
 	new_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1);
 	note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
 			 combine_notes);
@@ -229,79 +312,6 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
 	note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
 }
 
-/*
- * How to consolidate an int_node:
- * If there are > 1 non-NULL entries, give up and return non-zero.
- * Otherwise replace the int_node at the given index in the given parent node
- * with the only entry (or a NULL entry if no entries) from the given tree,
- * and return 0.
- */
-static int note_tree_consolidate(struct int_node *tree,
-	struct int_node *parent, unsigned char index)
-{
-	unsigned int i;
-	void *p = NULL;
-
-	assert(tree && parent);
-	assert(CLR_PTR_TYPE(parent->a[index]) == tree);
-
-	for (i = 0; i < 16; i++) {
-		if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
-			if (p) /* more than one entry */
-				return -2;
-			p = tree->a[i];
-		}
-	}
-
-	/* replace tree with p in parent[index] */
-	parent->a[index] = p;
-	free(tree);
-	return 0;
-}
-
-/*
- * To remove a leaf_node:
- * Search to the tree location appropriate for the given leaf_node's key:
- * - If location does not hold a matching entry, abort and do nothing.
- * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
- * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
- */
-static void note_tree_remove(struct notes_tree *t, struct int_node *tree,
-		unsigned char n, struct leaf_node *entry)
-{
-	struct leaf_node *l;
-	struct int_node *parent_stack[20];
-	unsigned char i, j;
-	void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
-
-	assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
-	if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
-		return; /* type mismatch, nothing to remove */
-	l = (struct leaf_node *) CLR_PTR_TYPE(*p);
-	if (hashcmp(l->key_sha1, entry->key_sha1))
-		return; /* key mismatch, nothing to remove */
-
-	/* we have found a matching entry */
-	free(l);
-	*p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
-
-	/* consolidate this tree level, and parent levels, if possible */
-	if (!n)
-		return; /* cannot consolidate top level */
-	/* first, build stack of ancestors between root and current node */
-	parent_stack[0] = t->root;
-	for (i = 0; i < n; i++) {
-		j = GET_NIBBLE(i, entry->key_sha1);
-		parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
-	}
-	assert(i == n && parent_stack[i] == tree);
-	/* next, unwind stack until note_tree_consolidate() is done */
-	while (i > 0 &&
-	       !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
-				      GET_NIBBLE(i - 1, entry->key_sha1)))
-		i--;
-}
-
 /* Free the entire notes data contained in the given tree */
 static void note_tree_free(struct int_node *tree)
 {
diff --git a/notes.h b/notes.h
index 20db42f..79ea797 100644
--- a/notes.h
+++ b/notes.h
@@ -12,7 +12,10 @@
  * resulting SHA1 into the first SHA1 argument (cur_sha1). A non-zero return
  * value indicates failure.
  *
- * The two given SHA1s must both be non-NULL and different from each other.
+ * The two given SHA1s shall both be non-NULL and different from each other.
+ * Either of them (but not both) may be == null_sha1, which indicates an
+ * empty/non-existent note. If the resulting SHA1 (cur_sha1) is == null_sha1,
+ * the note will be removed from the notes tree.
  *
  * The default combine_notes function (you get this when passing NULL) is
  * combine_notes_concatenate(), which appends the contents of the new note to
@@ -90,6 +93,17 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
 /*
  * Add the given note object to the given notes_tree structure
  *
+ * If there already exists a note for the given object_sha1, the given
+ * combine_notes function is invoked to break the tie. If not given (i.e.
+ * combine_notes == NULL), the default combine_notes function for the given
+ * notes_tree is used.
+ *
+ * Passing note_sha1 == null_sha1 indicates the addition of an
+ * empty/non-existent note. This is a (potentially expensive) no-op unless
+ * there already exists a note for the given object_sha1, AND combining that
+ * note with the empty note (using the given combine_notes function) results
+ * in a new/changed note.
+ *
  * IMPORTANT: The changes made by add_note() to the given notes_tree structure
  * are not persistent until a subsequent call to write_notes_tree() returns
  * zero.
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 04/13] notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (2 preceding siblings ...)
  2010-07-23 10:14 ` [WIP/RFC 03/13] notes.h/c: Clarify the handling of notes objects that are == null_sha1 Johan Herland
@ 2010-07-23 10:14 ` Johan Herland
  2010-07-23 15:23   ` Jonathan Nieder
  2010-07-23 10:14 ` [WIP/RFC 05/13] (trivial) t3303: Indent with tabs instead of spaces for consistency Johan Herland
                   ` (9 subsequent siblings)
  13 siblings, 1 reply; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:14 UTC (permalink / raw)
  To: git; +Cc: johan

The combine_notes_fn functions uses a non-zero return value to indicate
failure. However, this return value was converted to a call to die()
in note_tree_insert().

Instead, propagate this return value out to add_note(), and return it
from there to enable the caller to handle errors appropriately.

Signed-off-by: Johan Herland <johan@herland.net>
---
 builtin/notes.c |    3 ++
 notes-cache.c   |    3 +-
 notes.c         |   59 ++++++++++++++++++++++++++++--------------------------
 notes.h         |   11 +++++++--
 4 files changed, 43 insertions(+), 33 deletions(-)

diff --git a/builtin/notes.c b/builtin/notes.c
index 190005f..f653f59 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -574,6 +574,7 @@ static int add(int argc, const char **argv, const char *prefix)
 	if (is_null_sha1(new_note))
 		remove_note(t, object);
 	else
+		/* No return value checking; c_n_overwrite always returns 0 */
 		add_note(t, object, new_note, combine_notes_overwrite);
 
 	snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
@@ -653,6 +654,7 @@ static int copy(int argc, const char **argv, const char *prefix)
 		goto out;
 	}
 
+	/* No return value checking; c_n_overwrite always returns 0 */
 	add_note(t, object, from_note, combine_notes_overwrite);
 	commit_notes(t, "Notes added by 'git notes copy'");
 out:
@@ -713,6 +715,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
 	if (is_null_sha1(new_note))
 		remove_note(t, object);
 	else
+		/* No return value checking; c_n_overwrite always returns 0 */
 		add_note(t, object, new_note, combine_notes_overwrite);
 
 	snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
diff --git a/notes-cache.c b/notes-cache.c
index dee6d62..4c8984e 100644
--- a/notes-cache.c
+++ b/notes-cache.c
@@ -89,6 +89,5 @@ int notes_cache_put(struct notes_cache *c, unsigned char key_sha1[20],
 
 	if (write_sha1_file(data, size, "blob", value_sha1) < 0)
 		return -1;
-	add_note(&c->tree, key_sha1, value_sha1, NULL);
-	return 0;
+	return add_note(&c->tree, key_sha1, value_sha1, NULL);
 }
diff --git a/notes.c b/notes.c
index 9e92021..4f3d094 100644
--- a/notes.c
+++ b/notes.c
@@ -235,13 +235,14 @@ static void note_tree_remove(struct notes_tree *t, struct int_node *tree,
  * - Else, create a new int_node, holding both the node-at-location and the
  *   node-to-be-inserted, and store the new int_node into the location.
  */
-static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
+static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
 		unsigned char n, struct leaf_node *entry, unsigned char type,
 		combine_notes_fn combine_notes)
 {
 	struct int_node *new_node;
 	struct leaf_node *l;
 	void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
+	int ret = 0;
 
 	assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
 	l = (struct leaf_node *) CLR_PTR_TYPE(*p);
@@ -252,26 +253,21 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
 			free(entry);
 		else
 			*p = SET_PTR_TYPE(entry, type);
-		return;
+		return 0;
 	case PTR_TYPE_NOTE:
 		switch (type) {
 		case PTR_TYPE_NOTE:
 			if (!hashcmp(l->key_sha1, entry->key_sha1)) {
 				/* skip concatenation if l == entry */
 				if (!hashcmp(l->val_sha1, entry->val_sha1))
-					return;
+					return 0;
 
-				if (combine_notes(l->val_sha1, entry->val_sha1))
-					die("failed to combine notes %s and %s"
-					    " for object %s",
-					    sha1_to_hex(l->val_sha1),
-					    sha1_to_hex(entry->val_sha1),
-					    sha1_to_hex(l->key_sha1));
-
-				if (is_null_sha1(l->val_sha1))
+				ret = combine_notes(l->val_sha1,
+						    entry->val_sha1);
+				if (!ret && is_null_sha1(l->val_sha1))
 					note_tree_remove(t, tree, n, entry);
 				free(entry);
-				return;
+				return ret;
 			}
 			break;
 		case PTR_TYPE_SUBTREE:
@@ -280,7 +276,7 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
 				/* unpack 'entry' */
 				load_subtree(t, entry, tree, n);
 				free(entry);
-				return;
+				return 0;
 			}
 			break;
 		}
@@ -291,9 +287,8 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
 			*p = NULL;
 			load_subtree(t, l, tree, n);
 			free(l);
-			note_tree_insert(t, tree, n, entry, type,
-					 combine_notes);
-			return;
+			return note_tree_insert(t, tree, n, entry, type,
+						combine_notes);
 		}
 		break;
 	}
@@ -303,13 +298,17 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
 	       GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
 	if (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */
 		free(entry);
-		return;
+		return 0;
 	}
 	new_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1);
-	note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
-			 combine_notes);
-	*p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
-	note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
+	ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
+			       combine_notes);
+	if (!ret) {
+		*p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
+		ret = note_tree_insert(t, new_node, n + 1, entry, type,
+				       combine_notes);
+	}
+	return ret;
 }
 
 /* Free the entire notes data contained in the given tree */
@@ -452,8 +451,12 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 				l->key_sha1[19] = (unsigned char) len;
 				type = PTR_TYPE_SUBTREE;
 			}
-			note_tree_insert(t, node, n, l, type,
-					 combine_notes_concatenate);
+			if (note_tree_insert(t, node, n, l, type,
+					     combine_notes_concatenate))
+				die("Failed to load %s %s into notes tree "
+				    "from %s",
+				    type == PTR_TYPE_NOTE ? "note" : "subtree",
+				    sha1_to_hex(l->key_sha1), t->ref);
 		}
 		continue;
 
@@ -1014,7 +1017,7 @@ void init_display_notes(struct display_notes_opt *opt)
 	string_list_clear(&display_notes_refs, 0);
 }
 
-void add_note(struct notes_tree *t, const unsigned char *object_sha1,
+int add_note(struct notes_tree *t, const unsigned char *object_sha1,
 		const unsigned char *note_sha1, combine_notes_fn combine_notes)
 {
 	struct leaf_node *l;
@@ -1028,7 +1031,7 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1,
 	l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
 	hashcpy(l->key_sha1, object_sha1);
 	hashcpy(l->val_sha1, note_sha1);
-	note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
+	return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
 }
 
 void remove_note(struct notes_tree *t, const unsigned char *object_sha1)
@@ -1204,7 +1207,7 @@ void format_display_notes(const unsigned char *object_sha1,
 
 int copy_note(struct notes_tree *t,
 	      const unsigned char *from_obj, const unsigned char *to_obj,
-	      int force, combine_notes_fn combine_fn)
+	      int force, combine_notes_fn combine_notes)
 {
 	const unsigned char *note = get_note(t, from_obj);
 	const unsigned char *existing_note = get_note(t, to_obj);
@@ -1213,9 +1216,9 @@ int copy_note(struct notes_tree *t,
 		return 1;
 
 	if (note)
-		add_note(t, to_obj, note, combine_fn);
+		return add_note(t, to_obj, note, combine_notes);
 	else if (existing_note)
-		add_note(t, to_obj, null_sha1, combine_fn);
+		return add_note(t, to_obj, null_sha1, combine_notes);
 
 	return 0;
 }
diff --git a/notes.h b/notes.h
index 79ea797..b372575 100644
--- a/notes.h
+++ b/notes.h
@@ -104,11 +104,13 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
  * note with the empty note (using the given combine_notes function) results
  * in a new/changed note.
  *
+ * Returns zero on success; non-zero means combine_notes failed.
+ *
  * IMPORTANT: The changes made by add_note() to the given notes_tree structure
  * are not persistent until a subsequent call to write_notes_tree() returns
  * zero.
  */
-void add_note(struct notes_tree *t, const unsigned char *object_sha1,
+int add_note(struct notes_tree *t, const unsigned char *object_sha1,
 		const unsigned char *note_sha1, combine_notes_fn combine_notes);
 
 /*
@@ -131,7 +133,10 @@ const unsigned char *get_note(struct notes_tree *t,
 /*
  * Copy a note from one object to another in the given notes_tree.
  *
- * Fails if the to_obj already has a note unless 'force' is true.
+ * Returns 1 if the to_obj already has a note and 'force' is false. Otherwise,
+ * returns non-zero if 'force' is true, but the given combine_notes function
+ * failed to combine from_obj's note with to_obj's existing note.
+ * Returns zero on success.
  *
  * IMPORTANT: The changes made by copy_note() to the given notes_tree structure
  * are not persistent until a subsequent call to write_notes_tree() returns
@@ -139,7 +144,7 @@ const unsigned char *get_note(struct notes_tree *t,
  */
 int copy_note(struct notes_tree *t,
 	      const unsigned char *from_obj, const unsigned char *to_obj,
-	      int force, combine_notes_fn combine_fn);
+	      int force, combine_notes_fn combine_notes);
 
 /*
  * Flags controlling behaviour of for_each_note()
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 05/13] (trivial) t3303: Indent with tabs instead of spaces for consistency
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (3 preceding siblings ...)
  2010-07-23 10:14 ` [WIP/RFC 04/13] notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond Johan Herland
@ 2010-07-23 10:14 ` Johan Herland
  2010-07-23 10:14 ` [WIP/RFC 06/13] notes.c: Use two newlines (instead of one) when concatenating notes Johan Herland
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:14 UTC (permalink / raw)
  To: git; +Cc: johan

The rest of the file uses tabs for indenting. Fix the one function
that doesn't.

Signed-off-by: Johan Herland <johan@herland.net>
---
 t/t3303-notes-subtrees.sh |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/t/t3303-notes-subtrees.sh b/t/t3303-notes-subtrees.sh
index 75ec187..d571708 100755
--- a/t/t3303-notes-subtrees.sh
+++ b/t/t3303-notes-subtrees.sh
@@ -168,15 +168,15 @@ INPUT_END
 }
 
 verify_concatenated_notes () {
-    git log | grep "^    " > output &&
-    i=$number_of_commits &&
-    while [ $i -gt 0 ]; do
-        echo "    commit #$i" &&
-        echo "    first note for commit #$i" &&
-        echo "    second note for commit #$i" &&
-        i=$(($i-1));
-    done > expect &&
-    test_cmp expect output
+	git log | grep "^    " > output &&
+	i=$number_of_commits &&
+	while [ $i -gt 0 ]; do
+		echo "    commit #$i" &&
+		echo "    first note for commit #$i" &&
+		echo "    second note for commit #$i" &&
+		i=$(($i-1));
+	done > expect &&
+	test_cmp expect output
 }
 
 test_expect_success 'test notes in no fanout concatenated with 2/38-fanout' 'test_concatenated_notes "s|^..|&/|" ""'
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 06/13] notes.c: Use two newlines (instead of one) when concatenating notes
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (4 preceding siblings ...)
  2010-07-23 10:14 ` [WIP/RFC 05/13] (trivial) t3303: Indent with tabs instead of spaces for consistency Johan Herland
@ 2010-07-23 10:14 ` Johan Herland
  2010-07-23 10:14 ` [WIP/RFC 07/13] (trivial) git notes prune: Accept --verbose in addition to -v Johan Herland
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:14 UTC (permalink / raw)
  To: git; +Cc: johan

When using combine_notes_concatenate() to concatenate notes, it currently
ensures exactly one newline character between the given notes. However,
when using builtin/notes.c:create_note() to concatenate notes (e.g. by
'git notes append'), it adds a newline character to the trailing newline
of the preceding notes object, thus resulting in _two_ newlines (aka. a
blank line) separating contents of the two notes.

This patch brings combine_notes_concatenate() into consistency with
builtin/notes.c:create_note(), by ensuring exactly _two_ newline characters
between concatenated notes.

The patch also changes a few notes-related selftests accordingly.

Signed-off-by: Johan Herland <johan@herland.net>
---
 notes.c                       |    7 ++++---
 t/t3301-notes.sh              |    4 ++++
 t/t3303-notes-subtrees.sh     |    1 +
 t/t3404-rebase-interactive.sh |    1 +
 t/t9301-fast-import-notes.sh  |    5 +++++
 5 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/notes.c b/notes.c
index 4f3d094..28afe2c 100644
--- a/notes.c
+++ b/notes.c
@@ -814,16 +814,17 @@ int combine_notes_concatenate(unsigned char *cur_sha1,
 		return 0;
 	}
 
-	/* we will separate the notes by a newline anyway */
+	/* we will separate the notes by two newlines anyway */
 	if (cur_msg[cur_len - 1] == '\n')
 		cur_len--;
 
 	/* concatenate cur_msg and new_msg into buf */
-	buf_len = cur_len + 1 + new_len;
+	buf_len = cur_len + 2 + new_len;
 	buf = (char *) xmalloc(buf_len);
 	memcpy(buf, cur_msg, cur_len);
 	buf[cur_len] = '\n';
-	memcpy(buf + cur_len + 1, new_msg, new_len);
+	buf[cur_len + 1] = '\n';
+	memcpy(buf + cur_len + 2, new_msg, new_len);
 	free(cur_msg);
 	free(new_msg);
 
diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh
index 1d82f79..4bf4e52 100755
--- a/t/t3301-notes.sh
+++ b/t/t3301-notes.sh
@@ -955,6 +955,7 @@ Date:   Thu Apr 7 15:27:13 2005 -0700
 
 Notes (other):
     a fresh note
+$whitespace
     another fresh note
 EOF
 
@@ -976,8 +977,11 @@ Date:   Thu Apr 7 15:27:13 2005 -0700
 
 Notes (other):
     a fresh note
+$whitespace
     another fresh note
+$whitespace
     append 1
+$whitespace
     append 2
 EOF
 
diff --git a/t/t3303-notes-subtrees.sh b/t/t3303-notes-subtrees.sh
index d571708..704aee8 100755
--- a/t/t3303-notes-subtrees.sh
+++ b/t/t3303-notes-subtrees.sh
@@ -173,6 +173,7 @@ verify_concatenated_notes () {
 	while [ $i -gt 0 ]; do
 		echo "    commit #$i" &&
 		echo "    first note for commit #$i" &&
+		echo "    " &&
 		echo "    second note for commit #$i" &&
 		i=$(($i-1));
 	done > expect &&
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index 47ca88f..4b004bc 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -593,6 +593,7 @@ test_expect_success 'rebase -i can copy notes' '
 
 cat >expect <<EOF
 an earlier note
+
 a note
 EOF
 
diff --git a/t/t9301-fast-import-notes.sh b/t/t9301-fast-import-notes.sh
index a5c99d8..7cf8cd8 100755
--- a/t/t9301-fast-import-notes.sh
+++ b/t/t9301-fast-import-notes.sh
@@ -255,13 +255,18 @@ EOF
 
 INPUT_END
 
+whitespace="    "
+
 cat >expect <<EXPECT_END
     fourth commit
     pre-prefix of note for fourth commit
+$whitespace
     prefix of note for fourth commit
+$whitespace
     third note for fourth commit
     third commit
     prefix of note for third commit
+$whitespace
     third note for third commit
     second commit
     third note for second commit
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 07/13] (trivial) git notes prune: Accept --verbose in addition to -v
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (5 preceding siblings ...)
  2010-07-23 10:14 ` [WIP/RFC 06/13] notes.c: Use two newlines (instead of one) when concatenating notes Johan Herland
@ 2010-07-23 10:14 ` Johan Herland
  2010-07-23 10:14 ` [WIP/RFC 08/13] builtin/notes.c: Split notes ref DWIMmery into a separate function Johan Herland
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:14 UTC (permalink / raw)
  To: git; +Cc: johan

Signed-off-by: Johan Herland <johan@herland.net>
---
 Documentation/git-notes.txt |    4 +++-
 builtin/notes.c             |    2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt
index 5540af5..ce0ea03 100644
--- a/Documentation/git-notes.txt
+++ b/Documentation/git-notes.txt
@@ -133,7 +133,9 @@ OPTIONS
 	would be removed.
 
 -v::
-	Report all object names whose notes are removed.
+--verbose::
+	When pruning notes, report all object names whose notes are
+	removed.
 
 
 DISCUSSION
diff --git a/builtin/notes.c b/builtin/notes.c
index f653f59..888a9e3 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -802,7 +802,7 @@ static int prune(int argc, const char **argv, const char *prefix)
 	int show_only = 0, verbose = 0;
 	struct option options[] = {
 		OPT_BOOLEAN('n', NULL, &show_only, "do not remove, show only"),
-		OPT_BOOLEAN('v', NULL, &verbose, "report pruned notes"),
+		OPT_BOOLEAN('v', "verbose", &verbose, "report pruned notes"),
 		OPT_END()
 	};
 
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 08/13] builtin/notes.c: Split notes ref DWIMmery into a separate function
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (6 preceding siblings ...)
  2010-07-23 10:14 ` [WIP/RFC 07/13] (trivial) git notes prune: Accept --verbose in addition to -v Johan Herland
@ 2010-07-23 10:14 ` Johan Herland
  2010-07-23 10:15 ` [WIP/RFC 09/13] git notes merge: Initial implementation handling trivial merges only Johan Herland
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:14 UTC (permalink / raw)
  To: git; +Cc: johan

expand_notes_ref() is a new function that performs the DWIM transformation
of "foo" -> "refs/notes/foo" where notes refs are expected.

This is done in preparation for future patches which will also need this
DWIM functionality.

Signed-off-by: Johan Herland <johan@herland.net>
---
 builtin/notes.c |   17 +++++++++++------
 1 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/builtin/notes.c b/builtin/notes.c
index 888a9e3..e19042c 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -83,6 +83,16 @@ struct msg_arg {
 	struct strbuf buf;
 };
 
+static void expand_notes_ref(struct strbuf *sb)
+{
+	if (!prefixcmp(sb->buf, "refs/notes/"))
+		return; /* we're happy */
+	else if (!prefixcmp(sb->buf, "notes/"))
+		strbuf_insert(sb, 0, "refs/", 5);
+	else
+		strbuf_insert(sb, 0, "refs/notes/", 11);
+}
+
 static int list_each_note(const unsigned char *object_sha1,
 		const unsigned char *note_sha1, char *note_path,
 		void *cb_data)
@@ -840,13 +850,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
 
 	if (override_notes_ref) {
 		struct strbuf sb = STRBUF_INIT;
-		if (!prefixcmp(override_notes_ref, "refs/notes/"))
-			/* we're happy */;
-		else if (!prefixcmp(override_notes_ref, "notes/"))
-			strbuf_addstr(&sb, "refs/");
-		else
-			strbuf_addstr(&sb, "refs/notes/");
 		strbuf_addstr(&sb, override_notes_ref);
+		expand_notes_ref(&sb);
 		setenv("GIT_NOTES_REF", sb.buf, 1);
 		strbuf_release(&sb);
 	}
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 09/13] git notes merge: Initial implementation handling trivial merges only
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (7 preceding siblings ...)
  2010-07-23 10:14 ` [WIP/RFC 08/13] builtin/notes.c: Split notes ref DWIMmery into a separate function Johan Herland
@ 2010-07-23 10:15 ` Johan Herland
  2010-07-24  8:32   ` Stephen Boyd
  2010-07-23 10:15 ` [WIP/RFC 10/13] builtin/notes.c: Refactor creation of notes commits Johan Herland
                   ` (4 subsequent siblings)
  13 siblings, 1 reply; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:15 UTC (permalink / raw)
  To: git; +Cc: johan

This initial implementation of 'git notes merge' only handles the trivial
merge cases (i.e. where the merge is either a no-op, or a fast-forward).

The patch includes testcases for these trivial merge cases.

Future patches will extend the functionality of 'git notes merge'.

Signed-off-by: Johan Herland <johan@herland.net>
---
 Makefile               |    2 +
 builtin/notes.c        |   56 +++++++++
 notes-merge.c          |  103 ++++++++++++++++
 notes-merge.h          |   30 +++++
 t/t3308-notes-merge.sh |  313 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 504 insertions(+), 0 deletions(-)
 create mode 100644 notes-merge.c
 create mode 100644 notes-merge.h
 create mode 100755 t/t3308-notes-merge.sh

diff --git a/Makefile b/Makefile
index bc3c570..8a2a767 100644
--- a/Makefile
+++ b/Makefile
@@ -503,6 +503,7 @@ LIB_H += mailmap.h
 LIB_H += merge-recursive.h
 LIB_H += notes.h
 LIB_H += notes-cache.h
+LIB_H += notes-merge.h
 LIB_H += object.h
 LIB_H += pack.h
 LIB_H += pack-refs.h
@@ -593,6 +594,7 @@ LIB_OBJS += merge-recursive.o
 LIB_OBJS += name-hash.o
 LIB_OBJS += notes.o
 LIB_OBJS += notes-cache.o
+LIB_OBJS += notes-merge.o
 LIB_OBJS += object.o
 LIB_OBJS += pack-check.o
 LIB_OBJS += pack-refs.o
diff --git a/builtin/notes.c b/builtin/notes.c
index e19042c..3a15666 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -17,6 +17,7 @@
 #include "run-command.h"
 #include "parse-options.h"
 #include "string-list.h"
+#include "notes-merge.h"
 
 static const char * const git_notes_usage[] = {
 	"git notes [--ref <notes_ref>] [list [<object>]]",
@@ -25,6 +26,7 @@ static const char * const git_notes_usage[] = {
 	"git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
 	"git notes [--ref <notes_ref>] edit [<object>]",
 	"git notes [--ref <notes_ref>] show [<object>]",
+	"git notes [--ref <notes_ref>] merge [-v | -q] <notes_ref>",
 	"git notes [--ref <notes_ref>] remove [<object>]",
 	"git notes [--ref <notes_ref>] prune [-n | -v]",
 	NULL
@@ -61,6 +63,11 @@ static const char * const git_notes_show_usage[] = {
 	NULL
 };
 
+static const char * const git_notes_merge_usage[] = {
+	"git notes merge [<options>] <notes_ref>",
+	NULL
+};
+
 static const char * const git_notes_remove_usage[] = {
 	"git notes remove [<object>]",
 	NULL
@@ -774,6 +781,53 @@ static int show(int argc, const char **argv, const char *prefix)
 	return retval;
 }
 
+static int merge(int argc, const char **argv, const char *prefix)
+{
+	struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
+	unsigned char result_sha1[20];
+	struct notes_merge_options o;
+	int verbosity = 0, result;
+	struct option options[] = {
+		OPT__VERBOSITY(&verbosity),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_notes_merge_usage, 0);
+
+	if (1 < argc) {
+		error("too many parameters");
+		usage_with_options(git_notes_merge_usage, options);
+	} else if (1 > argc) {
+		error("too few parameters");
+		usage_with_options(git_notes_merge_usage, options);
+	}
+
+	init_notes_merge_options(&o);
+	o.verbosity = verbosity + 2; // default verbosity level is 2
+	o.local_ref = default_notes_ref();
+	strbuf_addstr(&remote_ref, argv[0]);
+	expand_notes_ref(&remote_ref);
+	o.remote_ref = remote_ref.buf;
+
+	result = notes_merge(&o, result_sha1);
+
+	strbuf_addf(&msg, "notes: Merged notes from %s into %s",
+		    remote_ref.buf, default_notes_ref());
+	if (result == 0) { /* Merge resulted (trivially) in result_sha1 */
+		/* Update default notes ref with new commit */
+		update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
+			   0, DIE_ON_ERR);
+	} else {
+		/* TODO: */
+		die("'git notes merge' cannot yet handle non-trivial merges!");
+	}
+
+	strbuf_release(&remote_ref);
+	strbuf_release(&msg);
+	return 0;
+}
+
 static int remove_cmd(int argc, const char **argv, const char *prefix)
 {
 	struct option options[] = {
@@ -866,6 +920,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
 		result = append_edit(argc, argv, prefix);
 	else if (!strcmp(argv[0], "show"))
 		result = show(argc, argv, prefix);
+	else if (!strcmp(argv[0], "merge"))
+		result = merge(argc, argv, prefix);
 	else if (!strcmp(argv[0], "remove"))
 		result = remove_cmd(argc, argv, prefix);
 	else if (!strcmp(argv[0], "prune"))
diff --git a/notes-merge.c b/notes-merge.c
new file mode 100644
index 0000000..5461827
--- /dev/null
+++ b/notes-merge.c
@@ -0,0 +1,103 @@
+#include "cache.h"
+#include "commit.h"
+#include "notes-merge.h"
+
+void init_notes_merge_options(struct notes_merge_options *o)
+{
+	memset(o, 0, sizeof(struct notes_merge_options));
+	o->verbosity = 2;
+}
+
+static int show(struct notes_merge_options *o, int v)
+{
+	return (o->verbosity >= v) || o->verbosity >= 5;
+}
+
+#define OUTPUT(o, v, ...) \
+	do { if (show((o), (v))) { printf(__VA_ARGS__); puts(""); } } while (0)
+
+int notes_merge(struct notes_merge_options *o,
+		unsigned char *result_sha1)
+{
+	unsigned char local_sha1[20], remote_sha1[20];
+	struct commit *local, *remote;
+	struct commit_list *bases = NULL;
+	const unsigned char *base_sha1;
+	int result = 0;
+
+	hashclr(result_sha1);
+
+	OUTPUT(o, 5, "notes_merge(o->local_ref = %s, o->remote_ref = %s)",
+	       o->local_ref, o->remote_ref);
+
+	if (!o->local_ref || get_sha1(o->local_ref, local_sha1)) {
+		/* empty notes ref => assume empty notes tree */
+		hashclr(local_sha1);
+		local = NULL;
+	} else if (!(local = lookup_commit_reference(local_sha1)))
+		die("Could not parse commit '%s'.", o->local_ref);
+	OUTPUT(o, 5, "\tlocal commit: %.7s", sha1_to_hex(local_sha1));
+
+	if (!o->remote_ref || get_sha1(o->remote_ref, remote_sha1)) {
+		/* empty notes ref => assume empty notes tree */
+		hashclr(remote_sha1);
+		remote = NULL;
+	}
+	if (!(remote = lookup_commit_reference(remote_sha1)))
+		die("Could not parse commit '%s'.", o->remote_ref);
+	OUTPUT(o, 5, "\tremote commit: %.7s", sha1_to_hex(remote_sha1));
+
+	if (!local) {
+		/* result == remote commit */
+		hashcpy(result_sha1, remote_sha1);
+		goto found_result;
+	}
+	if (!remote) {
+		/* result == local commit */
+		hashcpy(result_sha1, local_sha1);
+		goto found_result;
+	}
+	assert(local && remote);
+
+	/* Find merge bases */
+	bases = get_merge_bases(local, remote, 1);
+	if (!bases) {
+		base_sha1 = null_sha1;
+		OUTPUT(o, 4, "No merge base found; doing history-less merge");
+	} else if (!bases->next) {
+		base_sha1 = bases->item->object.sha1;
+		OUTPUT(o, 4, "One merge base found (%.7s)",
+		       sha1_to_hex(base_sha1));
+	} else {
+		/* TODO: How to handle multiple merge-bases? */
+		base_sha1 = bases->item->object.sha1;
+		OUTPUT(o, 3, "Multiple merge bases found. Using the first "
+		       "(%.7s)", sha1_to_hex(base_sha1));
+	}
+
+	OUTPUT(o, 4, "Merging remote commit %.7s into local commit %.7s with "
+	       "merge-base %.7s", sha1_to_hex(remote->object.sha1),
+	       sha1_to_hex(local->object.sha1), sha1_to_hex(base_sha1));
+
+	if (!hashcmp(remote->object.sha1, base_sha1)) {
+		/* Already merged; result == local commit */
+		OUTPUT(o, 2, "Already up-to-date!");
+		hashcpy(result_sha1, local->object.sha1);
+		goto found_result;
+	}
+	if (!hashcmp(local->object.sha1, base_sha1)) {
+		/* Fast-forward; result == remote commit */
+		OUTPUT(o, 2, "Fast-forward");
+		hashcpy(result_sha1, remote->object.sha1);
+		goto found_result;
+	}
+
+	/* TODO: */
+	result = error("notes_merge() cannot yet handle real merges.");
+
+found_result:
+	free_commit_list(bases);
+	OUTPUT(o, 5, "notes_merge(): result = %i, result_sha1 = %.7s",
+	       result, sha1_to_hex(result_sha1));
+	return result;
+}
diff --git a/notes-merge.h b/notes-merge.h
new file mode 100644
index 0000000..c4416e1
--- /dev/null
+++ b/notes-merge.h
@@ -0,0 +1,30 @@
+#ifndef NOTES_MERGE_H
+#define NOTES_MERGE_H
+
+struct notes_merge_options {
+	const char *local_ref;
+	const char *remote_ref;
+	int verbosity;
+};
+
+void init_notes_merge_options(struct notes_merge_options *o);
+
+/*
+ * Merge notes from o->remote_ref into o->local_ref
+ *
+ * The commits given by the two refs are merged, producing one of the following
+ * outcomes:
+ *
+ * 1. The merge trivially results in an existing commit (e.g. fast-forward or
+ *    already-up-to-date). The SHA1 of the result is written into 'result_sha1'
+ *    and 0 is returned.
+ * 2. The merge fails. result_sha1 is set to null_sha1, and non-zero returned.
+ *
+ * Either ref (but not both) may not exist in which case the missing ref is
+ * interpreted as an empty notes tree, and the merge trivially results in
+ * what the other ref points to.
+ */
+int notes_merge(struct notes_merge_options *o,
+		unsigned char *result_sha1);
+
+#endif
diff --git a/t/t3308-notes-merge.sh b/t/t3308-notes-merge.sh
new file mode 100755
index 0000000..a89fd11
--- /dev/null
+++ b/t/t3308-notes-merge.sh
@@ -0,0 +1,313 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Johan Herland
+#
+
+test_description='Test merging of notes trees'
+
+. ./test-lib.sh
+
+whitespace="    "
+
+test_expect_success setup '
+	git config core.notesRef refs/notes/x &&
+	: > a1 &&
+	git add a1 &&
+	test_tick &&
+	git commit -m 1st &&
+	git notes add -m "Notes on 1st commit" &&
+	: > a2 &&
+	git add a2 &&
+	test_tick &&
+	git commit -m 2nd &&
+	git notes add -m "Notes on 2nd commit" &&
+	: > a3 &&
+	git add a3 &&
+	test_tick &&
+	git commit -m 3rd &&
+	git notes add -m "Notes on 3rd commit" &&
+	: > a4 &&
+	git add a4 &&
+	test_tick &&
+	git commit -m 4th &&
+	git notes add -m "Notes on 4th commit" &&
+	: > a5 &&
+	git add a5 &&
+	test_tick &&
+	git commit -m 5th
+'
+
+cat >expect_notes_x <<EOF
+5e93d24084d32e1cb61f7070505b9d2530cca987 15023535574ded8b1a89052b32673f84cf9582b8
+8366731eeee53787d2bdf8fc1eff7d94757e8da0 1584215f1d29c65e99c6c6848626553fdd07fd75
+eede89064cd42441590d6afec6c37b321ada3389 268048bfb8a1fb38e703baceb8ab235421bf80c5
+daa55ffad6cb99bf64226532147ffcaf5ce8bdd1 34b09d6ffa51a8a03203627f0e369f607227364f
+EOF
+
+cat >expect_log_x <<EOF
+commit bd1753200303d0a0344be813e504253b3d98e74d
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:17:13 2005 -0700
+
+    5th
+
+commit 15023535574ded8b1a89052b32673f84cf9582b8
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:16:13 2005 -0700
+
+    4th
+
+Notes (x):
+    Notes on 4th commit
+
+commit 1584215f1d29c65e99c6c6848626553fdd07fd75
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:15:13 2005 -0700
+
+    3rd
+
+Notes (x):
+    Notes on 3rd commit
+
+commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:14:13 2005 -0700
+
+    2nd
+
+Notes (x):
+    Notes on 2nd commit
+
+commit 34b09d6ffa51a8a03203627f0e369f607227364f
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:13:13 2005 -0700
+
+    1st
+
+Notes (x):
+    Notes on 1st commit
+EOF
+
+test_expect_success 'verify initial notes (x)' '
+	git notes >output_notes_x &&
+	test_cmp expect_notes_x output_notes_x &&
+	git log >output_log_x &&
+	test_cmp expect_log_x output_log_x
+'
+
+cp expect_notes_x expect_notes_y
+
+cat >expect_log_y <<EOF
+commit bd1753200303d0a0344be813e504253b3d98e74d
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:17:13 2005 -0700
+
+    5th
+
+commit 15023535574ded8b1a89052b32673f84cf9582b8
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:16:13 2005 -0700
+
+    4th
+
+Notes (y):
+    Notes on 4th commit
+
+commit 1584215f1d29c65e99c6c6848626553fdd07fd75
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:15:13 2005 -0700
+
+    3rd
+
+Notes (y):
+    Notes on 3rd commit
+
+commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:14:13 2005 -0700
+
+    2nd
+
+Notes (y):
+    Notes on 2nd commit
+
+commit 34b09d6ffa51a8a03203627f0e369f607227364f
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:13:13 2005 -0700
+
+    1st
+
+Notes (y):
+    Notes on 1st commit
+EOF
+
+test_expect_success 'merge notes into empty notes ref (x => y)' '
+	git config core.notesRef refs/notes/y &&
+	git notes merge x &&
+	git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	git log >output_log_y &&
+	test_cmp expect_log_y output_log_y &&
+	# x and y should point to the same notes commit
+	test "$(git rev-parse refs/notes/x)" = "$(git rev-parse refs/notes/y)"
+'
+
+test_expect_success 'change notes on other notes ref (y)' '
+	# Leave notes to 1st commit alone
+	# Remove notes from 2nd commit
+	git notes remove HEAD~3 &&
+	# Append to 3rd commit notes
+	git notes append -m "More notes on 3rd commit" HEAD~2 &&
+	# Replace 4th commit notes
+	git notes add -f -m "New notes on 4th commit" HEAD^ &&
+	# Add new notes to 5th commit
+	git notes add -m "Notes on 5th commit" HEAD
+'
+
+cat >expect_notes_y <<EOF
+dec2502dac3ea161543f71930044deff93fa945c 15023535574ded8b1a89052b32673f84cf9582b8
+4069cdb399fd45463ec6eef8e051a16a03592d91 1584215f1d29c65e99c6c6848626553fdd07fd75
+daa55ffad6cb99bf64226532147ffcaf5ce8bdd1 34b09d6ffa51a8a03203627f0e369f607227364f
+0f2efbd00262f2fd41dfae33df8765618eeacd99 bd1753200303d0a0344be813e504253b3d98e74d
+EOF
+
+cat >expect_log_y <<EOF
+commit bd1753200303d0a0344be813e504253b3d98e74d
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:17:13 2005 -0700
+
+    5th
+
+Notes (y):
+    Notes on 5th commit
+
+commit 15023535574ded8b1a89052b32673f84cf9582b8
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:16:13 2005 -0700
+
+    4th
+
+Notes (y):
+    New notes on 4th commit
+
+commit 1584215f1d29c65e99c6c6848626553fdd07fd75
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:15:13 2005 -0700
+
+    3rd
+
+Notes (y):
+    Notes on 3rd commit
+$whitespace
+    More notes on 3rd commit
+
+commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:14:13 2005 -0700
+
+    2nd
+
+commit 34b09d6ffa51a8a03203627f0e369f607227364f
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:13:13 2005 -0700
+
+    1st
+
+Notes (y):
+    Notes on 1st commit
+EOF
+
+test_expect_success 'verify changed notes on other notes ref (y)' '
+	git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	git log >output_log_y &&
+	test_cmp expect_log_y output_log_y
+'
+
+test_expect_success 'verify unchanged notes on original notes ref (x)' '
+	GIT_NOTES_REF=refs/notes/x git notes >output_notes_x &&
+	test_cmp expect_notes_x output_notes_x &&
+	GIT_NOTES_REF=refs/notes/x git log >output_log_x &&
+	test_cmp expect_log_x output_log_x
+'
+
+test_expect_success 'merge original notes (x) into changed notes (y) => No-op' '
+	git notes merge x &&
+	# Verify that nothing changed on notes ref (y)
+	git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	git log >output_log_y &&
+	test_cmp expect_log_y output_log_y &&
+	# Also verify that nothing changed on original notes ref (x)
+	GIT_NOTES_REF=refs/notes/x git notes >output_notes_x &&
+	test_cmp expect_notes_x output_notes_x &&
+	GIT_NOTES_REF=refs/notes/x git log >output_log_x &&
+	test_cmp expect_log_x output_log_x
+'
+
+cp expect_notes_y expect_notes_x
+
+cat >expect_log_x <<EOF
+commit bd1753200303d0a0344be813e504253b3d98e74d
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:17:13 2005 -0700
+
+    5th
+
+Notes (x):
+    Notes on 5th commit
+
+commit 15023535574ded8b1a89052b32673f84cf9582b8
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:16:13 2005 -0700
+
+    4th
+
+Notes (x):
+    New notes on 4th commit
+
+commit 1584215f1d29c65e99c6c6848626553fdd07fd75
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:15:13 2005 -0700
+
+    3rd
+
+Notes (x):
+    Notes on 3rd commit
+$whitespace
+    More notes on 3rd commit
+
+commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:14:13 2005 -0700
+
+    2nd
+
+commit 34b09d6ffa51a8a03203627f0e369f607227364f
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:13:13 2005 -0700
+
+    1st
+
+Notes (x):
+    Notes on 1st commit
+EOF
+
+test_expect_success 'merge changed (y) into original (x) => Fast-forward' '
+	git config core.notesRef refs/notes/x &&
+	git notes merge y &&
+	# Verify new state of notes on notes ref (x)
+	git notes >output_notes_x &&
+	test_cmp expect_notes_x output_notes_x &&
+	git log >output_log_x &&
+	test_cmp expect_log_x output_log_x &&
+	# Also verify that nothing changed on other notes ref (y)
+	GIT_NOTES_REF=refs/notes/y git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	GIT_NOTES_REF=refs/notes/y git log >output_log_y &&
+	test_cmp expect_log_y output_log_y &&
+	# x and y should point to same the notes commit
+	test "$(git rev-parse refs/notes/x)" = "$(git rev-parse refs/notes/y)"
+'
+
+test_done
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 10/13] builtin/notes.c: Refactor creation of notes commits.
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (8 preceding siblings ...)
  2010-07-23 10:15 ` [WIP/RFC 09/13] git notes merge: Initial implementation handling trivial merges only Johan Herland
@ 2010-07-23 10:15 ` Johan Herland
  2010-07-23 10:15 ` [WIP/RFC 11/13] git notes merge: Handle real, non-conflicting notes merges Johan Herland
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:15 UTC (permalink / raw)
  To: git; +Cc: johan

Create new function create_notes_commit() which is slightly more general than
commit_notes() (accepts multiple commit parents and does not auto-update the
notes ref). This function will be used by the notes-merge functionality in
future patches.

Also rewrite builtin/notes.c:commit_notes() to reuse this new function.

Signed-off-by: Johan Herland <johan@herland.net>
---
 builtin.h       |    2 +-
 builtin/notes.c |   28 +++++-----------------------
 notes-merge.c   |   27 +++++++++++++++++++++++++++
 notes-merge.h   |   14 ++++++++++++++
 4 files changed, 47 insertions(+), 24 deletions(-)

diff --git a/builtin.h b/builtin.h
index ed6ee26..908d850 100644
--- a/builtin.h
+++ b/builtin.h
@@ -17,7 +17,7 @@ extern void prune_packed_objects(int);
 extern int fmt_merge_msg(int merge_summary, struct strbuf *in,
 	struct strbuf *out);
 extern int fmt_merge_msg_shortlog(struct strbuf *in, struct strbuf *out);
-extern int commit_notes(struct notes_tree *t, const char *msg);
+extern void commit_notes(struct notes_tree *t, const char *msg);
 
 struct notes_rewrite_cfg {
 	struct notes_tree **trees;
diff --git a/builtin/notes.c b/builtin/notes.c
index 3a15666..8fcf207 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -288,18 +288,17 @@ static int parse_reedit_arg(const struct option *opt, const char *arg, int unset
 	return parse_reuse_arg(opt, arg, unset);
 }
 
-int commit_notes(struct notes_tree *t, const char *msg)
+void commit_notes(struct notes_tree *t, const char *msg)
 {
-	struct commit_list *parent;
-	unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
 	struct strbuf buf = STRBUF_INIT;
+	unsigned char commit_sha1[20];
 
 	if (!t)
 		t = &default_notes_tree;
 	if (!t->initialized || !t->ref || !*t->ref)
 		die("Cannot commit uninitialized/unreferenced notes tree");
 	if (!t->dirty)
-		return 0; /* don't have to commit an unchanged tree */
+		return; /* don't have to commit an unchanged tree */
 
 	/* Prepare commit message and reflog message */
 	strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
@@ -307,27 +306,10 @@ int commit_notes(struct notes_tree *t, const char *msg)
 	if (buf.buf[buf.len - 1] != '\n')
 		strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
 
-	/* Convert notes tree to tree object */
-	if (write_notes_tree(t, tree_sha1))
-		die("Failed to write current notes tree to database");
-
-	/* Create new commit for the tree object */
-	if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
-		parent = xmalloc(sizeof(*parent));
-		parent->item = lookup_commit(prev_commit);
-		parent->next = NULL;
-	} else {
-		hashclr(prev_commit);
-		parent = NULL;
-	}
-	if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
-		die("Failed to commit notes tree to database");
-
-	/* Update notes ref with new commit */
-	update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
+	create_notes_commit(t, NULL, buf.buf + 7, commit_sha1);
+	update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
 
 	strbuf_release(&buf);
-	return 0;
 }
 
 combine_notes_fn parse_combine_notes_fn(const char *v)
diff --git a/notes-merge.c b/notes-merge.c
index 5461827..9a4dc6d 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "commit.h"
+#include "notes.h"
 #include "notes-merge.h"
 
 void init_notes_merge_options(struct notes_merge_options *o)
@@ -16,6 +17,32 @@ static int show(struct notes_merge_options *o, int v)
 #define OUTPUT(o, v, ...) \
 	do { if (show((o), (v))) { printf(__VA_ARGS__); puts(""); } } while (0)
 
+void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
+			 const char *msg, unsigned char *result_sha1)
+{
+	unsigned char tree_sha1[20];
+
+	assert(t->initialized);
+
+	if (write_notes_tree(t, tree_sha1))
+		die("Failed to write notes tree to database");
+
+	if (!parents) {
+		/* Deduce parent commit from t->ref */
+		unsigned char parent_sha1[20];
+		if (!read_ref(t->ref, parent_sha1)) {
+			struct commit *parent = lookup_commit(parent_sha1);
+			if (!parent || parse_commit(parent))
+				die("Failed to find/parse commit %s", t->ref);
+			commit_list_insert(parent, &parents);
+		}
+		/* else: t->ref points to nothing, assume root/orphan commit */
+	}
+
+	if (commit_tree(msg, tree_sha1, parents, result_sha1, NULL))
+		die("Failed to commit notes tree to database");
+}
+
 int notes_merge(struct notes_merge_options *o,
 		unsigned char *result_sha1)
 {
diff --git a/notes-merge.h b/notes-merge.h
index c4416e1..b570123 100644
--- a/notes-merge.h
+++ b/notes-merge.h
@@ -10,6 +10,20 @@ struct notes_merge_options {
 void init_notes_merge_options(struct notes_merge_options *o);
 
 /*
+ * Create new notes commit from the given notes tree
+ *
+ * Properties of the created commit:
+ * - tree: the result of converting t to a tree object with write_notes_tree().
+ * - parents: the given parents OR (if NULL) the commit referenced by t->ref.
+ * - author/committer: the default determined by commmit_tree().
+ * - commit message: msg
+ *
+ * The resulting commit SHA1 is stored in result_sha1.
+ */
+void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
+			 const char *msg, unsigned char *result_sha1);
+
+/*
  * Merge notes from o->remote_ref into o->local_ref
  *
  * The commits given by the two refs are merged, producing one of the following
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 11/13] git notes merge: Handle real, non-conflicting notes merges
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (9 preceding siblings ...)
  2010-07-23 10:15 ` [WIP/RFC 10/13] builtin/notes.c: Refactor creation of notes commits Johan Herland
@ 2010-07-23 10:15 ` Johan Herland
  2010-07-23 10:15 ` [WIP/RFC 12/13] git notes merge: Add automatic conflict resolvers (ours, theirs, union) Johan Herland
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:15 UTC (permalink / raw)
  To: git; +Cc: johan

This continuation of the 'git notes merge' implementation teaches notes-merge
to properly do real merges between notes trees: Two diffs are performed, one
from $base to $remote, and another from $base to $local. The paths in each
diff are normalized to SHA1 object names. The two diffs are then consolidated
into a single list of change pairs to be evaluated. Each change pair consist
of:

  - The annotated object's SHA1
  - The $base SHA1 (i.e. the common ancestor notes for this object)
  - The $local SHA1 (i.e. the current notes for this object)
  - The $remote SHA1 (i.e. the to-be-merged notes for this object)

>From the pair ($base -> $local, $base -> $remote), we can determine the merge
result using regular 3-way rules. If conflicts are encountered in this
process, we fail loudly and exit (conflict handling to be added in a future
patch), If we can complete the merge without conflicts, the resulting
notes tree is committed, and the current notes ref updated.

The patch includes added testcases verifying that we can successfully do real
conflict-less merges.

Signed-off-by: Johan Herland <johan@herland.net>
---
 builtin/notes.c        |   15 ++-
 notes-merge.c          |  323 +++++++++++++++++++++++++++++++++++++++++++-
 notes-merge.h          |   15 ++-
 t/t3308-notes-merge.sh |  356 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 698 insertions(+), 11 deletions(-)

diff --git a/builtin/notes.c b/builtin/notes.c
index 8fcf207..1d22635 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -767,6 +767,7 @@ static int merge(int argc, const char **argv, const char *prefix)
 {
 	struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
 	unsigned char result_sha1[20];
+	struct notes_tree *t;
 	struct notes_merge_options o;
 	int verbosity = 0, result;
 	struct option options[] = {
@@ -792,19 +793,23 @@ static int merge(int argc, const char **argv, const char *prefix)
 	expand_notes_ref(&remote_ref);
 	o.remote_ref = remote_ref.buf;
 
-	result = notes_merge(&o, result_sha1);
+	t = init_notes_check("merge");
 
 	strbuf_addf(&msg, "notes: Merged notes from %s into %s",
 		    remote_ref.buf, default_notes_ref());
-	if (result == 0) { /* Merge resulted (trivially) in result_sha1 */
+	o.commit_msg = msg.buf + 7; // skip "notes: " prefix
+
+	result = notes_merge(&o, t, result_sha1);
+
+	if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
 		/* Update default notes ref with new commit */
 		update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
 			   0, DIE_ON_ERR);
-	} else {
+	else
 		/* TODO: */
-		die("'git notes merge' cannot yet handle non-trivial merges!");
-	}
+		die("'git notes merge' cannot yet handle conflicts!");
 
+	free_notes(t);
 	strbuf_release(&remote_ref);
 	strbuf_release(&msg);
 	return 0;
diff --git a/notes-merge.c b/notes-merge.c
index 9a4dc6d..ba9703a 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -1,8 +1,14 @@
 #include "cache.h"
 #include "commit.h"
+#include "diff.h"
+#include "diffcore.h"
 #include "notes.h"
 #include "notes-merge.h"
 
+struct notes_merge_pair {
+	unsigned char obj[20], base[20], local[20], remote[20];
+};
+
 void init_notes_merge_options(struct notes_merge_options *o)
 {
 	memset(o, 0, sizeof(struct notes_merge_options));
@@ -17,6 +23,303 @@ static int show(struct notes_merge_options *o, int v)
 #define OUTPUT(o, v, ...) \
 	do { if (show((o), (v))) { printf(__VA_ARGS__); puts(""); } } while (0)
 
+static int path_to_sha1(const char *path, unsigned char *sha1)
+{
+	char hex_sha1[40];
+	int i = 0;
+	while (*path && i < 40) {
+		if (*path != '/')
+			hex_sha1[i++] = *path;
+		path++;
+	}
+	if (*path || i != 40)
+		return -1;
+	return get_sha1_hex(hex_sha1, sha1);
+}
+
+static int verify_notes_filepair(struct diff_filepair *p, unsigned char *sha1)
+{
+	switch (p->status) {
+	case DIFF_STATUS_MODIFIED:
+		assert(p->one->mode == p->two->mode);
+		assert(!is_null_sha1(p->one->sha1));
+		assert(!is_null_sha1(p->two->sha1));
+		break;
+	case DIFF_STATUS_ADDED:
+		assert(is_null_sha1(p->one->sha1));
+		break;
+	case DIFF_STATUS_DELETED:
+		assert(is_null_sha1(p->two->sha1));
+		break;
+	default:
+		return -1;
+	}
+	assert(!strcmp(p->one->path, p->two->path));
+	return path_to_sha1(p->one->path, sha1);
+}
+
+static struct notes_merge_pair *find_notes_merge_pair_pos(
+		struct notes_merge_pair *list, int len, unsigned char *obj,
+		int insert_new, int *occupied)
+{
+	/*
+	 * Both diff_tree_remote() and diff_tree_local() tend to process
+	 * merge_pairs in ascending order. Therefore, cache last returned
+	 * index, and search sequentially from there until the appropriate
+	 * position is found.
+	 *
+	 * Since inserts only happen from diff_tree_remote() (which mainly
+	 * _appends_), we don't care that inserting into the middle of the
+	 * list is expensive (using memmove()).
+	 */
+	static int last_index = 0;
+	int i = last_index < len ? last_index : len - 1;
+	int prev_cmp = 0, cmp = -1;
+	while (i >= 0 && i < len) {
+		cmp = hashcmp(obj, list[i].obj);
+		if (!cmp) /* obj belongs @ i */
+			break;
+		else if (cmp < 0 && prev_cmp <= 0) /* obj belongs < i */
+			i--;
+		else if (cmp < 0) /* obj belongs between i-1 and i */
+			break;
+		else if (cmp > 0 && prev_cmp >= 0) /* obj belongs > i */
+			i++;
+		else /* if (cmp > 0) */ { /* obj belongs between i and i+1 */
+			i++;
+			break;
+		}
+		prev_cmp = cmp;
+	}
+	if (i < 0)
+		i = 0;
+	/* obj belongs at, or immediately preceding, index i (0 <= i <= len) */
+
+	if (!cmp)
+		*occupied = 1;
+	else {
+		*occupied = 0;
+		if (insert_new && i < len) {
+			memmove(list + i + 1, list + i,
+				(len - i) * sizeof(struct notes_merge_pair));
+			memset(list + i, 0, sizeof(struct notes_merge_pair));
+		}
+	}
+	last_index = i;
+	return list + i;
+}
+
+static unsigned char uninitialized[20] =
+	"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
+	"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
+
+static struct notes_merge_pair *diff_tree_remote(struct notes_merge_options *o,
+						 const unsigned char *base,
+						 const unsigned char *remote,
+						 int *num_changes)
+{
+	struct diff_options opt;
+	struct notes_merge_pair *changes;
+	int i, len = 0;
+
+	OUTPUT(o, 5, "\tdiff_tree_remote(base = %.7s, remote = %.7s)",
+	       sha1_to_hex(base), sha1_to_hex(remote));
+
+	diff_setup(&opt);
+	DIFF_OPT_SET(&opt, RECURSIVE);
+	opt.output_format = DIFF_FORMAT_NO_OUTPUT;
+	if (diff_setup_done(&opt) < 0)
+		die("diff_setup_done failed");
+	diff_tree_sha1(base, remote, "", &opt);
+	diffcore_std(&opt);
+
+	changes = xcalloc(diff_queued_diff.nr, sizeof(struct notes_merge_pair));
+
+	for (i = 0; i < diff_queued_diff.nr; i++) {
+		struct diff_filepair *p = diff_queued_diff.queue[i];
+		struct notes_merge_pair *mp;
+		int occupied;
+		unsigned char obj[20];
+
+		if (verify_notes_filepair(p, obj)) {
+			OUTPUT(o, 5, "\t\tCannot merge entry '%s' (%c): "
+			       "%.7s -> %.7s. Skipping!", p->one->path,
+			       p->status, sha1_to_hex(p->one->sha1),
+			       sha1_to_hex(p->two->sha1));
+			continue;
+		}
+		mp = find_notes_merge_pair_pos(changes, len, obj, 1, &occupied);
+		if (occupied) {
+			/* We've found an addition/deletion pair */
+			assert(!hashcmp(mp->obj, obj));
+			if (is_null_sha1(p->one->sha1)) { /* addition */
+				assert(is_null_sha1(mp->remote));
+				hashcpy(mp->remote, p->two->sha1);
+			} else if (is_null_sha1(p->two->sha1)) { /* deletion */
+				assert(is_null_sha1(mp->base));
+				hashcpy(mp->base, p->one->sha1);
+			} else
+				assert(!"Invalid existing change recorded");
+		} else {
+			hashcpy(mp->obj, obj);
+			hashcpy(mp->base, p->one->sha1);
+			hashcpy(mp->local, uninitialized);
+			hashcpy(mp->remote, p->two->sha1);
+			len++;
+		}
+		OUTPUT(o, 5, "\t\tRecorded remote change for %s: %.7s -> %.7s",
+		       sha1_to_hex(mp->obj), sha1_to_hex(mp->base),
+		       sha1_to_hex(mp->remote));
+	}
+	diff_flush(&opt);
+	diff_tree_release_paths(&opt);
+
+	*num_changes = len;
+	return changes;
+}
+
+static void diff_tree_local(struct notes_merge_options *o,
+			    struct notes_merge_pair *changes, int len,
+			    const unsigned char *base,
+			    const unsigned char *local)
+{
+	struct diff_options opt;
+	int i;
+
+	OUTPUT(o, 5, "\tdiff_tree_local(len = %i, base = %.7s, local = %.7s)",
+	       len, sha1_to_hex(base), sha1_to_hex(local));
+
+	diff_setup(&opt);
+	DIFF_OPT_SET(&opt, RECURSIVE);
+	opt.output_format = DIFF_FORMAT_NO_OUTPUT;
+	if (diff_setup_done(&opt) < 0)
+		die("diff_setup_done failed");
+	diff_tree_sha1(base, local, "", &opt);
+	diffcore_std(&opt);
+
+	for (i = 0; i < diff_queued_diff.nr; i++) {
+		struct diff_filepair *p = diff_queued_diff.queue[i];
+		struct notes_merge_pair *mp;
+		int match;
+		unsigned char obj[20];
+
+		if (verify_notes_filepair(p, obj)) {
+			OUTPUT(o, 5, "\t\tCannot merge entry '%s' (%c): "
+			       "%.7s -> %.7s. Skipping!", p->one->path,
+			       p->status, sha1_to_hex(p->one->sha1),
+			       sha1_to_hex(p->two->sha1));
+			continue;
+		}
+		mp = find_notes_merge_pair_pos(changes, len, obj, 0, &match);
+		if (!match) {
+			OUTPUT(o, 5, "\t\tIgnoring local-only change for %s: "
+			       "%.7s -> %.7s", sha1_to_hex(obj),
+			       sha1_to_hex(p->one->sha1),
+			       sha1_to_hex(p->two->sha1));
+			continue;
+		}
+
+		assert(!hashcmp(mp->obj, obj));
+		if (is_null_sha1(p->two->sha1)) { /* deletion */
+			/*
+			 * Either this is a true deletion (1), or it is part
+			 * of an A/D pair (2), or D/A pair (3):
+			 *
+			 * (1) mp->local is uninitialized; set it to null_sha1
+			 * (2) mp->local is not uninitialized; don't touch it
+			 * (3) mp->local is uninitialized; set it to null_sha1
+			 *     (will be overwritten by following addition)
+			 */
+			if (!hashcmp(mp->local, uninitialized))
+				hashclr(mp->local);
+		} else if (is_null_sha1(p->one->sha1)) { /* addition */
+			/*
+			 * Either this is a true addition (1), or it is part
+			 * of an A/D pair (2), or D/A pair (3):
+			 *
+			 * (1) mp->local is uninitialized; set to p->two->sha1
+			 * (2) mp->local is uninitialized; set to p->two->sha1
+			 * (3) mp->local is null_sha1;     set to p->two->sha1
+			 */
+			assert(is_null_sha1(mp->local) ||
+			       !hashcmp(mp->local, uninitialized));
+			hashcpy(mp->local, p->two->sha1);
+		} else { /* modification */
+			/*
+			 * This is a true modification. p->one->sha1 shall
+			 * match mp->base, and mp->local shall be uninitialized.
+			 * Set mp->local to p->two->sha1.
+			 */
+			assert(!hashcmp(p->one->sha1, mp->base));
+			assert(!hashcmp(mp->local, uninitialized));
+			hashcpy(mp->local, p->two->sha1);
+		}
+		OUTPUT(o, 5, "\t\tRecorded local change for %s: %.7s -> %.7s",
+		       sha1_to_hex(mp->obj), sha1_to_hex(mp->base),
+		       sha1_to_hex(mp->local));
+	}
+	diff_flush(&opt);
+	diff_tree_release_paths(&opt);
+}
+
+static int merge_changes(struct notes_merge_options *o,
+			 struct notes_merge_pair *changes, int *num_changes,
+			 struct notes_tree *t)
+{
+	int i, conflicts = 0;
+
+	OUTPUT(o, 5, "\tmerge_changes(num_changes = %i)", *num_changes);
+	for (i = 0; i < *num_changes; i++) {
+		struct notes_merge_pair *p = changes + i;
+		OUTPUT(o, 5, "\t\t%.7s: %.7s -> %.7s/%.7s",
+		       sha1_to_hex(p->obj), sha1_to_hex(p->base),
+		       sha1_to_hex(p->local), sha1_to_hex(p->remote));
+
+		if (!hashcmp(p->base, p->remote)) {
+			/* no remote change; nothing to do */
+			OUTPUT(o, 5, "\t\t\tskipping (no remote change)");
+		} else if (!hashcmp(p->local, p->remote)) {
+			/* same change in local and remote; nothing to do */
+			OUTPUT(o, 5, "\t\t\tskipping (local == remote)");
+		} else if (!hashcmp(p->local, uninitialized) ||
+			   !hashcmp(p->local, p->base)) {
+			/* no local change; adopt remote change */
+			OUTPUT(o, 5, "\t\t\tno local change, adopting remote");
+			add_note(t, p->obj, p->remote, combine_notes_overwrite);
+		} else {
+			/* need file-level merge between local and remote */
+			OUTPUT(o, 5, "\t\t\tneed content-level merge");
+			conflicts += 1; /* TODO */
+		}
+	}
+
+	return conflicts;
+}
+
+static int merge_from_diffs(struct notes_merge_options *o,
+			    const unsigned char *base,
+			    const unsigned char *local,
+			    const unsigned char *remote, struct notes_tree *t)
+{
+	struct notes_merge_pair *changes;
+	int num_changes, conflicts;
+
+	OUTPUT(o, 5, "\tmerge_from_diffs(base = %.7s, local = %.7s, "
+	       "remote = %.7s)", sha1_to_hex(base), sha1_to_hex(local),
+	       sha1_to_hex(remote));
+
+	changes = diff_tree_remote(o, base, remote, &num_changes);
+	diff_tree_local(o, changes, num_changes, base, local);
+
+	conflicts = merge_changes(o, changes, &num_changes, t);
+	free(changes);
+
+	OUTPUT(o, 4, "Merge result: %i unmerged notes and a %s notes tree",
+	       conflicts, t->dirty ? "dirty" : "clean");
+
+	return conflicts ? -1 : 1;
+}
+
 void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
 			 const char *msg, unsigned char *result_sha1)
 {
@@ -44,14 +347,16 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
 }
 
 int notes_merge(struct notes_merge_options *o,
+		struct notes_tree *local_tree,
 		unsigned char *result_sha1)
 {
 	unsigned char local_sha1[20], remote_sha1[20];
 	struct commit *local, *remote;
 	struct commit_list *bases = NULL;
-	const unsigned char *base_sha1;
+	const unsigned char *base_sha1, *base_tree_sha1;
 	int result = 0;
 
+	assert(!strcmp(o->local_ref, local_tree->ref));
 	hashclr(result_sha1);
 
 	OUTPUT(o, 5, "notes_merge(o->local_ref = %s, o->remote_ref = %s)",
@@ -90,14 +395,17 @@ int notes_merge(struct notes_merge_options *o,
 	bases = get_merge_bases(local, remote, 1);
 	if (!bases) {
 		base_sha1 = null_sha1;
+		base_tree_sha1 = (unsigned char *)EMPTY_TREE_SHA1_BIN;
 		OUTPUT(o, 4, "No merge base found; doing history-less merge");
 	} else if (!bases->next) {
 		base_sha1 = bases->item->object.sha1;
+		base_tree_sha1 = bases->item->tree->object.sha1;
 		OUTPUT(o, 4, "One merge base found (%.7s)",
 		       sha1_to_hex(base_sha1));
 	} else {
 		/* TODO: How to handle multiple merge-bases? */
 		base_sha1 = bases->item->object.sha1;
+		base_tree_sha1 = bases->item->tree->object.sha1;
 		OUTPUT(o, 3, "Multiple merge bases found. Using the first "
 		       "(%.7s)", sha1_to_hex(base_sha1));
 	}
@@ -119,8 +427,17 @@ int notes_merge(struct notes_merge_options *o,
 		goto found_result;
 	}
 
-	/* TODO: */
-	result = error("notes_merge() cannot yet handle real merges.");
+	result = merge_from_diffs(o, base_tree_sha1, local->tree->object.sha1,
+				  remote->tree->object.sha1, local_tree);
+
+	if (result > 0) { /* successful non-trivial merge */
+		/* Commit result */
+		struct commit_list *parents = NULL;
+		commit_list_insert(remote, &parents); /* LIFO order */
+		commit_list_insert(local, &parents);
+		create_notes_commit(local_tree, parents, o->commit_msg,
+				    result_sha1);
+	}
 
 found_result:
 	free_commit_list(bases);
diff --git a/notes-merge.h b/notes-merge.h
index b570123..e42ce4b 100644
--- a/notes-merge.h
+++ b/notes-merge.h
@@ -4,6 +4,7 @@
 struct notes_merge_options {
 	const char *local_ref;
 	const char *remote_ref;
+	const char *commit_msg;
 	int verbosity;
 };
 
@@ -26,19 +27,27 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
 /*
  * Merge notes from o->remote_ref into o->local_ref
  *
+ * The given notes_tree 'local_tree' must be the notes_tree referenced by the
+ * o->local_ref. This is the notes_tree in which the object-level merge is
+ * performed.
+ *
  * The commits given by the two refs are merged, producing one of the following
  * outcomes:
  *
  * 1. The merge trivially results in an existing commit (e.g. fast-forward or
- *    already-up-to-date). The SHA1 of the result is written into 'result_sha1'
- *    and 0 is returned.
- * 2. The merge fails. result_sha1 is set to null_sha1, and non-zero returned.
+ *    already-up-to-date). 'local_tree' is untouched, the SHA1 of the result
+ *    is written into 'result_sha1' and 0 is returned.
+ * 2. The merge successfully completes, producing a merge commit. local_tree
+ *    contains the updated notes tree, the SHA1 of the resulting commit is
+ *    written into 'result_sha1', and 1 is returned.
+ * 3. The merge fails. result_sha1 is set to null_sha1, and -1 is returned.
  *
  * Either ref (but not both) may not exist in which case the missing ref is
  * interpreted as an empty notes tree, and the merge trivially results in
  * what the other ref points to.
  */
 int notes_merge(struct notes_merge_options *o,
+		struct notes_tree *local_tree,
 		unsigned char *result_sha1);
 
 #endif
diff --git a/t/t3308-notes-merge.sh b/t/t3308-notes-merge.sh
index a89fd11..d49df32 100755
--- a/t/t3308-notes-merge.sh
+++ b/t/t3308-notes-merge.sh
@@ -310,4 +310,360 @@ test_expect_success 'merge changed (y) into original (x) => Fast-forward' '
 	test "$(git rev-parse refs/notes/x)" = "$(git rev-parse refs/notes/y)"
 '
 
+test_expect_success 'merge empty notes ref (z => y)' '
+	# Prepare empty (but valid) notes ref (z)
+	git config core.notesRef refs/notes/z &&
+	git notes add -m "foo" &&
+	git notes remove &&
+	git notes >output_notes_z &&
+	test_cmp /dev/null output_notes_z &&
+	# Do the merge (z => y)
+	git config core.notesRef refs/notes/y &&
+	git notes merge z &&
+	git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	git log >output_log_y &&
+	test_cmp expect_log_y output_log_y &&
+	# y should no longer point to the same notes commit as x
+	test "$(git rev-parse refs/notes/x)" != "$(git rev-parse refs/notes/y)"
+'
+
+test_expect_success 'change notes on other notes ref (y)' '
+	# Append to 1st commit notes
+	git notes append -m "More notes on 1st commit" HEAD~4 &&
+	# Add new notes to 2nd commit
+	git notes add -m "New notes on 2nd commit" HEAD~3
+'
+
+cat >expect_notes_y <<EOF
+dec2502dac3ea161543f71930044deff93fa945c 15023535574ded8b1a89052b32673f84cf9582b8
+4069cdb399fd45463ec6eef8e051a16a03592d91 1584215f1d29c65e99c6c6848626553fdd07fd75
+d000d30e6ddcfce3a8122c403226a2ce2fd04d9d 268048bfb8a1fb38e703baceb8ab235421bf80c5
+43add6bd0c8c0bc871ac7991e0f5573cfba27804 34b09d6ffa51a8a03203627f0e369f607227364f
+0f2efbd00262f2fd41dfae33df8765618eeacd99 bd1753200303d0a0344be813e504253b3d98e74d
+EOF
+
+cat >expect_log_y <<EOF
+commit bd1753200303d0a0344be813e504253b3d98e74d
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:17:13 2005 -0700
+
+    5th
+
+Notes (y):
+    Notes on 5th commit
+
+commit 15023535574ded8b1a89052b32673f84cf9582b8
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:16:13 2005 -0700
+
+    4th
+
+Notes (y):
+    New notes on 4th commit
+
+commit 1584215f1d29c65e99c6c6848626553fdd07fd75
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:15:13 2005 -0700
+
+    3rd
+
+Notes (y):
+    Notes on 3rd commit
+$whitespace
+    More notes on 3rd commit
+
+commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:14:13 2005 -0700
+
+    2nd
+
+Notes (y):
+    New notes on 2nd commit
+
+commit 34b09d6ffa51a8a03203627f0e369f607227364f
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:13:13 2005 -0700
+
+    1st
+
+Notes (y):
+    Notes on 1st commit
+$whitespace
+    More notes on 1st commit
+EOF
+
+test_expect_success 'verify changed notes on other notes ref (y)' '
+	git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	git log >output_log_y &&
+	test_cmp expect_log_y output_log_y
+'
+
+cat >expect_notes_x <<EOF
+1f257a3a90328557c452f0817d6cc50c89d315d4 15023535574ded8b1a89052b32673f84cf9582b8
+daa55ffad6cb99bf64226532147ffcaf5ce8bdd1 34b09d6ffa51a8a03203627f0e369f607227364f
+0f2efbd00262f2fd41dfae33df8765618eeacd99 bd1753200303d0a0344be813e504253b3d98e74d
+EOF
+
+cat >expect_log_x <<EOF
+commit bd1753200303d0a0344be813e504253b3d98e74d
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:17:13 2005 -0700
+
+    5th
+
+Notes (x):
+    Notes on 5th commit
+
+commit 15023535574ded8b1a89052b32673f84cf9582b8
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:16:13 2005 -0700
+
+    4th
+
+Notes (x):
+    New notes on 4th commit
+$whitespace
+    More notes on 4th commit
+
+commit 1584215f1d29c65e99c6c6848626553fdd07fd75
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:15:13 2005 -0700
+
+    3rd
+
+commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:14:13 2005 -0700
+
+    2nd
+
+commit 34b09d6ffa51a8a03203627f0e369f607227364f
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:13:13 2005 -0700
+
+    1st
+
+Notes (x):
+    Notes on 1st commit
+EOF
+
+test_expect_success 'change notes on notes ref (x)' '
+	git config core.notesRef refs/notes/x &&
+	# Remove 3rd commit notes
+	git notes remove HEAD~2 &&
+	# Append to 4th commit notes
+	git notes append -m "More notes on 4th commit" HEAD^ &&
+	# Verify changed notes on notes ref (x)
+	git notes >output_notes_x &&
+	test_cmp expect_notes_x output_notes_x &&
+	git log >output_log_x &&
+	test_cmp expect_log_x output_log_x
+'
+
+cat >expect_notes_x <<EOF
+1f257a3a90328557c452f0817d6cc50c89d315d4 15023535574ded8b1a89052b32673f84cf9582b8
+d000d30e6ddcfce3a8122c403226a2ce2fd04d9d 268048bfb8a1fb38e703baceb8ab235421bf80c5
+43add6bd0c8c0bc871ac7991e0f5573cfba27804 34b09d6ffa51a8a03203627f0e369f607227364f
+0f2efbd00262f2fd41dfae33df8765618eeacd99 bd1753200303d0a0344be813e504253b3d98e74d
+EOF
+
+cat >expect_log_x <<EOF
+commit bd1753200303d0a0344be813e504253b3d98e74d
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:17:13 2005 -0700
+
+    5th
+
+Notes (x):
+    Notes on 5th commit
+
+commit 15023535574ded8b1a89052b32673f84cf9582b8
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:16:13 2005 -0700
+
+    4th
+
+Notes (x):
+    New notes on 4th commit
+$whitespace
+    More notes on 4th commit
+
+commit 1584215f1d29c65e99c6c6848626553fdd07fd75
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:15:13 2005 -0700
+
+    3rd
+
+commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:14:13 2005 -0700
+
+    2nd
+
+Notes (x):
+    New notes on 2nd commit
+
+commit 34b09d6ffa51a8a03203627f0e369f607227364f
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:13:13 2005 -0700
+
+    1st
+
+Notes (x):
+    Notes on 1st commit
+$whitespace
+    More notes on 1st commit
+EOF
+
+test_expect_success 'merge y into x => Non-conflicting 3-way merge' '
+	git notes merge y &&
+	# Verify new state of notes on other notes ref (x)
+	git notes >output_notes_x &&
+	test_cmp expect_notes_x output_notes_x &&
+	git log >output_log_x &&
+	test_cmp expect_log_x output_log_x &&
+	# Also verify that nothing changed on other notes ref (y)
+	GIT_NOTES_REF=refs/notes/y git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	GIT_NOTES_REF=refs/notes/y git log >output_log_y &&
+	test_cmp expect_log_y output_log_y
+'
+
+cat >expect_notes_w <<EOF
+05a4927951bcef347f51486575b878b2b60137f2 1584215f1d29c65e99c6c6848626553fdd07fd75
+d000d30e6ddcfce3a8122c403226a2ce2fd04d9d 268048bfb8a1fb38e703baceb8ab235421bf80c5
+EOF
+
+cat >expect_log_w <<EOF
+commit bd1753200303d0a0344be813e504253b3d98e74d
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:17:13 2005 -0700
+
+    5th
+
+commit 15023535574ded8b1a89052b32673f84cf9582b8
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:16:13 2005 -0700
+
+    4th
+
+commit 1584215f1d29c65e99c6c6848626553fdd07fd75
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:15:13 2005 -0700
+
+    3rd
+
+Notes (w):
+    New notes on 3rd commit
+
+commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:14:13 2005 -0700
+
+    2nd
+
+Notes (w):
+    New notes on 2nd commit
+
+commit 34b09d6ffa51a8a03203627f0e369f607227364f
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:13:13 2005 -0700
+
+    1st
+EOF
+
+test_expect_success 'create notes on new, separate notes ref (w)' '
+	git config core.notesRef refs/notes/w &&
+	# Add same note as refs/notes/y on 2nd commit
+	git notes add -m "New notes on 2nd commit" HEAD~3 &&
+	# Add new note on 3rd commit (non-conflicting)
+	git notes add -m "New notes on 3rd commit" HEAD~2 &&
+	# Verify state of notes on new, separate notes ref (w)
+	git notes >output_notes_w &&
+	test_cmp expect_notes_w output_notes_w &&
+	git log >output_log_w &&
+	test_cmp expect_log_w output_log_w
+'
+
+cat >expect_notes_x <<EOF
+1f257a3a90328557c452f0817d6cc50c89d315d4 15023535574ded8b1a89052b32673f84cf9582b8
+05a4927951bcef347f51486575b878b2b60137f2 1584215f1d29c65e99c6c6848626553fdd07fd75
+d000d30e6ddcfce3a8122c403226a2ce2fd04d9d 268048bfb8a1fb38e703baceb8ab235421bf80c5
+43add6bd0c8c0bc871ac7991e0f5573cfba27804 34b09d6ffa51a8a03203627f0e369f607227364f
+0f2efbd00262f2fd41dfae33df8765618eeacd99 bd1753200303d0a0344be813e504253b3d98e74d
+EOF
+
+cat >expect_log_x <<EOF
+commit bd1753200303d0a0344be813e504253b3d98e74d
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:17:13 2005 -0700
+
+    5th
+
+Notes (x):
+    Notes on 5th commit
+
+commit 15023535574ded8b1a89052b32673f84cf9582b8
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:16:13 2005 -0700
+
+    4th
+
+Notes (x):
+    New notes on 4th commit
+$whitespace
+    More notes on 4th commit
+
+commit 1584215f1d29c65e99c6c6848626553fdd07fd75
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:15:13 2005 -0700
+
+    3rd
+
+Notes (x):
+    New notes on 3rd commit
+
+commit 268048bfb8a1fb38e703baceb8ab235421bf80c5
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:14:13 2005 -0700
+
+    2nd
+
+Notes (x):
+    New notes on 2nd commit
+
+commit 34b09d6ffa51a8a03203627f0e369f607227364f
+Author: A U Thor <author@example.com>
+Date:   Thu Apr 7 15:13:13 2005 -0700
+
+    1st
+
+Notes (x):
+    Notes on 1st commit
+$whitespace
+    More notes on 1st commit
+EOF
+
+test_expect_success 'merge w into x => Non-conflicting history-less merge' '
+	git config core.notesRef refs/notes/x &&
+	git notes merge w &&
+	# Verify new state of notes on other notes ref (x)
+	git notes >output_notes_x &&
+	test_cmp expect_notes_x output_notes_x &&
+	git log >output_log_x &&
+	test_cmp expect_log_x output_log_x &&
+	# Also verify that nothing changed on other notes refs (y and w)
+	GIT_NOTES_REF=refs/notes/y git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	GIT_NOTES_REF=refs/notes/y git log >output_log_y &&
+	test_cmp expect_log_y output_log_y
+	GIT_NOTES_REF=refs/notes/w git notes >output_notes_w &&
+	test_cmp expect_notes_w output_notes_w &&
+	GIT_NOTES_REF=refs/notes/w git log >output_log_w &&
+	test_cmp expect_log_w output_log_w
+'
+
 test_done
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 12/13] git notes merge: Add automatic conflict resolvers (ours, theirs, union)
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (10 preceding siblings ...)
  2010-07-23 10:15 ` [WIP/RFC 11/13] git notes merge: Handle real, non-conflicting notes merges Johan Herland
@ 2010-07-23 10:15 ` Johan Herland
  2010-07-24  8:18   ` Stephen Boyd
  2010-07-23 10:15 ` [WIP/RFC 13/13] Documentation: Preliminary docs on 'git notes merge' Johan Herland
  2010-07-25 10:45 ` [WIP/RFC 00/13] git notes merge Sam Vilain
  13 siblings, 1 reply; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:15 UTC (permalink / raw)
  To: git; +Cc: johan

The new -X/--resolve command-line option to 'git notes merge' allow the user
to choose how notes merge conflicts should be resolved. There are four valid
resolvers to choose from:

1. "manual" (the default): This will let the user manually resolve conflicts.
   This option currently fails with an error message. It will be implemented
   properly in future patches.

2. "ours": This automatically chooses the local version of a conflict, and
   discards the remote version.

3. "theirs": This automatically chooses the remote version of a conflict, and
   discards the local version.

4. "union": This automatically resolves the conflict by appending the remote
   version to the local version.

The auto-resolvers are implement using the combine_notes_* functions from the
notes.h API.

The patch also includes testcases verifying the correct implementation of
these auto-resolvers.

Signed-off-by: Johan Herland <johan@herland.net>
---
 builtin/notes.c                     |   21 ++-
 notes-merge.c                       |   29 ++-
 notes-merge.h                       |    6 +
 t/t3309-notes-merge-auto-resolve.sh |  512 +++++++++++++++++++++++++++++++++++
 4 files changed, 566 insertions(+), 2 deletions(-)
 create mode 100755 t/t3309-notes-merge-auto-resolve.sh

diff --git a/builtin/notes.c b/builtin/notes.c
index 1d22635..e14c6f2 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -26,7 +26,7 @@ static const char * const git_notes_usage[] = {
 	"git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
 	"git notes [--ref <notes_ref>] edit [<object>]",
 	"git notes [--ref <notes_ref>] show [<object>]",
-	"git notes [--ref <notes_ref>] merge [-v | -q] <notes_ref>",
+	"git notes [--ref <notes_ref>] merge [-v | -q] [-X <strategy> ] <notes_ref>",
 	"git notes [--ref <notes_ref>] remove [<object>]",
 	"git notes [--ref <notes_ref>] prune [-n | -v]",
 	NULL
@@ -770,8 +770,12 @@ static int merge(int argc, const char **argv, const char *prefix)
 	struct notes_tree *t;
 	struct notes_merge_options o;
 	int verbosity = 0, result;
+	const char *resolver = NULL;
 	struct option options[] = {
 		OPT__VERBOSITY(&verbosity),
+		OPT_STRING('X', "resolve", &resolver, "strategy",
+			   "resolve notes conflicts using the given "
+			   "strategy (manual/ours/theirs/union)"),
 		OPT_END()
 	};
 
@@ -793,6 +797,21 @@ static int merge(int argc, const char **argv, const char *prefix)
 	expand_notes_ref(&remote_ref);
 	o.remote_ref = remote_ref.buf;
 
+	if (resolver) {
+		if (!strcmp(resolver, "manual"))
+			o.resolve = NOTES_MERGE_RESOLVE_MANUAL;
+		else if (!strcmp(resolver, "ours"))
+			o.resolve = NOTES_MERGE_RESOLVE_OURS;
+		else if (!strcmp(resolver, "theirs"))
+			o.resolve = NOTES_MERGE_RESOLVE_THEIRS;
+		else if (!strcmp(resolver, "union"))
+			o.resolve = NOTES_MERGE_RESOLVE_UNION;
+		else {
+			error("Unknown -X/--resolve strategy: %s", resolver);
+			usage_with_options(git_notes_merge_usage, options);
+		}
+	}
+
 	t = init_notes_check("merge");
 
 	strbuf_addf(&msg, "notes: Merged notes from %s into %s",
diff --git a/notes-merge.c b/notes-merge.c
index ba9703a..c7db60a 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -262,6 +262,33 @@ static void diff_tree_local(struct notes_merge_options *o,
 	diff_tree_release_paths(&opt);
 }
 
+static int merge_one_change(struct notes_merge_options *o,
+			    struct notes_merge_pair *p, struct notes_tree *t)
+{
+	/*
+	 * Return 0 if change was resolved (and added to notes_tree),
+	 * 1 if conflict
+	 */
+	switch (o->resolve) {
+	case NOTES_MERGE_RESOLVE_MANUAL:
+		return 1;
+	case NOTES_MERGE_RESOLVE_OURS:
+		OUTPUT(o, 2, "Using local notes for %s", sha1_to_hex(p->obj));
+		/* nothing to do */
+		return 0;
+	case NOTES_MERGE_RESOLVE_THEIRS:
+		OUTPUT(o, 2, "Using remote notes for %s", sha1_to_hex(p->obj));
+		add_note(t, p->obj, p->remote, combine_notes_overwrite);
+		return 0;
+	case NOTES_MERGE_RESOLVE_UNION:
+		OUTPUT(o, 2, "Concatenating local and remote notes for %s",
+		       sha1_to_hex(p->obj));
+		add_note(t, p->obj, p->remote, combine_notes_concatenate);
+		return 0;
+	}
+	die("Unknown resolve method (%i).", o->resolve);
+}
+
 static int merge_changes(struct notes_merge_options *o,
 			 struct notes_merge_pair *changes, int *num_changes,
 			 struct notes_tree *t)
@@ -289,7 +316,7 @@ static int merge_changes(struct notes_merge_options *o,
 		} else {
 			/* need file-level merge between local and remote */
 			OUTPUT(o, 5, "\t\t\tneed content-level merge");
-			conflicts += 1; /* TODO */
+			conflicts += merge_one_change(o, p, t);
 		}
 	}
 
diff --git a/notes-merge.h b/notes-merge.h
index e42ce4b..bd808d0 100644
--- a/notes-merge.h
+++ b/notes-merge.h
@@ -6,6 +6,12 @@ struct notes_merge_options {
 	const char *remote_ref;
 	const char *commit_msg;
 	int verbosity;
+	enum {
+		NOTES_MERGE_RESOLVE_MANUAL = 0,
+		NOTES_MERGE_RESOLVE_OURS,
+		NOTES_MERGE_RESOLVE_THEIRS,
+		NOTES_MERGE_RESOLVE_UNION
+	} resolve;
 };
 
 void init_notes_merge_options(struct notes_merge_options *o);
diff --git a/t/t3309-notes-merge-auto-resolve.sh b/t/t3309-notes-merge-auto-resolve.sh
new file mode 100755
index 0000000..171e795
--- /dev/null
+++ b/t/t3309-notes-merge-auto-resolve.sh
@@ -0,0 +1,512 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Johan Herland
+#
+
+test_description='Test notes merging with automatic resolvers'
+
+. ./test-lib.sh
+
+# Set up a notes merge scenario with all kinds of potential conflicts
+test_expect_success 'setup commits' '
+	# Create 15 commits with tags ("1st" through "15th")
+	: > a1 && git add a1 && test_tick && git commit -m 1st && git tag 1st &&
+	: > a2 && git add a2 && test_tick && git commit -m 2nd && git tag 2nd &&
+	: > a3 && git add a3 && test_tick && git commit -m 3rd && git tag 3rd &&
+	: > a4 && git add a4 && test_tick && git commit -m 4th && git tag 4th &&
+	: > a5 && git add a5 && test_tick && git commit -m 5th && git tag 5th &&
+	: > a6 && git add a6 && test_tick && git commit -m 6th && git tag 6th &&
+	: > a7 && git add a7 && test_tick && git commit -m 7th && git tag 7th &&
+	: > a8 && git add a8 && test_tick && git commit -m 8th && git tag 8th &&
+	: > a9 && git add a9 && test_tick && git commit -m 9th && git tag 9th &&
+	: > a10 && git add a10 && test_tick && git commit -m 10th && git tag 10th &&
+	: > a11 && git add a11 && test_tick && git commit -m 11th && git tag 11th &&
+	: > a12 && git add a12 && test_tick && git commit -m 12th && git tag 12th &&
+	: > a13 && git add a13 && test_tick && git commit -m 13th && git tag 13th &&
+	: > a14 && git add a14 && test_tick && git commit -m 14th && git tag 14th &&
+	: > a15 && git add a15 && test_tick && git commit -m 15th && git tag 15th
+'
+
+test_expect_success 'setup merge base (x)' '
+	git config core.notesRef refs/notes/x &&
+	git notes add -m "x notes on 6th commit" 6th &&
+	git notes add -m "x notes on 7th commit" 7th &&
+	git notes add -m "x notes on 8th commit" 8th &&
+	git notes add -m "x notes on 9th commit" 9th &&
+	git notes add -m "x notes on 10th commit" 10th &&
+	git notes add -m "x notes on 11th commit" 11th &&
+	git notes add -m "x notes on 12th commit" 12th &&
+	git notes add -m "x notes on 13th commit" 13th &&
+	git notes add -m "x notes on 14th commit" 14th &&
+	git notes add -m "x notes on 15th commit" 15th
+'
+
+cat >expect_notes_x <<EOF
+20c613c835011c48a5abe29170a2402ca6354910 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
+897003322b53bc6ca098e9324ee508362347e734 2ede89468182a62d0bde2583c736089bcf7d7e92
+11d97fdebfa5ceee540a3da07bce6fa0222bc082 387a89921c73d7ed72cd94d179c1c7048ca47756
+457a85d6c814ea208550f15fcc48f804ac8dc023 516bb9cbbfaa1be722bcf97df393a12e1e7c944d
+7abbc45126d680336fb24294f013a7cdfa3ed545 6352c5e33dbcab725fe0579be16aa2ba8eb369be
+dd161bc149470fd890dd4ab52a4cbd79bbd18c36 7038787dfe22a14c3867ce816dbba39845359719
+a3daf8a1e4e5dc3409a303ad8481d57bfea7f5d6 80d796defacd5db327b7a4e50099663902fbdc5c
+5d30216a129eeffa97d9694ffe8c74317a560315 e5d4fb5698d564ab8c73551538ecaf2b0c666185
+b0c95b954301d69da2bc3723f4cb1680d355937c f42b4e95384566bc7b31d9d3bfcec2d1a8f3e2e8
+b8d03e173f67f6505a76f6e00cf93440200dd9be ffed603236bfa3891c49644257a83598afe8ae5a
+EOF
+
+cat >expect_log_x <<EOF
+516bb9c 15th
+x notes on 15th commit
+
+f42b4e9 14th
+x notes on 14th commit
+
+e5d4fb5 13th
+x notes on 13th commit
+
+7038787 12th
+x notes on 12th commit
+
+6352c5e 11th
+x notes on 11th commit
+
+ffed603 10th
+x notes on 10th commit
+
+016e982 9th
+x notes on 9th commit
+
+80d796d 8th
+x notes on 8th commit
+
+2ede894 7th
+x notes on 7th commit
+
+387a899 6th
+x notes on 6th commit
+
+bd17532 5th
+
+1502353 4th
+
+1584215 3rd
+
+268048b 2nd
+
+34b09d6 1st
+
+EOF
+
+test_expect_success 'verify state of merge base (x)' '
+	git notes >output_notes_x &&
+	test_cmp expect_notes_x output_notes_x &&
+	git log --format="%h %s%n%N" >output_log_x &&
+	test_cmp expect_log_x output_log_x
+'
+
+test_expect_success 'setup local branch (y)' '
+	git update-ref refs/notes/y refs/notes/x &&
+	git config core.notesRef refs/notes/y &&
+	git notes add -f -m "y notes on 3rd commit" 3rd &&
+	git notes add -f -m "y notes on 4th commit" 4th &&
+	git notes add -f -m "y notes on 5th commit" 5th &&
+	git notes remove 6th &&
+	git notes remove 7th &&
+	git notes remove 8th &&
+	git notes add -f -m "y notes on 12th commit" 12th &&
+	git notes add -f -m "y notes on 13th commit" 13th &&
+	git notes add -f -m "y notes on 14th commit" 14th &&
+	git notes add -f -m "y notes on 15th commit" 15th
+'
+
+cat >expect_notes_y <<EOF
+20c613c835011c48a5abe29170a2402ca6354910 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
+e2bfd06a37dd2031684a59a6e2b033e212239c78 15023535574ded8b1a89052b32673f84cf9582b8
+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 1584215f1d29c65e99c6c6848626553fdd07fd75
+68b8630d25516028bed862719855b3d6768d7833 516bb9cbbfaa1be722bcf97df393a12e1e7c944d
+7abbc45126d680336fb24294f013a7cdfa3ed545 6352c5e33dbcab725fe0579be16aa2ba8eb369be
+a66055fa82f7a03fe0c02a6aba3287a85abf7c62 7038787dfe22a14c3867ce816dbba39845359719
+154508c7a0bcad82b6fe4b472bc4c26b3bf0825b bd1753200303d0a0344be813e504253b3d98e74d
+3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc e5d4fb5698d564ab8c73551538ecaf2b0c666185
+5de7ea7ad4f47e7ff91989fb82234634730f75df f42b4e95384566bc7b31d9d3bfcec2d1a8f3e2e8
+b8d03e173f67f6505a76f6e00cf93440200dd9be ffed603236bfa3891c49644257a83598afe8ae5a
+EOF
+
+cat >expect_log_y <<EOF
+516bb9c 15th
+y notes on 15th commit
+
+f42b4e9 14th
+y notes on 14th commit
+
+e5d4fb5 13th
+y notes on 13th commit
+
+7038787 12th
+y notes on 12th commit
+
+6352c5e 11th
+x notes on 11th commit
+
+ffed603 10th
+x notes on 10th commit
+
+016e982 9th
+x notes on 9th commit
+
+80d796d 8th
+
+2ede894 7th
+
+387a899 6th
+
+bd17532 5th
+y notes on 5th commit
+
+1502353 4th
+y notes on 4th commit
+
+1584215 3rd
+y notes on 3rd commit
+
+268048b 2nd
+
+34b09d6 1st
+
+EOF
+
+test_expect_success 'verify state of local branch (y)' '
+	git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	git log --format="%h %s%n%N" >output_log_y &&
+	test_cmp expect_log_y output_log_y
+'
+
+test_expect_success 'setup remote branch (z)' '
+	git update-ref refs/notes/z refs/notes/x &&
+	git config core.notesRef refs/notes/z &&
+	git notes add -f -m "z notes on 2nd commit" 2nd &&
+	git notes add -f -m "y notes on 4th commit" 4th &&
+	git notes add -f -m "z notes on 5th commit" 5th &&
+	git notes remove 6th &&
+	git notes add -f -m "z notes on 8th commit" 8th &&
+	git notes remove 9th &&
+	git notes add -f -m "z notes on 11th commit" 11th &&
+	git notes remove 12th &&
+	git notes add -f -m "y notes on 14th commit" 14th &&
+	git notes add -f -m "z notes on 15th commit" 15th
+'
+
+cat >expect_notes_z <<EOF
+e2bfd06a37dd2031684a59a6e2b033e212239c78 15023535574ded8b1a89052b32673f84cf9582b8
+283b48219aee9a4105f6cab337e789065c82c2b9 268048bfb8a1fb38e703baceb8ab235421bf80c5
+897003322b53bc6ca098e9324ee508362347e734 2ede89468182a62d0bde2583c736089bcf7d7e92
+9b4b2c61f0615412da3c10f98ff85b57c04ec765 516bb9cbbfaa1be722bcf97df393a12e1e7c944d
+7e3c53503a3db8dd996cb62e37c66e070b44b54d 6352c5e33dbcab725fe0579be16aa2ba8eb369be
+851e1638784a884c7dd26c5d41f3340f6387413a 80d796defacd5db327b7a4e50099663902fbdc5c
+99fc34adfc400b95c67b013115e37e31aa9a6d23 bd1753200303d0a0344be813e504253b3d98e74d
+5d30216a129eeffa97d9694ffe8c74317a560315 e5d4fb5698d564ab8c73551538ecaf2b0c666185
+5de7ea7ad4f47e7ff91989fb82234634730f75df f42b4e95384566bc7b31d9d3bfcec2d1a8f3e2e8
+b8d03e173f67f6505a76f6e00cf93440200dd9be ffed603236bfa3891c49644257a83598afe8ae5a
+EOF
+
+cat >expect_log_z <<EOF
+516bb9c 15th
+z notes on 15th commit
+
+f42b4e9 14th
+y notes on 14th commit
+
+e5d4fb5 13th
+x notes on 13th commit
+
+7038787 12th
+
+6352c5e 11th
+z notes on 11th commit
+
+ffed603 10th
+x notes on 10th commit
+
+016e982 9th
+
+80d796d 8th
+z notes on 8th commit
+
+2ede894 7th
+x notes on 7th commit
+
+387a899 6th
+
+bd17532 5th
+z notes on 5th commit
+
+1502353 4th
+y notes on 4th commit
+
+1584215 3rd
+
+268048b 2nd
+z notes on 2nd commit
+
+34b09d6 1st
+
+EOF
+
+test_expect_success 'verify state of remote branch (z)' '
+	git notes >output_notes_z &&
+	test_cmp expect_notes_z output_notes_z &&
+	git log --format="%h %s%n%N" >output_log_z &&
+	test_cmp expect_log_z output_log_z
+'
+
+# At this point, before merging z into y, we have the following status:
+#
+# commit | object  | base/x  | local/y | remote/z |? diff from x to y/z
+# -------|---------|---------|---------|----------||---------------------------
+# 1st    | 34b09d6 | [none]  | [none]  | [none]   || unchanged / unchanged
+# 2nd    | 268048b | [none]  | [none]  | 283b482  || unchanged / added
+# 3rd    | 1584215 | [none]  | 5772f42 | [none]   || added     / unchanged
+# 4th    | 1502353 | [none]  | e2bfd06 | e2bfd06  || added     / added (same)
+# 5th    | bd17532 | [none]  | 154508c | 99fc34a  |+ added     / added (diff)
+# 6th    | 387a899 | 11d97fd | [none]  | [none]   || removed   / removed
+# 7th    | 2ede894 | 8970033 | [none]  | 8970033  || removed   / unchanged
+# 8th    | 80d796d | a3daf8a | [none]  | 851e163  |+ removed   / changed
+# 9th    | 016e982 | 20c613c | 20c613c | [none]   || unchanged / removed
+# 10th   | ffed603 | b8d03e1 | b8d03e1 | b8d03e1  || unchanged / unchanged
+# 11th   | 6352c5e | 7abbc45 | 7abbc45 | 7e3c535  || unchanged / changed
+# 12th   | 7038787 | dd161bc | a66055f | [none]   |+ changed   / removed
+# 13th   | e5d4fb5 | 5d30216 | 3a631fd | 5d30216  || changed   / unchanged
+# 14th   | f42b4e9 | b0c95b9 | 5de7ea7 | 5de7ea7  || changed   / changed (same)
+# 15th   | 516bb9c | 457a85d | 68b8630 | 9b4b2c6  |+ changed   / changed (diff)
+
+test_expect_success 'merge z into y with invalid resolver => Fail/No changes' '
+	git config core.notesRef refs/notes/y &&
+	test_must_fail git notes merge --resolve=foo z &&
+	# Verify no changes (y)
+	git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	git log --format="%h %s%n%N" >output_log_y &&
+	test_cmp expect_log_y output_log_y
+'
+
+cat >expect_notes_ours <<EOF
+e2bfd06a37dd2031684a59a6e2b033e212239c78 15023535574ded8b1a89052b32673f84cf9582b8
+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 1584215f1d29c65e99c6c6848626553fdd07fd75
+283b48219aee9a4105f6cab337e789065c82c2b9 268048bfb8a1fb38e703baceb8ab235421bf80c5
+68b8630d25516028bed862719855b3d6768d7833 516bb9cbbfaa1be722bcf97df393a12e1e7c944d
+7e3c53503a3db8dd996cb62e37c66e070b44b54d 6352c5e33dbcab725fe0579be16aa2ba8eb369be
+a66055fa82f7a03fe0c02a6aba3287a85abf7c62 7038787dfe22a14c3867ce816dbba39845359719
+154508c7a0bcad82b6fe4b472bc4c26b3bf0825b bd1753200303d0a0344be813e504253b3d98e74d
+3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc e5d4fb5698d564ab8c73551538ecaf2b0c666185
+5de7ea7ad4f47e7ff91989fb82234634730f75df f42b4e95384566bc7b31d9d3bfcec2d1a8f3e2e8
+b8d03e173f67f6505a76f6e00cf93440200dd9be ffed603236bfa3891c49644257a83598afe8ae5a
+EOF
+
+cat >expect_log_ours <<EOF
+516bb9c 15th
+y notes on 15th commit
+
+f42b4e9 14th
+y notes on 14th commit
+
+e5d4fb5 13th
+y notes on 13th commit
+
+7038787 12th
+y notes on 12th commit
+
+6352c5e 11th
+z notes on 11th commit
+
+ffed603 10th
+x notes on 10th commit
+
+016e982 9th
+
+80d796d 8th
+
+2ede894 7th
+
+387a899 6th
+
+bd17532 5th
+y notes on 5th commit
+
+1502353 4th
+y notes on 4th commit
+
+1584215 3rd
+y notes on 3rd commit
+
+268048b 2nd
+z notes on 2nd commit
+
+34b09d6 1st
+
+EOF
+
+test_expect_success 'merge z into y with "ours" resolver => Non-conflicting 3-way merge' '
+	git notes merge --resolve=ours z &&
+	# Verify merge result
+	git notes >output_notes_ours &&
+	test_cmp expect_notes_ours output_notes_ours &&
+	git log --format="%h %s%n%N" >output_log_ours &&
+	test_cmp expect_log_ours output_log_ours
+'
+
+test_expect_success 'reset to pre-merge state (y)' '
+	git update-ref refs/notes/y refs/notes/y^1 &&
+	# Verify pre-merge state
+	git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	git log --format="%h %s%n%N" >output_log_y &&
+	test_cmp expect_log_y output_log_y
+'
+
+cat >expect_notes_theirs <<EOF
+e2bfd06a37dd2031684a59a6e2b033e212239c78 15023535574ded8b1a89052b32673f84cf9582b8
+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 1584215f1d29c65e99c6c6848626553fdd07fd75
+283b48219aee9a4105f6cab337e789065c82c2b9 268048bfb8a1fb38e703baceb8ab235421bf80c5
+9b4b2c61f0615412da3c10f98ff85b57c04ec765 516bb9cbbfaa1be722bcf97df393a12e1e7c944d
+7e3c53503a3db8dd996cb62e37c66e070b44b54d 6352c5e33dbcab725fe0579be16aa2ba8eb369be
+851e1638784a884c7dd26c5d41f3340f6387413a 80d796defacd5db327b7a4e50099663902fbdc5c
+99fc34adfc400b95c67b013115e37e31aa9a6d23 bd1753200303d0a0344be813e504253b3d98e74d
+3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc e5d4fb5698d564ab8c73551538ecaf2b0c666185
+5de7ea7ad4f47e7ff91989fb82234634730f75df f42b4e95384566bc7b31d9d3bfcec2d1a8f3e2e8
+b8d03e173f67f6505a76f6e00cf93440200dd9be ffed603236bfa3891c49644257a83598afe8ae5a
+EOF
+
+cat >expect_log_theirs <<EOF
+516bb9c 15th
+z notes on 15th commit
+
+f42b4e9 14th
+y notes on 14th commit
+
+e5d4fb5 13th
+y notes on 13th commit
+
+7038787 12th
+
+6352c5e 11th
+z notes on 11th commit
+
+ffed603 10th
+x notes on 10th commit
+
+016e982 9th
+
+80d796d 8th
+z notes on 8th commit
+
+2ede894 7th
+
+387a899 6th
+
+bd17532 5th
+z notes on 5th commit
+
+1502353 4th
+y notes on 4th commit
+
+1584215 3rd
+y notes on 3rd commit
+
+268048b 2nd
+z notes on 2nd commit
+
+34b09d6 1st
+
+EOF
+
+test_expect_success 'merge z into y with "theirs" resolver => Non-conflicting 3-way merge' '
+	git notes merge --resolve=theirs z &&
+	# Verify merge result
+	git notes >output_notes_theirs &&
+	test_cmp expect_notes_theirs output_notes_theirs &&
+	git log --format="%h %s%n%N" >output_log_theirs &&
+	test_cmp expect_log_theirs output_log_theirs
+'
+
+test_expect_success 'reset to pre-merge state (y)' '
+	git update-ref refs/notes/y refs/notes/y^1 &&
+	# Verify pre-merge state
+	git notes >output_notes_y &&
+	test_cmp expect_notes_y output_notes_y &&
+	git log --format="%h %s%n%N" >output_log_y &&
+	test_cmp expect_log_y output_log_y
+'
+
+cat >expect_notes_union <<EOF
+e2bfd06a37dd2031684a59a6e2b033e212239c78 15023535574ded8b1a89052b32673f84cf9582b8
+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 1584215f1d29c65e99c6c6848626553fdd07fd75
+283b48219aee9a4105f6cab337e789065c82c2b9 268048bfb8a1fb38e703baceb8ab235421bf80c5
+7c4e546efd0fe939f876beb262ece02797880b54 516bb9cbbfaa1be722bcf97df393a12e1e7c944d
+7e3c53503a3db8dd996cb62e37c66e070b44b54d 6352c5e33dbcab725fe0579be16aa2ba8eb369be
+a66055fa82f7a03fe0c02a6aba3287a85abf7c62 7038787dfe22a14c3867ce816dbba39845359719
+851e1638784a884c7dd26c5d41f3340f6387413a 80d796defacd5db327b7a4e50099663902fbdc5c
+6c841cc36ea496027290967ca96bd2bef54dbb47 bd1753200303d0a0344be813e504253b3d98e74d
+3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc e5d4fb5698d564ab8c73551538ecaf2b0c666185
+5de7ea7ad4f47e7ff91989fb82234634730f75df f42b4e95384566bc7b31d9d3bfcec2d1a8f3e2e8
+b8d03e173f67f6505a76f6e00cf93440200dd9be ffed603236bfa3891c49644257a83598afe8ae5a
+EOF
+
+cat >expect_log_union <<EOF
+516bb9c 15th
+y notes on 15th commit
+
+z notes on 15th commit
+
+f42b4e9 14th
+y notes on 14th commit
+
+e5d4fb5 13th
+y notes on 13th commit
+
+7038787 12th
+y notes on 12th commit
+
+6352c5e 11th
+z notes on 11th commit
+
+ffed603 10th
+x notes on 10th commit
+
+016e982 9th
+
+80d796d 8th
+z notes on 8th commit
+
+2ede894 7th
+
+387a899 6th
+
+bd17532 5th
+y notes on 5th commit
+
+z notes on 5th commit
+
+1502353 4th
+y notes on 4th commit
+
+1584215 3rd
+y notes on 3rd commit
+
+268048b 2nd
+z notes on 2nd commit
+
+34b09d6 1st
+
+EOF
+
+test_expect_success 'merge z into y with "union" resolver => Non-conflicting 3-way merge' '
+	git notes merge --resolve=union z &&
+	# Verify merge result
+	git notes >output_notes_union &&
+	test_cmp expect_notes_union output_notes_union &&
+	git log --format="%h %s%n%N" >output_log_union &&
+	test_cmp expect_log_union output_log_union
+'
+
+test_done
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [WIP/RFC 13/13] Documentation: Preliminary docs on 'git notes merge'
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (11 preceding siblings ...)
  2010-07-23 10:15 ` [WIP/RFC 12/13] git notes merge: Add automatic conflict resolvers (ours, theirs, union) Johan Herland
@ 2010-07-23 10:15 ` Johan Herland
  2010-07-24  8:21   ` Stephen Boyd
  2010-07-25 10:45 ` [WIP/RFC 00/13] git notes merge Sam Vilain
  13 siblings, 1 reply; 24+ messages in thread
From: Johan Herland @ 2010-07-23 10:15 UTC (permalink / raw)
  To: git; +Cc: johan

Signed-off-by: Johan Herland <johan@herland.net>
---
 Documentation/git-notes.txt |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt
index ce0ea03..f4ad4cc 100644
--- a/Documentation/git-notes.txt
+++ b/Documentation/git-notes.txt
@@ -14,6 +14,7 @@ SYNOPSIS
 'git notes' append [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]
 'git notes' edit [<object>]
 'git notes' show [<object>]
+'git notes' merge [-v | -q] [-X <strategy> ] <notes_ref>
 'git notes' remove [<object>]
 'git notes' prune [-n | -v]
 
@@ -83,6 +84,15 @@ edit::
 show::
 	Show the notes for a given object (defaults to HEAD).
 
+merge::
+	Merge the given notes ref into the current notes ref.
+	This will try to merge the changes made by the given
+	notes ref (called "remote") since the merge-base (if
+	any) into the current notes ref (called "local").
++
+If conflicts arise (and a strategy for auto-resolving conflicting notes
+(see the -X/--resolve option) is not given, the merge fails (TODO).
+
 remove::
 	Remove the notes for a given object (defaults to HEAD).
 	This is equivalent to specifying an empty note message to
@@ -132,8 +142,24 @@ OPTIONS
 	Do not remove anything; just report the object names whose notes
 	would be removed.
 
+-X <strategy>::
+--resolve=<strategy>::
+	When merging notes, resolve notes conflicts using the given
+	strategy. The following strategies are recognized: "manual"
+	(default), "ours", "theirs" and "union". "ours" automatically
+	resolves conflicting notes in favor of the local version (i.e.
+	the current notes ref). "theirs" auto-resolves notes conflicts
+	in favor of the remote version (i.e. the given notes ref being
+	merged into the current notes ref). "union" auto-resolves
+	notes conflicts by concatenating the local and remote versions.
+
+-q::
+--quiet::
+	When merging notes, operate quietly.
+
 -v::
 --verbose::
+	When merging notes, be more verbose.
 	When pruning notes, report all object names whose notes are
 	removed.
 
-- 
1.7.2.220.gea1d3

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [WIP/RFC 04/13] notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond
  2010-07-23 10:14 ` [WIP/RFC 04/13] notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond Johan Herland
@ 2010-07-23 15:23   ` Jonathan Nieder
  2010-07-23 22:39     ` Johan Herland
  0 siblings, 1 reply; 24+ messages in thread
From: Jonathan Nieder @ 2010-07-23 15:23 UTC (permalink / raw)
  To: Johan Herland; +Cc: git

Hi,

Johan Herland wrote:

> --- a/builtin/notes.c
> +++ b/builtin/notes.c
> @@ -574,6 +574,7 @@ static int add(int argc, const char **argv, const char *prefix)
>  	if (is_null_sha1(new_note))
>  		remove_note(t, object);
>  	else
> +		/* No return value checking; c_n_overwrite always returns 0 */
>  		add_note(t, object, new_note, combine_notes_overwrite);

I suspect something like

	if (add_note(t, object, ...))
		die("confused: combine_notes_overwrite failed");

would be less likely to fall out of date. ;-)

This whole series is good stuff.  I look forward to trying it
out.

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [WIP/RFC 04/13] notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond
  2010-07-23 15:23   ` Jonathan Nieder
@ 2010-07-23 22:39     ` Johan Herland
  0 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-23 22:39 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: git

On Friday 23 July 2010, Jonathan Nieder wrote:
> Johan Herland wrote:
> > +		/* No return value checking; c_n_overwrite always returns 0 */
> >  		add_note(t, object, new_note, combine_notes_overwrite);
> 
> I suspect something like
> 
> 	if (add_note(t, object, ...))
> 		die("confused: combine_notes_overwrite failed");
> 
> would be less likely to fall out of date. ;-)

Good call, I'll squash the following into the next iteration:

Thanks!

...Johan


diff --git a/builtin/notes.c b/builtin/notes.c
index 516401c..5aaae03 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -576,9 +576,8 @@ static int add(int argc, const char **argv, const char *prefix)
 
 	if (is_null_sha1(new_note))
 		remove_note(t, object);
-	else
-		/* No return value checking; c_n_overwrite always returns 0 */
-		add_note(t, object, new_note, combine_notes_overwrite);
+	else if (add_note(t, object, new_note, combine_notes_overwrite))
+		die("confused: combine_notes_overwrite failed");
 
 	snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
 		 is_null_sha1(new_note) ? "removed" : "added", "add");
@@ -657,8 +656,8 @@ static int copy(int argc, const char **argv, const char *prefix)
 		goto out;
 	}
 
-	/* No return value checking; c_n_overwrite always returns 0 */
-	add_note(t, object, from_note, combine_notes_overwrite);
+	if (add_note(t, object, from_note, combine_notes_overwrite))
+		die("confused: combine_notes_overwrite failed");
 	commit_notes(t, "Notes added by 'git notes copy'");
 out:
 	free_notes(t);
@@ -717,9 +716,8 @@ static int append_edit(int argc, const char **argv, const char *prefix)
 
 	if (is_null_sha1(new_note))
 		remove_note(t, object);
-	else
-		/* No return value checking; c_n_overwrite always returns 0 */
-		add_note(t, object, new_note, combine_notes_overwrite);
+	else if (add_note(t, object, new_note, combine_notes_overwrite))
+		die("confused: combine_notes_overwrite failed");
 
 	snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
 		 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
diff --git a/notes-merge.c b/notes-merge.c
index 122d6b9..47cd32a 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -442,12 +442,14 @@ static int merge_one_change(struct notes_merge_options *o,
 		return 0;
 	case NOTES_MERGE_RESOLVE_THEIRS:
 		OUTPUT(o, 2, "Using remote notes for %s", sha1_to_hex(p->obj));
-		add_note(t, p->obj, p->remote, combine_notes_overwrite);
+		if (add_note(t, p->obj, p->remote, combine_notes_overwrite))
+			die("confused: combine_notes_overwrite failed");
 		return 0;
 	case NOTES_MERGE_RESOLVE_UNION:
 		OUTPUT(o, 2, "Concatenating local and remote notes for %s",
 		       sha1_to_hex(p->obj));
-		add_note(t, p->obj, p->remote, combine_notes_concatenate);
+		if (add_note(t, p->obj, p->remote, combine_notes_concatenate))
+			die("confused: combine_notes_concatenate failed");
 		return 0;
 	}
 	die("Unknown resolve method (%i).", o->resolve);
@@ -489,7 +491,9 @@ static int merge_changes(struct notes_merge_options *o,
 			   !hashcmp(p->local, p->base)) {
 			/* no local change; adopt remote change */
 			OUTPUT(o, 5, "\t\t\tno local change, adopting remote");
-			add_note(t, p->obj, p->remote, combine_notes_overwrite);
+			if (add_note(t, p->obj, p->remote,
+				     combine_notes_overwrite))
+				die("confused: combine_notes_overwrite failed");
 		} else {
 			/* need file-level merge between local and remote */
 			OUTPUT(o, 5, "\t\t\tneed content-level merge");


-- 
Johan Herland, <johan@herland.net>
www.herland.net

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [WIP/RFC 12/13] git notes merge: Add automatic conflict resolvers (ours, theirs, union)
  2010-07-23 10:15 ` [WIP/RFC 12/13] git notes merge: Add automatic conflict resolvers (ours, theirs, union) Johan Herland
@ 2010-07-24  8:18   ` Stephen Boyd
  2010-07-25 18:58     ` Johan Herland
  0 siblings, 1 reply; 24+ messages in thread
From: Stephen Boyd @ 2010-07-24  8:18 UTC (permalink / raw)
  To: Johan Herland; +Cc: git

  On 07/23/2010 03:15 AM, Johan Herland wrote:
> +
> +# Set up a notes merge scenario with all kinds of potential conflicts
> +test_expect_success 'setup commits' '
> +	# Create 15 commits with tags ("1st" through "15th")
> +	:>  a1&&  git add a1&&  test_tick&&  git commit -m 1st&&  git tag 1st&&
> +	:>  a2&&  git add a2&&  test_tick&&  git commit -m 2nd&&  git tag 2nd&&
> +	:>  a3&&  git add a3&&  test_tick&&  git commit -m 3rd&&  git tag 3rd&&
> +	:>  a4&&  git add a4&&  test_tick&&  git commit -m 4th&&  git tag 4th&&
> +	:>  a5&&  git add a5&&  test_tick&&  git commit -m 5th&&  git tag 5th&&
> +	:>  a6&&  git add a6&&  test_tick&&  git commit -m 6th&&  git tag 6th&&
> +	:>  a7&&  git add a7&&  test_tick&&  git commit -m 7th&&  git tag 7th&&
> +	:>  a8&&  git add a8&&  test_tick&&  git commit -m 8th&&  git tag 8th&&
> +	:>  a9&&  git add a9&&  test_tick&&  git commit -m 9th&&  git tag 9th&&
> +	:>  a10&&  git add a10&&  test_tick&&  git commit -m 10th&&  git tag 10th&&
> +	:>  a11&&  git add a11&&  test_tick&&  git commit -m 11th&&  git tag 11th&&
> +	:>  a12&&  git add a12&&  test_tick&&  git commit -m 12th&&  git tag 12th&&
> +	:>  a13&&  git add a13&&  test_tick&&  git commit -m 13th&&  git tag 13th&&
> +	:>  a14&&  git add a14&&  test_tick&&  git commit -m 14th&&  git tag 14th&&
> +	:>  a15&&  git add a15&&  test_tick&&  git commit -m 15th&&  git tag 15th
> +'

Can you use test_commit here?

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [WIP/RFC 13/13] Documentation: Preliminary docs on 'git notes merge'
  2010-07-23 10:15 ` [WIP/RFC 13/13] Documentation: Preliminary docs on 'git notes merge' Johan Herland
@ 2010-07-24  8:21   ` Stephen Boyd
  2010-07-25 19:02     ` Johan Herland
  0 siblings, 1 reply; 24+ messages in thread
From: Stephen Boyd @ 2010-07-24  8:21 UTC (permalink / raw)
  To: Johan Herland; +Cc: git

  On 07/23/2010 03:15 AM, Johan Herland wrote:
> +-X<strategy>::
> +--resolve=<strategy>::
> +	When merging notes, resolve notes conflicts using the given
> +	strategy. The following strategies are recognized: "manual"
> +	(default), "ours", "theirs" and "union". "ours" automatically
> +	resolves conflicting notes in favor of the local version (i.e.
> +	the current notes ref). "theirs" auto-resolves notes conflicts
> +	in favor of the remote version (i.e. the given notes ref being
> +	merged into the current notes ref). "union" auto-resolves
> +	notes conflicts by concatenating the local and remote versions.
> +

We use both "auto-resolves" and "automatically resolves" in this paragraph. Perhaps it's better to use one term throughout?

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [WIP/RFC 09/13] git notes merge: Initial implementation handling trivial merges only
  2010-07-23 10:15 ` [WIP/RFC 09/13] git notes merge: Initial implementation handling trivial merges only Johan Herland
@ 2010-07-24  8:32   ` Stephen Boyd
  2010-07-25 14:14     ` Johan Herland
  0 siblings, 1 reply; 24+ messages in thread
From: Stephen Boyd @ 2010-07-24  8:32 UTC (permalink / raw)
  To: Johan Herland; +Cc: git

  On 07/23/2010 03:15 AM, Johan Herland wrote:
> +	if (1<  argc) {
> +		error("too many parameters");
> +		usage_with_options(git_notes_merge_usage, options);
> +	} else if (1>  argc) {
> +		error("too few parameters");
> +		usage_with_options(git_notes_merge_usage, options);
> +	}
> +
>

Looks like it only takes one <notes_ref>. In that case wouldn't it be better to say

     if (argc != 1) {
         error("Must specify a note ref to merge");
         usage_with_options(git_notes_merge_usage, options)
     }

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [WIP/RFC 00/13] git notes merge
  2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
                   ` (12 preceding siblings ...)
  2010-07-23 10:15 ` [WIP/RFC 13/13] Documentation: Preliminary docs on 'git notes merge' Johan Herland
@ 2010-07-25 10:45 ` Sam Vilain
  2010-07-25 16:44   ` Johan Herland
  13 siblings, 1 reply; 24+ messages in thread
From: Sam Vilain @ 2010-07-25 10:45 UTC (permalink / raw)
  To: Johan Herland; +Cc: git

On Fri, 2010-07-23 at 12:14 +0200, Johan Herland wrote:
> Hi,
> 
> Here are some rough patches implementing most of the 'git notes merge'
> functionality. There are still missing pieces in the area of manual
> conflict resolution, but what's here should be enough to start
> experimenting with automatic notes merge resolution.

Having implemented this before as a client-level solution I can applaud
the goal of tackling this.

There is also the issue of making the notes track automatically on 'git
clone' or something like that, don't know if anyone's looked at it yet
but that's another annoyance I've had..

Sam

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [WIP/RFC 09/13] git notes merge: Initial implementation handling trivial merges only
  2010-07-24  8:32   ` Stephen Boyd
@ 2010-07-25 14:14     ` Johan Herland
  0 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-25 14:14 UTC (permalink / raw)
  To: Stephen Boyd; +Cc: git

On Saturday 24 July 2010, Stephen Boyd wrote:
>   On 07/23/2010 03:15 AM, Johan Herland wrote:
> > +	if (1<  argc) {
> > +		error("too many parameters");
> > +		usage_with_options(git_notes_merge_usage, options);
> > +	} else if (1>  argc) {
> > +		error("too few parameters");
> > +		usage_with_options(git_notes_merge_usage, options);
> > +	}
> > +
> 
> Looks like it only takes one <notes_ref>. In that case wouldn't it be
> better to say
> 
>      if (argc != 1) {
>          error("Must specify a note ref to merge");
>          usage_with_options(git_notes_merge_usage, options)
>      }

Thanks. I'll squash the following into the next iteration:

...Johan

diff --git a/builtin/notes.c b/builtin/notes.c
index f2bc767..ca0e4d9 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -780,11 +780,8 @@ static int merge(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, options,
 			     git_notes_merge_usage, 0);
 
-	if (1 < argc) {
-		error("too many parameters");
-		usage_with_options(git_notes_merge_usage, options);
-	} else if (1 > argc) {
-		error("too few parameters");
+	if (argc != 1) {
+		error("Must specify a notes ref to merge");
 		usage_with_options(git_notes_merge_usage, options);
 	}
 


-- 
Johan Herland, <johan@herland.net>
www.herland.net

^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [WIP/RFC 00/13] git notes merge
  2010-07-25 10:45 ` [WIP/RFC 00/13] git notes merge Sam Vilain
@ 2010-07-25 16:44   ` Johan Herland
  0 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-25 16:44 UTC (permalink / raw)
  To: Sam Vilain; +Cc: git

On Sunday 25 July 2010, Sam Vilain wrote:
> On Fri, 2010-07-23 at 12:14 +0200, Johan Herland wrote:
> > Hi,
> > 
> > Here are some rough patches implementing most of the 'git notes merge'
> > functionality. There are still missing pieces in the area of manual
> > conflict resolution, but what's here should be enough to start
> > experimenting with automatic notes merge resolution.
> 
> Having implemented this before as a client-level solution I can applaud
> the goal of tackling this.
> 
> There is also the issue of making the notes track automatically on 'git
> clone' or something like that, don't know if anyone's looked at it yet
> but that's another annoyance I've had..

Agreed, although I see the solving the merge problem as a prerequisite to 
dealing with exchanging notes between repos. One thing at a time...


...Johan

-- 
Johan Herland, <johan@herland.net>
www.herland.net

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [WIP/RFC 12/13] git notes merge: Add automatic conflict resolvers (ours, theirs, union)
  2010-07-24  8:18   ` Stephen Boyd
@ 2010-07-25 18:58     ` Johan Herland
  0 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-25 18:58 UTC (permalink / raw)
  To: Stephen Boyd; +Cc: git

On Saturday 24 July 2010, Stephen Boyd wrote:
>   On 07/23/2010 03:15 AM, Johan Herland wrote:
> > +
> > +# Set up a notes merge scenario with all kinds of potential conflicts
> > +test_expect_success 'setup commits' '
> > +	# Create 15 commits with tags ("1st" through "15th")
> > [...]
> 
> Can you use test_commit here?

Yes, indeed. Thanks for noticing. I've rewritten the tests using
test_commit (and using variables to store commit SHA1s, so that the
tests are more robust against those changing)


...Johan

-- 
Johan Herland, <johan@herland.net>
www.herland.net

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [WIP/RFC 13/13] Documentation: Preliminary docs on 'git notes merge'
  2010-07-24  8:21   ` Stephen Boyd
@ 2010-07-25 19:02     ` Johan Herland
  0 siblings, 0 replies; 24+ messages in thread
From: Johan Herland @ 2010-07-25 19:02 UTC (permalink / raw)
  To: Stephen Boyd; +Cc: git

On Saturday 24 July 2010, Stephen Boyd wrote:
>   On 07/23/2010 03:15 AM, Johan Herland wrote:
> > +-X<strategy>::
> > +--resolve=<strategy>::
> > +	When merging notes, resolve notes conflicts using the given
> > +	strategy. The following strategies are recognized: "manual"
> > +	(default), "ours", "theirs" and "union". "ours" automatically
> > +	resolves conflicting notes in favor of the local version (i.e.
> > +	the current notes ref). "theirs" auto-resolves notes conflicts
> > +	in favor of the remote version (i.e. the given notes ref being
> > +	merged into the current notes ref). "union" auto-resolves
> > +	notes conflicts by concatenating the local and remote versions.
> > +
> 
> We use both "auto-resolves" and "automatically resolves" in this
> paragraph. Perhaps it's better to use one term throughout?

Agreed. I'll squash the following into the next iteration:


...Johan


diff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt
index f4ad4cc..9228349 100644
--- a/Documentation/git-notes.txt
+++ b/Documentation/git-notes.txt
@@ -90,8 +90,9 @@ merge::
 	notes ref (called "remote") since the merge-base (if
 	any) into the current notes ref (called "local").
 +
-If conflicts arise (and a strategy for auto-resolving conflicting notes
-(see the -X/--resolve option) is not given, the merge fails (TODO).
+If conflicts arise (and a strategy for automatically resolving
+conflicting notes (see the -X/--resolve option) is not given,
+the merge fails (TODO).
 
 remove::
 	Remove the notes for a given object (defaults to HEAD).
@@ -146,12 +147,14 @@ OPTIONS
 --resolve=<strategy>::
 	When merging notes, resolve notes conflicts using the given
 	strategy. The following strategies are recognized: "manual"
-	(default), "ours", "theirs" and "union". "ours" automatically
-	resolves conflicting notes in favor of the local version (i.e.
-	the current notes ref). "theirs" auto-resolves notes conflicts
-	in favor of the remote version (i.e. the given notes ref being
-	merged into the current notes ref). "union" auto-resolves
-	notes conflicts by concatenating the local and remote versions.
+	(default), "ours", "theirs" and "union".
+	"ours" automatically resolves conflicting notes in favor of the
+	local version (i.e. the current notes ref).
+	"theirs" automatically resolves notes conflicts in favor of the
+	remote version (i.e. the given notes ref being merged into the
+	current notes ref).
+	"union" automatically resolves notes conflicts by concatenating
+	the local and remote versions.
 
 -q::
 --quiet::



-- 
Johan Herland, <johan@herland.net>
www.herland.net

^ permalink raw reply related	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2010-07-25 19:02 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-23 10:14 [WIP/RFC 00/13] git notes merge Johan Herland
2010-07-23 10:14 ` [WIP/RFC 01/13] (trivial) notes.h: Minor documentation fixes to copy_notes() Johan Herland
2010-07-23 10:14 ` [WIP/RFC 02/13] notes.h: Make default_notes_ref() available in notes API Johan Herland
2010-07-23 10:14 ` [WIP/RFC 03/13] notes.h/c: Clarify the handling of notes objects that are == null_sha1 Johan Herland
2010-07-23 10:14 ` [WIP/RFC 04/13] notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond Johan Herland
2010-07-23 15:23   ` Jonathan Nieder
2010-07-23 22:39     ` Johan Herland
2010-07-23 10:14 ` [WIP/RFC 05/13] (trivial) t3303: Indent with tabs instead of spaces for consistency Johan Herland
2010-07-23 10:14 ` [WIP/RFC 06/13] notes.c: Use two newlines (instead of one) when concatenating notes Johan Herland
2010-07-23 10:14 ` [WIP/RFC 07/13] (trivial) git notes prune: Accept --verbose in addition to -v Johan Herland
2010-07-23 10:14 ` [WIP/RFC 08/13] builtin/notes.c: Split notes ref DWIMmery into a separate function Johan Herland
2010-07-23 10:15 ` [WIP/RFC 09/13] git notes merge: Initial implementation handling trivial merges only Johan Herland
2010-07-24  8:32   ` Stephen Boyd
2010-07-25 14:14     ` Johan Herland
2010-07-23 10:15 ` [WIP/RFC 10/13] builtin/notes.c: Refactor creation of notes commits Johan Herland
2010-07-23 10:15 ` [WIP/RFC 11/13] git notes merge: Handle real, non-conflicting notes merges Johan Herland
2010-07-23 10:15 ` [WIP/RFC 12/13] git notes merge: Add automatic conflict resolvers (ours, theirs, union) Johan Herland
2010-07-24  8:18   ` Stephen Boyd
2010-07-25 18:58     ` Johan Herland
2010-07-23 10:15 ` [WIP/RFC 13/13] Documentation: Preliminary docs on 'git notes merge' Johan Herland
2010-07-24  8:21   ` Stephen Boyd
2010-07-25 19:02     ` Johan Herland
2010-07-25 10:45 ` [WIP/RFC 00/13] git notes merge Sam Vilain
2010-07-25 16:44   ` Johan Herland

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.