{"thread":{"id":"25282","subject":"[PATCH 01/18] (trivial) notes.h: Minor documentation fixes to copy_notes()","startedAt":"2010-09-29T00:23:13Z","lastAt":"2010-10-21T02:12:54Z","messageCount":46,"participants":["Johan Herland","Sverre Rabbelier","Ævar Arnfjörð Bjarmason","Stephen Boyd","Jonathan Nieder","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":18},"messages":[{"id":"151991","messageId":"1285719811-10871-1-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":null,"subject":"[PATCH 00/18] git notes merge","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:13Z","receivedAt":"2010-09-29T00:23:13Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Hi,\n\nThis is the 2nd iteration of the 'git notes merge' patch series.\n\nChanges between the 1st WIP/RFC iteration and this version:\n\n- Rebased on top of e93487d which obsoletes patch #7 in the previous\n  series. AFAICS, it now merges cleanly against \"master\", and almost\n  cleanly (modulo one trivial conflict) against \"next\". If desired,\n  I can rebase the series onto \"next\" to avoid the trivial conflict.\n\n- (Jonathan Nieder) Future-proof by always checking add_note() return\n  value.\n\n- (Stephen Boyd) Simplify argc logic.\n\n- (Stephen Boyd) Use test_commit.\n\n- (Stephen Boyd) Use \"automatically resolves\" instead of\n  \"auto-resolves\".\n\n- Renamed -X/--resolve to -s/--strategy. This is more in line with\n  'git merge' (although the available strategies are different), and\n  allows us to use -X for strategy-specific options in the future.\n\n- Documentation: Moved documentation of notes merge strategies into\n  its own section.\n\n- Implemented manual conflict resolution of notes. Conflicting note\n  entries are stored in .git/NOTES_MERGE_WORKTREE/*, while the\n  non-conflicting note entries are committed, and referenced by\n  .git/NOTES_MERGE_PARTIAL (aka. the partial merge result). When the\n  conflicts have been edited/fixed, the user calls\n  'git notes merge --commit' to recombine the partial merge result and\n  the resolved conflicts to produce the final notes merge result.\n  Alternatively, the user can call 'git notes merge --reset' to abort\n  the notes merge.\n\n- Add another auto-resolving notes merge strategy: \"cat_sort_uniq\"\n\n- Test merging of notes trees at different fanout levels\n\n\nTo be done in later series:\n\n- Handle merging of notes trees with non-notes\n\n- Allow notes merge strategies to be specified (per (group of?) notes\n  ref) in the config\n\n\nSome open questions:\n\n- Should we refuse to finalize a notes merge when conflict markers\n  present in .git/NOTES_MERGE_WORKTREE? Since add/commit in regular\n  merges does NOT do this, I have not implemented it for notes merge.\n\n- When resolving notes merge conflicts, you can add/remove files/notes\n  in .git/NOTES_MERGE_WORKTREE; 'git notes merge --commit' does not\n  check that the notes have any relationship to the notes originally\n  put there by 'git notes merge'. Should we warn about removed and\n  added notes in .git/NOTES_MERGE_WORKTREE? Currently we don't, and\n  I'm not sure it's worth it. Users can always review the merge commit\n  afterwards.\n\n- Fetching and pushing note refs:\n  - Add refs/notes/* to default fetch refspec?\n  - A way to specify (at clone time) which refspec(s) to set up?\n  - A way for the remote repo to hint at which refspecs you might want\n    to set up (by default?)\n\n\nHave fun! :)\n\n...Johan\n\n\nJohan Herland (18):\n  (trivial) notes.h: Minor documentation fixes to copy_notes()\n  notes.h: Make default_notes_ref() available in notes API\n  notes.h/c: Clarify the handling of notes objects that are == null_sha1\n  notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond\n  (trivial) t3303: Indent with tabs instead of spaces for consistency\n  notes.c: Use two newlines (instead of one) when concatenating notes\n  builtin/notes.c: Split notes ref DWIMmery into a separate function\n  git notes merge: Initial implementation handling trivial merges only\n  builtin/notes.c: Refactor creation of notes commits.\n  git notes merge: Handle real, non-conflicting notes merges\n  git notes merge: Add automatic conflict resolvers (ours, theirs, union)\n  Documentation: Preliminary docs on 'git notes merge'\n  git notes merge: Manual conflict resolution, part 1/2\n  git notes merge: Manual conflict resolution, part 2/2\n  git notes merge: List conflicting notes in notes merge commit message\n  git notes merge: --commit should fail if underlying notes ref has moved\n  git notes merge: Add another auto-resolving strategy: \"cat_sort_uniq\"\n  git notes merge: Add testcases for merging notes trees at different fanouts\n\n Documentation/git-notes.txt           |   81 ++++-\n Makefile                              |    2 +\n builtin.h                             |    2 +-\n builtin/notes.c                       |  245 ++++++++++--\n notes-cache.c                         |    3 +-\n notes-merge.c                         |  718 +++++++++++++++++++++++++++++++++\n notes-merge.h                         |   92 +++++\n notes.c                               |  272 +++++++++-----\n notes.h                               |   47 ++-\n t/t3301-notes.sh                      |    4 +\n t/t3303-notes-subtrees.sh             |   19 +-\n t/t3308-notes-merge.sh                |  327 +++++++++++++++\n t/t3309-notes-merge-auto-resolve.sh   |  647 +++++++++++++++++++++++++++++\n t/t3310-notes-merge-manual-resolve.sh |  556 +++++++++++++++++++++++++\n t/t3311-notes-merge-fanout.sh         |  436 ++++++++++++++++++++\n t/t3404-rebase-interactive.sh         |    1 +\n t/t9301-fast-import-notes.sh          |    5 +\n 17 files changed, 3315 insertions(+), 142 deletions(-)\n create mode 100644 notes-merge.c\n create mode 100644 notes-merge.h\n create mode 100755 t/t3308-notes-merge.sh\n create mode 100755 t/t3309-notes-merge-auto-resolve.sh\n create mode 100755 t/t3310-notes-merge-manual-resolve.sh\n create mode 100755 t/t3311-notes-merge-fanout.sh\n\n--\n1.7.3.98.g5ad7d9\n"},{"id":"151986","messageId":"1285719811-10871-2-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 01/18] (trivial) notes.h: Minor documentation fixes to copy_notes()","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:14Z","receivedAt":"2010-09-29T00:23:14Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Signed-off-by: Johan Herland <johan@herland.net>\n---\n notes.h |    5 +++++\n 1 files changed, 5 insertions(+), 0 deletions(-)\n\ndiff --git a/notes.h b/notes.h\nindex 65fc3a6..c0288b0 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -104,6 +104,10 @@ const unsigned char *get_note(struct notes_tree *t,\n  * Copy a note from one object to another in the given notes_tree.\n  *\n  * Fails if the to_obj already has a note unless 'force' is true.\n+ *\n+ * IMPORTANT: The changes made by copy_note() to the given notes_tree structure\n+ * are not persistent until a subsequent call to write_notes_tree() returns\n+ * zero.\n  */\n int copy_note(struct notes_tree *t,\n \t      const unsigned char *from_obj, const unsigned char *to_obj,\n@@ -149,6 +153,7 @@ int copy_note(struct notes_tree *t,\n  * notes tree) from within the callback:\n  * - add_note()\n  * - remove_note()\n+ * - copy_note()\n  * - free_notes()\n  */\n typedef int each_note_fn(const unsigned char *object_sha1,\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151987","messageId":"1285719811-10871-3-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 02/18] notes.h: Make default_notes_ref() available in notes API","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:15Z","receivedAt":"2010-09-29T00:23:15Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Signed-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |    2 +-\n notes.h |   14 ++++++++++++++\n 2 files changed, 15 insertions(+), 1 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 1978244..79fd850 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -898,7 +898,7 @@ static int notes_display_config(const char *k, const char *v, void *cb)\n \treturn 0;\n }\n \n-static const char *default_notes_ref(void)\n+const char *default_notes_ref(void)\n {\n \tconst char *notes_ref = NULL;\n \tif (!notes_ref)\ndiff --git a/notes.h b/notes.h\nindex c0288b0..20db42f 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -44,6 +44,20 @@ extern struct notes_tree {\n } default_notes_tree;\n \n /*\n+ * Return the default notes ref.\n+ *\n+ * The default notes ref is the notes ref that is used when notes_ref == NULL\n+ * is passed to init_notes().\n+ *\n+ * This the first of the following to be defined:\n+ * 1. The '--ref' option to 'git notes', if given\n+ * 2. The $GIT_NOTES_REF environment variable, if set\n+ * 3. The value of the core.notesRef config variable, if set\n+ * 4. GIT_NOTES_DEFAULT_REF (i.e. \"refs/notes/commits\")\n+ */\n+const char *default_notes_ref(void);\n+\n+/*\n  * Flags controlling behaviour of notes tree initialization\n  *\n  * Default behaviour is to initialize the notes tree from the tree object\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151999","messageId":"1285719811-10871-4-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 03/18] notes.h/c: Clarify the handling of notes objects that are == null_sha1","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:16Z","receivedAt":"2010-09-29T00:23:16Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Clearly specify how combine_notes functions are expected to handle null_sha1\nin input, and how they may set the result to null_sha1 in order to cause\nthe removal of a note.\n\nAlso document that passing note_sha1 == null_sha1 to add_note() is usually\na no-op, except in cases where combining it with an existing note yields a\nnew/changed result.\n\nThe only functional changes in this patch concern the handling of null_sha1\nin notes_tree_insert(). Otherwise the patch consists solely of reordering\nfunctions in notes.c to avoid use-before-declaration, and adding\ndocumentation to notes.h.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |  158 +++++++++++++++++++++++++++++++++-----------------------------\n notes.h |   16 ++++++-\n 2 files changed, 99 insertions(+), 75 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 79fd850..9e92021 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -150,6 +150,79 @@ static struct leaf_node *note_tree_find(struct notes_tree *t,\n }\n \n /*\n+ * How to consolidate an int_node:\n+ * If there are > 1 non-NULL entries, give up and return non-zero.\n+ * Otherwise replace the int_node at the given index in the given parent node\n+ * with the only entry (or a NULL entry if no entries) from the given tree,\n+ * and return 0.\n+ */\n+static int note_tree_consolidate(struct int_node *tree,\n+\tstruct int_node *parent, unsigned char index)\n+{\n+\tunsigned int i;\n+\tvoid *p = NULL;\n+\n+\tassert(tree && parent);\n+\tassert(CLR_PTR_TYPE(parent->a[index]) == tree);\n+\n+\tfor (i = 0; i < 16; i++) {\n+\t\tif (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {\n+\t\t\tif (p) /* more than one entry */\n+\t\t\t\treturn -2;\n+\t\t\tp = tree->a[i];\n+\t\t}\n+\t}\n+\n+\t/* replace tree with p in parent[index] */\n+\tparent->a[index] = p;\n+\tfree(tree);\n+\treturn 0;\n+}\n+\n+/*\n+ * To remove a leaf_node:\n+ * Search to the tree location appropriate for the given leaf_node's key:\n+ * - If location does not hold a matching entry, abort and do nothing.\n+ * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).\n+ * - Consolidate int_nodes repeatedly, while walking up the tree towards root.\n+ */\n+static void note_tree_remove(struct notes_tree *t, struct int_node *tree,\n+\t\tunsigned char n, struct leaf_node *entry)\n+{\n+\tstruct leaf_node *l;\n+\tstruct int_node *parent_stack[20];\n+\tunsigned char i, j;\n+\tvoid **p = note_tree_search(t, &tree, &n, entry->key_sha1);\n+\n+\tassert(GET_PTR_TYPE(entry) == 0); /* no type bits set */\n+\tif (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)\n+\t\treturn; /* type mismatch, nothing to remove */\n+\tl = (struct leaf_node *) CLR_PTR_TYPE(*p);\n+\tif (hashcmp(l->key_sha1, entry->key_sha1))\n+\t\treturn; /* key mismatch, nothing to remove */\n+\n+\t/* we have found a matching entry */\n+\tfree(l);\n+\t*p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);\n+\n+\t/* consolidate this tree level, and parent levels, if possible */\n+\tif (!n)\n+\t\treturn; /* cannot consolidate top level */\n+\t/* first, build stack of ancestors between root and current node */\n+\tparent_stack[0] = t->root;\n+\tfor (i = 0; i < n; i++) {\n+\t\tj = GET_NIBBLE(i, entry->key_sha1);\n+\t\tparent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);\n+\t}\n+\tassert(i == n && parent_stack[i] == tree);\n+\t/* next, unwind stack until note_tree_consolidate() is done */\n+\twhile (i > 0 &&\n+\t       !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],\n+\t\t\t\t      GET_NIBBLE(i - 1, entry->key_sha1)))\n+\t\ti--;\n+}\n+\n+/*\n  * To insert a leaf_node:\n  * Search to the tree location appropriate for the given leaf_node's key:\n  * - If location is unused (NULL), store the tweaked pointer directly there\n@@ -175,7 +248,10 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n \tswitch (GET_PTR_TYPE(*p)) {\n \tcase PTR_TYPE_NULL:\n \t\tassert(!*p);\n-\t\t*p = SET_PTR_TYPE(entry, type);\n+\t\tif (is_null_sha1(entry->val_sha1))\n+\t\t\tfree(entry);\n+\t\telse\n+\t\t\t*p = SET_PTR_TYPE(entry, type);\n \t\treturn;\n \tcase PTR_TYPE_NOTE:\n \t\tswitch (type) {\n@@ -191,6 +267,9 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n \t\t\t\t\t    sha1_to_hex(l->val_sha1),\n \t\t\t\t\t    sha1_to_hex(entry->val_sha1),\n \t\t\t\t\t    sha1_to_hex(l->key_sha1));\n+\n+\t\t\t\tif (is_null_sha1(l->val_sha1))\n+\t\t\t\t\tnote_tree_remove(t, tree, n, entry);\n \t\t\t\tfree(entry);\n \t\t\t\treturn;\n \t\t\t}\n@@ -222,6 +301,10 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n \t/* non-matching leaf_node */\n \tassert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||\n \t       GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);\n+\tif (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */\n+\t\tfree(entry);\n+\t\treturn;\n+\t}\n \tnew_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1);\n \tnote_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),\n \t\t\t combine_notes);\n@@ -229,79 +312,6 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n \tnote_tree_insert(t, new_node, n + 1, entry, type, combine_notes);\n }\n \n-/*\n- * How to consolidate an int_node:\n- * If there are > 1 non-NULL entries, give up and return non-zero.\n- * Otherwise replace the int_node at the given index in the given parent node\n- * with the only entry (or a NULL entry if no entries) from the given tree,\n- * and return 0.\n- */\n-static int note_tree_consolidate(struct int_node *tree,\n-\tstruct int_node *parent, unsigned char index)\n-{\n-\tunsigned int i;\n-\tvoid *p = NULL;\n-\n-\tassert(tree && parent);\n-\tassert(CLR_PTR_TYPE(parent->a[index]) == tree);\n-\n-\tfor (i = 0; i < 16; i++) {\n-\t\tif (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {\n-\t\t\tif (p) /* more than one entry */\n-\t\t\t\treturn -2;\n-\t\t\tp = tree->a[i];\n-\t\t}\n-\t}\n-\n-\t/* replace tree with p in parent[index] */\n-\tparent->a[index] = p;\n-\tfree(tree);\n-\treturn 0;\n-}\n-\n-/*\n- * To remove a leaf_node:\n- * Search to the tree location appropriate for the given leaf_node's key:\n- * - If location does not hold a matching entry, abort and do nothing.\n- * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).\n- * - Consolidate int_nodes repeatedly, while walking up the tree towards root.\n- */\n-static void note_tree_remove(struct notes_tree *t, struct int_node *tree,\n-\t\tunsigned char n, struct leaf_node *entry)\n-{\n-\tstruct leaf_node *l;\n-\tstruct int_node *parent_stack[20];\n-\tunsigned char i, j;\n-\tvoid **p = note_tree_search(t, &tree, &n, entry->key_sha1);\n-\n-\tassert(GET_PTR_TYPE(entry) == 0); /* no type bits set */\n-\tif (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)\n-\t\treturn; /* type mismatch, nothing to remove */\n-\tl = (struct leaf_node *) CLR_PTR_TYPE(*p);\n-\tif (hashcmp(l->key_sha1, entry->key_sha1))\n-\t\treturn; /* key mismatch, nothing to remove */\n-\n-\t/* we have found a matching entry */\n-\tfree(l);\n-\t*p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);\n-\n-\t/* consolidate this tree level, and parent levels, if possible */\n-\tif (!n)\n-\t\treturn; /* cannot consolidate top level */\n-\t/* first, build stack of ancestors between root and current node */\n-\tparent_stack[0] = t->root;\n-\tfor (i = 0; i < n; i++) {\n-\t\tj = GET_NIBBLE(i, entry->key_sha1);\n-\t\tparent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);\n-\t}\n-\tassert(i == n && parent_stack[i] == tree);\n-\t/* next, unwind stack until note_tree_consolidate() is done */\n-\twhile (i > 0 &&\n-\t       !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],\n-\t\t\t\t      GET_NIBBLE(i - 1, entry->key_sha1)))\n-\t\ti--;\n-}\n-\n /* Free the entire notes data contained in the given tree */\n static void note_tree_free(struct int_node *tree)\n {\ndiff --git a/notes.h b/notes.h\nindex 20db42f..79ea797 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -12,7 +12,10 @@\n  * resulting SHA1 into the first SHA1 argument (cur_sha1). A non-zero return\n  * value indicates failure.\n  *\n- * The two given SHA1s must both be non-NULL and different from each other.\n+ * The two given SHA1s shall both be non-NULL and different from each other.\n+ * Either of them (but not both) may be == null_sha1, which indicates an\n+ * empty/non-existent note. If the resulting SHA1 (cur_sha1) is == null_sha1,\n+ * the note will be removed from the notes tree.\n  *\n  * The default combine_notes function (you get this when passing NULL) is\n  * combine_notes_concatenate(), which appends the contents of the new note to\n@@ -90,6 +93,17 @@ void init_notes(struct notes_tree *t, const char *notes_ref,\n /*\n  * Add the given note object to the given notes_tree structure\n  *\n+ * If there already exists a note for the given object_sha1, the given\n+ * combine_notes function is invoked to break the tie. If not given (i.e.\n+ * combine_notes == NULL), the default combine_notes function for the given\n+ * notes_tree is used.\n+ *\n+ * Passing note_sha1 == null_sha1 indicates the addition of an\n+ * empty/non-existent note. This is a (potentially expensive) no-op unless\n+ * there already exists a note for the given object_sha1, AND combining that\n+ * note with the empty note (using the given combine_notes function) results\n+ * in a new/changed note.\n+ *\n  * IMPORTANT: The changes made by add_note() to the given notes_tree structure\n  * are not persistent until a subsequent call to write_notes_tree() returns\n  * zero.\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"152002","messageId":"1285719811-10871-5-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 04/18] notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:17Z","receivedAt":"2010-09-29T00:23:17Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"The combine_notes_fn functions uses a non-zero return value to indicate\nfailure. However, this return value was converted to a call to die()\nin note_tree_insert().\n\nInstead, propagate this return value out to add_note(), and return it\nfrom there to enable the caller to handle errors appropriately.\n\nThis patch has been improved by the following contributions:\n- Jonathan Nieder: Future-proof by always checking add_note() return value\n\nCc: Jonathan Nieder <jrnieder@gmail.com>\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n builtin/notes.c |   11 +++++----\n notes-cache.c   |    3 +-\n notes.c         |   59 ++++++++++++++++++++++++++++--------------------------\n notes.h         |   11 +++++++--\n 4 files changed, 46 insertions(+), 38 deletions(-)\n\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex fbc347c..35f6eb6 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -573,8 +573,8 @@ static int add(int argc, const char **argv, const char *prefix)\n \n \tif (is_null_sha1(new_note))\n \t\tremove_note(t, object);\n-\telse\n-\t\tadd_note(t, object, new_note, combine_notes_overwrite);\n+\telse if (add_note(t, object, new_note, combine_notes_overwrite))\n+\t\tdie(\"confused: combine_notes_overwrite failed\");\n \n \tsnprintf(logmsg, sizeof(logmsg), \"Notes %s by 'git notes %s'\",\n \t\t is_null_sha1(new_note) ? \"removed\" : \"added\", \"add\");\n@@ -653,7 +653,8 @@ static int copy(int argc, const char **argv, const char *prefix)\n \t\tgoto out;\n \t}\n \n-\tadd_note(t, object, from_note, combine_notes_overwrite);\n+\tif (add_note(t, object, from_note, combine_notes_overwrite))\n+\t\tdie(\"confused: combine_notes_overwrite failed\");\n \tcommit_notes(t, \"Notes added by 'git notes copy'\");\n out:\n \tfree_notes(t);\n@@ -712,8 +713,8 @@ static int append_edit(int argc, const char **argv, const char *prefix)\n \n \tif (is_null_sha1(new_note))\n \t\tremove_note(t, object);\n-\telse\n-\t\tadd_note(t, object, new_note, combine_notes_overwrite);\n+\telse if (add_note(t, object, new_note, combine_notes_overwrite))\n+\t\tdie(\"confused: combine_notes_overwrite failed\");\n \n \tsnprintf(logmsg, sizeof(logmsg), \"Notes %s by 'git notes %s'\",\n \t\t is_null_sha1(new_note) ? \"removed\" : \"added\", argv[0]);\ndiff --git a/notes-cache.c b/notes-cache.c\nindex dee6d62..4c8984e 100644\n--- a/notes-cache.c\n+++ b/notes-cache.c\n@@ -89,6 +89,5 @@ int notes_cache_put(struct notes_cache *c, unsigned char key_sha1[20],\n \n \tif (write_sha1_file(data, size, \"blob\", value_sha1) < 0)\n \t\treturn -1;\n-\tadd_note(&c->tree, key_sha1, value_sha1, NULL);\n-\treturn 0;\n+\treturn add_note(&c->tree, key_sha1, value_sha1, NULL);\n }\ndiff --git a/notes.c b/notes.c\nindex 9e92021..4f3d094 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -235,13 +235,14 @@ static void note_tree_remove(struct notes_tree *t, struct int_node *tree,\n  * - Else, create a new int_node, holding both the node-at-location and the\n  *   node-to-be-inserted, and store the new int_node into the location.\n  */\n-static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n+static int note_tree_insert(struct notes_tree *t, struct int_node *tree,\n \t\tunsigned char n, struct leaf_node *entry, unsigned char type,\n \t\tcombine_notes_fn combine_notes)\n {\n \tstruct int_node *new_node;\n \tstruct leaf_node *l;\n \tvoid **p = note_tree_search(t, &tree, &n, entry->key_sha1);\n+\tint ret = 0;\n \n \tassert(GET_PTR_TYPE(entry) == 0); /* no type bits set */\n \tl = (struct leaf_node *) CLR_PTR_TYPE(*p);\n@@ -252,26 +253,21 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n \t\t\tfree(entry);\n \t\telse\n \t\t\t*p = SET_PTR_TYPE(entry, type);\n-\t\treturn;\n+\t\treturn 0;\n \tcase PTR_TYPE_NOTE:\n \t\tswitch (type) {\n \t\tcase PTR_TYPE_NOTE:\n \t\t\tif (!hashcmp(l->key_sha1, entry->key_sha1)) {\n \t\t\t\t/* skip concatenation if l == entry */\n \t\t\t\tif (!hashcmp(l->val_sha1, entry->val_sha1))\n-\t\t\t\t\treturn;\n+\t\t\t\t\treturn 0;\n \n-\t\t\t\tif (combine_notes(l->val_sha1, entry->val_sha1))\n-\t\t\t\t\tdie(\"failed to combine notes %s and %s\"\n-\t\t\t\t\t    \" for object %s\",\n-\t\t\t\t\t    sha1_to_hex(l->val_sha1),\n-\t\t\t\t\t    sha1_to_hex(entry->val_sha1),\n-\t\t\t\t\t    sha1_to_hex(l->key_sha1));\n-\n-\t\t\t\tif (is_null_sha1(l->val_sha1))\n+\t\t\t\tret = combine_notes(l->val_sha1,\n+\t\t\t\t\t\t    entry->val_sha1);\n+\t\t\t\tif (!ret && is_null_sha1(l->val_sha1))\n \t\t\t\t\tnote_tree_remove(t, tree, n, entry);\n \t\t\t\tfree(entry);\n-\t\t\t\treturn;\n+\t\t\t\treturn ret;\n \t\t\t}\n \t\t\tbreak;\n \t\tcase PTR_TYPE_SUBTREE:\n@@ -280,7 +276,7 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n \t\t\t\t/* unpack 'entry' */\n \t\t\t\tload_subtree(t, entry, tree, n);\n \t\t\t\tfree(entry);\n-\t\t\t\treturn;\n+\t\t\t\treturn 0;\n \t\t\t}\n \t\t\tbreak;\n \t\t}\n@@ -291,9 +287,8 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n \t\t\t*p = NULL;\n \t\t\tload_subtree(t, l, tree, n);\n \t\t\tfree(l);\n-\t\t\tnote_tree_insert(t, tree, n, entry, type,\n-\t\t\t\t\t combine_notes);\n-\t\t\treturn;\n+\t\t\treturn note_tree_insert(t, tree, n, entry, type,\n+\t\t\t\t\t\tcombine_notes);\n \t\t}\n \t\tbreak;\n \t}\n@@ -303,13 +298,17 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n \t       GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);\n \tif (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */\n \t\tfree(entry);\n-\t\treturn;\n+\t\treturn 0;\n \t}\n \tnew_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1);\n-\tnote_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),\n-\t\t\t combine_notes);\n-\t*p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);\n-\tnote_tree_insert(t, new_node, n + 1, entry, type, combine_notes);\n+\tret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),\n+\t\t\t       combine_notes);\n+\tif (!ret) {\n+\t\t*p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);\n+\t\tret = note_tree_insert(t, new_node, n + 1, entry, type,\n+\t\t\t\t       combine_notes);\n+\t}\n+\treturn ret;\n }\n \n /* Free the entire notes data contained in the given tree */\n@@ -452,8 +451,12 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,\n \t\t\t\tl->key_sha1[19] = (unsigned char) len;\n \t\t\t\ttype = PTR_TYPE_SUBTREE;\n \t\t\t}\n-\t\t\tnote_tree_insert(t, node, n, l, type,\n-\t\t\t\t\t combine_notes_concatenate);\n+\t\t\tif (note_tree_insert(t, node, n, l, type,\n+\t\t\t\t\t     combine_notes_concatenate))\n+\t\t\t\tdie(\"Failed to load %s %s into notes tree \"\n+\t\t\t\t    \"from %s\",\n+\t\t\t\t    type == PTR_TYPE_NOTE ? \"note\" : \"subtree\",\n+\t\t\t\t    sha1_to_hex(l->key_sha1), t->ref);\n \t\t}\n \t\tcontinue;\n \n@@ -1014,7 +1017,7 @@ void init_display_notes(struct display_notes_opt *opt)\n \tstring_list_clear(&display_notes_refs, 0);\n }\n \n-void add_note(struct notes_tree *t, const unsigned char *object_sha1,\n+int add_note(struct notes_tree *t, const unsigned char *object_sha1,\n \t\tconst unsigned char *note_sha1, combine_notes_fn combine_notes)\n {\n \tstruct leaf_node *l;\n@@ -1028,7 +1031,7 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1,\n \tl = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));\n \thashcpy(l->key_sha1, object_sha1);\n \thashcpy(l->val_sha1, note_sha1);\n-\tnote_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);\n+\treturn note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);\n }\n \n void remove_note(struct notes_tree *t, const unsigned char *object_sha1)\n@@ -1204,7 +1207,7 @@ void format_display_notes(const unsigned char *object_sha1,\n \n int copy_note(struct notes_tree *t,\n \t      const unsigned char *from_obj, const unsigned char *to_obj,\n-\t      int force, combine_notes_fn combine_fn)\n+\t      int force, combine_notes_fn combine_notes)\n {\n \tconst unsigned char *note = get_note(t, from_obj);\n \tconst unsigned char *existing_note = get_note(t, to_obj);\n@@ -1213,9 +1216,9 @@ int copy_note(struct notes_tree *t,\n \t\treturn 1;\n \n \tif (note)\n-\t\tadd_note(t, to_obj, note, combine_fn);\n+\t\treturn add_note(t, to_obj, note, combine_notes);\n \telse if (existing_note)\n-\t\tadd_note(t, to_obj, null_sha1, combine_fn);\n+\t\treturn add_note(t, to_obj, null_sha1, combine_notes);\n \n \treturn 0;\n }\ndiff --git a/notes.h b/notes.h\nindex 79ea797..b372575 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -104,11 +104,13 @@ void init_notes(struct notes_tree *t, const char *notes_ref,\n  * note with the empty note (using the given combine_notes function) results\n  * in a new/changed note.\n  *\n+ * Returns zero on success; non-zero means combine_notes failed.\n+ *\n  * IMPORTANT: The changes made by add_note() to the given notes_tree structure\n  * are not persistent until a subsequent call to write_notes_tree() returns\n  * zero.\n  */\n-void add_note(struct notes_tree *t, const unsigned char *object_sha1,\n+int add_note(struct notes_tree *t, const unsigned char *object_sha1,\n \t\tconst unsigned char *note_sha1, combine_notes_fn combine_notes);\n \n /*\n@@ -131,7 +133,10 @@ const unsigned char *get_note(struct notes_tree *t,\n /*\n  * Copy a note from one object to another in the given notes_tree.\n  *\n- * Fails if the to_obj already has a note unless 'force' is true.\n+ * Returns 1 if the to_obj already has a note and 'force' is false. Otherwise,\n+ * returns non-zero if 'force' is true, but the given combine_notes function\n+ * failed to combine from_obj's note with to_obj's existing note.\n+ * Returns zero on success.\n  *\n  * IMPORTANT: The changes made by copy_note() to the given notes_tree structure\n  * are not persistent until a subsequent call to write_notes_tree() returns\n@@ -139,7 +144,7 @@ const unsigned char *get_note(struct notes_tree *t,\n  */\n int copy_note(struct notes_tree *t,\n \t      const unsigned char *from_obj, const unsigned char *to_obj,\n-\t      int force, combine_notes_fn combine_fn);\n+\t      int force, combine_notes_fn combine_notes);\n \n /*\n  * Flags controlling behaviour of for_each_note()\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151989","messageId":"1285719811-10871-6-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 05/18] (trivial) t3303: Indent with tabs instead of spaces for consistency","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:18Z","receivedAt":"2010-09-29T00:23:18Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"The rest of the file uses tabs for indenting. Fix the one function\nthat doesn't.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n t/t3303-notes-subtrees.sh |   18 +++++++++---------\n 1 files changed, 9 insertions(+), 9 deletions(-)\n\ndiff --git a/t/t3303-notes-subtrees.sh b/t/t3303-notes-subtrees.sh\nindex 75ec187..d571708 100755\n--- a/t/t3303-notes-subtrees.sh\n+++ b/t/t3303-notes-subtrees.sh\n@@ -168,15 +168,15 @@ INPUT_END\n }\n \n verify_concatenated_notes () {\n-    git log | grep \"^    \" > output &&\n-    i=$number_of_commits &&\n-    while [ $i -gt 0 ]; do\n-        echo \"    commit #$i\" &&\n-        echo \"    first note for commit #$i\" &&\n-        echo \"    second note for commit #$i\" &&\n-        i=$(($i-1));\n-    done > expect &&\n-    test_cmp expect output\n+\tgit log | grep \"^    \" > output &&\n+\ti=$number_of_commits &&\n+\twhile [ $i -gt 0 ]; do\n+\t\techo \"    commit #$i\" &&\n+\t\techo \"    first note for commit #$i\" &&\n+\t\techo \"    second note for commit #$i\" &&\n+\t\ti=$(($i-1));\n+\tdone > expect &&\n+\ttest_cmp expect output\n }\n \n test_expect_success 'test notes in no fanout concatenated with 2/38-fanout' 'test_concatenated_notes \"s|^..|&/|\" \"\"'\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151988","messageId":"1285719811-10871-7-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 06/18] notes.c: Use two newlines (instead of one) when concatenating notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:19Z","receivedAt":"2010-09-29T00:23:19Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"When using combine_notes_concatenate() to concatenate notes, it currently\nensures exactly one newline character between the given notes. However,\nwhen using builtin/notes.c:create_note() to concatenate notes (e.g. by\n'git notes append'), it adds a newline character to the trailing newline\nof the preceding notes object, thus resulting in _two_ newlines (aka. a\nblank line) separating contents of the two notes.\n\nThis patch brings combine_notes_concatenate() into consistency with\nbuiltin/notes.c:create_note(), by ensuring exactly _two_ newline characters\nbetween concatenated notes.\n\nThe patch also changes a few notes-related selftests accordingly.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c                       |    7 ++++---\n t/t3301-notes.sh              |    4 ++++\n t/t3303-notes-subtrees.sh     |    1 +\n t/t3404-rebase-interactive.sh |    1 +\n t/t9301-fast-import-notes.sh  |    5 +++++\n 5 files changed, 15 insertions(+), 3 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 4f3d094..28afe2c 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -814,16 +814,17 @@ int combine_notes_concatenate(unsigned char *cur_sha1,\n \t\treturn 0;\n \t}\n \n-\t/* we will separate the notes by a newline anyway */\n+\t/* we will separate the notes by two newlines anyway */\n \tif (cur_msg[cur_len - 1] == '\\n')\n \t\tcur_len--;\n \n \t/* concatenate cur_msg and new_msg into buf */\n-\tbuf_len = cur_len + 1 + new_len;\n+\tbuf_len = cur_len + 2 + new_len;\n \tbuf = (char *) xmalloc(buf_len);\n \tmemcpy(buf, cur_msg, cur_len);\n \tbuf[cur_len] = '\\n';\n-\tmemcpy(buf + cur_len + 1, new_msg, new_len);\n+\tbuf[cur_len + 1] = '\\n';\n+\tmemcpy(buf + cur_len + 2, new_msg, new_len);\n \tfree(cur_msg);\n \tfree(new_msg);\n \ndiff --git a/t/t3301-notes.sh b/t/t3301-notes.sh\nindex 1d82f79..4bf4e52 100755\n--- a/t/t3301-notes.sh\n+++ b/t/t3301-notes.sh\n@@ -955,6 +955,7 @@ Date:   Thu Apr 7 15:27:13 2005 -0700\n \n Notes (other):\n     a fresh note\n+$whitespace\n     another fresh note\n EOF\n \n@@ -976,8 +977,11 @@ Date:   Thu Apr 7 15:27:13 2005 -0700\n \n Notes (other):\n     a fresh note\n+$whitespace\n     another fresh note\n+$whitespace\n     append 1\n+$whitespace\n     append 2\n EOF\n \ndiff --git a/t/t3303-notes-subtrees.sh b/t/t3303-notes-subtrees.sh\nindex d571708..704aee8 100755\n--- a/t/t3303-notes-subtrees.sh\n+++ b/t/t3303-notes-subtrees.sh\n@@ -173,6 +173,7 @@ verify_concatenated_notes () {\n \twhile [ $i -gt 0 ]; do\n \t\techo \"    commit #$i\" &&\n \t\techo \"    first note for commit #$i\" &&\n+\t\techo \"    \" &&\n \t\techo \"    second note for commit #$i\" &&\n \t\ti=$(($i-1));\n \tdone > expect &&\ndiff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh\nindex 9f03ce6..9ed70dc 100755\n--- a/t/t3404-rebase-interactive.sh\n+++ b/t/t3404-rebase-interactive.sh\n@@ -593,6 +593,7 @@ test_expect_success 'rebase -i can copy notes' '\n \n cat >expect <<EOF\n an earlier note\n+\n a note\n EOF\n \ndiff --git a/t/t9301-fast-import-notes.sh b/t/t9301-fast-import-notes.sh\nindex a5c99d8..7cf8cd8 100755\n--- a/t/t9301-fast-import-notes.sh\n+++ b/t/t9301-fast-import-notes.sh\n@@ -255,13 +255,18 @@ EOF\n \n INPUT_END\n \n+whitespace=\"    \"\n+\n cat >expect <<EXPECT_END\n     fourth commit\n     pre-prefix of note for fourth commit\n+$whitespace\n     prefix of note for fourth commit\n+$whitespace\n     third note for fourth commit\n     third commit\n     prefix of note for third commit\n+$whitespace\n     third note for third commit\n     second commit\n     third note for second commit\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"152003","messageId":"1285719811-10871-8-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 07/18] builtin/notes.c: Split notes ref DWIMmery into a separate function","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:20Z","receivedAt":"2010-09-29T00:23:20Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"expand_notes_ref() is a new function that performs the DWIM transformation\nof \"foo\" -> \"refs/notes/foo\" where notes refs are expected.\n\nThis is done in preparation for future patches which will also need this\nDWIM functionality.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n builtin/notes.c |   17 +++++++++++------\n 1 files changed, 11 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex 35f6eb6..9c91c59 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -83,6 +83,16 @@ struct msg_arg {\n \tstruct strbuf buf;\n };\n \n+static void expand_notes_ref(struct strbuf *sb)\n+{\n+\tif (!prefixcmp(sb->buf, \"refs/notes/\"))\n+\t\treturn; /* we're happy */\n+\telse if (!prefixcmp(sb->buf, \"notes/\"))\n+\t\tstrbuf_insert(sb, 0, \"refs/\", 5);\n+\telse\n+\t\tstrbuf_insert(sb, 0, \"refs/notes/\", 11);\n+}\n+\n static int list_each_note(const unsigned char *object_sha1,\n \t\tconst unsigned char *note_sha1, char *note_path,\n \t\tvoid *cb_data)\n@@ -839,13 +849,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)\n \n \tif (override_notes_ref) {\n \t\tstruct strbuf sb = STRBUF_INIT;\n-\t\tif (!prefixcmp(override_notes_ref, \"refs/notes/\"))\n-\t\t\t/* we're happy */;\n-\t\telse if (!prefixcmp(override_notes_ref, \"notes/\"))\n-\t\t\tstrbuf_addstr(&sb, \"refs/\");\n-\t\telse\n-\t\t\tstrbuf_addstr(&sb, \"refs/notes/\");\n \t\tstrbuf_addstr(&sb, override_notes_ref);\n+\t\texpand_notes_ref(&sb);\n \t\tsetenv(\"GIT_NOTES_REF\", sb.buf, 1);\n \t\tstrbuf_release(&sb);\n \t}\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151992","messageId":"1285719811-10871-9-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 08/18] git notes merge: Initial implementation handling trivial merges only","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:21Z","receivedAt":"2010-09-29T00:23:21Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"This initial implementation of 'git notes merge' only handles the trivial\nmerge cases (i.e. where the merge is either a no-op, or a fast-forward).\n\nThe patch includes testcases for these trivial merge cases.\n\nFuture patches will extend the functionality of 'git notes merge'.\n\nThis patch has been improved by the following contributions:\n- Stephen Boyd: Simplify argc logic\n- Stephen Boyd: Use test_commit\n\nCc: Stephen Boyd <bebarino@gmail.com>\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n Makefile               |    2 +\n builtin/notes.c        |   53 ++++++++++++++++++\n notes-merge.c          |  103 +++++++++++++++++++++++++++++++++++\n notes-merge.h          |   30 ++++++++++\n t/t3308-notes-merge.sh |  139 ++++++++++++++++++++++++++++++++++++++++++++++++\n 5 files changed, 327 insertions(+), 0 deletions(-)\n create mode 100644 notes-merge.c\n create mode 100644 notes-merge.h\n create mode 100755 t/t3308-notes-merge.sh\n\ndiff --git a/Makefile b/Makefile\nindex f33648d..14c0ff1 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -503,6 +503,7 @@ LIB_H += mailmap.h\n LIB_H += merge-recursive.h\n LIB_H += notes.h\n LIB_H += notes-cache.h\n+LIB_H += notes-merge.h\n LIB_H += object.h\n LIB_H += pack.h\n LIB_H += pack-refs.h\n@@ -593,6 +594,7 @@ LIB_OBJS += merge-recursive.o\n LIB_OBJS += name-hash.o\n LIB_OBJS += notes.o\n LIB_OBJS += notes-cache.o\n+LIB_OBJS += notes-merge.o\n LIB_OBJS += object.o\n LIB_OBJS += pack-check.o\n LIB_OBJS += pack-refs.o\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex 9c91c59..ec1d042 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -17,6 +17,7 @@\n #include \"run-command.h\"\n #include \"parse-options.h\"\n #include \"string-list.h\"\n+#include \"notes-merge.h\"\n \n static const char * const git_notes_usage[] = {\n \t\"git notes [--ref <notes_ref>] [list [<object>]]\",\n@@ -25,6 +26,7 @@ static const char * const git_notes_usage[] = {\n \t\"git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]\",\n \t\"git notes [--ref <notes_ref>] edit [<object>]\",\n \t\"git notes [--ref <notes_ref>] show [<object>]\",\n+\t\"git notes [--ref <notes_ref>] merge [-v | -q] <notes_ref>\",\n \t\"git notes [--ref <notes_ref>] remove [<object>]\",\n \t\"git notes [--ref <notes_ref>] prune [-n | -v]\",\n \tNULL\n@@ -61,6 +63,11 @@ static const char * const git_notes_show_usage[] = {\n \tNULL\n };\n \n+static const char * const git_notes_merge_usage[] = {\n+\t\"git notes merge [<options>] <notes_ref>\",\n+\tNULL\n+};\n+\n static const char * const git_notes_remove_usage[] = {\n \t\"git notes remove [<object>]\",\n \tNULL\n@@ -772,6 +779,50 @@ static int show(int argc, const char **argv, const char *prefix)\n \treturn retval;\n }\n \n+static int merge(int argc, const char **argv, const char *prefix)\n+{\n+\tstruct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;\n+\tunsigned char result_sha1[20];\n+\tstruct notes_merge_options o;\n+\tint verbosity = 0, result;\n+\tstruct option options[] = {\n+\t\tOPT__VERBOSITY(&verbosity),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_notes_merge_usage, 0);\n+\n+\tif (argc != 1) {\n+\t\terror(\"Must specify a notes ref to merge\");\n+\t\tusage_with_options(git_notes_merge_usage, options);\n+\t}\n+\n+\tinit_notes_merge_options(&o);\n+\to.verbosity = verbosity + 2; // default verbosity level is 2\n+\to.local_ref = default_notes_ref();\n+\tstrbuf_addstr(&remote_ref, argv[0]);\n+\texpand_notes_ref(&remote_ref);\n+\to.remote_ref = remote_ref.buf;\n+\n+\tresult = notes_merge(&o, result_sha1);\n+\n+\tstrbuf_addf(&msg, \"notes: Merged notes from %s into %s\",\n+\t\t    remote_ref.buf, default_notes_ref());\n+\tif (result == 0) { /* Merge resulted (trivially) in result_sha1 */\n+\t\t/* Update default notes ref with new commit */\n+\t\tupdate_ref(msg.buf, default_notes_ref(), result_sha1, NULL,\n+\t\t\t   0, DIE_ON_ERR);\n+\t} else {\n+\t\t/* TODO: */\n+\t\tdie(\"'git notes merge' cannot yet handle non-trivial merges!\");\n+\t}\n+\n+\tstrbuf_release(&remote_ref);\n+\tstrbuf_release(&msg);\n+\treturn 0;\n+}\n+\n static int remove_cmd(int argc, const char **argv, const char *prefix)\n {\n \tstruct option options[] = {\n@@ -865,6 +916,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)\n \t\tresult = append_edit(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"show\"))\n \t\tresult = show(argc, argv, prefix);\n+\telse if (!strcmp(argv[0], \"merge\"))\n+\t\tresult = merge(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"remove\"))\n \t\tresult = remove_cmd(argc, argv, prefix);\n \telse if (!strcmp(argv[0], \"prune\"))\ndiff --git a/notes-merge.c b/notes-merge.c\nnew file mode 100644\nindex 0000000..5461827\n--- /dev/null\n+++ b/notes-merge.c\n@@ -0,0 +1,103 @@\n+#include \"cache.h\"\n+#include \"commit.h\"\n+#include \"notes-merge.h\"\n+\n+void init_notes_merge_options(struct notes_merge_options *o)\n+{\n+\tmemset(o, 0, sizeof(struct notes_merge_options));\n+\to->verbosity = 2;\n+}\n+\n+static int show(struct notes_merge_options *o, int v)\n+{\n+\treturn (o->verbosity >= v) || o->verbosity >= 5;\n+}\n+\n+#define OUTPUT(o, v, ...) \\\n+\tdo { if (show((o), (v))) { printf(__VA_ARGS__); puts(\"\"); } } while (0)\n+\n+int notes_merge(struct notes_merge_options *o,\n+\t\tunsigned char *result_sha1)\n+{\n+\tunsigned char local_sha1[20], remote_sha1[20];\n+\tstruct commit *local, *remote;\n+\tstruct commit_list *bases = NULL;\n+\tconst unsigned char *base_sha1;\n+\tint result = 0;\n+\n+\thashclr(result_sha1);\n+\n+\tOUTPUT(o, 5, \"notes_merge(o->local_ref = %s, o->remote_ref = %s)\",\n+\t       o->local_ref, o->remote_ref);\n+\n+\tif (!o->local_ref || get_sha1(o->local_ref, local_sha1)) {\n+\t\t/* empty notes ref => assume empty notes tree */\n+\t\thashclr(local_sha1);\n+\t\tlocal = NULL;\n+\t} else if (!(local = lookup_commit_reference(local_sha1)))\n+\t\tdie(\"Could not parse commit '%s'.\", o->local_ref);\n+\tOUTPUT(o, 5, \"\\tlocal commit: %.7s\", sha1_to_hex(local_sha1));\n+\n+\tif (!o->remote_ref || get_sha1(o->remote_ref, remote_sha1)) {\n+\t\t/* empty notes ref => assume empty notes tree */\n+\t\thashclr(remote_sha1);\n+\t\tremote = NULL;\n+\t}\n+\tif (!(remote = lookup_commit_reference(remote_sha1)))\n+\t\tdie(\"Could not parse commit '%s'.\", o->remote_ref);\n+\tOUTPUT(o, 5, \"\\tremote commit: %.7s\", sha1_to_hex(remote_sha1));\n+\n+\tif (!local) {\n+\t\t/* result == remote commit */\n+\t\thashcpy(result_sha1, remote_sha1);\n+\t\tgoto found_result;\n+\t}\n+\tif (!remote) {\n+\t\t/* result == local commit */\n+\t\thashcpy(result_sha1, local_sha1);\n+\t\tgoto found_result;\n+\t}\n+\tassert(local && remote);\n+\n+\t/* Find merge bases */\n+\tbases = get_merge_bases(local, remote, 1);\n+\tif (!bases) {\n+\t\tbase_sha1 = null_sha1;\n+\t\tOUTPUT(o, 4, \"No merge base found; doing history-less merge\");\n+\t} else if (!bases->next) {\n+\t\tbase_sha1 = bases->item->object.sha1;\n+\t\tOUTPUT(o, 4, \"One merge base found (%.7s)\",\n+\t\t       sha1_to_hex(base_sha1));\n+\t} else {\n+\t\t/* TODO: How to handle multiple merge-bases? */\n+\t\tbase_sha1 = bases->item->object.sha1;\n+\t\tOUTPUT(o, 3, \"Multiple merge bases found. Using the first \"\n+\t\t       \"(%.7s)\", sha1_to_hex(base_sha1));\n+\t}\n+\n+\tOUTPUT(o, 4, \"Merging remote commit %.7s into local commit %.7s with \"\n+\t       \"merge-base %.7s\", sha1_to_hex(remote->object.sha1),\n+\t       sha1_to_hex(local->object.sha1), sha1_to_hex(base_sha1));\n+\n+\tif (!hashcmp(remote->object.sha1, base_sha1)) {\n+\t\t/* Already merged; result == local commit */\n+\t\tOUTPUT(o, 2, \"Already up-to-date!\");\n+\t\thashcpy(result_sha1, local->object.sha1);\n+\t\tgoto found_result;\n+\t}\n+\tif (!hashcmp(local->object.sha1, base_sha1)) {\n+\t\t/* Fast-forward; result == remote commit */\n+\t\tOUTPUT(o, 2, \"Fast-forward\");\n+\t\thashcpy(result_sha1, remote->object.sha1);\n+\t\tgoto found_result;\n+\t}\n+\n+\t/* TODO: */\n+\tresult = error(\"notes_merge() cannot yet handle real merges.\");\n+\n+found_result:\n+\tfree_commit_list(bases);\n+\tOUTPUT(o, 5, \"notes_merge(): result = %i, result_sha1 = %.7s\",\n+\t       result, sha1_to_hex(result_sha1));\n+\treturn result;\n+}\ndiff --git a/notes-merge.h b/notes-merge.h\nnew file mode 100644\nindex 0000000..c4416e1\n--- /dev/null\n+++ b/notes-merge.h\n@@ -0,0 +1,30 @@\n+#ifndef NOTES_MERGE_H\n+#define NOTES_MERGE_H\n+\n+struct notes_merge_options {\n+\tconst char *local_ref;\n+\tconst char *remote_ref;\n+\tint verbosity;\n+};\n+\n+void init_notes_merge_options(struct notes_merge_options *o);\n+\n+/*\n+ * Merge notes from o->remote_ref into o->local_ref\n+ *\n+ * The commits given by the two refs are merged, producing one of the following\n+ * outcomes:\n+ *\n+ * 1. The merge trivially results in an existing commit (e.g. fast-forward or\n+ *    already-up-to-date). The SHA1 of the result is written into 'result_sha1'\n+ *    and 0 is returned.\n+ * 2. The merge fails. result_sha1 is set to null_sha1, and non-zero returned.\n+ *\n+ * Either ref (but not both) may not exist in which case the missing ref is\n+ * interpreted as an empty notes tree, and the merge trivially results in\n+ * what the other ref points to.\n+ */\n+int notes_merge(struct notes_merge_options *o,\n+\t\tunsigned char *result_sha1);\n+\n+#endif\ndiff --git a/t/t3308-notes-merge.sh b/t/t3308-notes-merge.sh\nnew file mode 100755\nindex 0000000..0342e37\n--- /dev/null\n+++ b/t/t3308-notes-merge.sh\n@@ -0,0 +1,139 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2010 Johan Herland\n+#\n+\n+test_description='Test merging of notes trees'\n+\n+. ./test-lib.sh\n+\n+test_expect_success setup '\n+\ttest_commit 1st &&\n+\ttest_commit 2nd &&\n+\ttest_commit 3rd &&\n+\ttest_commit 4th &&\n+\ttest_commit 5th &&\n+\t# Create notes on 4 first commits\n+\tgit config core.notesRef refs/notes/x &&\n+\tgit notes add -m \"Notes on 1st commit\" 1st &&\n+\tgit notes add -m \"Notes on 2nd commit\" 2nd &&\n+\tgit notes add -m \"Notes on 3rd commit\" 3rd &&\n+\tgit notes add -m \"Notes on 4th commit\" 4th\n+'\n+\n+commit_sha1=$(git rev-parse 1st^{commit})\n+commit_sha2=$(git rev-parse 2nd^{commit})\n+commit_sha3=$(git rev-parse 3rd^{commit})\n+commit_sha4=$(git rev-parse 4th^{commit})\n+commit_sha5=$(git rev-parse 5th^{commit})\n+\n+verify_notes () {\n+\tnotes_ref=\"$1\"\n+\tgit -c core.notesRef=\"refs/notes/$notes_ref\" notes |\n+\t\tsort >\"output_notes_$notes_ref\" &&\n+\ttest_cmp \"expect_notes_$notes_ref\" \"output_notes_$notes_ref\" &&\n+\tgit -c core.notesRef=\"refs/notes/$notes_ref\" log --format=\"%H %s%n%N\" \\\n+\t\t>\"output_log_$notes_ref\" &&\n+\ttest_cmp \"expect_log_$notes_ref\" \"output_log_$notes_ref\"\n+}\n+\n+cat <<EOF | sort >expect_notes_x\n+5e93d24084d32e1cb61f7070505b9d2530cca987 $commit_sha4\n+8366731eeee53787d2bdf8fc1eff7d94757e8da0 $commit_sha3\n+eede89064cd42441590d6afec6c37b321ada3389 $commit_sha2\n+daa55ffad6cb99bf64226532147ffcaf5ce8bdd1 $commit_sha1\n+EOF\n+\n+cat >expect_log_x <<EOF\n+$commit_sha5 5th\n+\n+$commit_sha4 4th\n+Notes on 4th commit\n+\n+$commit_sha3 3rd\n+Notes on 3rd commit\n+\n+$commit_sha2 2nd\n+Notes on 2nd commit\n+\n+$commit_sha1 1st\n+Notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'verify initial notes (x)' '\n+\tverify_notes x\n+'\n+\n+cp expect_notes_x expect_notes_y\n+cp expect_log_x expect_log_y\n+\n+test_expect_success 'merge notes into empty notes ref (x => y)' '\n+\tgit config core.notesRef refs/notes/y &&\n+\tgit notes merge x &&\n+\tverify_notes y &&\n+\t# x and y should point to the same notes commit\n+\ttest \"$(git rev-parse refs/notes/x)\" = \"$(git rev-parse refs/notes/y)\"\n+'\n+\n+test_expect_success 'change notes on other notes ref (y)' '\n+\t# Not touching notes to 1st commit\n+\tgit notes remove 2nd &&\n+\tgit notes append -m \"More notes on 3rd commit\" 3rd &&\n+\tgit notes add -f -m \"New notes on 4th commit\" 4th &&\n+\tgit notes add -m \"Notes on 5th commit\" 5th\n+'\n+\n+cat <<EOF | sort >expect_notes_y\n+0f2efbd00262f2fd41dfae33df8765618eeacd99 $commit_sha5\n+dec2502dac3ea161543f71930044deff93fa945c $commit_sha4\n+4069cdb399fd45463ec6eef8e051a16a03592d91 $commit_sha3\n+daa55ffad6cb99bf64226532147ffcaf5ce8bdd1 $commit_sha1\n+EOF\n+\n+cat >expect_log_y <<EOF\n+$commit_sha5 5th\n+Notes on 5th commit\n+\n+$commit_sha4 4th\n+New notes on 4th commit\n+\n+$commit_sha3 3rd\n+Notes on 3rd commit\n+\n+More notes on 3rd commit\n+\n+$commit_sha2 2nd\n+\n+$commit_sha1 1st\n+Notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'verify changed notes on other notes ref (y)' '\n+\tverify_notes y\n+'\n+\n+test_expect_success 'verify unchanged notes on original notes ref (x)' '\n+\tverify_notes x\n+'\n+\n+test_expect_success 'merge original notes (x) into changed notes (y) => No-op' '\n+\tgit notes merge -vvv x &&\n+\tverify_notes y &&\n+\tverify_notes x\n+'\n+\n+cp expect_notes_y expect_notes_x\n+cp expect_log_y expect_log_x\n+\n+test_expect_success 'merge changed (y) into original (x) => Fast-forward' '\n+\tgit config core.notesRef refs/notes/x &&\n+\tgit notes merge y &&\n+\tverify_notes x &&\n+\tverify_notes y &&\n+\t# x and y should point to same the notes commit\n+\ttest \"$(git rev-parse refs/notes/x)\" = \"$(git rev-parse refs/notes/y)\"\n+'\n+\n+test_done\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"152001","messageId":"1285719811-10871-10-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 09/18] builtin/notes.c: Refactor creation of notes commits.","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:22Z","receivedAt":"2010-09-29T00:23:22Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Create new function create_notes_commit() which is slightly more general than\ncommit_notes() (accepts multiple commit parents and does not auto-update the\nnotes ref). This function will be used by the notes-merge functionality in\nfuture patches.\n\nAlso rewrite builtin/notes.c:commit_notes() to reuse this new function.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n builtin.h       |    2 +-\n builtin/notes.c |   28 +++++-----------------------\n notes-merge.c   |   27 +++++++++++++++++++++++++++\n notes-merge.h   |   14 ++++++++++++++\n 4 files changed, 47 insertions(+), 24 deletions(-)\n\ndiff --git a/builtin.h b/builtin.h\nindex ed6ee26..908d850 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -17,7 +17,7 @@ extern void prune_packed_objects(int);\n extern int fmt_merge_msg(int merge_summary, struct strbuf *in,\n \tstruct strbuf *out);\n extern int fmt_merge_msg_shortlog(struct strbuf *in, struct strbuf *out);\n-extern int commit_notes(struct notes_tree *t, const char *msg);\n+extern void commit_notes(struct notes_tree *t, const char *msg);\n \n struct notes_rewrite_cfg {\n \tstruct notes_tree **trees;\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex ec1d042..717548d 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -288,18 +288,17 @@ static int parse_reedit_arg(const struct option *opt, const char *arg, int unset\n \treturn parse_reuse_arg(opt, arg, unset);\n }\n \n-int commit_notes(struct notes_tree *t, const char *msg)\n+void commit_notes(struct notes_tree *t, const char *msg)\n {\n-\tstruct commit_list *parent;\n-\tunsigned char tree_sha1[20], prev_commit[20], new_commit[20];\n \tstruct strbuf buf = STRBUF_INIT;\n+\tunsigned char commit_sha1[20];\n \n \tif (!t)\n \t\tt = &default_notes_tree;\n \tif (!t->initialized || !t->ref || !*t->ref)\n \t\tdie(\"Cannot commit uninitialized/unreferenced notes tree\");\n \tif (!t->dirty)\n-\t\treturn 0; /* don't have to commit an unchanged tree */\n+\t\treturn; /* don't have to commit an unchanged tree */\n \n \t/* Prepare commit message and reflog message */\n \tstrbuf_addstr(&buf, \"notes: \"); /* commit message starts at index 7 */\n@@ -307,27 +306,10 @@ int commit_notes(struct notes_tree *t, const char *msg)\n \tif (buf.buf[buf.len - 1] != '\\n')\n \t\tstrbuf_addch(&buf, '\\n'); /* Make sure msg ends with newline */\n \n-\t/* Convert notes tree to tree object */\n-\tif (write_notes_tree(t, tree_sha1))\n-\t\tdie(\"Failed to write current notes tree to database\");\n-\n-\t/* Create new commit for the tree object */\n-\tif (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */\n-\t\tparent = xmalloc(sizeof(*parent));\n-\t\tparent->item = lookup_commit(prev_commit);\n-\t\tparent->next = NULL;\n-\t} else {\n-\t\thashclr(prev_commit);\n-\t\tparent = NULL;\n-\t}\n-\tif (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))\n-\t\tdie(\"Failed to commit notes tree to database\");\n-\n-\t/* Update notes ref with new commit */\n-\tupdate_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);\n+\tcreate_notes_commit(t, NULL, buf.buf + 7, commit_sha1);\n+\tupdate_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);\n \n \tstrbuf_release(&buf);\n-\treturn 0;\n }\n \n combine_notes_fn parse_combine_notes_fn(const char *v)\ndiff --git a/notes-merge.c b/notes-merge.c\nindex 5461827..9a4dc6d 100644\n--- a/notes-merge.c\n+++ b/notes-merge.c\n@@ -1,5 +1,6 @@\n #include \"cache.h\"\n #include \"commit.h\"\n+#include \"notes.h\"\n #include \"notes-merge.h\"\n \n void init_notes_merge_options(struct notes_merge_options *o)\n@@ -16,6 +17,32 @@ static int show(struct notes_merge_options *o, int v)\n #define OUTPUT(o, v, ...) \\\n \tdo { if (show((o), (v))) { printf(__VA_ARGS__); puts(\"\"); } } while (0)\n \n+void create_notes_commit(struct notes_tree *t, struct commit_list *parents,\n+\t\t\t const char *msg, unsigned char *result_sha1)\n+{\n+\tunsigned char tree_sha1[20];\n+\n+\tassert(t->initialized);\n+\n+\tif (write_notes_tree(t, tree_sha1))\n+\t\tdie(\"Failed to write notes tree to database\");\n+\n+\tif (!parents) {\n+\t\t/* Deduce parent commit from t->ref */\n+\t\tunsigned char parent_sha1[20];\n+\t\tif (!read_ref(t->ref, parent_sha1)) {\n+\t\t\tstruct commit *parent = lookup_commit(parent_sha1);\n+\t\t\tif (!parent || parse_commit(parent))\n+\t\t\t\tdie(\"Failed to find/parse commit %s\", t->ref);\n+\t\t\tcommit_list_insert(parent, &parents);\n+\t\t}\n+\t\t/* else: t->ref points to nothing, assume root/orphan commit */\n+\t}\n+\n+\tif (commit_tree(msg, tree_sha1, parents, result_sha1, NULL))\n+\t\tdie(\"Failed to commit notes tree to database\");\n+}\n+\n int notes_merge(struct notes_merge_options *o,\n \t\tunsigned char *result_sha1)\n {\ndiff --git a/notes-merge.h b/notes-merge.h\nindex c4416e1..b570123 100644\n--- a/notes-merge.h\n+++ b/notes-merge.h\n@@ -10,6 +10,20 @@ struct notes_merge_options {\n void init_notes_merge_options(struct notes_merge_options *o);\n \n /*\n+ * Create new notes commit from the given notes tree\n+ *\n+ * Properties of the created commit:\n+ * - tree: the result of converting t to a tree object with write_notes_tree().\n+ * - parents: the given parents OR (if NULL) the commit referenced by t->ref.\n+ * - author/committer: the default determined by commmit_tree().\n+ * - commit message: msg\n+ *\n+ * The resulting commit SHA1 is stored in result_sha1.\n+ */\n+void create_notes_commit(struct notes_tree *t, struct commit_list *parents,\n+\t\t\t const char *msg, unsigned char *result_sha1);\n+\n+/*\n  * Merge notes from o->remote_ref into o->local_ref\n  *\n  * The commits given by the two refs are merged, producing one of the following\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151990","messageId":"1285719811-10871-11-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 10/18] git notes merge: Handle real, non-conflicting notes merges","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:23Z","receivedAt":"2010-09-29T00:23:23Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"This continuation of the 'git notes merge' implementation teaches notes-merge\nto properly do real merges between notes trees: Two diffs are performed, one\nfrom $base to $remote, and another from $base to $local. The paths in each\ndiff are normalized to SHA1 object names. The two diffs are then consolidated\ninto a single list of change pairs to be evaluated. Each change pair consist\nof:\n\n  - The annotated object's SHA1\n  - The $base SHA1 (i.e. the common ancestor notes for this object)\n  - The $local SHA1 (i.e. the current notes for this object)\n  - The $remote SHA1 (i.e. the to-be-merged notes for this object)\n\n>From the pair ($base -> $local, $base -> $remote), we can determine the merge\nresult using regular 3-way rules. If conflicts are encountered in this\nprocess, we fail loudly and exit (conflict handling to be added in a future\npatch), If we can complete the merge without conflicts, the resulting\nnotes tree is committed, and the current notes ref updated.\n\nThe patch includes added testcases verifying that we can successfully do real\nconflict-less merges.\n\nThis patch has been improved by the following contributions:\n- Jonathan Nieder: Future-proof by always checking add_note() return value\n- Stephen Boyd: Use test_commit\n\nCc: Jonathan Nieder <jrnieder@gmail.com>\nCc: Stephen Boyd <bebarino@gmail.com>\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n builtin/notes.c        |   15 ++-\n notes-merge.c          |  325 +++++++++++++++++++++++++++++++++++++++++++++++-\n notes-merge.h          |   15 ++-\n t/t3308-notes-merge.sh |  188 ++++++++++++++++++++++++++++\n 4 files changed, 532 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex 717548d..82d2ffe 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -765,6 +765,7 @@ static int merge(int argc, const char **argv, const char *prefix)\n {\n \tstruct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;\n \tunsigned char result_sha1[20];\n+\tstruct notes_tree *t;\n \tstruct notes_merge_options o;\n \tint verbosity = 0, result;\n \tstruct option options[] = {\n@@ -787,19 +788,23 @@ static int merge(int argc, const char **argv, const char *prefix)\n \texpand_notes_ref(&remote_ref);\n \to.remote_ref = remote_ref.buf;\n \n-\tresult = notes_merge(&o, result_sha1);\n+\tt = init_notes_check(\"merge\");\n \n \tstrbuf_addf(&msg, \"notes: Merged notes from %s into %s\",\n \t\t    remote_ref.buf, default_notes_ref());\n-\tif (result == 0) { /* Merge resulted (trivially) in result_sha1 */\n+\to.commit_msg = msg.buf + 7; // skip \"notes: \" prefix\n+\n+\tresult = notes_merge(&o, t, result_sha1);\n+\n+\tif (result >= 0) /* Merge resulted (trivially) in result_sha1 */\n \t\t/* Update default notes ref with new commit */\n \t\tupdate_ref(msg.buf, default_notes_ref(), result_sha1, NULL,\n \t\t\t   0, DIE_ON_ERR);\n-\t} else {\n+\telse\n \t\t/* TODO: */\n-\t\tdie(\"'git notes merge' cannot yet handle non-trivial merges!\");\n-\t}\n+\t\tdie(\"'git notes merge' cannot yet handle conflicts!\");\n \n+\tfree_notes(t);\n \tstrbuf_release(&remote_ref);\n \tstrbuf_release(&msg);\n \treturn 0;\ndiff --git a/notes-merge.c b/notes-merge.c\nindex 9a4dc6d..f625ebd 100644\n--- a/notes-merge.c\n+++ b/notes-merge.c\n@@ -1,8 +1,14 @@\n #include \"cache.h\"\n #include \"commit.h\"\n+#include \"diff.h\"\n+#include \"diffcore.h\"\n #include \"notes.h\"\n #include \"notes-merge.h\"\n \n+struct notes_merge_pair {\n+\tunsigned char obj[20], base[20], local[20], remote[20];\n+};\n+\n void init_notes_merge_options(struct notes_merge_options *o)\n {\n \tmemset(o, 0, sizeof(struct notes_merge_options));\n@@ -17,6 +23,305 @@ static int show(struct notes_merge_options *o, int v)\n #define OUTPUT(o, v, ...) \\\n \tdo { if (show((o), (v))) { printf(__VA_ARGS__); puts(\"\"); } } while (0)\n \n+static int path_to_sha1(const char *path, unsigned char *sha1)\n+{\n+\tchar hex_sha1[40];\n+\tint i = 0;\n+\twhile (*path && i < 40) {\n+\t\tif (*path != '/')\n+\t\t\thex_sha1[i++] = *path;\n+\t\tpath++;\n+\t}\n+\tif (*path || i != 40)\n+\t\treturn -1;\n+\treturn get_sha1_hex(hex_sha1, sha1);\n+}\n+\n+static int verify_notes_filepair(struct diff_filepair *p, unsigned char *sha1)\n+{\n+\tswitch (p->status) {\n+\tcase DIFF_STATUS_MODIFIED:\n+\t\tassert(p->one->mode == p->two->mode);\n+\t\tassert(!is_null_sha1(p->one->sha1));\n+\t\tassert(!is_null_sha1(p->two->sha1));\n+\t\tbreak;\n+\tcase DIFF_STATUS_ADDED:\n+\t\tassert(is_null_sha1(p->one->sha1));\n+\t\tbreak;\n+\tcase DIFF_STATUS_DELETED:\n+\t\tassert(is_null_sha1(p->two->sha1));\n+\t\tbreak;\n+\tdefault:\n+\t\treturn -1;\n+\t}\n+\tassert(!strcmp(p->one->path, p->two->path));\n+\treturn path_to_sha1(p->one->path, sha1);\n+}\n+\n+static struct notes_merge_pair *find_notes_merge_pair_pos(\n+\t\tstruct notes_merge_pair *list, int len, unsigned char *obj,\n+\t\tint insert_new, int *occupied)\n+{\n+\t/*\n+\t * Both diff_tree_remote() and diff_tree_local() tend to process\n+\t * merge_pairs in ascending order. Therefore, cache last returned\n+\t * index, and search sequentially from there until the appropriate\n+\t * position is found.\n+\t *\n+\t * Since inserts only happen from diff_tree_remote() (which mainly\n+\t * _appends_), we don't care that inserting into the middle of the\n+\t * list is expensive (using memmove()).\n+\t */\n+\tstatic int last_index = 0;\n+\tint i = last_index < len ? last_index : len - 1;\n+\tint prev_cmp = 0, cmp = -1;\n+\twhile (i >= 0 && i < len) {\n+\t\tcmp = hashcmp(obj, list[i].obj);\n+\t\tif (!cmp) /* obj belongs @ i */\n+\t\t\tbreak;\n+\t\telse if (cmp < 0 && prev_cmp <= 0) /* obj belongs < i */\n+\t\t\ti--;\n+\t\telse if (cmp < 0) /* obj belongs between i-1 and i */\n+\t\t\tbreak;\n+\t\telse if (cmp > 0 && prev_cmp >= 0) /* obj belongs > i */\n+\t\t\ti++;\n+\t\telse /* if (cmp > 0) */ { /* obj belongs between i and i+1 */\n+\t\t\ti++;\n+\t\t\tbreak;\n+\t\t}\n+\t\tprev_cmp = cmp;\n+\t}\n+\tif (i < 0)\n+\t\ti = 0;\n+\t/* obj belongs at, or immediately preceding, index i (0 <= i <= len) */\n+\n+\tif (!cmp)\n+\t\t*occupied = 1;\n+\telse {\n+\t\t*occupied = 0;\n+\t\tif (insert_new && i < len) {\n+\t\t\tmemmove(list + i + 1, list + i,\n+\t\t\t\t(len - i) * sizeof(struct notes_merge_pair));\n+\t\t\tmemset(list + i, 0, sizeof(struct notes_merge_pair));\n+\t\t}\n+\t}\n+\tlast_index = i;\n+\treturn list + i;\n+}\n+\n+static unsigned char uninitialized[20] =\n+\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" \\\n+\t\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\";\n+\n+static struct notes_merge_pair *diff_tree_remote(struct notes_merge_options *o,\n+\t\t\t\t\t\t const unsigned char *base,\n+\t\t\t\t\t\t const unsigned char *remote,\n+\t\t\t\t\t\t int *num_changes)\n+{\n+\tstruct diff_options opt;\n+\tstruct notes_merge_pair *changes;\n+\tint i, len = 0;\n+\n+\tOUTPUT(o, 5, \"\\tdiff_tree_remote(base = %.7s, remote = %.7s)\",\n+\t       sha1_to_hex(base), sha1_to_hex(remote));\n+\n+\tdiff_setup(&opt);\n+\tDIFF_OPT_SET(&opt, RECURSIVE);\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tif (diff_setup_done(&opt) < 0)\n+\t\tdie(\"diff_setup_done failed\");\n+\tdiff_tree_sha1(base, remote, \"\", &opt);\n+\tdiffcore_std(&opt);\n+\n+\tchanges = xcalloc(diff_queued_diff.nr, sizeof(struct notes_merge_pair));\n+\n+\tfor (i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct notes_merge_pair *mp;\n+\t\tint occupied;\n+\t\tunsigned char obj[20];\n+\n+\t\tif (verify_notes_filepair(p, obj)) {\n+\t\t\tOUTPUT(o, 5, \"\\t\\tCannot merge entry '%s' (%c): \"\n+\t\t\t       \"%.7s -> %.7s. Skipping!\", p->one->path,\n+\t\t\t       p->status, sha1_to_hex(p->one->sha1),\n+\t\t\t       sha1_to_hex(p->two->sha1));\n+\t\t\tcontinue;\n+\t\t}\n+\t\tmp = find_notes_merge_pair_pos(changes, len, obj, 1, &occupied);\n+\t\tif (occupied) {\n+\t\t\t/* We've found an addition/deletion pair */\n+\t\t\tassert(!hashcmp(mp->obj, obj));\n+\t\t\tif (is_null_sha1(p->one->sha1)) { /* addition */\n+\t\t\t\tassert(is_null_sha1(mp->remote));\n+\t\t\t\thashcpy(mp->remote, p->two->sha1);\n+\t\t\t} else if (is_null_sha1(p->two->sha1)) { /* deletion */\n+\t\t\t\tassert(is_null_sha1(mp->base));\n+\t\t\t\thashcpy(mp->base, p->one->sha1);\n+\t\t\t} else\n+\t\t\t\tassert(!\"Invalid existing change recorded\");\n+\t\t} else {\n+\t\t\thashcpy(mp->obj, obj);\n+\t\t\thashcpy(mp->base, p->one->sha1);\n+\t\t\thashcpy(mp->local, uninitialized);\n+\t\t\thashcpy(mp->remote, p->two->sha1);\n+\t\t\tlen++;\n+\t\t}\n+\t\tOUTPUT(o, 5, \"\\t\\tRecorded remote change for %s: %.7s -> %.7s\",\n+\t\t       sha1_to_hex(mp->obj), sha1_to_hex(mp->base),\n+\t\t       sha1_to_hex(mp->remote));\n+\t}\n+\tdiff_flush(&opt);\n+\tdiff_tree_release_paths(&opt);\n+\n+\t*num_changes = len;\n+\treturn changes;\n+}\n+\n+static void diff_tree_local(struct notes_merge_options *o,\n+\t\t\t    struct notes_merge_pair *changes, int len,\n+\t\t\t    const unsigned char *base,\n+\t\t\t    const unsigned char *local)\n+{\n+\tstruct diff_options opt;\n+\tint i;\n+\n+\tOUTPUT(o, 5, \"\\tdiff_tree_local(len = %i, base = %.7s, local = %.7s)\",\n+\t       len, sha1_to_hex(base), sha1_to_hex(local));\n+\n+\tdiff_setup(&opt);\n+\tDIFF_OPT_SET(&opt, RECURSIVE);\n+\topt.output_format = DIFF_FORMAT_NO_OUTPUT;\n+\tif (diff_setup_done(&opt) < 0)\n+\t\tdie(\"diff_setup_done failed\");\n+\tdiff_tree_sha1(base, local, \"\", &opt);\n+\tdiffcore_std(&opt);\n+\n+\tfor (i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct notes_merge_pair *mp;\n+\t\tint match;\n+\t\tunsigned char obj[20];\n+\n+\t\tif (verify_notes_filepair(p, obj)) {\n+\t\t\tOUTPUT(o, 5, \"\\t\\tCannot merge entry '%s' (%c): \"\n+\t\t\t       \"%.7s -> %.7s. Skipping!\", p->one->path,\n+\t\t\t       p->status, sha1_to_hex(p->one->sha1),\n+\t\t\t       sha1_to_hex(p->two->sha1));\n+\t\t\tcontinue;\n+\t\t}\n+\t\tmp = find_notes_merge_pair_pos(changes, len, obj, 0, &match);\n+\t\tif (!match) {\n+\t\t\tOUTPUT(o, 5, \"\\t\\tIgnoring local-only change for %s: \"\n+\t\t\t       \"%.7s -> %.7s\", sha1_to_hex(obj),\n+\t\t\t       sha1_to_hex(p->one->sha1),\n+\t\t\t       sha1_to_hex(p->two->sha1));\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tassert(!hashcmp(mp->obj, obj));\n+\t\tif (is_null_sha1(p->two->sha1)) { /* deletion */\n+\t\t\t/*\n+\t\t\t * Either this is a true deletion (1), or it is part\n+\t\t\t * of an A/D pair (2), or D/A pair (3):\n+\t\t\t *\n+\t\t\t * (1) mp->local is uninitialized; set it to null_sha1\n+\t\t\t * (2) mp->local is not uninitialized; don't touch it\n+\t\t\t * (3) mp->local is uninitialized; set it to null_sha1\n+\t\t\t *     (will be overwritten by following addition)\n+\t\t\t */\n+\t\t\tif (!hashcmp(mp->local, uninitialized))\n+\t\t\t\thashclr(mp->local);\n+\t\t} else if (is_null_sha1(p->one->sha1)) { /* addition */\n+\t\t\t/*\n+\t\t\t * Either this is a true addition (1), or it is part\n+\t\t\t * of an A/D pair (2), or D/A pair (3):\n+\t\t\t *\n+\t\t\t * (1) mp->local is uninitialized; set to p->two->sha1\n+\t\t\t * (2) mp->local is uninitialized; set to p->two->sha1\n+\t\t\t * (3) mp->local is null_sha1;     set to p->two->sha1\n+\t\t\t */\n+\t\t\tassert(is_null_sha1(mp->local) ||\n+\t\t\t       !hashcmp(mp->local, uninitialized));\n+\t\t\thashcpy(mp->local, p->two->sha1);\n+\t\t} else { /* modification */\n+\t\t\t/*\n+\t\t\t * This is a true modification. p->one->sha1 shall\n+\t\t\t * match mp->base, and mp->local shall be uninitialized.\n+\t\t\t * Set mp->local to p->two->sha1.\n+\t\t\t */\n+\t\t\tassert(!hashcmp(p->one->sha1, mp->base));\n+\t\t\tassert(!hashcmp(mp->local, uninitialized));\n+\t\t\thashcpy(mp->local, p->two->sha1);\n+\t\t}\n+\t\tOUTPUT(o, 5, \"\\t\\tRecorded local change for %s: %.7s -> %.7s\",\n+\t\t       sha1_to_hex(mp->obj), sha1_to_hex(mp->base),\n+\t\t       sha1_to_hex(mp->local));\n+\t}\n+\tdiff_flush(&opt);\n+\tdiff_tree_release_paths(&opt);\n+}\n+\n+static int merge_changes(struct notes_merge_options *o,\n+\t\t\t struct notes_merge_pair *changes, int *num_changes,\n+\t\t\t struct notes_tree *t)\n+{\n+\tint i, conflicts = 0;\n+\n+\tOUTPUT(o, 5, \"\\tmerge_changes(num_changes = %i)\", *num_changes);\n+\tfor (i = 0; i < *num_changes; i++) {\n+\t\tstruct notes_merge_pair *p = changes + i;\n+\t\tOUTPUT(o, 5, \"\\t\\t%.7s: %.7s -> %.7s/%.7s\",\n+\t\t       sha1_to_hex(p->obj), sha1_to_hex(p->base),\n+\t\t       sha1_to_hex(p->local), sha1_to_hex(p->remote));\n+\n+\t\tif (!hashcmp(p->base, p->remote)) {\n+\t\t\t/* no remote change; nothing to do */\n+\t\t\tOUTPUT(o, 5, \"\\t\\t\\tskipping (no remote change)\");\n+\t\t} else if (!hashcmp(p->local, p->remote)) {\n+\t\t\t/* same change in local and remote; nothing to do */\n+\t\t\tOUTPUT(o, 5, \"\\t\\t\\tskipping (local == remote)\");\n+\t\t} else if (!hashcmp(p->local, uninitialized) ||\n+\t\t\t   !hashcmp(p->local, p->base)) {\n+\t\t\t/* no local change; adopt remote change */\n+\t\t\tOUTPUT(o, 5, \"\\t\\t\\tno local change, adopting remote\");\n+\t\t\tif (add_note(t, p->obj, p->remote,\n+\t\t\t\t     combine_notes_overwrite))\n+\t\t\t\tdie(\"confused: combine_notes_overwrite failed\");\n+\t\t} else {\n+\t\t\t/* need file-level merge between local and remote */\n+\t\t\tOUTPUT(o, 5, \"\\t\\t\\tneed content-level merge\");\n+\t\t\tconflicts += 1; /* TODO */\n+\t\t}\n+\t}\n+\n+\treturn conflicts;\n+}\n+\n+static int merge_from_diffs(struct notes_merge_options *o,\n+\t\t\t    const unsigned char *base,\n+\t\t\t    const unsigned char *local,\n+\t\t\t    const unsigned char *remote, struct notes_tree *t)\n+{\n+\tstruct notes_merge_pair *changes;\n+\tint num_changes, conflicts;\n+\n+\tOUTPUT(o, 5, \"\\tmerge_from_diffs(base = %.7s, local = %.7s, \"\n+\t       \"remote = %.7s)\", sha1_to_hex(base), sha1_to_hex(local),\n+\t       sha1_to_hex(remote));\n+\n+\tchanges = diff_tree_remote(o, base, remote, &num_changes);\n+\tdiff_tree_local(o, changes, num_changes, base, local);\n+\n+\tconflicts = merge_changes(o, changes, &num_changes, t);\n+\tfree(changes);\n+\n+\tOUTPUT(o, 4, \"Merge result: %i unmerged notes and a %s notes tree\",\n+\t       conflicts, t->dirty ? \"dirty\" : \"clean\");\n+\n+\treturn conflicts ? -1 : 1;\n+}\n+\n void create_notes_commit(struct notes_tree *t, struct commit_list *parents,\n \t\t\t const char *msg, unsigned char *result_sha1)\n {\n@@ -44,14 +349,16 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,\n }\n \n int notes_merge(struct notes_merge_options *o,\n+\t\tstruct notes_tree *local_tree,\n \t\tunsigned char *result_sha1)\n {\n \tunsigned char local_sha1[20], remote_sha1[20];\n \tstruct commit *local, *remote;\n \tstruct commit_list *bases = NULL;\n-\tconst unsigned char *base_sha1;\n+\tconst unsigned char *base_sha1, *base_tree_sha1;\n \tint result = 0;\n \n+\tassert(!strcmp(o->local_ref, local_tree->ref));\n \thashclr(result_sha1);\n \n \tOUTPUT(o, 5, \"notes_merge(o->local_ref = %s, o->remote_ref = %s)\",\n@@ -90,14 +397,17 @@ int notes_merge(struct notes_merge_options *o,\n \tbases = get_merge_bases(local, remote, 1);\n \tif (!bases) {\n \t\tbase_sha1 = null_sha1;\n+\t\tbase_tree_sha1 = (unsigned char *)EMPTY_TREE_SHA1_BIN;\n \t\tOUTPUT(o, 4, \"No merge base found; doing history-less merge\");\n \t} else if (!bases->next) {\n \t\tbase_sha1 = bases->item->object.sha1;\n+\t\tbase_tree_sha1 = bases->item->tree->object.sha1;\n \t\tOUTPUT(o, 4, \"One merge base found (%.7s)\",\n \t\t       sha1_to_hex(base_sha1));\n \t} else {\n \t\t/* TODO: How to handle multiple merge-bases? */\n \t\tbase_sha1 = bases->item->object.sha1;\n+\t\tbase_tree_sha1 = bases->item->tree->object.sha1;\n \t\tOUTPUT(o, 3, \"Multiple merge bases found. Using the first \"\n \t\t       \"(%.7s)\", sha1_to_hex(base_sha1));\n \t}\n@@ -119,8 +429,17 @@ int notes_merge(struct notes_merge_options *o,\n \t\tgoto found_result;\n \t}\n \n-\t/* TODO: */\n-\tresult = error(\"notes_merge() cannot yet handle real merges.\");\n+\tresult = merge_from_diffs(o, base_tree_sha1, local->tree->object.sha1,\n+\t\t\t\t  remote->tree->object.sha1, local_tree);\n+\n+\tif (result > 0) { /* successful non-trivial merge */\n+\t\t/* Commit result */\n+\t\tstruct commit_list *parents = NULL;\n+\t\tcommit_list_insert(remote, &parents); /* LIFO order */\n+\t\tcommit_list_insert(local, &parents);\n+\t\tcreate_notes_commit(local_tree, parents, o->commit_msg,\n+\t\t\t\t    result_sha1);\n+\t}\n \n found_result:\n \tfree_commit_list(bases);\ndiff --git a/notes-merge.h b/notes-merge.h\nindex b570123..e42ce4b 100644\n--- a/notes-merge.h\n+++ b/notes-merge.h\n@@ -4,6 +4,7 @@\n struct notes_merge_options {\n \tconst char *local_ref;\n \tconst char *remote_ref;\n+\tconst char *commit_msg;\n \tint verbosity;\n };\n \n@@ -26,19 +27,27 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,\n /*\n  * Merge notes from o->remote_ref into o->local_ref\n  *\n+ * The given notes_tree 'local_tree' must be the notes_tree referenced by the\n+ * o->local_ref. This is the notes_tree in which the object-level merge is\n+ * performed.\n+ *\n  * The commits given by the two refs are merged, producing one of the following\n  * outcomes:\n  *\n  * 1. The merge trivially results in an existing commit (e.g. fast-forward or\n- *    already-up-to-date). The SHA1 of the result is written into 'result_sha1'\n- *    and 0 is returned.\n- * 2. The merge fails. result_sha1 is set to null_sha1, and non-zero returned.\n+ *    already-up-to-date). 'local_tree' is untouched, the SHA1 of the result\n+ *    is written into 'result_sha1' and 0 is returned.\n+ * 2. The merge successfully completes, producing a merge commit. local_tree\n+ *    contains the updated notes tree, the SHA1 of the resulting commit is\n+ *    written into 'result_sha1', and 1 is returned.\n+ * 3. The merge fails. result_sha1 is set to null_sha1, and -1 is returned.\n  *\n  * Either ref (but not both) may not exist in which case the missing ref is\n  * interpreted as an empty notes tree, and the merge trivially results in\n  * what the other ref points to.\n  */\n int notes_merge(struct notes_merge_options *o,\n+\t\tstruct notes_tree *local_tree,\n \t\tunsigned char *result_sha1);\n \n #endif\ndiff --git a/t/t3308-notes-merge.sh b/t/t3308-notes-merge.sh\nindex 0342e37..b211e82 100755\n--- a/t/t3308-notes-merge.sh\n+++ b/t/t3308-notes-merge.sh\n@@ -136,4 +136,192 @@ test_expect_success 'merge changed (y) into original (x) => Fast-forward' '\n \ttest \"$(git rev-parse refs/notes/x)\" = \"$(git rev-parse refs/notes/y)\"\n '\n \n+test_expect_success 'merge empty notes ref (z => y)' '\n+\t# Prepare empty (but valid) notes ref (z)\n+\tgit config core.notesRef refs/notes/z &&\n+\tgit notes add -m \"foo\" &&\n+\tgit notes remove &&\n+\tgit notes >output_notes_z &&\n+\ttest_cmp /dev/null output_notes_z &&\n+\t# Do the merge (z => y)\n+\tgit config core.notesRef refs/notes/y &&\n+\tgit notes merge z &&\n+\tverify_notes y &&\n+\t# y should no longer point to the same notes commit as x\n+\ttest \"$(git rev-parse refs/notes/x)\" != \"$(git rev-parse refs/notes/y)\"\n+'\n+\n+cat <<EOF | sort >expect_notes_y\n+0f2efbd00262f2fd41dfae33df8765618eeacd99 $commit_sha5\n+dec2502dac3ea161543f71930044deff93fa945c $commit_sha4\n+4069cdb399fd45463ec6eef8e051a16a03592d91 $commit_sha3\n+d000d30e6ddcfce3a8122c403226a2ce2fd04d9d $commit_sha2\n+43add6bd0c8c0bc871ac7991e0f5573cfba27804 $commit_sha1\n+EOF\n+\n+cat >expect_log_y <<EOF\n+$commit_sha5 5th\n+Notes on 5th commit\n+\n+$commit_sha4 4th\n+New notes on 4th commit\n+\n+$commit_sha3 3rd\n+Notes on 3rd commit\n+\n+More notes on 3rd commit\n+\n+$commit_sha2 2nd\n+New notes on 2nd commit\n+\n+$commit_sha1 1st\n+Notes on 1st commit\n+\n+More notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'change notes on other notes ref (y)' '\n+\t# Append to 1st commit notes\n+\tgit notes append -m \"More notes on 1st commit\" 1st &&\n+\t# Add new notes to 2nd commit\n+\tgit notes add -m \"New notes on 2nd commit\" 2nd &&\n+\tverify_notes y\n+'\n+\n+cat <<EOF | sort >expect_notes_x\n+0f2efbd00262f2fd41dfae33df8765618eeacd99 $commit_sha5\n+1f257a3a90328557c452f0817d6cc50c89d315d4 $commit_sha4\n+daa55ffad6cb99bf64226532147ffcaf5ce8bdd1 $commit_sha1\n+EOF\n+\n+cat >expect_log_x <<EOF\n+$commit_sha5 5th\n+Notes on 5th commit\n+\n+$commit_sha4 4th\n+New notes on 4th commit\n+\n+More notes on 4th commit\n+\n+$commit_sha3 3rd\n+\n+$commit_sha2 2nd\n+\n+$commit_sha1 1st\n+Notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'change notes on notes ref (x)' '\n+\tgit config core.notesRef refs/notes/x &&\n+\tgit notes remove 3rd &&\n+\tgit notes append -m \"More notes on 4th commit\" 4th &&\n+\tverify_notes x\n+'\n+\n+cat <<EOF | sort >expect_notes_x\n+0f2efbd00262f2fd41dfae33df8765618eeacd99 $commit_sha5\n+1f257a3a90328557c452f0817d6cc50c89d315d4 $commit_sha4\n+d000d30e6ddcfce3a8122c403226a2ce2fd04d9d $commit_sha2\n+43add6bd0c8c0bc871ac7991e0f5573cfba27804 $commit_sha1\n+EOF\n+\n+cat >expect_log_x <<EOF\n+$commit_sha5 5th\n+Notes on 5th commit\n+\n+$commit_sha4 4th\n+New notes on 4th commit\n+\n+More notes on 4th commit\n+\n+$commit_sha3 3rd\n+\n+$commit_sha2 2nd\n+New notes on 2nd commit\n+\n+$commit_sha1 1st\n+Notes on 1st commit\n+\n+More notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'merge y into x => Non-conflicting 3-way merge' '\n+\tgit notes merge y &&\n+\tverify_notes x &&\n+\tverify_notes y\n+'\n+\n+cat <<EOF | sort >expect_notes_w\n+05a4927951bcef347f51486575b878b2b60137f2 $commit_sha3\n+d000d30e6ddcfce3a8122c403226a2ce2fd04d9d $commit_sha2\n+EOF\n+\n+cat >expect_log_w <<EOF\n+$commit_sha5 5th\n+\n+$commit_sha4 4th\n+\n+$commit_sha3 3rd\n+New notes on 3rd commit\n+\n+$commit_sha2 2nd\n+New notes on 2nd commit\n+\n+$commit_sha1 1st\n+\n+EOF\n+\n+test_expect_success 'create notes on new, separate notes ref (w)' '\n+\tgit config core.notesRef refs/notes/w &&\n+\t# Add same note as refs/notes/y on 2nd commit\n+\tgit notes add -m \"New notes on 2nd commit\" 2nd &&\n+\t# Add new note on 3rd commit (non-conflicting)\n+\tgit notes add -m \"New notes on 3rd commit\" 3rd &&\n+\t# Verify state of notes on new, separate notes ref (w)\n+\tverify_notes w\n+'\n+\n+cat <<EOF | sort >expect_notes_x\n+0f2efbd00262f2fd41dfae33df8765618eeacd99 $commit_sha5\n+1f257a3a90328557c452f0817d6cc50c89d315d4 $commit_sha4\n+05a4927951bcef347f51486575b878b2b60137f2 $commit_sha3\n+d000d30e6ddcfce3a8122c403226a2ce2fd04d9d $commit_sha2\n+43add6bd0c8c0bc871ac7991e0f5573cfba27804 $commit_sha1\n+EOF\n+\n+cat >expect_log_x <<EOF\n+$commit_sha5 5th\n+Notes on 5th commit\n+\n+$commit_sha4 4th\n+New notes on 4th commit\n+\n+More notes on 4th commit\n+\n+$commit_sha3 3rd\n+New notes on 3rd commit\n+\n+$commit_sha2 2nd\n+New notes on 2nd commit\n+\n+$commit_sha1 1st\n+Notes on 1st commit\n+\n+More notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'merge w into x => Non-conflicting history-less merge' '\n+\tgit config core.notesRef refs/notes/x &&\n+\tgit notes merge w &&\n+\t# Verify new state of notes on other notes ref (x)\n+\tverify_notes x &&\n+\t# Also verify that nothing changed on other notes refs (y and w)\n+\tverify_notes y &&\n+\tverify_notes w\n+'\n+\n test_done\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151995","messageId":"1285719811-10871-12-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 11/18] git notes merge: Add automatic conflict resolvers (ours, theirs, union)","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:24Z","receivedAt":"2010-09-29T00:23:24Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"The new -s/--strategy command-line option to 'git notes merge' allow the user\nto choose how notes merge conflicts should be resolved. There are four valid\nstrategies to choose from:\n\n1. \"manual\" (the default): This will let the user manually resolve conflicts.\n   This option currently fails with an error message. It will be implemented\n   properly in future patches.\n\n2. \"ours\": This automatically chooses the local version of a conflict, and\n   discards the remote version.\n\n3. \"theirs\": This automatically chooses the remote version of a conflict, and\n   discards the local version.\n\n4. \"union\": This automatically resolves the conflict by appending the remote\n   version to the local version.\n\nThe strategies are implemented using the combine_notes_* functions from the\nnotes.h API.\n\nThe patch also includes testcases verifying the correct implementation of\nthese strategies.\n\nThis patch has been improved by the following contributions:\n- Jonathan Nieder: Future-proof by always checking add_note() return value\n- Stephen Boyd: Use test_commit\n\nCc: Jonathan Nieder <jrnieder@gmail.com>\nCc: Stephen Boyd <bebarino@gmail.com>\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n builtin/notes.c                     |   21 ++-\n notes-merge.c                       |   31 ++-\n notes-merge.h                       |    6 +\n t/t3309-notes-merge-auto-resolve.sh |  502 +++++++++++++++++++++++++++++++++++\n 4 files changed, 558 insertions(+), 2 deletions(-)\n create mode 100755 t/t3309-notes-merge-auto-resolve.sh\n\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex 82d2ffe..79290db 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -26,7 +26,7 @@ static const char * const git_notes_usage[] = {\n \t\"git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]\",\n \t\"git notes [--ref <notes_ref>] edit [<object>]\",\n \t\"git notes [--ref <notes_ref>] show [<object>]\",\n-\t\"git notes [--ref <notes_ref>] merge [-v | -q] <notes_ref>\",\n+\t\"git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>\",\n \t\"git notes [--ref <notes_ref>] remove [<object>]\",\n \t\"git notes [--ref <notes_ref>] prune [-n | -v]\",\n \tNULL\n@@ -768,8 +768,12 @@ static int merge(int argc, const char **argv, const char *prefix)\n \tstruct notes_tree *t;\n \tstruct notes_merge_options o;\n \tint verbosity = 0, result;\n+\tconst char *strategy = NULL;\n \tstruct option options[] = {\n \t\tOPT__VERBOSITY(&verbosity),\n+\t\tOPT_STRING('s', \"strategy\", &strategy, \"strategy\",\n+\t\t\t   \"resolve notes conflicts using the given \"\n+\t\t\t   \"strategy (manual/ours/theirs/union)\"),\n \t\tOPT_END()\n \t};\n \n@@ -788,6 +792,21 @@ static int merge(int argc, const char **argv, const char *prefix)\n \texpand_notes_ref(&remote_ref);\n \to.remote_ref = remote_ref.buf;\n \n+\tif (strategy) {\n+\t\tif (!strcmp(strategy, \"manual\"))\n+\t\t\to.strategy = NOTES_MERGE_RESOLVE_MANUAL;\n+\t\telse if (!strcmp(strategy, \"ours\"))\n+\t\t\to.strategy = NOTES_MERGE_RESOLVE_OURS;\n+\t\telse if (!strcmp(strategy, \"theirs\"))\n+\t\t\to.strategy = NOTES_MERGE_RESOLVE_THEIRS;\n+\t\telse if (!strcmp(strategy, \"union\"))\n+\t\t\to.strategy = NOTES_MERGE_RESOLVE_UNION;\n+\t\telse {\n+\t\t\terror(\"Unknown -X/--resolve strategy: %s\", strategy);\n+\t\t\tusage_with_options(git_notes_merge_usage, options);\n+\t\t}\n+\t}\n+\n \tt = init_notes_check(\"merge\");\n \n \tstrbuf_addf(&msg, \"notes: Merged notes from %s into %s\",\ndiff --git a/notes-merge.c b/notes-merge.c\nindex f625ebd..6fa59d8 100644\n--- a/notes-merge.c\n+++ b/notes-merge.c\n@@ -262,6 +262,35 @@ static void diff_tree_local(struct notes_merge_options *o,\n \tdiff_tree_release_paths(&opt);\n }\n \n+static int merge_one_change(struct notes_merge_options *o,\n+\t\t\t    struct notes_merge_pair *p, struct notes_tree *t)\n+{\n+\t/*\n+\t * Return 0 if change was resolved (and added to notes_tree),\n+\t * 1 if conflict\n+\t */\n+\tswitch (o->strategy) {\n+\tcase NOTES_MERGE_RESOLVE_MANUAL:\n+\t\treturn 1;\n+\tcase NOTES_MERGE_RESOLVE_OURS:\n+\t\tOUTPUT(o, 2, \"Using local notes for %s\", sha1_to_hex(p->obj));\n+\t\t/* nothing to do */\n+\t\treturn 0;\n+\tcase NOTES_MERGE_RESOLVE_THEIRS:\n+\t\tOUTPUT(o, 2, \"Using remote notes for %s\", sha1_to_hex(p->obj));\n+\t\tif (add_note(t, p->obj, p->remote, combine_notes_overwrite))\n+\t\t\tdie(\"confused: combine_notes_overwrite failed\");\n+\t\treturn 0;\n+\tcase NOTES_MERGE_RESOLVE_UNION:\n+\t\tOUTPUT(o, 2, \"Concatenating local and remote notes for %s\",\n+\t\t       sha1_to_hex(p->obj));\n+\t\tif (add_note(t, p->obj, p->remote, combine_notes_concatenate))\n+\t\t\tdie(\"confused: combine_notes_concatenate failed\");\n+\t\treturn 0;\n+\t}\n+\tdie(\"Unknown strategy (%i).\", o->strategy);\n+}\n+\n static int merge_changes(struct notes_merge_options *o,\n \t\t\t struct notes_merge_pair *changes, int *num_changes,\n \t\t\t struct notes_tree *t)\n@@ -291,7 +320,7 @@ static int merge_changes(struct notes_merge_options *o,\n \t\t} else {\n \t\t\t/* need file-level merge between local and remote */\n \t\t\tOUTPUT(o, 5, \"\\t\\t\\tneed content-level merge\");\n-\t\t\tconflicts += 1; /* TODO */\n+\t\t\tconflicts += merge_one_change(o, p, t);\n \t\t}\n \t}\n \ndiff --git a/notes-merge.h b/notes-merge.h\nindex e42ce4b..e392049 100644\n--- a/notes-merge.h\n+++ b/notes-merge.h\n@@ -6,6 +6,12 @@ struct notes_merge_options {\n \tconst char *remote_ref;\n \tconst char *commit_msg;\n \tint verbosity;\n+\tenum {\n+\t\tNOTES_MERGE_RESOLVE_MANUAL = 0,\n+\t\tNOTES_MERGE_RESOLVE_OURS,\n+\t\tNOTES_MERGE_RESOLVE_THEIRS,\n+\t\tNOTES_MERGE_RESOLVE_UNION\n+\t} strategy;\n };\n \n void init_notes_merge_options(struct notes_merge_options *o);\ndiff --git a/t/t3309-notes-merge-auto-resolve.sh b/t/t3309-notes-merge-auto-resolve.sh\nnew file mode 100755\nindex 0000000..1a1fc7e\n--- /dev/null\n+++ b/t/t3309-notes-merge-auto-resolve.sh\n@@ -0,0 +1,502 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2010 Johan Herland\n+#\n+\n+test_description='Test notes merging with auto-resolving strategies'\n+\n+. ./test-lib.sh\n+\n+# Set up a notes merge scenario with all kinds of potential conflicts\n+test_expect_success 'setup commits' '\n+\ttest_commit 1st &&\n+\ttest_commit 2nd &&\n+\ttest_commit 3rd &&\n+\ttest_commit 4th &&\n+\ttest_commit 5th &&\n+\ttest_commit 6th &&\n+\ttest_commit 7th &&\n+\ttest_commit 8th &&\n+\ttest_commit 9th &&\n+\ttest_commit 10th &&\n+\ttest_commit 11th &&\n+\ttest_commit 12th &&\n+\ttest_commit 13th &&\n+\ttest_commit 14th &&\n+\ttest_commit 15th\n+'\n+\n+commit_sha1=$(git rev-parse 1st^{commit})\n+commit_sha2=$(git rev-parse 2nd^{commit})\n+commit_sha3=$(git rev-parse 3rd^{commit})\n+commit_sha4=$(git rev-parse 4th^{commit})\n+commit_sha5=$(git rev-parse 5th^{commit})\n+commit_sha6=$(git rev-parse 6th^{commit})\n+commit_sha7=$(git rev-parse 7th^{commit})\n+commit_sha8=$(git rev-parse 8th^{commit})\n+commit_sha9=$(git rev-parse 9th^{commit})\n+commit_sha10=$(git rev-parse 10th^{commit})\n+commit_sha11=$(git rev-parse 11th^{commit})\n+commit_sha12=$(git rev-parse 12th^{commit})\n+commit_sha13=$(git rev-parse 13th^{commit})\n+commit_sha14=$(git rev-parse 14th^{commit})\n+commit_sha15=$(git rev-parse 15th^{commit})\n+\n+verify_notes () {\n+\tnotes_ref=\"$1\"\n+\tsuffix=\"$2\"\n+\tgit -c core.notesRef=\"refs/notes/$notes_ref\" notes |\n+\t\tsort >\"output_notes_$suffix\" &&\n+\ttest_cmp \"expect_notes_$suffix\" \"output_notes_$suffix\" &&\n+\tgit -c core.notesRef=\"refs/notes/$notes_ref\" log --format=\"%H %s%n%N\" \\\n+\t\t>\"output_log_$suffix\" &&\n+\ttest_cmp \"expect_log_$suffix\" \"output_log_$suffix\"\n+}\n+\n+test_expect_success 'setup merge base (x)' '\n+\tgit config core.notesRef refs/notes/x &&\n+\tgit notes add -m \"x notes on 6th commit\" 6th &&\n+\tgit notes add -m \"x notes on 7th commit\" 7th &&\n+\tgit notes add -m \"x notes on 8th commit\" 8th &&\n+\tgit notes add -m \"x notes on 9th commit\" 9th &&\n+\tgit notes add -m \"x notes on 10th commit\" 10th &&\n+\tgit notes add -m \"x notes on 11th commit\" 11th &&\n+\tgit notes add -m \"x notes on 12th commit\" 12th &&\n+\tgit notes add -m \"x notes on 13th commit\" 13th &&\n+\tgit notes add -m \"x notes on 14th commit\" 14th &&\n+\tgit notes add -m \"x notes on 15th commit\" 15th\n+'\n+\n+cat <<EOF | sort >expect_notes_x\n+457a85d6c814ea208550f15fcc48f804ac8dc023 $commit_sha15\n+b0c95b954301d69da2bc3723f4cb1680d355937c $commit_sha14\n+5d30216a129eeffa97d9694ffe8c74317a560315 $commit_sha13\n+dd161bc149470fd890dd4ab52a4cbd79bbd18c36 $commit_sha12\n+7abbc45126d680336fb24294f013a7cdfa3ed545 $commit_sha11\n+b8d03e173f67f6505a76f6e00cf93440200dd9be $commit_sha10\n+20c613c835011c48a5abe29170a2402ca6354910 $commit_sha9\n+a3daf8a1e4e5dc3409a303ad8481d57bfea7f5d6 $commit_sha8\n+897003322b53bc6ca098e9324ee508362347e734 $commit_sha7\n+11d97fdebfa5ceee540a3da07bce6fa0222bc082 $commit_sha6\n+EOF\n+\n+cat >expect_log_x <<EOF\n+$commit_sha15 15th\n+x notes on 15th commit\n+\n+$commit_sha14 14th\n+x notes on 14th commit\n+\n+$commit_sha13 13th\n+x notes on 13th commit\n+\n+$commit_sha12 12th\n+x notes on 12th commit\n+\n+$commit_sha11 11th\n+x notes on 11th commit\n+\n+$commit_sha10 10th\n+x notes on 10th commit\n+\n+$commit_sha9 9th\n+x notes on 9th commit\n+\n+$commit_sha8 8th\n+x notes on 8th commit\n+\n+$commit_sha7 7th\n+x notes on 7th commit\n+\n+$commit_sha6 6th\n+x notes on 6th commit\n+\n+$commit_sha5 5th\n+\n+$commit_sha4 4th\n+\n+$commit_sha3 3rd\n+\n+$commit_sha2 2nd\n+\n+$commit_sha1 1st\n+\n+EOF\n+\n+test_expect_success 'verify state of merge base (x)' 'verify_notes x x'\n+\n+test_expect_success 'setup local branch (y)' '\n+\tgit update-ref refs/notes/y refs/notes/x &&\n+\tgit config core.notesRef refs/notes/y &&\n+\tgit notes add -f -m \"y notes on 3rd commit\" 3rd &&\n+\tgit notes add -f -m \"y notes on 4th commit\" 4th &&\n+\tgit notes add -f -m \"y notes on 5th commit\" 5th &&\n+\tgit notes remove 6th &&\n+\tgit notes remove 7th &&\n+\tgit notes remove 8th &&\n+\tgit notes add -f -m \"y notes on 12th commit\" 12th &&\n+\tgit notes add -f -m \"y notes on 13th commit\" 13th &&\n+\tgit notes add -f -m \"y notes on 14th commit\" 14th &&\n+\tgit notes add -f -m \"y notes on 15th commit\" 15th\n+'\n+\n+cat <<EOF | sort >expect_notes_y\n+68b8630d25516028bed862719855b3d6768d7833 $commit_sha15\n+5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14\n+3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc $commit_sha13\n+a66055fa82f7a03fe0c02a6aba3287a85abf7c62 $commit_sha12\n+7abbc45126d680336fb24294f013a7cdfa3ed545 $commit_sha11\n+b8d03e173f67f6505a76f6e00cf93440200dd9be $commit_sha10\n+20c613c835011c48a5abe29170a2402ca6354910 $commit_sha9\n+154508c7a0bcad82b6fe4b472bc4c26b3bf0825b $commit_sha5\n+e2bfd06a37dd2031684a59a6e2b033e212239c78 $commit_sha4\n+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3\n+EOF\n+\n+cat >expect_log_y <<EOF\n+$commit_sha15 15th\n+y notes on 15th commit\n+\n+$commit_sha14 14th\n+y notes on 14th commit\n+\n+$commit_sha13 13th\n+y notes on 13th commit\n+\n+$commit_sha12 12th\n+y notes on 12th commit\n+\n+$commit_sha11 11th\n+x notes on 11th commit\n+\n+$commit_sha10 10th\n+x notes on 10th commit\n+\n+$commit_sha9 9th\n+x notes on 9th commit\n+\n+$commit_sha8 8th\n+\n+$commit_sha7 7th\n+\n+$commit_sha6 6th\n+\n+$commit_sha5 5th\n+y notes on 5th commit\n+\n+$commit_sha4 4th\n+y notes on 4th commit\n+\n+$commit_sha3 3rd\n+y notes on 3rd commit\n+\n+$commit_sha2 2nd\n+\n+$commit_sha1 1st\n+\n+EOF\n+\n+test_expect_success 'verify state of local branch (y)' 'verify_notes y y'\n+\n+test_expect_success 'setup remote branch (z)' '\n+\tgit update-ref refs/notes/z refs/notes/x &&\n+\tgit config core.notesRef refs/notes/z &&\n+\tgit notes add -f -m \"z notes on 2nd commit\" 2nd &&\n+\tgit notes add -f -m \"y notes on 4th commit\" 4th &&\n+\tgit notes add -f -m \"z notes on 5th commit\" 5th &&\n+\tgit notes remove 6th &&\n+\tgit notes add -f -m \"z notes on 8th commit\" 8th &&\n+\tgit notes remove 9th &&\n+\tgit notes add -f -m \"z notes on 11th commit\" 11th &&\n+\tgit notes remove 12th &&\n+\tgit notes add -f -m \"y notes on 14th commit\" 14th &&\n+\tgit notes add -f -m \"z notes on 15th commit\" 15th\n+'\n+\n+cat <<EOF | sort >expect_notes_z\n+9b4b2c61f0615412da3c10f98ff85b57c04ec765 $commit_sha15\n+5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14\n+5d30216a129eeffa97d9694ffe8c74317a560315 $commit_sha13\n+7e3c53503a3db8dd996cb62e37c66e070b44b54d $commit_sha11\n+b8d03e173f67f6505a76f6e00cf93440200dd9be $commit_sha10\n+851e1638784a884c7dd26c5d41f3340f6387413a $commit_sha8\n+897003322b53bc6ca098e9324ee508362347e734 $commit_sha7\n+99fc34adfc400b95c67b013115e37e31aa9a6d23 $commit_sha5\n+e2bfd06a37dd2031684a59a6e2b033e212239c78 $commit_sha4\n+283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2\n+EOF\n+\n+cat >expect_log_z <<EOF\n+$commit_sha15 15th\n+z notes on 15th commit\n+\n+$commit_sha14 14th\n+y notes on 14th commit\n+\n+$commit_sha13 13th\n+x notes on 13th commit\n+\n+$commit_sha12 12th\n+\n+$commit_sha11 11th\n+z notes on 11th commit\n+\n+$commit_sha10 10th\n+x notes on 10th commit\n+\n+$commit_sha9 9th\n+\n+$commit_sha8 8th\n+z notes on 8th commit\n+\n+$commit_sha7 7th\n+x notes on 7th commit\n+\n+$commit_sha6 6th\n+\n+$commit_sha5 5th\n+z notes on 5th commit\n+\n+$commit_sha4 4th\n+y notes on 4th commit\n+\n+$commit_sha3 3rd\n+\n+$commit_sha2 2nd\n+z notes on 2nd commit\n+\n+$commit_sha1 1st\n+\n+EOF\n+\n+test_expect_success 'verify state of remote branch (z)' 'verify_notes z z'\n+\n+# At this point, before merging z into y, we have the following status:\n+#\n+# commit | base/x  | local/y | remote/z | diff from x to y/z         | result\n+# -------|---------|---------|----------|----------------------------|-------\n+# 1st    | [none]  | [none]  | [none]   | unchanged / unchanged      | [none]\n+# 2nd    | [none]  | [none]  | 283b482  | unchanged / added          | 283b482\n+# 3rd    | [none]  | 5772f42 | [none]   | added     / unchanged      | 5772f42\n+# 4th    | [none]  | e2bfd06 | e2bfd06  | added     / added (same)   | e2bfd06\n+# 5th    | [none]  | 154508c | 99fc34a  | added     / added (diff)   | ???\n+# 6th    | 11d97fd | [none]  | [none]   | removed   / removed        | [none]\n+# 7th    | 8970033 | [none]  | 8970033  | removed   / unchanged      | [none]\n+# 8th    | a3daf8a | [none]  | 851e163  | removed   / changed        | ???\n+# 9th    | 20c613c | 20c613c | [none]   | unchanged / removed        | [none]\n+# 10th   | b8d03e1 | b8d03e1 | b8d03e1  | unchanged / unchanged      | b8d03e1\n+# 11th   | 7abbc45 | 7abbc45 | 7e3c535  | unchanged / changed        | 7e3c535\n+# 12th   | dd161bc | a66055f | [none]   | changed   / removed        | ???\n+# 13th   | 5d30216 | 3a631fd | 5d30216  | changed   / unchanged      | 3a631fd\n+# 14th   | b0c95b9 | 5de7ea7 | 5de7ea7  | changed   / changed (same) | 5de7ea7\n+# 15th   | 457a85d | 68b8630 | 9b4b2c6  | changed   / changed (diff) | ???\n+\n+test_expect_success 'merge z into y with invalid strategy => Fail/No changes' '\n+\tgit config core.notesRef refs/notes/y &&\n+\ttest_must_fail git notes merge --strategy=foo z &&\n+\t# Verify no changes (y)\n+\tverify_notes y y\n+'\n+\n+cat <<EOF | sort >expect_notes_ours\n+68b8630d25516028bed862719855b3d6768d7833 $commit_sha15\n+5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14\n+3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc $commit_sha13\n+a66055fa82f7a03fe0c02a6aba3287a85abf7c62 $commit_sha12\n+7e3c53503a3db8dd996cb62e37c66e070b44b54d $commit_sha11\n+b8d03e173f67f6505a76f6e00cf93440200dd9be $commit_sha10\n+154508c7a0bcad82b6fe4b472bc4c26b3bf0825b $commit_sha5\n+e2bfd06a37dd2031684a59a6e2b033e212239c78 $commit_sha4\n+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3\n+283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2\n+EOF\n+\n+cat >expect_log_ours <<EOF\n+$commit_sha15 15th\n+y notes on 15th commit\n+\n+$commit_sha14 14th\n+y notes on 14th commit\n+\n+$commit_sha13 13th\n+y notes on 13th commit\n+\n+$commit_sha12 12th\n+y notes on 12th commit\n+\n+$commit_sha11 11th\n+z notes on 11th commit\n+\n+$commit_sha10 10th\n+x notes on 10th commit\n+\n+$commit_sha9 9th\n+\n+$commit_sha8 8th\n+\n+$commit_sha7 7th\n+\n+$commit_sha6 6th\n+\n+$commit_sha5 5th\n+y notes on 5th commit\n+\n+$commit_sha4 4th\n+y notes on 4th commit\n+\n+$commit_sha3 3rd\n+y notes on 3rd commit\n+\n+$commit_sha2 2nd\n+z notes on 2nd commit\n+\n+$commit_sha1 1st\n+\n+EOF\n+\n+test_expect_success 'merge z into y with \"ours\" strategy => Non-conflicting 3-way merge' '\n+\tgit notes merge --strategy=ours z &&\n+\tverify_notes y ours\n+'\n+\n+test_expect_success 'reset to pre-merge state (y)' '\n+\tgit update-ref refs/notes/y refs/notes/y^1 &&\n+\t# Verify pre-merge state\n+\tverify_notes y y\n+'\n+\n+cat <<EOF | sort >expect_notes_theirs\n+9b4b2c61f0615412da3c10f98ff85b57c04ec765 $commit_sha15\n+5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14\n+3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc $commit_sha13\n+7e3c53503a3db8dd996cb62e37c66e070b44b54d $commit_sha11\n+b8d03e173f67f6505a76f6e00cf93440200dd9be $commit_sha10\n+851e1638784a884c7dd26c5d41f3340f6387413a $commit_sha8\n+99fc34adfc400b95c67b013115e37e31aa9a6d23 $commit_sha5\n+e2bfd06a37dd2031684a59a6e2b033e212239c78 $commit_sha4\n+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3\n+283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2\n+EOF\n+\n+cat >expect_log_theirs <<EOF\n+$commit_sha15 15th\n+z notes on 15th commit\n+\n+$commit_sha14 14th\n+y notes on 14th commit\n+\n+$commit_sha13 13th\n+y notes on 13th commit\n+\n+$commit_sha12 12th\n+\n+$commit_sha11 11th\n+z notes on 11th commit\n+\n+$commit_sha10 10th\n+x notes on 10th commit\n+\n+$commit_sha9 9th\n+\n+$commit_sha8 8th\n+z notes on 8th commit\n+\n+$commit_sha7 7th\n+\n+$commit_sha6 6th\n+\n+$commit_sha5 5th\n+z notes on 5th commit\n+\n+$commit_sha4 4th\n+y notes on 4th commit\n+\n+$commit_sha3 3rd\n+y notes on 3rd commit\n+\n+$commit_sha2 2nd\n+z notes on 2nd commit\n+\n+$commit_sha1 1st\n+\n+EOF\n+\n+test_expect_success 'merge z into y with \"theirs\" strategy => Non-conflicting 3-way merge' '\n+\tgit notes merge --strategy=theirs z &&\n+\tverify_notes y theirs\n+'\n+\n+test_expect_success 'reset to pre-merge state (y)' '\n+\tgit update-ref refs/notes/y refs/notes/y^1 &&\n+\t# Verify pre-merge state\n+\tverify_notes y y\n+'\n+\n+cat <<EOF | sort >expect_notes_union\n+7c4e546efd0fe939f876beb262ece02797880b54 $commit_sha15\n+5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14\n+3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc $commit_sha13\n+a66055fa82f7a03fe0c02a6aba3287a85abf7c62 $commit_sha12\n+7e3c53503a3db8dd996cb62e37c66e070b44b54d $commit_sha11\n+b8d03e173f67f6505a76f6e00cf93440200dd9be $commit_sha10\n+851e1638784a884c7dd26c5d41f3340f6387413a $commit_sha8\n+6c841cc36ea496027290967ca96bd2bef54dbb47 $commit_sha5\n+e2bfd06a37dd2031684a59a6e2b033e212239c78 $commit_sha4\n+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3\n+283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2\n+EOF\n+\n+cat >expect_log_union <<EOF\n+$commit_sha15 15th\n+y notes on 15th commit\n+\n+z notes on 15th commit\n+\n+$commit_sha14 14th\n+y notes on 14th commit\n+\n+$commit_sha13 13th\n+y notes on 13th commit\n+\n+$commit_sha12 12th\n+y notes on 12th commit\n+\n+$commit_sha11 11th\n+z notes on 11th commit\n+\n+$commit_sha10 10th\n+x notes on 10th commit\n+\n+$commit_sha9 9th\n+\n+$commit_sha8 8th\n+z notes on 8th commit\n+\n+$commit_sha7 7th\n+\n+$commit_sha6 6th\n+\n+$commit_sha5 5th\n+y notes on 5th commit\n+\n+z notes on 5th commit\n+\n+$commit_sha4 4th\n+y notes on 4th commit\n+\n+$commit_sha3 3rd\n+y notes on 3rd commit\n+\n+$commit_sha2 2nd\n+z notes on 2nd commit\n+\n+$commit_sha1 1st\n+\n+EOF\n+\n+test_expect_success 'merge z into y with \"union\" strategy => Non-conflicting 3-way merge' '\n+\tgit notes merge --strategy=union z &&\n+\tverify_notes y union\n+'\n+\n+test_done\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"152000","messageId":"1285719811-10871-13-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 12/18] Documentation: Preliminary docs on 'git notes merge'","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:25Z","receivedAt":"2010-09-29T00:23:25Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"This patch has been improved by the following contributions:\n- Stephen Boyd: Use \"automatically resolves\" instead of \"auto-resolves\"\n\nCc: Stephen Boyd <bebarino@gmail.com>\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n Documentation/git-notes.txt |   44 ++++++++++++++++++++++++++++++++++++++++++-\n 1 files changed, 43 insertions(+), 1 deletions(-)\n\ndiff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt\nindex 2981d8c..b9a061c 100644\n--- a/Documentation/git-notes.txt\n+++ b/Documentation/git-notes.txt\n@@ -14,6 +14,7 @@ SYNOPSIS\n 'git notes' append [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]\n 'git notes' edit [<object>]\n 'git notes' show [<object>]\n+'git notes' merge [-v | -q] [-s <strategy> ] <notes_ref>\n 'git notes' remove [<object>]\n 'git notes' prune [-n | -v]\n \n@@ -83,6 +84,16 @@ edit::\n show::\n \tShow the notes for a given object (defaults to HEAD).\n \n+merge::\n+\tMerge the given notes ref into the current notes ref.\n+\tThis will try to merge the changes made by the given\n+\tnotes ref (called \"remote\") since the merge-base (if\n+\tany) into the current notes ref (called \"local\").\n++\n+If conflicts arise (and a strategy for automatically resolving\n+conflicting notes (see the -s/--strategy option) is not given,\n+the merge fails (TODO).\n+\n remove::\n \tRemove the notes for a given object (defaults to HEAD).\n \tThis is equivalent to specifying an empty note message to\n@@ -133,9 +144,23 @@ OPTIONS\n \tDo not remove anything; just report the object names whose notes\n \twould be removed.\n \n+-s <strategy>::\n+--strategy=<strategy>::\n+\tWhen merging notes, resolve notes conflicts using the given\n+\tstrategy. The following strategies are recognized: \"manual\"\n+\t(default), \"ours\", \"theirs\" and \"union\".\n+\tSee the \"NOTES MERGE STRATEGIES\" section below for more\n+\tinformation on each notes merge strategy.\n+\n+-q::\n+--quiet::\n+\tWhen merging notes, operate quietly.\n+\n -v::\n --verbose::\n-\tReport all object names whose notes are removed.\n+\tWhen merging notes, be more verbose.\n+\tWhen pruning notes, report all object names whose notes are\n+\tremoved.\n \n \n DISCUSSION\n@@ -163,6 +188,23 @@ object, in which case the history of the notes can be read with\n `git log -p -g <refname>`.\n \n \n+NOTES MERGE STRATEGIES\n+----------------------\n+\n+The default notes merge strategy is \"manual\", which is not yet\n+implemented (TODO).\n+\n+\"ours\" automatically resolves conflicting notes in favor of the local\n+version (i.e. the current notes ref).\n+\n+\"theirs\" automatically resolves notes conflicts in favor of the remote\n+version (i.e. the given notes ref being merged into the current notes\n+ref).\n+\n+\"union\" automatically resolves notes conflicts by concatenating the\n+local and remote versions.\n+\n+\n EXAMPLES\n --------\n \n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151998","messageId":"1285719811-10871-14-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 13/18] git notes merge: Manual conflict resolution, part 1/2","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:26Z","receivedAt":"2010-09-29T00:23:26Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Conflicts (that are to be resolved manually) are written into a special-\npurpose working tree, located at .git/NOTES_MERGE_WORKTREE. Within this\ndirectory, conflicting notes entries are stored (with conflict markers\nproduced by ll_merge()) using the SHA1 of the annotated object. The\n.git/NOTES_MERGE_WORKTREE directory will only contain the _conflicting_\nnote entries. The non-conflicting note entries (aka. the partial merge\nresult) are stored in 'local_tree', and the SHA1 of the resulting commit\nis written to 'result_sha1'. The return value from notes_merge() is -1.\n\nThe user is told to edit the files within the .git/NOTES_MERGE_WORKTREE\ndirectory in order to resolve the conflicts.\n\nThe patch also contains documentation and testcases for the correct setup\nof .git/NOTES_MERGE_WORKTREE.\n\nThe next part will recombine the partial notes merge result with the\nresolved conflicts in .git/NOTES_MERGE_WORKTREE to produce the complete\nmerge result.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n Documentation/git-notes.txt           |   10 +-\n builtin/notes.c                       |    8 +-\n notes-merge.c                         |  166 ++++++++++++++++++-\n notes-merge.h                         |   11 +-\n t/t3310-notes-merge-manual-resolve.sh |  292 +++++++++++++++++++++++++++++++++\n 5 files changed, 474 insertions(+), 13 deletions(-)\n create mode 100755 t/t3310-notes-merge-manual-resolve.sh\n\ndiff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt\nindex b9a061c..12fda54 100644\n--- a/Documentation/git-notes.txt\n+++ b/Documentation/git-notes.txt\n@@ -92,7 +92,9 @@ merge::\n +\n If conflicts arise (and a strategy for automatically resolving\n conflicting notes (see the -s/--strategy option) is not given,\n-the merge fails (TODO).\n+the \"manual\" resolver is used. This resolver checks out the\n+conflicting notes in a special worktree (`.git/NOTES_MERGE_WORKTREE`),\n+and instructs the user to manually resolve the conflicts there.\n \n remove::\n \tRemove the notes for a given object (defaults to HEAD).\n@@ -191,8 +193,10 @@ object, in which case the history of the notes can be read with\n NOTES MERGE STRATEGIES\n ----------------------\n \n-The default notes merge strategy is \"manual\", which is not yet\n-implemented (TODO).\n+The default notes merge strategy is \"manual\", which checks out\n+conflicting notes in a special work tree for resolving notes conflicts\n+(`.git/NOTES_MERGE_WORKTREE`), and instructs the user to resolve the\n+conflicts in that work tree.\n \n \"ours\" automatically resolves conflicting notes in favor of the local\n version (i.e. the current notes ref).\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex 79290db..b1f3ae6 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -819,14 +819,14 @@ static int merge(int argc, const char **argv, const char *prefix)\n \t\t/* Update default notes ref with new commit */\n \t\tupdate_ref(msg.buf, default_notes_ref(), result_sha1, NULL,\n \t\t\t   0, DIE_ON_ERR);\n-\telse\n-\t\t/* TODO: */\n-\t\tdie(\"'git notes merge' cannot yet handle conflicts!\");\n+\telse /* Merge has unresolved conflicts */\n+\t\tprintf(\"Automatic notes merge failed. Fix conflicts in %s.\\n\",\n+\t\t       git_path(NOTES_MERGE_WORKTREE));\n \n \tfree_notes(t);\n \tstrbuf_release(&remote_ref);\n \tstrbuf_release(&msg);\n-\treturn 0;\n+\treturn result < 0; /* return non-zero on conflicts */\n }\n \n static int remove_cmd(int argc, const char **argv, const char *prefix)\ndiff --git a/notes-merge.c b/notes-merge.c\nindex 6fa59d8..8efa003 100644\n--- a/notes-merge.c\n+++ b/notes-merge.c\n@@ -2,6 +2,9 @@\n #include \"commit.h\"\n #include \"diff.h\"\n #include \"diffcore.h\"\n+#include \"xdiff-interface.h\"\n+#include \"ll-merge.h\"\n+#include \"dir.h\"\n #include \"notes.h\"\n #include \"notes-merge.h\"\n \n@@ -262,16 +265,169 @@ static void diff_tree_local(struct notes_merge_options *o,\n \tdiff_tree_release_paths(&opt);\n }\n \n+static void check_notes_merge_worktree(struct notes_merge_options *o)\n+{\n+\tif (!o->has_worktree) {\n+\t\t/*\n+\t\t * Must establish NOTES_MERGE_WORKTREE.\n+\t\t * Abort if NOTES_MERGE_WORKTREE already exists\n+\t\t */\n+\t\tif (file_exists(git_path(NOTES_MERGE_WORKTREE))) {\n+\t\t\tif (advice_resolve_conflict)\n+\t\t\t\tdie(\"You have not concluded your previous \"\n+\t\t\t\t    \"notes merge (%s exists).\\nPlease, use \"\n+\t\t\t\t    \"'git notes merge --commit' or 'git notes \"\n+\t\t\t\t    \"merge --reset' to commit/abort the \"\n+\t\t\t\t    \"previous merge before you start a new \"\n+\t\t\t\t    \"notes merge.\", git_path(\"NOTES_MERGE_*\"));\n+\t\t\telse\n+\t\t\t\tdie(\"You have not concluded your notes merge \"\n+\t\t\t\t    \"(%s exists).\", git_path(\"NOTES_MERGE_*\"));\n+\t\t}\n+\n+\t\tif (safe_create_leading_directories(git_path(\n+\t\t\t\tNOTES_MERGE_WORKTREE \"/.test\")))\n+\t\t\tdie_errno(\"unable to create directory %s\",\n+\t\t\t\t  git_path(NOTES_MERGE_WORKTREE));\n+\t\to->has_worktree = 1;\n+\t} else if (!file_exists(git_path(NOTES_MERGE_WORKTREE)))\n+\t\t/* NOTES_MERGE_WORKTREE should already be established */\n+\t\tdie(\"missing '%s'. This should not happen\",\n+\t\t    git_path(NOTES_MERGE_WORKTREE));\n+}\n+\n+static void write_buf_to_worktree(const unsigned char *obj,\n+\t\t\t\t  void *buf, unsigned long size)\n+{\n+\tint fd;\n+\tchar *path = git_path(NOTES_MERGE_WORKTREE \"/%s\", sha1_to_hex(obj));\n+\tif (safe_create_leading_directories(path))\n+\t\tdie_errno(\"unable to create directory for '%s'\", path);\n+\tif (file_exists(path))\n+\t\tdie(\"found existing file at '%s'\", path);\n+\n+\tfd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0666);\n+\tif (fd < 0)\n+\t\tdie_errno(\"failed to open '%s'\", path);\n+\n+\twhile (size > 0) {\n+\t\tlong ret = write_in_full(fd, buf, size);\n+\t\tif (ret < 0) {\n+\t\t\t/* Ignore epipe */\n+\t\t\tif (errno == EPIPE)\n+\t\t\t\tbreak;\n+\t\t\tdie_errno(\"notes-merge\");\n+\t\t} else if (!ret) {\n+\t\t\tdie(\"notes-merge: disk full?\");\n+\t\t}\n+\t\tsize -= ret;\n+\t\tbuf += ret;\n+\t}\n+\n+\tclose(fd);\n+}\n+\n+static void write_note_to_worktree(const unsigned char *obj,\n+\t\t\t\t   const unsigned char *note)\n+{\n+\tenum object_type type;\n+\tunsigned long size;\n+\tvoid *buf = read_sha1_file(note, &type, &size);\n+\n+\tif (!buf)\n+\t\tdie(\"cannot read note %s for object %s\",\n+\t\t    sha1_to_hex(note), sha1_to_hex(obj));\n+\tif (type != OBJ_BLOB)\n+\t\tdie(\"blob expected in note %s for object %s\",\n+\t\t    sha1_to_hex(note), sha1_to_hex(obj));\n+\twrite_buf_to_worktree(obj, buf, size);\n+\tfree(buf);\n+}\n+\n+static int ll_merge_in_worktree(struct notes_merge_options *o,\n+\t\t\t\tstruct notes_merge_pair *p)\n+{\n+\tmmbuffer_t result_buf;\n+\tmmfile_t base, local, remote;\n+\tint status;\n+\n+\tread_mmblob(&base, p->base);\n+\tread_mmblob(&local, p->local);\n+\tread_mmblob(&remote, p->remote);\n+\n+\tstatus = ll_merge(&result_buf, sha1_to_hex(p->obj), &base, NULL,\n+\t\t\t  &local, o->local_ref, &remote, o->remote_ref, 0);\n+\n+\tfree(base.ptr);\n+\tfree(local.ptr);\n+\tfree(remote.ptr);\n+\n+\tif ((status < 0) || !result_buf.ptr)\n+\t\tdie(\"Failed to execute internal merge\");\n+\n+\twrite_buf_to_worktree(p->obj, result_buf.ptr, result_buf.size);\n+\tfree(result_buf.ptr);\n+\n+\treturn status;\n+}\n+\n+static int merge_one_change_manual(struct notes_merge_options *o,\n+\t\t\t\t   struct notes_merge_pair *p,\n+\t\t\t\t   struct notes_tree *t)\n+{\n+\tconst char *lref = o->local_ref ? o->local_ref : \"local version\";\n+\tconst char *rref = o->remote_ref ? o->remote_ref : \"remote version\";\n+\n+\tOUTPUT(o, 5, \"\\t\\t\\tmerge_one_change_manual(obj = %.7s, base = %.7s, \"\n+\t       \"local = %.7s, remote = %.7s)\",\n+\t       sha1_to_hex(p->obj), sha1_to_hex(p->base),\n+\t       sha1_to_hex(p->local), sha1_to_hex(p->remote));\n+\n+\tOUTPUT(o, 2, \"Auto-merging notes for %s\", sha1_to_hex(p->obj));\n+\tcheck_notes_merge_worktree(o);\n+\tif (is_null_sha1(p->local)) {\n+\t\t/* D/F conflict, checkout p->remote */\n+\t\tassert(!is_null_sha1(p->remote));\n+\t\tOUTPUT(o, 1, \"CONFLICT (delete/modify): Notes for object %s \"\n+\t\t       \"deleted in %s and modified in %s. Version from %s \"\n+\t\t       \"left in tree.\", sha1_to_hex(p->obj), lref, rref, rref);\n+\t\twrite_note_to_worktree(p->obj, p->remote);\n+\t} else if (is_null_sha1(p->remote)) {\n+\t\t/* D/F conflict, checkout p->local */\n+\t\tassert(!is_null_sha1(p->local));\n+\t\tOUTPUT(o, 1, \"CONFLICT (delete/modify): Notes for object %s \"\n+\t\t       \"deleted in %s and modified in %s. Version from %s \"\n+\t\t       \"left in tree.\", sha1_to_hex(p->obj), rref, lref, lref);\n+\t\twrite_note_to_worktree(p->obj, p->local);\n+\t} else {\n+\t\t/* \"regular\" conflict, checkout result of ll_merge() */\n+\t\tconst char *reason = \"content\";\n+\t\tif (is_null_sha1(p->base))\n+\t\t\treason = \"add/add\";\n+\t\tassert(!is_null_sha1(p->local));\n+\t\tassert(!is_null_sha1(p->remote));\n+\t\tOUTPUT(o, 1, \"CONFLICT (%s): Merge conflict in notes for \"\n+\t\t       \"object %s\", reason, sha1_to_hex(p->obj));\n+\t\tll_merge_in_worktree(o, p);\n+\t}\n+\n+\tOUTPUT(o, 5, \"\\t\\t\\tremoving from partial merge result\");\n+\tremove_note(t, p->obj);\n+\n+\treturn 1;\n+}\n+\n static int merge_one_change(struct notes_merge_options *o,\n \t\t\t    struct notes_merge_pair *p, struct notes_tree *t)\n {\n \t/*\n-\t * Return 0 if change was resolved (and added to notes_tree),\n-\t * 1 if conflict\n+\t * Return 0 if change is successfully resolved (stored in notes_tree).\n+\t * Return 1 is change results in a conflict (NOT stored in notes_tree,\n+\t * but instead written to NOTES_MERGE_WORKTREE with conflict markers).\n \t */\n \tswitch (o->strategy) {\n \tcase NOTES_MERGE_RESOLVE_MANUAL:\n-\t\treturn 1;\n+\t\treturn merge_one_change_manual(o, p, t);\n \tcase NOTES_MERGE_RESOLVE_OURS:\n \t\tOUTPUT(o, 2, \"Using local notes for %s\", sha1_to_hex(p->obj));\n \t\t/* nothing to do */\n@@ -461,8 +617,8 @@ int notes_merge(struct notes_merge_options *o,\n \tresult = merge_from_diffs(o, base_tree_sha1, local->tree->object.sha1,\n \t\t\t\t  remote->tree->object.sha1, local_tree);\n \n-\tif (result > 0) { /* successful non-trivial merge */\n-\t\t/* Commit result */\n+\tif (result != 0) { /* non-trivial merge (with or without conflicts) */\n+\t\t/* Commit (partial) result */\n \t\tstruct commit_list *parents = NULL;\n \t\tcommit_list_insert(remote, &parents); /* LIFO order */\n \t\tcommit_list_insert(local, &parents);\ndiff --git a/notes-merge.h b/notes-merge.h\nindex e392049..e9a8ce6 100644\n--- a/notes-merge.h\n+++ b/notes-merge.h\n@@ -1,6 +1,8 @@\n #ifndef NOTES_MERGE_H\n #define NOTES_MERGE_H\n \n+#define NOTES_MERGE_WORKTREE \"NOTES_MERGE_WORKTREE\"\n+\n struct notes_merge_options {\n \tconst char *local_ref;\n \tconst char *remote_ref;\n@@ -12,6 +14,7 @@ struct notes_merge_options {\n \t\tNOTES_MERGE_RESOLVE_THEIRS,\n \t\tNOTES_MERGE_RESOLVE_UNION\n \t} strategy;\n+\tunsigned has_worktree:1;\n };\n \n void init_notes_merge_options(struct notes_merge_options *o);\n@@ -46,7 +49,13 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,\n  * 2. The merge successfully completes, producing a merge commit. local_tree\n  *    contains the updated notes tree, the SHA1 of the resulting commit is\n  *    written into 'result_sha1', and 1 is returned.\n- * 3. The merge fails. result_sha1 is set to null_sha1, and -1 is returned.\n+ * 3. The merge results in conflicts. This is similar to #2 in that the\n+ *    partial merge result (i.e. merge result minus the unmerged entries)\n+ *    are stored in 'local_tree', and the SHA1 or the resulting commit\n+ *    (to be amended when the conflicts have been resolved) is written into\n+ *    'result_sha1'. The unmerged entries are written into the\n+ *    .git/NOTES_MERGE_WORKTREE directory with conflict markers.\n+ *    -1 is returned.\n  *\n  * Either ref (but not both) may not exist in which case the missing ref is\n  * interpreted as an empty notes tree, and the merge trivially results in\ndiff --git a/t/t3310-notes-merge-manual-resolve.sh b/t/t3310-notes-merge-manual-resolve.sh\nnew file mode 100755\nindex 0000000..eadb6b7\n--- /dev/null\n+++ b/t/t3310-notes-merge-manual-resolve.sh\n@@ -0,0 +1,292 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2010 Johan Herland\n+#\n+\n+test_description='Test notes merging with manual conflict resolution'\n+\n+. ./test-lib.sh\n+\n+# Set up a notes merge scenario with different kinds of conflicts\n+test_expect_success 'setup commits' '\n+\ttest_commit 1st &&\n+\ttest_commit 2nd &&\n+\ttest_commit 3rd &&\n+\ttest_commit 4th &&\n+\ttest_commit 5th\n+'\n+\n+commit_sha1=$(git rev-parse 1st^{commit})\n+commit_sha2=$(git rev-parse 2nd^{commit})\n+commit_sha3=$(git rev-parse 3rd^{commit})\n+commit_sha4=$(git rev-parse 4th^{commit})\n+commit_sha5=$(git rev-parse 5th^{commit})\n+\n+verify_notes () {\n+\tnotes_ref=\"$1\"\n+\tgit -c core.notesRef=\"refs/notes/$notes_ref\" notes |\n+\t\tsort >\"output_notes_$notes_ref\" &&\n+\ttest_cmp \"expect_notes_$notes_ref\" \"output_notes_$notes_ref\" &&\n+\tgit -c core.notesRef=\"refs/notes/$notes_ref\" log --format=\"%H %s%n%N\" \\\n+\t\t>\"output_log_$notes_ref\" &&\n+\ttest_cmp \"expect_log_$notes_ref\" \"output_log_$notes_ref\"\n+}\n+\n+cat <<EOF | sort >expect_notes_x\n+6e8e3febca3c2bb896704335cc4d0c34cb2f8715 $commit_sha4\n+e5388c10860456ee60673025345fe2e153eb8cf8 $commit_sha3\n+ceefa674873670e7ecd131814d909723cce2b669 $commit_sha2\n+EOF\n+\n+cat >expect_log_x <<EOF\n+$commit_sha5 5th\n+\n+$commit_sha4 4th\n+x notes on 4th commit\n+\n+$commit_sha3 3rd\n+x notes on 3rd commit\n+\n+$commit_sha2 2nd\n+x notes on 2nd commit\n+\n+$commit_sha1 1st\n+\n+EOF\n+\n+test_expect_success 'setup merge base (x)' '\n+\tgit config core.notesRef refs/notes/x &&\n+\tgit notes add -m \"x notes on 2nd commit\" 2nd &&\n+\tgit notes add -m \"x notes on 3rd commit\" 3rd &&\n+\tgit notes add -m \"x notes on 4th commit\" 4th &&\n+\tverify_notes x\n+'\n+\n+cat <<EOF | sort >expect_notes_y\n+e2bfd06a37dd2031684a59a6e2b033e212239c78 $commit_sha4\n+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3\n+b0a6021ec006d07e80e9b20ec9b444cbd9d560d3 $commit_sha1\n+EOF\n+\n+cat >expect_log_y <<EOF\n+$commit_sha5 5th\n+\n+$commit_sha4 4th\n+y notes on 4th commit\n+\n+$commit_sha3 3rd\n+y notes on 3rd commit\n+\n+$commit_sha2 2nd\n+\n+$commit_sha1 1st\n+y notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'setup local branch (y)' '\n+\tgit update-ref refs/notes/y refs/notes/x &&\n+\tgit config core.notesRef refs/notes/y &&\n+\tgit notes add -f -m \"y notes on 1st commit\" 1st &&\n+\tgit notes remove 2nd &&\n+\tgit notes add -f -m \"y notes on 3rd commit\" 3rd &&\n+\tgit notes add -f -m \"y notes on 4th commit\" 4th &&\n+\tverify_notes y\n+'\n+\n+cat <<EOF | sort >expect_notes_z\n+cff59c793c20bb49a4e01bc06fb06bad642e0d54 $commit_sha4\n+283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2\n+0a81da8956346e19bcb27a906f04af327e03e31b $commit_sha1\n+EOF\n+\n+cat >expect_log_z <<EOF\n+$commit_sha5 5th\n+\n+$commit_sha4 4th\n+z notes on 4th commit\n+\n+$commit_sha3 3rd\n+\n+$commit_sha2 2nd\n+z notes on 2nd commit\n+\n+$commit_sha1 1st\n+z notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'setup remote branch (z)' '\n+\tgit update-ref refs/notes/z refs/notes/x &&\n+\tgit config core.notesRef refs/notes/z &&\n+\tgit notes add -f -m \"z notes on 1st commit\" 1st &&\n+\tgit notes add -f -m \"z notes on 2nd commit\" 2nd &&\n+\tgit notes remove 3rd &&\n+\tgit notes add -f -m \"z notes on 4th commit\" 4th &&\n+\tverify_notes z\n+'\n+\n+# At this point, before merging z into y, we have the following status:\n+#\n+# commit | base/x  | local/y | remote/z | diff from x to y/z\n+# -------|---------|---------|----------|---------------------------\n+# 1st    | [none]  | b0a6021 | 0a81da8  | added     / added (diff)\n+# 2nd    | ceefa67 | [none]  | 283b482  | removed   / changed\n+# 3rd    | e5388c1 | 5772f42 | [none]   | changed   / removed\n+# 4th    | 6e8e3fe | e2bfd06 | cff59c7  | changed   / changed (diff)\n+# 5th    | [none]  | [none]  | [none]   | [none]\n+\n+cat <<EOF | sort >expect_conflicts\n+$commit_sha1\n+$commit_sha2\n+$commit_sha3\n+$commit_sha4\n+EOF\n+\n+cat >expect_conflict_$commit_sha1 <<EOF\n+<<<<<<< refs/notes/m\n+y notes on 1st commit\n+=======\n+z notes on 1st commit\n+>>>>>>> refs/notes/z\n+EOF\n+\n+cat >expect_conflict_$commit_sha2 <<EOF\n+z notes on 2nd commit\n+EOF\n+\n+cat >expect_conflict_$commit_sha3 <<EOF\n+y notes on 3rd commit\n+EOF\n+\n+cat >expect_conflict_$commit_sha4 <<EOF\n+<<<<<<< refs/notes/m\n+y notes on 4th commit\n+=======\n+z notes on 4th commit\n+>>>>>>> refs/notes/z\n+EOF\n+\n+cp expect_notes_y expect_notes_m\n+cp expect_log_y expect_log_m\n+\n+git rev-parse refs/notes/y > pre_merge_y\n+\n+test_expect_success 'merge z into m (== y) with default (\"manual\") resolver => Conflicting 3-way merge' '\n+\tgit update-ref refs/notes/m refs/notes/y &&\n+\tgit config core.notesRef refs/notes/m &&\n+\ttest_must_fail git notes merge z >output &&\n+\t# Output should point to where to resolve conflicts\n+\tgrep -q \"\\\\.git/NOTES_MERGE_WORKTREE\" output &&\n+\t# Inspect merge conflicts\n+\tls .git/NOTES_MERGE_WORKTREE >output_conflicts &&\n+\ttest_cmp expect_conflicts output_conflicts &&\n+\t( for f in $(cat expect_conflicts); do\n+\t\ttest_cmp \"expect_conflict_$f\" \".git/NOTES_MERGE_WORKTREE/$f\" ||\n+\t\texit 1\n+\tdone ) &&\n+\t# Verify that current notes tree (pre-merge) has not changed (m == y)\n+\tverify_notes y &&\n+\tverify_notes m &&\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(cat pre_merge_y)\"\n+'\n+\n+cat <<EOF | sort >expect_notes_z\n+00494adecf2d9635a02fa431308d67993f853968 $commit_sha4\n+283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2\n+0a81da8956346e19bcb27a906f04af327e03e31b $commit_sha1\n+EOF\n+\n+cat >expect_log_z <<EOF\n+$commit_sha5 5th\n+\n+$commit_sha4 4th\n+z notes on 4th commit\n+\n+More z notes on 4th commit\n+\n+$commit_sha3 3rd\n+\n+$commit_sha2 2nd\n+z notes on 2nd commit\n+\n+$commit_sha1 1st\n+z notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'change notes in z' '\n+\tgit notes --ref z append -m \"More z notes on 4th commit\" 4th &&\n+\tverify_notes z\n+'\n+\n+test_expect_success 'cannot do merge w/conflicts when previous merge is unfinished' '\n+\ttest -d .git/NOTES_MERGE_WORKTREE &&\n+\ttest_must_fail git notes merge z >output 2>&1 &&\n+\t# Output should indicate what is wrong\n+\tgrep -q \"\\\\.git/NOTES_MERGE_\\\\* exists\" output\n+'\n+\n+# Setup non-conflicting merge between x and new notes ref w\n+\n+cat <<EOF | sort >expect_notes_w\n+ceefa674873670e7ecd131814d909723cce2b669 $commit_sha2\n+f75d1df88cbfe4258d49852f26cfc83f2ad4494b $commit_sha1\n+EOF\n+\n+cat >expect_log_w <<EOF\n+$commit_sha5 5th\n+\n+$commit_sha4 4th\n+\n+$commit_sha3 3rd\n+\n+$commit_sha2 2nd\n+x notes on 2nd commit\n+\n+$commit_sha1 1st\n+w notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'setup unrelated notes ref (w)' '\n+\tgit config core.notesRef refs/notes/w &&\n+\tgit notes add -m \"w notes on 1st commit\" 1st &&\n+\tgit notes add -m \"x notes on 2nd commit\" 2nd &&\n+\tverify_notes w\n+'\n+\n+cat <<EOF | sort >expect_notes_w\n+6e8e3febca3c2bb896704335cc4d0c34cb2f8715 $commit_sha4\n+e5388c10860456ee60673025345fe2e153eb8cf8 $commit_sha3\n+ceefa674873670e7ecd131814d909723cce2b669 $commit_sha2\n+f75d1df88cbfe4258d49852f26cfc83f2ad4494b $commit_sha1\n+EOF\n+\n+cat >expect_log_w <<EOF\n+$commit_sha5 5th\n+\n+$commit_sha4 4th\n+x notes on 4th commit\n+\n+$commit_sha3 3rd\n+x notes on 3rd commit\n+\n+$commit_sha2 2nd\n+x notes on 2nd commit\n+\n+$commit_sha1 1st\n+w notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'can do merge without conflicts even if previous merge is unfinished (x => w)' '\n+\ttest -d .git/NOTES_MERGE_WORKTREE &&\n+\tgit notes merge x &&\n+\tverify_notes w &&\n+\t# Verify that other notes refs has not changed (x and y)\n+\tverify_notes x &&\n+\tverify_notes y\n+'\n+\n+test_done\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151993","messageId":"1285719811-10871-15-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 14/18] git notes merge: Manual conflict resolution, part 2/2","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:27Z","receivedAt":"2010-09-29T00:23:27Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"When the notes merge conflicts in .git/NOTES_MERGE_WORKTREE have been\nresolved, we need to record a new notes commit on the appropriate notes\nref with the resolved notes.\n\nThis patch implements 'git notes merge --commit' which the user should\nrun after resolving conflicts in the notes merge worktree. This command\nfinalizes the notes merge by recombining the partial notes tree from\npart 1 with the now-resolved conflicts in the notes merge worktree in a\nmerge commit, and updating the appropriate ref to this merge commit.\n\nIn order to correctly finalize the merge, we need to keep track of three\nthings:\n\n- The partial merge result from part 1, containing the auto-merged notes.\n  This is now stored into a ref called .git/NOTES_MERGE_PARTIAL.\n- The unmerged notes. These are already stored in\n  .git/NOTES_MERGE_WORKTREE, thanks to part 1.\n- The notes ref to be updated by the finalized merge result. This is now\n  stored in a symref called .git/NOTES_MERGE_REF.\n\nIn addition to \"git notes merge --commit\", which uses the above details\nto create the finalized notes merge commit, this patch also implements\n\"git notes merge --reset\", which aborts the ongoing notes merge by simply\nremoving the files/directory described above.\n\nFTR, \"git notes merge --commit\" reuses \"git notes merge --reset\" to remove\nthe information described above (.git/NOTES_MERGE_*) after the notes merge\nhave been successfully finalized.\n\nThe patch also contains documentation and testcases for the two new options.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n Documentation/git-notes.txt           |   23 +++++\n builtin/notes.c                       |  107 +++++++++++++++++++-\n notes-merge.c                         |   69 +++++++++++++\n notes-merge.h                         |   23 +++++\n t/t3310-notes-merge-manual-resolve.sh |  176 +++++++++++++++++++++++++++++++++\n 5 files changed, 395 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt\nindex 12fda54..ede7755 100644\n--- a/Documentation/git-notes.txt\n+++ b/Documentation/git-notes.txt\n@@ -15,6 +15,8 @@ SYNOPSIS\n 'git notes' edit [<object>]\n 'git notes' show [<object>]\n 'git notes' merge [-v | -q] [-s <strategy> ] <notes_ref>\n+'git notes' merge --commit [-v | -q]\n+'git notes' merge --reset [-v | -q]\n 'git notes' remove [<object>]\n 'git notes' prune [-n | -v]\n \n@@ -95,6 +97,9 @@ conflicting notes (see the -s/--strategy option) is not given,\n the \"manual\" resolver is used. This resolver checks out the\n conflicting notes in a special worktree (`.git/NOTES_MERGE_WORKTREE`),\n and instructs the user to manually resolve the conflicts there.\n+When done, the user can either finalize the merge with\n+'git notes merge --commit', or abort the merge with\n+'git notes merge --reset'.\n \n remove::\n \tRemove the notes for a given object (defaults to HEAD).\n@@ -154,6 +159,21 @@ OPTIONS\n \tSee the \"NOTES MERGE STRATEGIES\" section below for more\n \tinformation on each notes merge strategy.\n \n+--commit::\n+\tFinalize an in-progress 'git notes merge', i.e. a notes merge with\n+\tconflicts. When 'git notes merge' encounters conflicts, it s\n+\twhich returned one or more conflicts (that you have now resolved)\n+\tin .git/NOTES_MERGE_WORKTREE. This amends the partial merge commit\n+\tcreated by 'git notes merge' (stored in .git/NOTES_MERGE_PARTIAL)\n+\tby adding the notes in .git/NOTES_MERGE_WORKTREE. The notes ref\n+\tstored in the .git/NOTES_MERGE_REF symref is updated to the\n+\tresulting commit.\n+\n+--reset::\n+\tReset/abort a in-progress 'git notes merge', i.e. a notes merge\n+\twith conflicts. This simply removes all files related to the\n+\tnotes merge.\n+\n -q::\n --quiet::\n \tWhen merging notes, operate quietly.\n@@ -197,6 +217,9 @@ The default notes merge strategy is \"manual\", which checks out\n conflicting notes in a special work tree for resolving notes conflicts\n (`.git/NOTES_MERGE_WORKTREE`), and instructs the user to resolve the\n conflicts in that work tree.\n+When done, the user can either finalize the merge with\n+'git notes merge --commit', or abort the merge with\n+'git notes merge --reset'.\n \n \"ours\" automatically resolves conflicting notes in favor of the local\n version (i.e. the current notes ref).\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex b1f3ae6..8be7d9e 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -27,6 +27,8 @@ static const char * const git_notes_usage[] = {\n \t\"git notes [--ref <notes_ref>] edit [<object>]\",\n \t\"git notes [--ref <notes_ref>] show [<object>]\",\n \t\"git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>\",\n+\t\"git notes merge --commit [-v | -q]\",\n+\t\"git notes merge --reset [-v | -q]\",\n \t\"git notes [--ref <notes_ref>] remove [<object>]\",\n \t\"git notes [--ref <notes_ref>] prune [-n | -v]\",\n \tNULL\n@@ -65,6 +67,8 @@ static const char * const git_notes_show_usage[] = {\n \n static const char * const git_notes_merge_usage[] = {\n \t\"git notes merge [<options>] <notes_ref>\",\n+\t\"git notes merge --commit [<options>]\",\n+\t\"git notes merge --reset [<options>]\",\n \tNULL\n };\n \n@@ -761,32 +765,119 @@ static int show(int argc, const char **argv, const char *prefix)\n \treturn retval;\n }\n \n+static int merge_reset(struct notes_merge_options *o)\n+{\n+\tint ret = 0;\n+\n+\t/*\n+\t * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call\n+\t * notes_merge_reset() to remove .git/NOTES_MERGE_WORKTREE.\n+\t */\n+\n+\tif (delete_ref(\"NOTES_MERGE_PARTIAL\", NULL, 0))\n+\t\tret += error(\"Failed to delete ref NOTES_MERGE_PARTIAL\");\n+\tif (delete_ref(\"NOTES_MERGE_REF\", NULL, REF_NODEREF))\n+\t\tret += error(\"Failed to delete ref NOTES_MERGE_REF\");\n+\tif (notes_merge_reset(o))\n+\t\tret += error(\"Failed to remove 'git notes merge' worktree\");\n+\treturn ret;\n+}\n+\n+static int merge_commit(struct notes_merge_options *o)\n+{\n+\tstruct strbuf msg = STRBUF_INIT;\n+\tunsigned char sha1[20];\n+\tstruct notes_tree *t;\n+\tstruct commit *partial;\n+\tstruct pretty_print_context pretty_ctx;\n+\n+\t/*\n+\t * Read partial merge result from .git/NOTES_MERGE_PARTIAL,\n+\t * and target notes ref from .git/NOTES_MERGE_REF.\n+\t */\n+\n+\tif (get_sha1(\"NOTES_MERGE_PARTIAL\", sha1))\n+\t\tdie(\"Failed to read ref NOTES_MERGE_PARTIAL\");\n+\telse if (!(partial = lookup_commit_reference(sha1)))\n+\t\tdie(\"Could not find commit from NOTES_MERGE_PARTIAL.\");\n+\telse if (parse_commit(partial))\n+\t\tdie(\"Could not parse commit from NOTES_MERGE_PARTIAL.\");\n+\n+\tt = xcalloc(1, sizeof(struct notes_tree));\n+\tinit_notes(t, \"NOTES_MERGE_PARTIAL\", combine_notes_overwrite, 0);\n+\n+\to->local_ref = resolve_ref(\"NOTES_MERGE_REF\", sha1, 0, 0);\n+\tif (!o->local_ref)\n+\t\tdie(\"Failed to resolve NOTES_MERGE_REF\");\n+\n+\tif (notes_merge_commit(o, t, partial, sha1))\n+\t\tdie(\"Failed to finalize notes merge\");\n+\n+\t/* Reuse existing commit message in reflog message */\n+\tmemset(&pretty_ctx, 0, sizeof(pretty_ctx));\n+\tformat_commit_message(partial, \"%s\", &msg, &pretty_ctx);\n+\tstrbuf_trim(&msg);\n+\tstrbuf_insert(&msg, 0, \"notes: \", 7);\n+\tupdate_ref(msg.buf, o->local_ref, sha1, NULL, 0, DIE_ON_ERR);\n+\n+\tfree_notes(t);\n+\tstrbuf_release(&msg);\n+\treturn merge_reset(o);\n+}\n+\n static int merge(int argc, const char **argv, const char *prefix)\n {\n \tstruct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;\n \tunsigned char result_sha1[20];\n \tstruct notes_tree *t;\n \tstruct notes_merge_options o;\n+\tint do_merge = 0, do_commit = 0, do_reset = 0;\n \tint verbosity = 0, result;\n \tconst char *strategy = NULL;\n \tstruct option options[] = {\n+\t\tOPT_GROUP(\"General options\"),\n \t\tOPT__VERBOSITY(&verbosity),\n+\t\tOPT_GROUP(\"Merge options\"),\n \t\tOPT_STRING('s', \"strategy\", &strategy, \"strategy\",\n \t\t\t   \"resolve notes conflicts using the given \"\n \t\t\t   \"strategy (manual/ours/theirs/union)\"),\n+\t\tOPT_GROUP(\"Committing unmerged notes\"),\n+\t\t{ OPTION_BOOLEAN, 0, \"commit\", &do_commit, NULL,\n+\t\t\t\"finalize notes merge by committing unmerged notes\",\n+\t\t\tPARSE_OPT_NOARG | PARSE_OPT_NONEG },\n+\t\tOPT_GROUP(\"Aborting notes merge resolution\"),\n+\t\t{ OPTION_BOOLEAN, 0, \"reset\", &do_reset, NULL,\n+\t\t\t\"abort notes merge\",\n+\t\t\tPARSE_OPT_NOARG | PARSE_OPT_NONEG },\n \t\tOPT_END()\n \t};\n \n \targc = parse_options(argc, argv, prefix, options,\n \t\t\t     git_notes_merge_usage, 0);\n \n-\tif (argc != 1) {\n+\tif (strategy || do_commit + do_reset == 0)\n+\t\tdo_merge = 1;\n+\tif (do_commit + do_reset + do_merge != 1) {\n+\t\terror(\"cannot mix --commit, --reset or -s/--strategy\");\n+\t\tusage_with_options(git_notes_merge_usage, options);\n+\t}\n+\n+\tif (do_merge && argc != 1) {\n \t\terror(\"Must specify a notes ref to merge\");\n \t\tusage_with_options(git_notes_merge_usage, options);\n+\t} else if (!do_merge && argc) {\n+\t\terror(\"too many parameters\");\n+\t\tusage_with_options(git_notes_merge_usage, options);\n \t}\n \n \tinit_notes_merge_options(&o);\n \to.verbosity = verbosity + 2; // default verbosity level is 2\n+\n+\tif (do_reset)\n+\t\treturn merge_reset(&o);\n+\tif (do_commit)\n+\t\treturn merge_commit(&o);\n+\n \to.local_ref = default_notes_ref();\n \tstrbuf_addstr(&remote_ref, argv[0]);\n \texpand_notes_ref(&remote_ref);\n@@ -819,9 +910,19 @@ static int merge(int argc, const char **argv, const char *prefix)\n \t\t/* Update default notes ref with new commit */\n \t\tupdate_ref(msg.buf, default_notes_ref(), result_sha1, NULL,\n \t\t\t   0, DIE_ON_ERR);\n-\telse /* Merge has unresolved conflicts */\n-\t\tprintf(\"Automatic notes merge failed. Fix conflicts in %s.\\n\",\n+\telse { /* Merge has unresolved conflicts */\n+\t\t/* Update .git/NOTES_MERGE_PARTIAL with partial merge result */\n+\t\tupdate_ref(msg.buf, \"NOTES_MERGE_PARTIAL\", result_sha1, NULL,\n+\t\t\t   0, DIE_ON_ERR);\n+\t\t/* Store ref-to-be-updated into .git/NOTES_MERGE_REF */\n+\t\tif (create_symref(\"NOTES_MERGE_REF\", default_notes_ref(), NULL))\n+\t\t\tdie(\"Failed to store link to current notes ref (%s)\",\n+\t\t\t    default_notes_ref());\n+\t\tprintf(\"Automatic notes merge failed. Fix conflicts in %s and \"\n+\t\t       \"commit the result with 'git notes merge --commit', or \"\n+\t\t       \"abort the merge with 'git notes merge --reset'.\\n\",\n \t\t       git_path(NOTES_MERGE_WORKTREE));\n+\t}\n \n \tfree_notes(t);\n \tstrbuf_release(&remote_ref);\ndiff --git a/notes-merge.c b/notes-merge.c\nindex 8efa003..cf1f8da 100644\n--- a/notes-merge.c\n+++ b/notes-merge.c\n@@ -632,3 +632,72 @@ found_result:\n \t       result, sha1_to_hex(result_sha1));\n \treturn result;\n }\n+\n+int notes_merge_commit(struct notes_merge_options *o,\n+\t\t       struct notes_tree *partial_tree,\n+\t\t       struct commit *partial_commit,\n+\t\t       unsigned char *result_sha1)\n+{\n+\t/*\n+\t * Iterate through files in .git/NOTES_MERGE_WORKTREE and add all\n+\t * found notes to 'partial_tree'. Write the updates notes tree to\n+\t * the DB, and commit the resulting tree object while reusing the\n+\t * commit message and parents from 'partial_commit'.\n+\t * Finally store the new commit object SHA1 into 'result_sha1'.\n+\t */\n+\tstruct dir_struct dir;\n+\tconst char *path = git_path(NOTES_MERGE_WORKTREE \"/\");\n+\tint path_len = strlen(path), i;\n+\tconst char *msg = strstr(partial_commit->buffer, \"\\n\\n\");\n+\n+\tOUTPUT(o, 3, \"Committing notes in notes merge worktree at %.*s\",\n+\t       path_len - 1, path);\n+\n+\tif (!msg || msg[2] == '\\0')\n+\t\tdie(\"partial notes commit has empty message\");\n+\tmsg += 2;\n+\n+\tmemset(&dir, 0, sizeof(dir));\n+\tread_directory(&dir, path, path_len, NULL);\n+\tfor (i = 0; i < dir.nr; i++) {\n+\t\tstruct dir_entry *ent = dir.entries[i];\n+\t\tstruct stat st;\n+\t\tconst char *relpath = ent->name + path_len;\n+\t\tunsigned char obj_sha1[20], blob_sha1[20];\n+\n+\t\tif (ent->len - path_len != 40 || get_sha1_hex(relpath, obj_sha1)) {\n+\t\t\tOUTPUT(o, 3, \"Skipping non-SHA1 entry '%s'\", ent->name);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\t/* write file as blob, and add to partial_tree */\n+\t\tif (stat(ent->name, &st))\n+\t\t\tdie_errno(\"Failed to stat '%s'\", ent->name);\n+\t\tif (index_path(blob_sha1, ent->name, &st, 1))\n+\t\t\tdie(\"Failed to write blob object from '%s'\", ent->name);\n+\t\tif (add_note(partial_tree, obj_sha1, blob_sha1, NULL))\n+\t\t\tdie(\"Failed to add resolved note '%s' to notes tree\",\n+\t\t\t    ent->name);\n+\t\tOUTPUT(o, 4, \"Added resolved note for object %s: %s\",\n+\t\t       sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1));\n+\t}\n+\n+\tcreate_notes_commit(partial_tree, partial_commit->parents, msg,\n+\t\t\t    result_sha1);\n+\tOUTPUT(o, 4, \"Finalized notes merge commit: %s\",\n+\t       sha1_to_hex(result_sha1));\n+\treturn 0;\n+}\n+\n+int notes_merge_reset(struct notes_merge_options *o)\n+{\n+\t/* Remove .git/NOTES_MERGE_WORKTREE directory and all files within */\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tint ret;\n+\n+\tstrbuf_addstr(&buf, git_path(NOTES_MERGE_WORKTREE));\n+\tOUTPUT(o, 3, \"Removing notes merge worktree at %s\", buf.buf);\n+\tret = remove_dir_recursively(&buf, 0);\n+\tstrbuf_release(&buf);\n+\treturn ret;\n+}\ndiff --git a/notes-merge.h b/notes-merge.h\nindex e9a8ce6..e2f3459 100644\n--- a/notes-merge.h\n+++ b/notes-merge.h\n@@ -65,4 +65,27 @@ int notes_merge(struct notes_merge_options *o,\n \t\tstruct notes_tree *local_tree,\n \t\tunsigned char *result_sha1);\n \n+/*\n+ * Finalize conflict resolution from an earlier notes_merge()\n+ *\n+ * The given notes tree 'partial_tree' must be the notes_tree corresponding to\n+ * the given 'partial_commit', the partial result commit created by a previous\n+ * call to notes_merge().\n+ *\n+ * This function will add the (now resolved) notes in .git/NOTES_MERGE_WORKTREE\n+ * to 'partial_tree', and create a final notes merge commit, the SHA1 of which\n+ * will be stored in 'result_sha1'.\n+ */\n+int notes_merge_commit(struct notes_merge_options *o,\n+\t\t       struct notes_tree *partial_tree,\n+\t\t       struct commit *partial_commit,\n+\t\t       unsigned char *result_sha1);\n+\n+/*\n+ * Abort conflict resolution from an earlier notes_merge()\n+ *\n+ * Removes the notes merge worktree in .git/NOTES_MERGE_WORKTREE.\n+ */\n+int notes_merge_reset(struct notes_merge_options *o);\n+\n #endif\ndiff --git a/t/t3310-notes-merge-manual-resolve.sh b/t/t3310-notes-merge-manual-resolve.sh\nindex eadb6b7..1db0649 100755\n--- a/t/t3310-notes-merge-manual-resolve.sh\n+++ b/t/t3310-notes-merge-manual-resolve.sh\n@@ -171,6 +171,7 @@ cp expect_notes_y expect_notes_m\n cp expect_log_y expect_log_m\n \n git rev-parse refs/notes/y > pre_merge_y\n+git rev-parse refs/notes/z > pre_merge_z\n \n test_expect_success 'merge z into m (== y) with default (\"manual\") resolver => Conflicting 3-way merge' '\n \tgit update-ref refs/notes/m refs/notes/y &&\n@@ -289,4 +290,179 @@ test_expect_success 'can do merge without conflicts even if previous merge is un\n \tverify_notes y\n '\n \n+cat <<EOF | sort >expect_notes_m\n+021faa20e931fb48986ffc6282b4bb05553ac946 $commit_sha4\n+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3\n+283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2\n+0a59e787e6d688aa6309e56e8c1b89431a0fc1c1 $commit_sha1\n+EOF\n+\n+cat >expect_log_m <<EOF\n+$commit_sha5 5th\n+\n+$commit_sha4 4th\n+y and z notes on 4th commit\n+\n+$commit_sha3 3rd\n+y notes on 3rd commit\n+\n+$commit_sha2 2nd\n+z notes on 2nd commit\n+\n+$commit_sha1 1st\n+y and z notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'finalize conflicting merge (z => m)' '\n+\t# Resolve conflicts and finalize merge\n+\tcat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&\n+y and z notes on 1st commit\n+EOF\n+\tcat >.git/NOTES_MERGE_WORKTREE/$commit_sha4 <<EOF &&\n+y and z notes on 4th commit\n+EOF\n+\tgit notes merge --commit &&\n+\t# No .git/NOTES_MERGE_* files left\n+\ttest_must_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&\n+\ttest_cmp /dev/null output &&\n+\t# Merge commit has pre-merge y and pre-merge z as parents\n+\ttest \"$(git rev-parse refs/notes/m^1)\" = \"$(cat pre_merge_y)\" &&\n+\ttest \"$(git rev-parse refs/notes/m^2)\" = \"$(cat pre_merge_z)\" &&\n+\t# Merge commit mentions the notes refs merged\n+\tgit log -1 --format=%B refs/notes/m > merge_commit_msg &&\n+\tgrep -q refs/notes/m merge_commit_msg &&\n+\tgrep -q refs/notes/z merge_commit_msg &&\n+\t# Verify contents of merge result\n+\tverify_notes m &&\n+\t# Verify that other notes refs has not changed (w, x, y and z)\n+\tverify_notes w &&\n+\tverify_notes x &&\n+\tverify_notes y &&\n+\tverify_notes z\n+'\n+\n+cat >expect_conflict_$commit_sha4 <<EOF\n+<<<<<<< refs/notes/m\n+y notes on 4th commit\n+=======\n+z notes on 4th commit\n+\n+More z notes on 4th commit\n+>>>>>>> refs/notes/z\n+EOF\n+\n+cp expect_notes_y expect_notes_m\n+cp expect_log_y expect_log_m\n+\n+git rev-parse refs/notes/y > pre_merge_y\n+git rev-parse refs/notes/z > pre_merge_z\n+\n+test_expect_success 'redo merge of z into m (== y) with default (\"manual\") resolver => Conflicting 3-way merge' '\n+\tgit update-ref refs/notes/m refs/notes/y &&\n+\tgit config core.notesRef refs/notes/m &&\n+\ttest_must_fail git notes merge z >output &&\n+\t# Output should point to where to resolve conflicts\n+\tgrep -q \"\\\\.git/NOTES_MERGE_WORKTREE\" output &&\n+\t# Inspect merge conflicts\n+\tls .git/NOTES_MERGE_WORKTREE >output_conflicts &&\n+\ttest_cmp expect_conflicts output_conflicts &&\n+\t( for f in $(cat expect_conflicts); do\n+\t\ttest_cmp \"expect_conflict_$f\" \".git/NOTES_MERGE_WORKTREE/$f\" ||\n+\t\texit 1\n+\tdone ) &&\n+\t# Verify that current notes tree (pre-merge) has not changed (m == y)\n+\tverify_notes y &&\n+\tverify_notes m &&\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(cat pre_merge_y)\"\n+'\n+\n+test_expect_success 'abort notes merge' '\n+\tgit notes merge --reset &&\n+\t# No .git/NOTES_MERGE_* files left\n+\ttest_must_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&\n+\ttest_cmp /dev/null output &&\n+\t# m has not moved (still == y)\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(cat pre_merge_y)\"\n+\t# Verify that other notes refs has not changed (w, x, y and z)\n+\tverify_notes w &&\n+\tverify_notes x &&\n+\tverify_notes y &&\n+\tverify_notes z\n+'\n+\n+git rev-parse refs/notes/y > pre_merge_y\n+git rev-parse refs/notes/z > pre_merge_z\n+\n+test_expect_success 'redo merge of z into m (== y) with default (\"manual\") resolver => Conflicting 3-way merge' '\n+\ttest_must_fail git notes merge z >output &&\n+\t# Output should point to where to resolve conflicts\n+\tgrep -q \"\\\\.git/NOTES_MERGE_WORKTREE\" output &&\n+\t# Inspect merge conflicts\n+\tls .git/NOTES_MERGE_WORKTREE >output_conflicts &&\n+\ttest_cmp expect_conflicts output_conflicts &&\n+\t( for f in $(cat expect_conflicts); do\n+\t\ttest_cmp \"expect_conflict_$f\" \".git/NOTES_MERGE_WORKTREE/$f\" ||\n+\t\texit 1\n+\tdone ) &&\n+\t# Verify that current notes tree (pre-merge) has not changed (m == y)\n+\tverify_notes y &&\n+\tverify_notes m &&\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(cat pre_merge_y)\"\n+'\n+\n+cat <<EOF | sort >expect_notes_m\n+304dfb4325cf243025b9957486eb605a9b51c199 $commit_sha5\n+283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2\n+0a59e787e6d688aa6309e56e8c1b89431a0fc1c1 $commit_sha1\n+EOF\n+\n+cat >expect_log_m <<EOF\n+$commit_sha5 5th\n+new note on 5th commit\n+\n+$commit_sha4 4th\n+\n+$commit_sha3 3rd\n+\n+$commit_sha2 2nd\n+z notes on 2nd commit\n+\n+$commit_sha1 1st\n+y and z notes on 1st commit\n+\n+EOF\n+\n+test_expect_success 'add + remove notes in finalized merge (z => m)' '\n+\t# Resolve one conflict\n+\tcat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&\n+y and z notes on 1st commit\n+EOF\n+\t# Remove another conflict\n+\trm .git/NOTES_MERGE_WORKTREE/$commit_sha4 &&\n+\t# Remove a D/F conflict\n+\trm .git/NOTES_MERGE_WORKTREE/$commit_sha3 &&\n+\t# Add a new note\n+\techo \"new note on 5th commit\" > .git/NOTES_MERGE_WORKTREE/$commit_sha5 &&\n+\t# Finalize merge\n+\tgit notes merge --commit &&\n+\t# No .git/NOTES_MERGE_* files left\n+\ttest_must_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&\n+\ttest_cmp /dev/null output &&\n+\t# Merge commit has pre-merge y and pre-merge z as parents\n+\ttest \"$(git rev-parse refs/notes/m^1)\" = \"$(cat pre_merge_y)\" &&\n+\ttest \"$(git rev-parse refs/notes/m^2)\" = \"$(cat pre_merge_z)\" &&\n+\t# Merge commit mentions the notes refs merged\n+\tgit log -1 --format=%B refs/notes/m > merge_commit_msg &&\n+\tgrep -q refs/notes/m merge_commit_msg &&\n+\tgrep -q refs/notes/z merge_commit_msg &&\n+\t# Verify contents of merge result\n+\tverify_notes m &&\n+\t# Verify that other notes refs has not changed (w, x, y and z)\n+\tverify_notes w &&\n+\tverify_notes x &&\n+\tverify_notes y &&\n+\tverify_notes z\n+'\n+\n test_done\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151996","messageId":"1285719811-10871-16-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 15/18] git notes merge: List conflicting notes in notes merge commit message","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:28Z","receivedAt":"2010-09-29T00:23:28Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"This brings notes merge in line with regular merge's behaviour.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n builtin/notes.c                       |    2 +-\n notes-merge.c                         |   11 ++++++++++-\n notes-merge.h                         |    2 +-\n t/t3310-notes-merge-manual-resolve.sh |   12 ++++++++++++\n 4 files changed, 24 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex 8be7d9e..961dcbf 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -902,7 +902,7 @@ static int merge(int argc, const char **argv, const char *prefix)\n \n \tstrbuf_addf(&msg, \"notes: Merged notes from %s into %s\",\n \t\t    remote_ref.buf, default_notes_ref());\n-\to.commit_msg = msg.buf + 7; // skip \"notes: \" prefix\n+\tstrbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); // skip \"notes: \"\n \n \tresult = notes_merge(&o, t, result_sha1);\n \ndiff --git a/notes-merge.c b/notes-merge.c\nindex cf1f8da..8b34a32 100644\n--- a/notes-merge.c\n+++ b/notes-merge.c\n@@ -7,6 +7,7 @@\n #include \"dir.h\"\n #include \"notes.h\"\n #include \"notes-merge.h\"\n+#include \"strbuf.h\"\n \n struct notes_merge_pair {\n \tunsigned char obj[20], base[20], local[20], remote[20];\n@@ -15,6 +16,7 @@ struct notes_merge_pair {\n void init_notes_merge_options(struct notes_merge_options *o)\n {\n \tmemset(o, 0, sizeof(struct notes_merge_options));\n+\tstrbuf_init(&(o->commit_msg), 0);\n \to->verbosity = 2;\n }\n \n@@ -383,6 +385,12 @@ static int merge_one_change_manual(struct notes_merge_options *o,\n \t       sha1_to_hex(p->obj), sha1_to_hex(p->base),\n \t       sha1_to_hex(p->local), sha1_to_hex(p->remote));\n \n+\t/* add \"Conflicts:\" section to commit message first time through */\n+\tif (!o->has_worktree)\n+\t\tstrbuf_addstr(&(o->commit_msg), \"\\n\\nConflicts:\\n\");\n+\n+\tstrbuf_addf(&(o->commit_msg), \"\\t%s\\n\", sha1_to_hex(p->obj));\n+\n \tOUTPUT(o, 2, \"Auto-merging notes for %s\", sha1_to_hex(p->obj));\n \tcheck_notes_merge_worktree(o);\n \tif (is_null_sha1(p->local)) {\n@@ -622,12 +630,13 @@ int notes_merge(struct notes_merge_options *o,\n \t\tstruct commit_list *parents = NULL;\n \t\tcommit_list_insert(remote, &parents); /* LIFO order */\n \t\tcommit_list_insert(local, &parents);\n-\t\tcreate_notes_commit(local_tree, parents, o->commit_msg,\n+\t\tcreate_notes_commit(local_tree, parents, o->commit_msg.buf,\n \t\t\t\t    result_sha1);\n \t}\n \n found_result:\n \tfree_commit_list(bases);\n+\tstrbuf_release(&(o->commit_msg));\n \tOUTPUT(o, 5, \"notes_merge(): result = %i, result_sha1 = %.7s\",\n \t       result, sha1_to_hex(result_sha1));\n \treturn result;\ndiff --git a/notes-merge.h b/notes-merge.h\nindex e2f3459..a6ccb6a 100644\n--- a/notes-merge.h\n+++ b/notes-merge.h\n@@ -6,7 +6,7 @@\n struct notes_merge_options {\n \tconst char *local_ref;\n \tconst char *remote_ref;\n-\tconst char *commit_msg;\n+\tstruct strbuf commit_msg;\n \tint verbosity;\n \tenum {\n \t\tNOTES_MERGE_RESOLVE_MANUAL = 0,\ndiff --git a/t/t3310-notes-merge-manual-resolve.sh b/t/t3310-notes-merge-manual-resolve.sh\nindex 1db0649..8568307 100755\n--- a/t/t3310-notes-merge-manual-resolve.sh\n+++ b/t/t3310-notes-merge-manual-resolve.sh\n@@ -333,6 +333,12 @@ EOF\n \tgit log -1 --format=%B refs/notes/m > merge_commit_msg &&\n \tgrep -q refs/notes/m merge_commit_msg &&\n \tgrep -q refs/notes/z merge_commit_msg &&\n+\t# Merge commit mentions conflicting notes\n+\tgrep -q \"Conflicts\" merge_commit_msg &&\n+\t( for sha1 in $(cat expect_conflicts); do\n+\t\tgrep -q \"$sha1\" merge_commit_msg ||\n+\t\texit 1\n+\tdone ) &&\n \t# Verify contents of merge result\n \tverify_notes m &&\n \t# Verify that other notes refs has not changed (w, x, y and z)\n@@ -456,6 +462,12 @@ EOF\n \tgit log -1 --format=%B refs/notes/m > merge_commit_msg &&\n \tgrep -q refs/notes/m merge_commit_msg &&\n \tgrep -q refs/notes/z merge_commit_msg &&\n+\t# Merge commit mentions conflicting notes\n+\tgrep -q \"Conflicts\" merge_commit_msg &&\n+\t( for sha1 in $(cat expect_conflicts); do\n+\t\tgrep -q \"$sha1\" merge_commit_msg ||\n+\t\texit 1\n+\tdone ) &&\n \t# Verify contents of merge result\n \tverify_notes m &&\n \t# Verify that other notes refs has not changed (w, x, y and z)\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151994","messageId":"1285719811-10871-17-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 16/18] git notes merge: --commit should fail if underlying notes ref has moved","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:29Z","receivedAt":"2010-09-29T00:23:29Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"When manually resolving a notes merge, if the merging ref has moved since\nthe merge started, we should fail to complete the merge, and alert the user\nto what's going on.\n\nThis situation may arise if you start a 'git notes merge' which results in\nconflicts, and you then update the current notes ref (using for example\n'git notes add/copy/amend/edit/remove/prune', 'git update-ref', etc.),\nbefore you get around to resolving the notes conflicts and calling\n'git notes merge --commit'.\n\nWe detect this situation by comparing the first parent of the partial merge\ncommit (which was created when the merge started) to the current value of the\nmerging notes ref (pointed to by the .git/NOTES_MERGE_REF symref).\n\nIf we don't fail in this situation, the notes merge commit would overwrite\nthe updated notes ref, thus losing the changes that happened in the meantime.\n\nThe patch includes a testcase verifying that we fail correctly in this\nsituation.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n builtin/notes.c                       |   11 ++++-\n t/t3310-notes-merge-manual-resolve.sh |   76 +++++++++++++++++++++++++++++++++\n 2 files changed, 85 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex 961dcbf..27cab4b 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -786,7 +786,7 @@ static int merge_reset(struct notes_merge_options *o)\n static int merge_commit(struct notes_merge_options *o)\n {\n \tstruct strbuf msg = STRBUF_INIT;\n-\tunsigned char sha1[20];\n+\tunsigned char sha1[20], parent_sha1[20];\n \tstruct notes_tree *t;\n \tstruct commit *partial;\n \tstruct pretty_print_context pretty_ctx;\n@@ -803,6 +803,11 @@ static int merge_commit(struct notes_merge_options *o)\n \telse if (parse_commit(partial))\n \t\tdie(\"Could not parse commit from NOTES_MERGE_PARTIAL.\");\n \n+\tif (partial->parents)\n+\t\thashcpy(parent_sha1, partial->parents->item->object.sha1);\n+\telse\n+\t\thashclr(parent_sha1);\n+\n \tt = xcalloc(1, sizeof(struct notes_tree));\n \tinit_notes(t, \"NOTES_MERGE_PARTIAL\", combine_notes_overwrite, 0);\n \n@@ -818,7 +823,9 @@ static int merge_commit(struct notes_merge_options *o)\n \tformat_commit_message(partial, \"%s\", &msg, &pretty_ctx);\n \tstrbuf_trim(&msg);\n \tstrbuf_insert(&msg, 0, \"notes: \", 7);\n-\tupdate_ref(msg.buf, o->local_ref, sha1, NULL, 0, DIE_ON_ERR);\n+\tupdate_ref(msg.buf, o->local_ref, sha1,\n+\t\t   is_null_sha1(parent_sha1) ? NULL : parent_sha1,\n+\t\t   0, DIE_ON_ERR);\n \n \tfree_notes(t);\n \tstrbuf_release(&msg);\ndiff --git a/t/t3310-notes-merge-manual-resolve.sh b/t/t3310-notes-merge-manual-resolve.sh\nindex 8568307..dfd4954 100755\n--- a/t/t3310-notes-merge-manual-resolve.sh\n+++ b/t/t3310-notes-merge-manual-resolve.sh\n@@ -477,4 +477,80 @@ EOF\n \tverify_notes z\n '\n \n+cp expect_notes_y expect_notes_m\n+cp expect_log_y expect_log_m\n+\n+test_expect_success 'redo merge of z into m (== y) with default (\"manual\") resolver => Conflicting 3-way merge' '\n+\tgit update-ref refs/notes/m refs/notes/y &&\n+\ttest_must_fail git notes merge z >output &&\n+\t# Output should point to where to resolve conflicts\n+\tgrep -q \"\\\\.git/NOTES_MERGE_WORKTREE\" output &&\n+\t# Inspect merge conflicts\n+\tls .git/NOTES_MERGE_WORKTREE >output_conflicts &&\n+\ttest_cmp expect_conflicts output_conflicts &&\n+\t( for f in $(cat expect_conflicts); do\n+\t\ttest_cmp \"expect_conflict_$f\" \".git/NOTES_MERGE_WORKTREE/$f\" ||\n+\t\texit 1\n+\tdone ) &&\n+\t# Verify that current notes tree (pre-merge) has not changed (m == y)\n+\tverify_notes y &&\n+\tverify_notes m &&\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(cat pre_merge_y)\"\n+'\n+\n+cp expect_notes_w expect_notes_m\n+cp expect_log_w expect_log_m\n+\n+test_expect_success 'reset notes ref m to somewhere else (w)' '\n+\tgit update-ref refs/notes/m refs/notes/w &&\n+\tverify_notes m &&\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(git rev-parse refs/notes/w)\"\n+'\n+\n+test_expect_success 'fail to finalize conflicting merge if underlying ref has moved in the meantime (m != NOTES_MERGE_PARTIAL^1)' '\n+\t# Resolve conflicts\n+\tcat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&\n+y and z notes on 1st commit\n+EOF\n+\tcat >.git/NOTES_MERGE_WORKTREE/$commit_sha4 <<EOF &&\n+y and z notes on 4th commit\n+EOF\n+\t# Fail to finalize merge\n+\ttest_must_fail git notes merge --commit >output 2>&1 &&\n+\t# .git/NOTES_MERGE_* must remain\n+\ttest -f .git/NOTES_MERGE_PARTIAL &&\n+\ttest -f .git/NOTES_MERGE_REF &&\n+\ttest -f .git/NOTES_MERGE_WORKTREE/$commit_sha1 &&\n+\ttest -f .git/NOTES_MERGE_WORKTREE/$commit_sha2 &&\n+\ttest -f .git/NOTES_MERGE_WORKTREE/$commit_sha3 &&\n+\ttest -f .git/NOTES_MERGE_WORKTREE/$commit_sha4 &&\n+\t# Refs are unchanged\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(git rev-parse refs/notes/w)\"\n+\ttest \"$(git rev-parse refs/notes/y)\" = \"$(git rev-parse NOTES_MERGE_PARTIAL^1)\"\n+\ttest \"$(git rev-parse refs/notes/m)\" != \"$(git rev-parse NOTES_MERGE_PARTIAL^1)\"\n+\t# Mention refs/notes/m, and its current and expected value in output\n+\tgrep -q \"refs/notes/m\" output &&\n+\tgrep -q \"$(git rev-parse refs/notes/m)\" output &&\n+\tgrep -q \"$(git rev-parse NOTES_MERGE_PARTIAL^1)\" output &&\n+\t# Verify that other notes refs has not changed (w, x, y and z)\n+\tverify_notes w &&\n+\tverify_notes x &&\n+\tverify_notes y &&\n+\tverify_notes z\n+'\n+\n+test_expect_success 'resolve situation by aborting the notes merge' '\n+\tgit notes merge --reset &&\n+\t# No .git/NOTES_MERGE_* files left\n+\ttest_must_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&\n+\ttest_cmp /dev/null output &&\n+\t# m has not moved (still == w)\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(git rev-parse refs/notes/w)\"\n+\t# Verify that other notes refs has not changed (w, x, y and z)\n+\tverify_notes w &&\n+\tverify_notes x &&\n+\tverify_notes y &&\n+\tverify_notes z\n+'\n+\n test_done\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"151997","messageId":"1285719811-10871-18-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 17/18] git notes merge: Add another auto-resolving strategy: \"cat_sort_uniq\"","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:30Z","receivedAt":"2010-09-29T00:23:30Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"This new strategy is similar to \"concatenate\", but in addition to\nconcatenating the two note candidates, this strategy sorts the resulting\nlines, and removes duplicate lines from the result. This is equivalent to\napplying the \"cat | sort | uniq\" shell pipeline to the two note candidates.\n\nThis strategy is useful if the notes follow a line-based format where one\nwants to avoid duplicate lines in the merge result.\n\nNote that if either of the note candidates contain duplicate lines _prior_\nto the merge, these will also be removed by this merge strategy.\n\nThe patch also contains tests and documentation for the new strategy.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n Documentation/git-notes.txt         |   12 +++-\n builtin/notes.c                     |    8 ++-\n notes-merge.c                       |    6 ++\n notes-merge.h                       |    3 +-\n notes.c                             |   76 ++++++++++++++++++\n notes.h                             |    1 +\n t/t3309-notes-merge-auto-resolve.sh |  145 +++++++++++++++++++++++++++++++++++\n 7 files changed, 247 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt\nindex ede7755..874b77a 100644\n--- a/Documentation/git-notes.txt\n+++ b/Documentation/git-notes.txt\n@@ -155,7 +155,7 @@ OPTIONS\n --strategy=<strategy>::\n \tWhen merging notes, resolve notes conflicts using the given\n \tstrategy. The following strategies are recognized: \"manual\"\n-\t(default), \"ours\", \"theirs\" and \"union\".\n+\t(default), \"ours\", \"theirs\", \"union\" and \"cat_sort_uniq\".\n \tSee the \"NOTES MERGE STRATEGIES\" section below for more\n \tinformation on each notes merge strategy.\n \n@@ -231,6 +231,16 @@ ref).\n \"union\" automatically resolves notes conflicts by concatenating the\n local and remote versions.\n \n+\"cat_sort_uniq\" is similar to \"union\", but in addition to concatenating\n+the local and remote versions, this strategy also sorts the resulting\n+lines, and removes duplicate lines from the result. This is equivalent\n+to applying the \"cat | sort | uniq\" shell pipeline to the local and\n+remote versions. This strategy is useful if the notes follow a line-based\n+format where one wants to avoid duplicated lines in the merge result.\n+Note that if either the local or remote version contain duplicate lines\n+prior to the merge, these will also be removed by this notes merge\n+strategy.\n+\n \n EXAMPLES\n --------\ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex 27cab4b..ddd8d54 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -324,6 +324,8 @@ combine_notes_fn parse_combine_notes_fn(const char *v)\n \t\treturn combine_notes_ignore;\n \telse if (!strcasecmp(v, \"concatenate\"))\n \t\treturn combine_notes_concatenate;\n+\telse if (!strcasecmp(v, \"cat_sort_uniq\"))\n+\t\treturn combine_notes_cat_sort_uniq;\n \telse\n \t\treturn NULL;\n }\n@@ -846,8 +848,8 @@ static int merge(int argc, const char **argv, const char *prefix)\n \t\tOPT__VERBOSITY(&verbosity),\n \t\tOPT_GROUP(\"Merge options\"),\n \t\tOPT_STRING('s', \"strategy\", &strategy, \"strategy\",\n-\t\t\t   \"resolve notes conflicts using the given \"\n-\t\t\t   \"strategy (manual/ours/theirs/union)\"),\n+\t\t\t   \"resolve notes conflicts using the given strategy \"\n+\t\t\t   \"(manual/ours/theirs/union/cat_sort_uniq)\"),\n \t\tOPT_GROUP(\"Committing unmerged notes\"),\n \t\t{ OPTION_BOOLEAN, 0, \"commit\", &do_commit, NULL,\n \t\t\t\"finalize notes merge by committing unmerged notes\",\n@@ -899,6 +901,8 @@ static int merge(int argc, const char **argv, const char *prefix)\n \t\t\to.strategy = NOTES_MERGE_RESOLVE_THEIRS;\n \t\telse if (!strcmp(strategy, \"union\"))\n \t\t\to.strategy = NOTES_MERGE_RESOLVE_UNION;\n+\t\telse if (!strcmp(strategy, \"cat_sort_uniq\"))\n+\t\t\to.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;\n \t\telse {\n \t\t\terror(\"Unknown -X/--resolve strategy: %s\", strategy);\n \t\t\tusage_with_options(git_notes_merge_usage, options);\ndiff --git a/notes-merge.c b/notes-merge.c\nindex 8b34a32..3d5be04 100644\n--- a/notes-merge.c\n+++ b/notes-merge.c\n@@ -451,6 +451,12 @@ static int merge_one_change(struct notes_merge_options *o,\n \t\tif (add_note(t, p->obj, p->remote, combine_notes_concatenate))\n \t\t\tdie(\"confused: combine_notes_concatenate failed\");\n \t\treturn 0;\n+\tcase NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ:\n+\t\tOUTPUT(o, 2, \"Concatenating unique lines in local and remote \"\n+\t\t       \"notes for %s\", sha1_to_hex(p->obj));\n+\t\tif (add_note(t, p->obj, p->remote, combine_notes_cat_sort_uniq))\n+\t\t\tdie(\"confused: combine_notes_cat_sort_uniq failed\");\n+\t\treturn 0;\n \t}\n \tdie(\"Unknown strategy (%i).\", o->strategy);\n }\ndiff --git a/notes-merge.h b/notes-merge.h\nindex a6ccb6a..0690f40 100644\n--- a/notes-merge.h\n+++ b/notes-merge.h\n@@ -12,7 +12,8 @@ struct notes_merge_options {\n \t\tNOTES_MERGE_RESOLVE_MANUAL = 0,\n \t\tNOTES_MERGE_RESOLVE_OURS,\n \t\tNOTES_MERGE_RESOLVE_THEIRS,\n-\t\tNOTES_MERGE_RESOLVE_UNION\n+\t\tNOTES_MERGE_RESOLVE_UNION,\n+\t\tNOTES_MERGE_RESOLVE_CAT_SORT_UNIQ\n \t} strategy;\n \tunsigned has_worktree:1;\n };\ndiff --git a/notes.c b/notes.c\nindex 28afe2c..5f846d9 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -847,6 +847,82 @@ int combine_notes_ignore(unsigned char *cur_sha1,\n \treturn 0;\n }\n \n+static int string_list_add_note_lines(struct string_list *sort_uniq_list,\n+\t\t\t\t      const unsigned char *sha1)\n+{\n+\tchar *data;\n+\tunsigned long len;\n+\tenum object_type t;\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tstruct strbuf **lines = NULL;\n+\tint i, list_index;\n+\n+\tif (is_null_sha1(sha1))\n+\t\treturn 0;\n+\n+\t/* read_sha1_file NUL-terminates */\n+\tdata = read_sha1_file(sha1, &t, &len);\n+\tif (t != OBJ_BLOB || !data || !len) {\n+\t\tfree(data);\n+\t\treturn t != OBJ_BLOB || !data;\n+\t}\n+\n+\tstrbuf_attach(&buf, data, len, len + 1);\n+\tlines = strbuf_split(&buf, '\\n');\n+\n+\tfor (i = 0; lines[i]; i++) {\n+\t\tif (lines[i]->buf[lines[i]->len - 1] == '\\n')\n+\t\t\tstrbuf_setlen(lines[i], lines[i]->len - 1);\n+\t\tif (!lines[i]->len)\n+\t\t\tcontinue; /* skip empty lines */\n+\t\tlist_index = string_list_find_insert_index(sort_uniq_list,\n+\t\t\t\t\t\t\t   lines[i]->buf, 0);\n+\t\tif (list_index < 0)\n+\t\t\tcontinue; /* skip duplicate lines */\n+\t\tstring_list_insert_at_index(sort_uniq_list, list_index,\n+\t\t\t\t\t    lines[i]->buf);\n+\t}\n+\n+\tstrbuf_list_free(lines);\n+\tstrbuf_release(&buf);\n+\treturn 0;\n+}\n+\n+static int string_list_join_lines_helper(struct string_list_item *item,\n+\t\t\t\t\t void *cb_data)\n+{\n+\tstruct strbuf *buf = cb_data;\n+\tstrbuf_addstr(buf, item->string);\n+\tstrbuf_addch(buf, '\\n');\n+\treturn 0;\n+}\n+\n+int combine_notes_cat_sort_uniq(unsigned char *cur_sha1,\n+\t\tconst unsigned char *new_sha1)\n+{\n+\tstruct string_list sort_uniq_list = { NULL, 0, 0, 1 };\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tint ret = 1;\n+\n+\t/* read both note blob objects into unique_lines */\n+\tif (string_list_add_note_lines(&sort_uniq_list, cur_sha1))\n+\t\tgoto out;\n+\tif (string_list_add_note_lines(&sort_uniq_list, new_sha1))\n+\t\tgoto out;\n+\n+\t/* create a new blob object from sort_uniq_list */\n+\tif (for_each_string_list(&sort_uniq_list,\n+\t\t\t\t string_list_join_lines_helper, &buf))\n+\t\tgoto out;\n+\n+\tret = write_sha1_file(buf.buf, buf.len, blob_type, cur_sha1);\n+\n+out:\n+\tstrbuf_release(&buf);\n+\tstring_list_clear(&sort_uniq_list, 0);\n+\treturn ret;\n+}\n+\n static int string_list_add_one_ref(const char *path, const unsigned char *sha1,\n \t\t\t\t   int flag, void *cb)\n {\ndiff --git a/notes.h b/notes.h\nindex b372575..93a7365 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -27,6 +27,7 @@ typedef int (*combine_notes_fn)(unsigned char *cur_sha1, const unsigned char *ne\n int combine_notes_concatenate(unsigned char *cur_sha1, const unsigned char *new_sha1);\n int combine_notes_overwrite(unsigned char *cur_sha1, const unsigned char *new_sha1);\n int combine_notes_ignore(unsigned char *cur_sha1, const unsigned char *new_sha1);\n+int combine_notes_cat_sort_uniq(unsigned char *cur_sha1, const unsigned char *new_sha1);\n \n /*\n  * Notes tree object\ndiff --git a/t/t3309-notes-merge-auto-resolve.sh b/t/t3309-notes-merge-auto-resolve.sh\nindex 1a1fc7e..461fd84 100755\n--- a/t/t3309-notes-merge-auto-resolve.sh\n+++ b/t/t3309-notes-merge-auto-resolve.sh\n@@ -499,4 +499,149 @@ test_expect_success 'merge z into y with \"union\" strategy => Non-conflicting 3-w\n \tverify_notes y union\n '\n \n+test_expect_success 'reset to pre-merge state (y)' '\n+\tgit update-ref refs/notes/y refs/notes/y^1 &&\n+\t# Verify pre-merge state\n+\tverify_notes y y\n+'\n+\n+cat <<EOF | sort >expect_notes_union2\n+d682107b8bf7a7aea1e537a8d5cb6a12b60135f1 $commit_sha15\n+5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14\n+3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc $commit_sha13\n+a66055fa82f7a03fe0c02a6aba3287a85abf7c62 $commit_sha12\n+7e3c53503a3db8dd996cb62e37c66e070b44b54d $commit_sha11\n+b8d03e173f67f6505a76f6e00cf93440200dd9be $commit_sha10\n+851e1638784a884c7dd26c5d41f3340f6387413a $commit_sha8\n+357b6ca14c7afd59b7f8b8aaaa6b8b723771135b $commit_sha5\n+e2bfd06a37dd2031684a59a6e2b033e212239c78 $commit_sha4\n+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3\n+283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2\n+EOF\n+\n+cat >expect_log_union2 <<EOF\n+$commit_sha15 15th\n+z notes on 15th commit\n+\n+y notes on 15th commit\n+\n+$commit_sha14 14th\n+y notes on 14th commit\n+\n+$commit_sha13 13th\n+y notes on 13th commit\n+\n+$commit_sha12 12th\n+y notes on 12th commit\n+\n+$commit_sha11 11th\n+z notes on 11th commit\n+\n+$commit_sha10 10th\n+x notes on 10th commit\n+\n+$commit_sha9 9th\n+\n+$commit_sha8 8th\n+z notes on 8th commit\n+\n+$commit_sha7 7th\n+\n+$commit_sha6 6th\n+\n+$commit_sha5 5th\n+z notes on 5th commit\n+\n+y notes on 5th commit\n+\n+$commit_sha4 4th\n+y notes on 4th commit\n+\n+$commit_sha3 3rd\n+y notes on 3rd commit\n+\n+$commit_sha2 2nd\n+z notes on 2nd commit\n+\n+$commit_sha1 1st\n+\n+EOF\n+\n+test_expect_success 'merge y into z with \"union\" strategy => Non-conflicting 3-way merge' '\n+\tgit config core.notesRef refs/notes/z &&\n+\tgit notes merge --strategy=union y &&\n+\tverify_notes z union2\n+'\n+\n+test_expect_success 'reset to pre-merge state (z)' '\n+\tgit update-ref refs/notes/z refs/notes/z^1 &&\n+\t# Verify pre-merge state\n+\tverify_notes z z\n+'\n+\n+cat <<EOF | sort >expect_notes_cat_sort_uniq\n+6be90240b5f54594203e25d9f2f64b7567175aee $commit_sha15\n+5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14\n+3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc $commit_sha13\n+a66055fa82f7a03fe0c02a6aba3287a85abf7c62 $commit_sha12\n+7e3c53503a3db8dd996cb62e37c66e070b44b54d $commit_sha11\n+b8d03e173f67f6505a76f6e00cf93440200dd9be $commit_sha10\n+851e1638784a884c7dd26c5d41f3340f6387413a $commit_sha8\n+660311d7f78dc53db12ac373a43fca7465381a7e $commit_sha5\n+e2bfd06a37dd2031684a59a6e2b033e212239c78 $commit_sha4\n+5772f42408c0dd6f097a7ca2d24de0e78d1c46b1 $commit_sha3\n+283b48219aee9a4105f6cab337e789065c82c2b9 $commit_sha2\n+EOF\n+\n+cat >expect_log_cat_sort_uniq <<EOF\n+$commit_sha15 15th\n+y notes on 15th commit\n+z notes on 15th commit\n+\n+$commit_sha14 14th\n+y notes on 14th commit\n+\n+$commit_sha13 13th\n+y notes on 13th commit\n+\n+$commit_sha12 12th\n+y notes on 12th commit\n+\n+$commit_sha11 11th\n+z notes on 11th commit\n+\n+$commit_sha10 10th\n+x notes on 10th commit\n+\n+$commit_sha9 9th\n+\n+$commit_sha8 8th\n+z notes on 8th commit\n+\n+$commit_sha7 7th\n+\n+$commit_sha6 6th\n+\n+$commit_sha5 5th\n+y notes on 5th commit\n+z notes on 5th commit\n+\n+$commit_sha4 4th\n+y notes on 4th commit\n+\n+$commit_sha3 3rd\n+y notes on 3rd commit\n+\n+$commit_sha2 2nd\n+z notes on 2nd commit\n+\n+$commit_sha1 1st\n+\n+EOF\n+\n+test_expect_success 'merge y into z with \"cat_sort_uniq\" strategy => Non-conflicting 3-way merge' '\n+\tgit notes merge --strategy=cat_sort_uniq y &&\n+\tverify_notes z cat_sort_uniq\n+'\n+\n test_done\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"152004","messageId":"1285719811-10871-19-git-send-email-johan@herland.net","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"[PATCH 18/18] git notes merge: Add testcases for merging notes trees at different fanouts","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T00:23:31Z","receivedAt":"2010-09-29T00:23:31Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Notes trees may exist at different fanout levels internally. This\nimplementation detail should not be visible to the user, and it should\ncertainly not affect the merging of notes tree.\n\nThis patch adds testcases verifying the correctness of 'git notes merge'\nwhen merging notes trees at different fanout levels.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n t/t3311-notes-merge-fanout.sh |  436 +++++++++++++++++++++++++++++++++++++++++\n 1 files changed, 436 insertions(+), 0 deletions(-)\n create mode 100755 t/t3311-notes-merge-fanout.sh\n\ndiff --git a/t/t3311-notes-merge-fanout.sh b/t/t3311-notes-merge-fanout.sh\nnew file mode 100755\nindex 0000000..d1c7b69\n--- /dev/null\n+++ b/t/t3311-notes-merge-fanout.sh\n@@ -0,0 +1,436 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2010 Johan Herland\n+#\n+\n+test_description='Test notes merging at various fanout levels'\n+\n+. ./test-lib.sh\n+\n+verify_notes () {\n+\tnotes_ref=\"$1\"\n+\tcommit=\"$2\"\n+\tif test -f \"expect_notes_$notes_ref\"\n+\tthen\n+\t\tgit -c core.notesRef=\"refs/notes/$notes_ref\" notes |\n+\t\t\tsort >\"output_notes_$notes_ref\" &&\n+\t\ttest_cmp \"expect_notes_$notes_ref\" \"output_notes_$notes_ref\" ||\n+\t\t\treturn 1\n+\tfi &&\n+\tgit -c core.notesRef=\"refs/notes/$notes_ref\" log --format=\"%H %s%n%N\" \\\n+\t\t\"$commit\" >\"output_log_$notes_ref\" &&\n+\ttest_cmp \"expect_log_$notes_ref\" \"output_log_$notes_ref\"\n+}\n+\n+verify_fanout () {\n+\tnotes_ref=\"$1\"\n+\t# Expect entire notes tree to have a fanout == 1\n+\tgit rev-parse --quiet --verify \"refs/notes/$notes_ref\" >/dev/null &&\n+\tgit ls-tree -r --name-only \"refs/notes/$notes_ref\" |\n+\twhile read path\n+\tdo\n+\t\tcase \"$path\" in\n+\t\t??/??????????????????????????????????????)\n+\t\t\t: true\n+\t\t\t;;\n+\t\t*)\n+\t\t\techo \"Invalid path \\\"$path\\\"\" &&\n+\t\t\treturn 1\n+\t\t\t;;\n+\t\tesac\n+\tdone\n+}\n+\n+verify_no_fanout () {\n+\tnotes_ref=\"$1\"\n+\t# Expect entire notes tree to have a fanout == 0\n+\tgit rev-parse --quiet --verify \"refs/notes/$notes_ref\" >/dev/null &&\n+\tgit ls-tree -r --name-only \"refs/notes/$notes_ref\" |\n+\twhile read path\n+\tdo\n+\t\tcase \"$path\" in\n+\t\t????????????????????????????????????????)\n+\t\t\t: true\n+\t\t\t;;\n+\t\t*)\n+\t\t\techo \"Invalid path \\\"$path\\\"\" &&\n+\t\t\treturn 1\n+\t\t\t;;\n+\t\tesac\n+\tdone\n+}\n+\n+# Set up a notes merge scenario with different kinds of conflicts\n+test_expect_success 'setup a few initial commits with notes (notes ref: x)' '\n+\tgit config core.notesRef refs/notes/x &&\n+\tfor i in 1 2 3 4 5\n+\tdo\n+\t\ttest_commit \"commit$i\" >/dev/null &&\n+\t\tgit notes add -m \"notes for commit$i\" || return 1\n+\tdone\n+'\n+\n+commit_sha1=$(git rev-parse commit1^{commit})\n+commit_sha2=$(git rev-parse commit2^{commit})\n+commit_sha3=$(git rev-parse commit3^{commit})\n+commit_sha4=$(git rev-parse commit4^{commit})\n+commit_sha5=$(git rev-parse commit5^{commit})\n+\n+cat <<EOF | sort >expect_notes_x\n+aed91155c7a72c2188e781fdf40e0f3761b299db $commit_sha5\n+99fab268f9d7ee7b011e091a436c78def8eeee69 $commit_sha4\n+953c20ae26c7aa0b428c20693fe38bc687f9d1a9 $commit_sha3\n+6358796131b8916eaa2dde6902642942a1cb37e1 $commit_sha2\n+b02d459c32f0e68f2fe0981033bb34f38776ba47 $commit_sha1\n+EOF\n+\n+cat >expect_log_x <<EOF\n+$commit_sha5 commit5\n+notes for commit5\n+\n+$commit_sha4 commit4\n+notes for commit4\n+\n+$commit_sha3 commit3\n+notes for commit3\n+\n+$commit_sha2 commit2\n+notes for commit2\n+\n+$commit_sha1 commit1\n+notes for commit1\n+\n+EOF\n+\n+test_expect_success 'sanity check (x)' '\n+\tverify_notes x commit5 &&\n+\tverify_no_fanout x\n+'\n+\n+num=300\n+\n+cp expect_log_x expect_log_y\n+\n+test_expect_success 'Add a few hundred commits w/notes to trigger fanout (x -> y)' '\n+\tgit update-ref refs/notes/y refs/notes/x &&\n+\tgit config core.notesRef refs/notes/y &&\n+\ti=5 &&\n+\twhile test $i -lt $num\n+\tdo\n+\t\ti=$(($i + 1)) &&\n+\t\ttest_commit \"commit$i\" >/dev/null &&\n+\t\tgit notes add -m \"notes for commit$i\" || return 1\n+\tdone &&\n+\ttest \"$(git rev-parse refs/notes/y)\" != \"$(git rev-parse refs/notes/x)\" &&\n+\t# Expected number of commits and notes\n+\ttest \"$(git rev-list HEAD | wc -l)\" = \"$num\" &&\n+\ttest \"$(git notes list | wc -l)\" = \"$num\" &&\n+\t# 5 first notes unchanged\n+\tverify_notes y commit5\n+'\n+\n+test_expect_success 'notes tree has fanout (y)' 'verify_fanout y'\n+\n+test_expect_success 'No-op merge (already included) (x => y)' '\n+\tgit update-ref refs/notes/m refs/notes/y &&\n+\tgit config core.notesRef refs/notes/m &&\n+\tgit notes merge x &&\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(git rev-parse refs/notes/y)\"\n+'\n+\n+test_expect_success 'Fast-forward merge (y => x)' '\n+\tgit update-ref refs/notes/m refs/notes/x &&\n+\tgit notes merge y &&\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(git rev-parse refs/notes/y)\"\n+'\n+\n+cat <<EOF | sort >expect_notes_z\n+9f506ee70e20379d7f78204c77b334f43d77410d $commit_sha3\n+23a47d6ea7d589895faf800752054818e1e7627b $commit_sha2\n+b02d459c32f0e68f2fe0981033bb34f38776ba47 $commit_sha1\n+EOF\n+\n+cat >expect_log_z <<EOF\n+$commit_sha5 commit5\n+\n+$commit_sha4 commit4\n+\n+$commit_sha3 commit3\n+notes for commit3\n+\n+appended notes for commit3\n+\n+$commit_sha2 commit2\n+new notes for commit2\n+\n+$commit_sha1 commit1\n+notes for commit1\n+\n+EOF\n+\n+test_expect_success 'change some of the initial 5 notes (x -> z)' '\n+\tgit update-ref refs/notes/z refs/notes/x &&\n+\tgit config core.notesRef refs/notes/z &&\n+\tgit notes add -f -m \"new notes for commit2\" commit2 &&\n+\tgit notes append -m \"appended notes for commit3\" commit3 &&\n+\tgit notes remove commit4 &&\n+\tgit notes remove commit5 &&\n+\tverify_notes z commit5\n+'\n+\n+test_expect_success 'notes tree has no fanout (z)' 'verify_no_fanout z'\n+\n+cp expect_log_z expect_log_m\n+\n+test_expect_success 'successful merge without conflicts (y => z)' '\n+\tgit update-ref refs/notes/m refs/notes/z &&\n+\tgit config core.notesRef refs/notes/m &&\n+\tgit notes merge y &&\n+\tverify_notes m commit5 &&\n+\t# x/y/z unchanged\n+\tverify_notes x commit5 &&\n+\tverify_notes y commit5 &&\n+\tverify_notes z commit5\n+'\n+\n+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'\n+\n+cat >expect_log_w <<EOF\n+$commit_sha5 commit5\n+\n+$commit_sha4 commit4\n+other notes for commit4\n+\n+$commit_sha3 commit3\n+other notes for commit3\n+\n+$commit_sha2 commit2\n+notes for commit2\n+\n+$commit_sha1 commit1\n+other notes for commit1\n+\n+EOF\n+\n+test_expect_success 'introduce conflicting changes (y -> w)' '\n+\tgit update-ref refs/notes/w refs/notes/y &&\n+\tgit config core.notesRef refs/notes/w &&\n+\tgit notes add -f -m \"other notes for commit1\" commit1 &&\n+\tgit notes add -f -m \"other notes for commit3\" commit3 &&\n+\tgit notes add -f -m \"other notes for commit4\" commit4 &&\n+\tgit notes remove commit5 &&\n+\tverify_notes w commit5\n+'\n+\n+cat >expect_log_m <<EOF\n+$commit_sha5 commit5\n+\n+$commit_sha4 commit4\n+other notes for commit4\n+\n+$commit_sha3 commit3\n+other notes for commit3\n+\n+$commit_sha2 commit2\n+new notes for commit2\n+\n+$commit_sha1 commit1\n+other notes for commit1\n+\n+EOF\n+\n+test_expect_success 'successful merge using \"ours\" strategy (z => w)' '\n+\tgit update-ref refs/notes/m refs/notes/w &&\n+\tgit config core.notesRef refs/notes/m &&\n+\tgit notes merge -s ours z &&\n+\tverify_notes m commit5 &&\n+\t# w/x/y/z unchanged\n+\tverify_notes w commit5 &&\n+\tverify_notes x commit5 &&\n+\tverify_notes y commit5 &&\n+\tverify_notes z commit5\n+'\n+\n+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'\n+\n+cat >expect_log_m <<EOF\n+$commit_sha5 commit5\n+\n+$commit_sha4 commit4\n+\n+$commit_sha3 commit3\n+notes for commit3\n+\n+appended notes for commit3\n+\n+$commit_sha2 commit2\n+new notes for commit2\n+\n+$commit_sha1 commit1\n+other notes for commit1\n+\n+EOF\n+\n+test_expect_success 'successful merge using \"theirs\" strategy (z => w)' '\n+\tgit update-ref refs/notes/m refs/notes/w &&\n+\tgit notes merge -s theirs z &&\n+\tverify_notes m commit5 &&\n+\t# w/x/y/z unchanged\n+\tverify_notes w commit5 &&\n+\tverify_notes x commit5 &&\n+\tverify_notes y commit5 &&\n+\tverify_notes z commit5\n+'\n+\n+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'\n+\n+cat >expect_log_m <<EOF\n+$commit_sha5 commit5\n+\n+$commit_sha4 commit4\n+other notes for commit4\n+\n+$commit_sha3 commit3\n+other notes for commit3\n+\n+notes for commit3\n+\n+appended notes for commit3\n+\n+$commit_sha2 commit2\n+new notes for commit2\n+\n+$commit_sha1 commit1\n+other notes for commit1\n+\n+EOF\n+\n+test_expect_success 'successful merge using \"union\" strategy (z => w)' '\n+\tgit update-ref refs/notes/m refs/notes/w &&\n+\tgit notes merge -s union z &&\n+\tverify_notes m commit5 &&\n+\t# w/x/y/z unchanged\n+\tverify_notes w commit5 &&\n+\tverify_notes x commit5 &&\n+\tverify_notes y commit5 &&\n+\tverify_notes z commit5\n+'\n+\n+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'\n+\n+cat >expect_log_m <<EOF\n+$commit_sha5 commit5\n+\n+$commit_sha4 commit4\n+other notes for commit4\n+\n+$commit_sha3 commit3\n+appended notes for commit3\n+notes for commit3\n+other notes for commit3\n+\n+$commit_sha2 commit2\n+new notes for commit2\n+\n+$commit_sha1 commit1\n+other notes for commit1\n+\n+EOF\n+\n+test_expect_success 'successful merge using \"cat_sort_uniq\" strategy (z => w)' '\n+\tgit update-ref refs/notes/m refs/notes/w &&\n+\tgit notes merge -s cat_sort_uniq z &&\n+\tverify_notes m commit5 &&\n+\t# w/x/y/z unchanged\n+\tverify_notes w commit5 &&\n+\tverify_notes x commit5 &&\n+\tverify_notes y commit5 &&\n+\tverify_notes z commit5\n+'\n+\n+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'\n+\n+# We're merging z into w. Here are the conflicts we expect:\n+#\n+# commit | x -> w    | x -> z    | conflict?\n+# -------|-----------|-----------|----------\n+# 1      | changed   | unchanged | no, use w\n+# 2      | unchanged | changed   | no, use z\n+# 3      | changed   | changed   | yes (w, then z in conflict markers)\n+# 4      | changed   | deleted   | yes (w)\n+# 5      | deleted   | deleted   | no, deleted\n+\n+test_expect_success 'fails to merge using \"manual\" strategy (z => w)' '\n+\tgit update-ref refs/notes/m refs/notes/w &&\n+\ttest_must_fail git notes merge z\n+'\n+\n+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'\n+\n+cat <<EOF | sort >expect_conflicts\n+$commit_sha3\n+$commit_sha4\n+EOF\n+\n+cat >expect_conflict_$commit_sha3 <<EOF\n+<<<<<<< refs/notes/m\n+other notes for commit3\n+=======\n+notes for commit3\n+\n+appended notes for commit3\n+>>>>>>> refs/notes/z\n+EOF\n+\n+cat >expect_conflict_$commit_sha4 <<EOF\n+other notes for commit4\n+EOF\n+\n+test_expect_success 'verify conflict entries (with no fanout)' '\n+\tls .git/NOTES_MERGE_WORKTREE >output_conflicts &&\n+\ttest_cmp expect_conflicts output_conflicts &&\n+\t( for f in $(cat expect_conflicts); do\n+\t\ttest_cmp \"expect_conflict_$f\" \".git/NOTES_MERGE_WORKTREE/$f\" ||\n+\t\texit 1\n+\tdone ) &&\n+\t# Verify that current notes tree (pre-merge) has not changed (m == w)\n+\ttest \"$(git rev-parse refs/notes/m)\" = \"$(git rev-parse refs/notes/w)\"\n+'\n+\n+cat >expect_log_m <<EOF\n+$commit_sha5 commit5\n+\n+$commit_sha4 commit4\n+other notes for commit4\n+\n+$commit_sha3 commit3\n+other notes for commit3\n+\n+appended notes for commit3\n+\n+$commit_sha2 commit2\n+new notes for commit2\n+\n+$commit_sha1 commit1\n+other notes for commit1\n+\n+EOF\n+\n+test_expect_success 'resolve and finalize merge (z => w)' '\n+\tcat >.git/NOTES_MERGE_WORKTREE/$commit_sha3 <<EOF &&\n+other notes for commit3\n+\n+appended notes for commit3\n+EOF\n+\tgit notes merge --commit &&\n+\tverify_notes m commit5 &&\n+\t# w/x/y/z unchanged\n+\tverify_notes w commit5 &&\n+\tverify_notes x commit5 &&\n+\tverify_notes y commit5 &&\n+\tverify_notes z commit5\n+'\n+\n+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'\n+\n+test_done\n-- \n1.7.3.98.g5ad7d9\n"},{"id":"152060","messageId":"AANLkTi=gL_6ueBvO7URth-rbbHyZLpwYEAMwErh1ciba@mail.gmail.com","threadId":"25282","inReplyTo":"1285719811-10871-1-git-send-email-johan@herland.net","subject":"Re: [PATCH 00/18] git notes merge","fromName":"Sverre Rabbelier","fromEmail":"srabbelier@gmail.com","sentAt":"2010-09-29T14:56:28Z","receivedAt":"2010-09-29T14:56:28Z","isPatch":true,"sender":{"key":"srabbelier@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3098?v=4"},"body":"Heya,\n\nOn Wed, Sep 29, 2010 at 02:23, Johan Herland <johan@herland.net> wrote:\n>  Alternatively, the user can call 'git notes merge --reset' to abort\n>  the notes merge.\n\nI'm torn, but I think that since we actually can here, we should call\nit 'git notes merge --abort'. I know that there's no 'git merge\n--abort', but IIRC that's for technical reasons only.\n\n-- \nCheers,\n\nSverre Rabbelier\n"},{"id":"152062","messageId":"201009291716.52831.johan@herland.net","threadId":"25282","inReplyTo":"AANLkTi=gL_6ueBvO7URth-rbbHyZLpwYEAMwErh1ciba@mail.gmail.com","subject":"Re: [PATCH 00/18] git notes merge","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T15:16:52Z","receivedAt":"2010-09-29T15:16:52Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Wednesday 29 September 2010, Sverre Rabbelier wrote:\n> On Wed, Sep 29, 2010 at 02:23, Johan Herland wrote:\n> >  Alternatively, the user can call 'git notes merge --reset' to\n> > abort the notes merge.\n>\n> I'm torn, but I think that since we actually can here, we should call\n> it 'git notes merge --abort'.\n\nYeah, I'm torn as well. What about providing both? Or is that bloat?\n\n> I know that there's no 'git merge --abort', but IIRC that's for\n> technical reasons only. \n\nMaybe there _should_ be a 'git merge --abort' (as a synonym to 'git \nreset --merge')?\n\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"152063","messageId":"AANLkTinjAoAYW2as1VfeWJ=t4MORjC9s7=eMCbYttier@mail.gmail.com","threadId":"25282","inReplyTo":"201009291716.52831.johan@herland.net","subject":"Re: [PATCH 00/18] git notes merge","fromName":"Sverre Rabbelier","fromEmail":"srabbelier@gmail.com","sentAt":"2010-09-29T15:20:52Z","receivedAt":"2010-09-29T15:20:52Z","isPatch":true,"sender":{"key":"srabbelier@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3098?v=4"},"body":"Heya,\n\nOn Wed, Sep 29, 2010 at 17:16, Johan Herland <johan@herland.net> wrote:\n> Yeah, I'm torn as well. What about providing both? Or is that bloat?\n\nDefinitely not both, that would be confusing, and would limit us if we\ndecide to add 'git merge --abort' later on.\n\n>> I know that there's no 'git merge --abort', but IIRC that's for\n>> technical reasons only.\n>\n> Maybe there _should_ be a 'git merge --abort' (as a synonym to 'git\n> reset --merge')?\n\nHmmm, I don't know if that does what the user wants, (I haven't used\n'git reset --merge' before), but if it does, that sounds like a good\nsolution.\n\n-- \nCheers,\n\nSverre Rabbelier\n"},{"id":"152068","messageId":"201009291804.13404.johan@herland.net","threadId":"25282","inReplyTo":"AANLkTinjAoAYW2as1VfeWJ=t4MORjC9s7=eMCbYttier@mail.gmail.com","subject":"Re: [PATCH 00/18] git notes merge","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T16:04:13Z","receivedAt":"2010-09-29T16:04:13Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Wednesday 29 September 2010, Sverre Rabbelier wrote:\n> On Wed, Sep 29, 2010 at 17:16, Johan Herland wrote:\n> > Yeah, I'm torn as well. What about providing both? Or is that\n> > bloat?\n>\n> Definitely not both, that would be confusing, and would limit us if\n> we decide to add 'git merge --abort' later on.\n\nYeah, but for consistency's sake I don't want to name it 'git notes \nmerge --abort' if there's not a corresponding 'git merge --abort'.\n\n> >> I know that there's no 'git merge --abort', but IIRC that's for\n> >> technical reasons only.\n> >\n> > Maybe there _should_ be a 'git merge --abort' (as a synonym to 'git\n> > reset --merge')?\n>\n> Hmmm, I don't know if that does what the user wants, (I haven't used\n> 'git reset --merge' before), but if it does, that sounds like a good\n> solution.\n\n>From git-merge(1):\n\n\"If you tried a merge which resulted in complex conflicts and want to \nstart over, you can recover with git reset --merge.\"\n\nAFAICS, there's no better candidate synonym for 'git merge --abort'.\n\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"152069","messageId":"AANLkTimzii1TEqRY40idz_i8qXHGFnoCZa29eH31U6n7@mail.gmail.com","threadId":"25282","inReplyTo":"1285719811-10871-15-git-send-email-johan@herland.net","subject":"Re: [PATCH 14/18] git notes merge: Manual conflict resolution, part 2/2","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2010-09-29T16:19:30Z","receivedAt":"2010-09-29T16:19:30Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"On Wed, Sep 29, 2010 at 00:23, Johan Herland <johan@herland.net> wrote:\n\n> +--commit::\n> +       Finalize an in-progress 'git notes merge', i.e. a notes merge with\n> +       conflicts. When 'git notes merge' encounters conflicts, it s\n> +       which returned one or more conflicts (that you have now resolved)\n\nThis \"it s ...\" sentence doesn't make sense.\n"},{"id":"152070","messageId":"AANLkTingYp89fY9QxzmdJgF587C29cArxyBU--QcBY5G@mail.gmail.com","threadId":"25282","inReplyTo":"1285719811-10871-11-git-send-email-johan@herland.net","subject":"Re: [PATCH 10/18] git notes merge: Handle real, non-conflicting notes merges","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2010-09-29T16:20:54Z","receivedAt":"2010-09-29T16:20:54Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"On Wed, Sep 29, 2010 at 00:23, Johan Herland <johan@herland.net> wrote:\n\n> +       o.commit_msg = msg.buf + 7; // skip \"notes: \" prefix\n\nDon't use C99 comments.\n"},{"id":"152104","messageId":"201009300125.30301.johan@herland.net","threadId":"25282","inReplyTo":"AANLkTingYp89fY9QxzmdJgF587C29cArxyBU--QcBY5G@mail.gmail.com","subject":"Re: [PATCH 10/18] git notes merge: Handle real, non-conflicting notes merges","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T23:25:29Z","receivedAt":"2010-09-29T23:25:29Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Wednesday 29 September 2010, Ævar Arnfjörð Bjarmason wrote:\n> On Wed, Sep 29, 2010 at 00:23, Johan Herland <johan@herland.net> wrote:\n> > +       o.commit_msg = msg.buf + 7; // skip \"notes: \" prefix\n> \n> Don't use C99 comments.\n\nThanks. Fix will be in the next iteration.\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"152105","messageId":"201009300137.10291.johan@herland.net","threadId":"25282","inReplyTo":"AANLkTimzii1TEqRY40idz_i8qXHGFnoCZa29eH31U6n7@mail.gmail.com","subject":"Re: [PATCH 14/18] git notes merge: Manual conflict resolution, part 2/2","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-09-29T23:37:10Z","receivedAt":"2010-09-29T23:37:10Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Wednesday 29 September 2010, Ævar Arnfjörð Bjarmason wrote:\n> On Wed, Sep 29, 2010 at 00:23, Johan Herland <johan@herland.net> wrote:\n> > +--commit::\n> > +       Finalize an in-progress 'git notes merge', i.e. a notes merge\n> > with +       conflicts. When 'git notes merge' encounters conflicts,\n> > it s +       which returned one or more conflicts (that you have now\n> > resolved)\n> \n> This \"it s ...\" sentence doesn't make sense.\n\nIndeed. Will be fixed in the next iteration. Here's the updated text:\n\n--commit::\n\tFinalize an in-progress 'git notes merge'. Use this option\n\twhen you have resolved the conflicts that 'git notes merge'\n\tstored in .git/NOTES_MERGE_WORKTREE. This amends the partial\n\tmerge commit created by 'git notes merge' (stored in\n\t.git/NOTES_MERGE_PARTIAL) by adding the notes in\n\t.git/NOTES_MERGE_WORKTREE. The notes ref stored in the\n\t.git/NOTES_MERGE_REF symref is updated to the resulting commit.\n\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"152287","messageId":"4CA6F385.7030207@gmail.com","threadId":"25282","inReplyTo":"1285719811-10871-13-git-send-email-johan@herland.net","subject":"Re: [PATCH 12/18] Documentation: Preliminary docs on 'git notes merge'","fromName":"Stephen Boyd","fromEmail":"bebarino@gmail.com","sentAt":"2010-10-02T08:55:33Z","receivedAt":"2010-10-02T08:55:33Z","isPatch":true,"sender":{"key":"bebarino@gmail.com","avatar":"https://avatars.githubusercontent.com/u/38832?v=4"},"body":"On 09/28/2010 05:23 PM, Johan Herland wrote:\n> ++\n> +If conflicts arise (and a strategy for automatically resolving\n> +conflicting notes (see the -s/--strategy option) is not given,\n> +the merge fails (TODO).\n\nUnbalanced '(' here. Can you just drop the first one?\n"},{"id":"152288","messageId":"4CA6F806.9030209@gmail.com","threadId":"25282","inReplyTo":"1285719811-10871-12-git-send-email-johan@herland.net","subject":"Re: [PATCH 11/18] git notes merge: Add automatic conflict resolvers (ours, theirs, union)","fromName":"Stephen Boyd","fromEmail":"bebarino@gmail.com","sentAt":"2010-10-02T09:14:46Z","receivedAt":"2010-10-02T09:14:46Z","isPatch":true,"sender":{"key":"bebarino@gmail.com","avatar":"https://avatars.githubusercontent.com/u/38832?v=4"},"body":"On 09/28/2010 05:23 PM, Johan Herland wrote:\n> @@ -788,6 +792,21 @@ static int merge(int argc, const char **argv, const char *prefix)\n>  \texpand_notes_ref(&remote_ref);\n>  \to.remote_ref = remote_ref.buf;\n>  \n> +\tif (strategy) {\n> +\t\tif (!strcmp(strategy, \"manual\"))\n> +\t\t\to.strategy = NOTES_MERGE_RESOLVE_MANUAL;\n> +\t\telse if (!strcmp(strategy, \"ours\"))\n> +\t\t\to.strategy = NOTES_MERGE_RESOLVE_OURS;\n> +\t\telse if (!strcmp(strategy, \"theirs\"))\n> +\t\t\to.strategy = NOTES_MERGE_RESOLVE_THEIRS;\n> +\t\telse if (!strcmp(strategy, \"union\"))\n> +\t\t\to.strategy = NOTES_MERGE_RESOLVE_UNION;\n> +\t\telse {\n> +\t\t\terror(\"Unknown -X/--resolve strategy: %s\", strategy);\n\nIs it -X/--resolve or -s/--strategy? This error confuses me.\n\n> diff --git a/notes-merge.c b/notes-merge.c\n> index f625ebd..6fa59d8 100644\n> --- a/notes-merge.c\n> +++ b/notes-merge.c\n> @@ -262,6 +262,35 @@ static void diff_tree_local(struct notes_merge_options *o,\n>  \tdiff_tree_release_paths(&opt);\n>  }\n>  \n> +static int merge_one_change(struct notes_merge_options *o,\n> +\t\t\t    struct notes_merge_pair *p, struct notes_tree *t)\n> +{\n> +\t/*\n> +\t * Return 0 if change was resolved (and added to notes_tree),\n> +\t * 1 if conflict\n> +\t */\n> +\tswitch (o->strategy) {\n> +\tcase NOTES_MERGE_RESOLVE_MANUAL:\n> +\t\treturn 1;\n> +\tcase NOTES_MERGE_RESOLVE_OURS:\n> +\t\tOUTPUT(o, 2, \"Using local notes for %s\", sha1_to_hex(p->obj));\n> +\t\t/* nothing to do */\n> +\t\treturn 0;\n> +\tcase NOTES_MERGE_RESOLVE_THEIRS:\n> +\t\tOUTPUT(o, 2, \"Using remote notes for %s\", sha1_to_hex(p->obj));\n> +\t\tif (add_note(t, p->obj, p->remote, combine_notes_overwrite))\n> +\t\t\tdie(\"confused: combine_notes_overwrite failed\");\n\nThis will say:\n\nfatal: confused: combine_notes_overwrite failed\n\nDo we actually need the \"confused\" part? Heh, maybe we need a confused()\nfunction?\n"},{"id":"152537","messageId":"201010041710.02046.johan@herland.net","threadId":"25282","inReplyTo":"4CA6F806.9030209@gmail.com","subject":"Re: [PATCH 11/18] git notes merge: Add automatic conflict resolvers (ours, theirs, union)","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-10-04T15:10:01Z","receivedAt":"2010-10-04T15:10:01Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Saturday 2. October 2010 11.14.46 Stephen Boyd wrote:\n> On 09/28/2010 05:23 PM, Johan Herland wrote:\n> > @@ -788,6 +792,21 @@ static int merge(int argc, const char **argv, const\n> > char *prefix)\n> > \n> >  \texpand_notes_ref(&remote_ref);\n> >  \to.remote_ref = remote_ref.buf;\n> > \n> > +\tif (strategy) {\n> > +\t\tif (!strcmp(strategy, \"manual\"))\n> > +\t\t\to.strategy = NOTES_MERGE_RESOLVE_MANUAL;\n> > +\t\telse if (!strcmp(strategy, \"ours\"))\n> > +\t\t\to.strategy = NOTES_MERGE_RESOLVE_OURS;\n> > +\t\telse if (!strcmp(strategy, \"theirs\"))\n> > +\t\t\to.strategy = NOTES_MERGE_RESOLVE_THEIRS;\n> > +\t\telse if (!strcmp(strategy, \"union\"))\n> > +\t\t\to.strategy = NOTES_MERGE_RESOLVE_UNION;\n> > +\t\telse {\n> > +\t\t\terror(\"Unknown -X/--resolve strategy: %s\", strategy);\n> \n> Is it -X/--resolve or -s/--strategy? This error confuses me.\n\nNo, the message should say \"Unknown -s/--strategy: %s\". Will be fixed in the \nnext iteration. Thanks for noticing.\n\n> > diff --git a/notes-merge.c b/notes-merge.c\n> > index f625ebd..6fa59d8 100644\n> > --- a/notes-merge.c\n> > +++ b/notes-merge.c\n> > @@ -262,6 +262,35 @@ static void diff_tree_local(struct\n> > notes_merge_options *o,\n> > \n> >  \tdiff_tree_release_paths(&opt);\n> >  \n> >  }\n> > \n> > +static int merge_one_change(struct notes_merge_options *o,\n> > +\t\t\t    struct notes_merge_pair *p, struct notes_tree *t)\n> > +{\n> > +\t/*\n> > +\t * Return 0 if change was resolved (and added to notes_tree),\n> > +\t * 1 if conflict\n> > +\t */\n> > +\tswitch (o->strategy) {\n> > +\tcase NOTES_MERGE_RESOLVE_MANUAL:\n> > +\t\treturn 1;\n> > +\tcase NOTES_MERGE_RESOLVE_OURS:\n> > +\t\tOUTPUT(o, 2, \"Using local notes for %s\", sha1_to_hex(p->obj));\n> > +\t\t/* nothing to do */\n> > +\t\treturn 0;\n> > +\tcase NOTES_MERGE_RESOLVE_THEIRS:\n> > +\t\tOUTPUT(o, 2, \"Using remote notes for %s\", sha1_to_hex(p-\n>obj));\n> > +\t\tif (add_note(t, p->obj, p->remote, combine_notes_overwrite))\n> > +\t\t\tdie(\"confused: combine_notes_overwrite failed\");\n> \n> This will say:\n> \n> fatal: confused: combine_notes_overwrite failed\n> \n> Do we actually need the \"confused\" part? Heh, maybe we need a confused()\n> function?\n\nWell, combine_notes_overwrite() can only return 0, so if we get a non-zero \nreturn here I will certainly be confused, since this should be impossible. \nMaybe better to drop the check altogether and simply leave add_note(...)?\n\n\nHave fun! :)\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"152538","messageId":"201010041715.08879.johan@herland.net","threadId":"25282","inReplyTo":"4CA6F385.7030207@gmail.com","subject":"Re: [PATCH 12/18] Documentation: Preliminary docs on 'git notes merge'","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-10-04T15:15:08Z","receivedAt":"2010-10-04T15:15:08Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Saturday 2. October 2010 10.55.33 Stephen Boyd wrote:\n> On 09/28/2010 05:23 PM, Johan Herland wrote:\n> > ++\n> > +If conflicts arise (and a strategy for automatically resolving\n> > +conflicting notes (see the -s/--strategy option) is not given,\n> > +the merge fails (TODO).\n> \n> Unbalanced '(' here. Can you just drop the first one?\n\nDone. Will be in next iteration.\n\n\nThanks,\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"152676","messageId":"20101005145543.GD12797@burratino","threadId":"25282","inReplyTo":"1285719811-10871-2-git-send-email-johan@herland.net","subject":"Re: [PATCH 01/18] (trivial) notes.h: Minor documentation fixes to copy_notes()","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-10-05T14:55:43Z","receivedAt":"2010-10-05T14:55:43Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Johan Herland wrote:\n\n> --- a/notes.h\n> +++ b/notes.h\n> @@ -104,6 +104,10 @@ const unsigned char *get_note(struct notes_tree *t,\n>   * Copy a note from one object to another in the given notes_tree.\n>   *\n>   * Fails if the to_obj already has a note unless 'force' is true.\n> + *\n> + * IMPORTANT: The changes made by copy_note() to the given notes_tree structure\n> + * are not persistent until a subsequent call to write_notes_tree() returns\n> + * zero.\n\nThis reminds me: I sometimes wish there were a\nDocumentation/technical/api-notes.txt giving a high-level overview of\nthe API, something like this to start (warning: formatting probably\nbroken):\n\n-- 8< --\nnotes API\n=========\n\nSo you want to write or access persistent, per-object text?  The notes\nAPI might help.\n\nCalling sequence\n----------------\n\nThe caller:\n\n* Allocates and clears a `struct notes_tree`, then fills it based on a\n  new or existing notes ref with `init_notes()`.\n\n* Adds, removes, and retrieves notes as desired:\n\n  . To add notes: use `write_sha1_file()` to create a blob object\n    containing the information to be stored, and add it to the\n    in-core notes tree with `add_note()`.\n\n  . Retrieve notes as blob objects with `get_note()`, or as\n    text with `format_note()`.\n\n  . Change which objects a note is attached to with `copy_note()`\n    and `remove_note()`.\n\n* Can iterate over all notes with `for_each_note()`.\n\n* Can remove notes attached to missing objects with `prune_notes()`.\n\n* (Optionally) makes changes persist with `write_notes_tree()`.\n\n* Frees resources associated to the notes tree with `free_notes()`.\n\nA program like 'git log' that needs to find the notes corresponding\nto certain objects in multiple notes trees might instead use the\ndisplay notes API.  In this case, the caller:\n\n* (Optionally) prepares a `struct display_notes_opt` with settings\n  about which notes trees to use.\n\n* Initializes the display notes machinery with `init_display_notes()`.\n\n* Retrieves notes for each object of interest with\n  `format_display_notes()`.\n-- >8 --\n"},{"id":"152680","messageId":"20101005152120.GE12797@burratino","threadId":"25282","inReplyTo":"1285719811-10871-4-git-send-email-johan@herland.net","subject":"Re: [PATCH 03/18] notes.h/c: Clarify the handling of notes objects that are == null_sha1","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-10-05T15:21:20Z","receivedAt":"2010-10-05T15:21:20Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Johan Herland wrote:\n\n> The only functional changes in this patch concern the handling of null_sha1\n> in notes_tree_insert(). Otherwise the patch consists solely of reordering\n> functions in notes.c to avoid use-before-declaration\n\nWould it makes sense to split off that no-op as a separate patch?\n\n> --- a/notes.c\n> +++ b/notes.c\n> @@ -175,7 +248,10 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n>  \tswitch (GET_PTR_TYPE(*p)) {\n>  \tcase PTR_TYPE_NULL:\n>  \t\tassert(!*p);\n> -\t\t*p = SET_PTR_TYPE(entry, type);\n> +\t\tif (is_null_sha1(entry->val_sha1))\n> +\t\t\tfree(entry);\n> +\t\telse\n> +\t\t\t*p = SET_PTR_TYPE(entry, type);\n>  \t\treturn;\n>  \tcase PTR_TYPE_NOTE:\n>  \t\tswitch (type) {\n\nNo note present, but the node for one is.  This skips insertion of\nempty notes, for consistency with:\n\n> @@ -191,6 +267,9 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n>  \t\t\t\t\t    sha1_to_hex(l->val_sha1),\n>  \t\t\t\t\t    sha1_to_hex(entry->val_sha1),\n>  \t\t\t\t\t    sha1_to_hex(l->key_sha1));\n> +\n> +\t\t\t\tif (is_null_sha1(l->val_sha1))\n> +\t\t\t\t\tnote_tree_remove(t, tree, n, entry);\n\nThe note-present case, where the combine_notes() function can\nreturn a null sha1 to request that a note be removed.\n\n>  \t\t\t\tfree(entry);\n>  \t\t\t\treturn;\n>  \t\t\t}\n> @@ -222,6 +301,10 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n>  \t/* non-matching leaf_node */\n>  \tassert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||\n>  \t       GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);\n> +\tif (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */\n> +\t\tfree(entry);\n> +\t\treturn;\n> +\t}\n\nThe more usual no-note-present case.  Again, this skips insertion\nof empty notes.\n\nDo I understand correctly that the point of the main point of\nthis patch is to allow combine_notes() functions to request\nthat a note be deleted?  If so, it would be nice if the commit\nmessage said so.\n\nRegardless, for what it's worth,\nAcked-by: Jonathan Nieder <jrnieder@gmail.com>\n"},{"id":"152679","messageId":"201010051722.21081.johan@herland.net","threadId":"25282","inReplyTo":"20101005145543.GD12797@burratino","subject":"Re: [PATCH 01/18] (trivial) notes.h: Minor documentation fixes to copy_notes()","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-10-05T15:22:20Z","receivedAt":"2010-10-05T15:22:20Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Tuesday 5. October 2010 16.55.43 Jonathan Nieder wrote:\n> Johan Herland wrote:\n> > --- a/notes.h\n> > +++ b/notes.h\n> > @@ -104,6 +104,10 @@ const unsigned char *get_note(struct notes_tree *t,\n> > \n> >   * Copy a note from one object to another in the given notes_tree.\n> >   *\n> >   * Fails if the to_obj already has a note unless 'force' is true.\n> > \n> > + *\n> > + * IMPORTANT: The changes made by copy_note() to the given notes_tree structure\n> > + * are not persistent until a subsequent call to write_notes_tree() returns\n> > + * zero.\n> \n> This reminds me: I sometimes wish there were a\n> Documentation/technical/api-notes.txt giving a high-level overview of\n> the API,\n\nI thought studying the notes.h file and its comments was a sufficient\nintroduction on how to work with notes in code. I'm certainly biased,\nthough (as I obviously already know how to work with notes in code)...\n\nIf you believe that api-notes.txt is a valuable addition to\nDocumentation/, I have no problem ack-ing a patch that adds it.\n\n> something like this to start (warning: formatting probably broken):\n> \n> -- 8< --\n> notes API\n> =========\n> \n> So you want to write or access persistent, per-object text?  The notes\n> API might help.\n> \n> Calling sequence\n> ----------------\n> \n> The caller:\n> \n> * Allocates and clears a `struct notes_tree`, then fills it based on a\n>   new or existing notes ref with `init_notes()`.\n> \n> * Adds, removes, and retrieves notes as desired:\n> \n>   . To add notes: use `write_sha1_file()` to create a blob object\n>     containing the information to be stored, and add it to the\n>     in-core notes tree with `add_note()`.\n> \n>   . Retrieve notes as blob objects with `get_note()`, or as\n>     text with `format_note()`.\n> \n>   . Change which objects a note is attached to with `copy_note()`\n>     and `remove_note()`.\n> \n> * Can iterate over all notes with `for_each_note()`.\n> \n> * Can remove notes attached to missing objects with `prune_notes()`.\n> \n> * (Optionally) makes changes persist with `write_notes_tree()`.\n> \n> * Frees resources associated to the notes tree with `free_notes()`.\n> \n> A program like 'git log' that needs to find the notes corresponding\n> to certain objects in multiple notes trees might instead use the\n> display notes API.  In this case, the caller:\n> \n> * (Optionally) prepares a `struct display_notes_opt` with settings\n>   about which notes trees to use.\n> \n> * Initializes the display notes machinery with `init_display_notes()`.\n> \n> * Retrieves notes for each object of interest with\n>   `format_display_notes()`.\n> -- >8 --\n\nThis looks like a good start.\n\n\n...Johan\n\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"152681","messageId":"20101005152651.GF12797@burratino","threadId":"25282","inReplyTo":"201010051722.21081.johan@herland.net","subject":"Re: [PATCH 01/18] (trivial) notes.h: Minor documentation fixes to copy_notes()","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-10-05T15:26:51Z","receivedAt":"2010-10-05T15:26:51Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Johan Herland wrote:\n\n> I thought studying the notes.h file and its comments was a sufficient\n> introduction on how to work with notes in code.\n\nYeah, I took most of the explanation from notes.h.  But it can be nice\nto have the high-level \"here are the functions you have to call\"\ninformation in one place without hunting around for it.\n\n> This looks like a good start.\n\nThanks.\n"},{"id":"152685","messageId":"20101005153838.GG12797@burratino","threadId":"25282","inReplyTo":"1285719811-10871-5-git-send-email-johan@herland.net","subject":"Re: [PATCH 04/18] notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-10-05T15:38:38Z","receivedAt":"2010-10-05T15:38:38Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Johan Herland wrote:\n\n> The combine_notes_fn functions uses a non-zero return value to indicate\n> failure. However, this return value was converted to a call to die()\n> in note_tree_insert().\n\nFor what it's worth, I still like it.\n\n> Instead, propagate this return value out to add_note(), and return it\n> from there\n\nIt's always odd deciding what to do in the code paths that \"can't\nhappen\".  Ideally one would want static analyzers to check that, while\nat the same time subjecting the user to some nice graceful fallback\nbehavior when it happens anyway.\n\n\tfatal: confused: combine_notes_overwrite failed\n\nIn this case I'm not sure what the message in die() should be, but\nit seems sane to die with some message.  Maybe this should be\nmentioned in the commit message, though?\n"},{"id":"152687","messageId":"20101005155053.GH12797@burratino","threadId":"25282","inReplyTo":"1285719811-10871-8-git-send-email-johan@herland.net","subject":"Re: [PATCH 07/18] builtin/notes.c: Split notes ref DWIMmery into a separate function","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-10-05T15:50:53Z","receivedAt":"2010-10-05T15:50:53Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Johan Herland wrote:\n\n> --- a/builtin/notes.c\n> +++ b/builtin/notes.c\n> @@ -83,6 +83,16 @@ struct msg_arg {\n>  \tstruct strbuf buf;\n>  };\n>  \n> +static void expand_notes_ref(struct strbuf *sb)\n> +{\n> +\tif (!prefixcmp(sb->buf, \"refs/notes/\"))\n> +\t\treturn; /* we're happy */\n> +\telse if (!prefixcmp(sb->buf, \"notes/\"))\n> +\t\tstrbuf_insert(sb, 0, \"refs/\", 5);\n> +\telse\n> +\t\tstrbuf_insert(sb, 0, \"refs/notes/\", 11);\n> +}\n\nAside: I'm not sure this is the most convenient rule to use after all.\n\nExample:\n\n $ git log --notes-ref=charon/notes/full\n fatal: unrecognized argument: --notes-ref=charon/notes/full\n $ git log --show-notes=charon/notes/full\n warning: notes ref refs/notes/charon/notes/full is invalid\n...\n $ git log --show-notes=remotes/charon/notes/full\n warning: notes ref refs/notes/remotes/charon/notes/full is invalid\n...\n $ git log --show-notes=refs/remotes/charon/notes/full -1\n commit 16461e8e5fc5b2dbe9176b9a8313c395e1e07304\n Merge: c3e5a06 79bd09f\n Author: Junio C Hamano <gitster@pobox.com>\n Date:   Thu Sep 30 16:02:27 2010 -0700\n \n     Merge branch 'il/remote-fd-ext' into pu\n     \n     * il/remote-fd-ext:\n       fixup! git-remote-fd\n \n Notes (remotes/charon/notes/full):\n     Pu-Overview:\n         What's cooking in git.git (Sep 2010, #07; Wed, 29)\n $\n\nAnd now that I think of it, the revision args parser uses its own code\nfor this...\n\nRegardless, this patch is a step in the right direction imho.\n"},{"id":"152732","messageId":"201010060030.26335.johan@herland.net","threadId":"25282","inReplyTo":"20101005152120.GE12797@burratino","subject":"Re: [PATCH 03/18] notes.h/c: Clarify the handling of notes objects that are == null_sha1","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-10-05T22:30:25Z","receivedAt":"2010-10-05T22:30:25Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Tuesday 5. October 2010 17.21.20 Jonathan Nieder wrote:\n> Johan Herland wrote:\n> > The only functional changes in this patch concern the handling of\n> > null_sha1 in notes_tree_insert(). Otherwise the patch consists solely of\n> > reordering functions in notes.c to avoid use-before-declaration\n> \n> Would it makes sense to split off that no-op as a separate patch?\n\nYes. This will be done in the next iteration.\n\n> > --- a/notes.c\n> > +++ b/notes.c\n> > @@ -175,7 +248,10 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n> > \n> >  \tswitch (GET_PTR_TYPE(*p)) {\n> >  \t\n> >  \tcase PTR_TYPE_NULL:\n> >  \t\tassert(!*p);\n> > \n> > -\t\t*p = SET_PTR_TYPE(entry, type);\n> > +\t\tif (is_null_sha1(entry->val_sha1))\n> > +\t\t\tfree(entry);\n> > +\t\telse\n> > +\t\t\t*p = SET_PTR_TYPE(entry, type);\n> > \n> >  \t\treturn;\n> >  \t\n> >  \tcase PTR_TYPE_NOTE:\n> >  \t\tswitch (type) {\n> \n> No note present, but the node for one is.  This skips insertion of\n> empty notes, for consistency with:\n> \n> > @@ -191,6 +267,9 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n> > \n> >  \t\t\t\t\t    sha1_to_hex(l->val_sha1),\n> >  \t\t\t\t\t    sha1_to_hex(entry->val_sha1),\n> >  \t\t\t\t\t    sha1_to_hex(l->key_sha1));\n> > \n> > +\n> > +\t\t\t\tif (is_null_sha1(l->val_sha1))\n> > +\t\t\t\t\tnote_tree_remove(t, tree, n, entry);\n> \n> The note-present case, where the combine_notes() function can\n> return a null sha1 to request that a note be removed.\n> \n> >  \t\t\t\tfree(entry);\n> >  \t\t\t\treturn;\n> >  \t\t\t\n> >  \t\t\t}\n> > \n> > @@ -222,6 +301,10 @@ static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n> > \n> >  \t/* non-matching leaf_node */\n> >  \tassert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||\n> >  \t\n> >  \t       GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);\n> > \n> > +\tif (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */\n> > +\t\tfree(entry);\n> > +\t\treturn;\n> > +\t}\n> \n> The more usual no-note-present case.  Again, this skips insertion\n> of empty notes.\n\nAll correct.\n\n> Do I understand correctly that the point of the main point of\n> this patch is to allow combine_notes() functions to request\n> that a note be deleted?  If so, it would be nice if the commit\n> message said so.\n\nIndeed, that is the main point. I believe the paragraph following the\ncommit subject did indeed explain this, but I will try to clarify this\nfurther in the next iteration.\n\n> Regardless, for what it's worth,\n> Acked-by: Jonathan Nieder <jrnieder@gmail.com>\n\nThanks,\n\n...Johan\n\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"152775","messageId":"201010062140.56999.johan@herland.net","threadId":"25282","inReplyTo":"20101005153838.GG12797@burratino","subject":"Re: [PATCH 04/18] notes.h/c: Propagate combine_notes_fn return value to add_note() and beyond","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-10-06T19:40:56Z","receivedAt":"2010-10-06T19:40:56Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Tuesday 5. October 2010 17.38.38 Jonathan Nieder wrote:\n> Johan Herland wrote:\n> > The combine_notes_fn functions uses a non-zero return value to indicate\n> > failure. However, this return value was converted to a call to die()\n> > in note_tree_insert().\n> \n> For what it's worth, I still like it.\n> \n> > Instead, propagate this return value out to add_note(), and return it\n> > from there\n> \n> It's always odd deciding what to do in the code paths that \"can't\n> happen\".  Ideally one would want static analyzers to check that, while\n> at the same time subjecting the user to some nice graceful fallback\n> behavior when it happens anyway.\n> \n> \tfatal: confused: combine_notes_overwrite failed\n> \n> In this case I'm not sure what the message in die() should be, but\n> it seems sane to die with some message.  Maybe this should be\n> mentioned in the commit message, though?\n\nOk, I've added a paragraph to the commit message mentioning that most of \nadd_note()'s callers now implement the die() previously located in \nnotes_tree_insert(). Will be in the next iteration.\n\n\nThanks,\n\n...Johan\n\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"152841","messageId":"20101007043721.GD2285@burratino","threadId":"25282","inReplyTo":"1285719811-10871-9-git-send-email-johan@herland.net","subject":"Test script style (Re: [PATCH 08/18] git notes merge: Initial implementation handling trivial merges only)","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-10-07T04:37:21Z","receivedAt":"2010-10-07T04:37:21Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Johan Herland wrote:\n\n> This initial implementation of 'git notes merge' only handles the trivial\n> merge cases (i.e. where the merge is either a no-op, or a fast-forward).\n\nMm, sounds like a nice thing to have anyway.\n\nNow to digress: some generalities about test scripts.  This will\nprobably be very tedious.  I'm writing it as groundwork before\nreviewing your test (since I don't feel on solid ground about this\ntopic at all).\n\nMore focused thoughts on other aspects of the patch will follow in a\nseparate message.\n\nFirst:\n\n\tSee http://thread.gmane.org/gmane.comp.version-control.git/155596/focus=155673\n\tIf you don't like what you see, no need to read the rest\n\tof this message. :)\n\nMotivation:\n\n\tNot being the best of shell script readers (or writers,\n\tfor that matter), I need all the help from stylistic cues\n\tI can get.  Old test scripts have a consistent style\n\tgenerally, but newer ones are starting to diverge from\n\tone another.\n\nA rough test format:\n\n\t#!/bin/sh\n\t#\n\t# Copyright (c) 2010 ...\n\t#\n\n\ttest_description='...\n\t. ./test-lib.sh\n\t. ...\n\n\tconstant data\n\tfunction definitions\n\n\tsetup }\n\ttest  }\n\ttest  } repeat as necessary\n\ttest  }\n\n\ttest_done\n\nTest description:\n\n\ttest_description = 'One-line description\n\n\tSome words or diagrams describing the invariants (e.g., history)\n\tmaintained between tests'\n\nConstant data:\n\n\tSimple commands to produce test vectors, expected output, and\n\tother variables and files that it might be convenient to have\n\tunchanged available throughout the test script.\n\n\tBecause not guarded by a test assertion, this section would\n\tnot include any git commands, nor any commands that might fail\n\tor write to the terminal.  So: this section might use\n\t\"cat <<\\\", \"mkdir -p\", \"cp\", \"cp -R\", \"mv\", \"printf\", \"foo=\",\n\t\"unset\", and \"export\", but not much else.\n\nFunction definitions:\n\n\tTests can define functions inside a test_expect_success block,\n\ttoo, but the generally useful functions would go up front with\n\tthe constant data.\n\nSetup:\n\n\ttest_expect_success '(setup | set up) ...' '\n\t\tCommands to set up for the next batch of tests.\n\t\tCan rely on previous setup and can do just about\n\t\tanything.\n\t'\n\nTests:\n\ttest_expect_success|failure '... some claim...' '\n\t\tCommands to test that claim.\n\t\tCould do all sorts of things, as long as they do not\n\t\tdisturb the invariants established by the\n\t\tconstant_data and setup sections.\n\t'\n\nWhat is not present:\n\n\tA test in this style would not include any commands except for\n\ttest_done outside a test assertion after the first\n\ttest_expect_success.\n\nThis is meant to be a sort of compromise between what t/README says\n(\"Put all code inside test_expect_success and other assertions\") and\nwhat people actually do.  I have no problem with people doing\nsomething else --- in fact, some tests would not make any sense in\nthis style.  In general, if a person is writing maintainable tests\nthat do not slow down \"make test\" by much, it does not make much sense\nto put new obstacles in her way.\n\nWhat I expect is that new tests would be easier to read and extend in\nthis style, since to get started, all a person has to pay attention to\nare the setup commands.  So a potential collaborator could say \"if you\nuse this structure, it will make my life easier\".\n\nThoughts?  Would it make sense to eventually put something like this\nin t/CodingStyle or nearby?\n"},{"id":"152844","messageId":"7vk4luy4zu.fsf@alter.siamese.dyndns.org","threadId":"25282","inReplyTo":"20101007043721.GD2285@burratino","subject":"Re: Test script style (Re: [PATCH 08/18] git notes merge: Initial implementation handling trivial merges only)","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-10-07T04:57:25Z","receivedAt":"2010-10-07T04:57:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jonathan Nieder <jrnieder@gmail.com> writes:\n\n> Thoughts?  Would it make sense to eventually put something like this\n> in t/CodingStyle or nearby?\n\n> Tests:\n> \ttest_expect_success|failure '... some claim...' '\n> \t\tCommands to test that claim.\n> \t\tCould do all sorts of things, as long as they do not\n> \t\tdisturb the invariants established by the\n> \t\tconstant_data and setup sections.\n> \t'\n\nIn practice, I am afraid it would take a lot more discipline than any of\nthe current test script does to actually preserve the invariants.  But if\nwe can arrange that, it would be ideal.  For one thing, it would finally\nmake the subtest skip feature of GIT_SKIP_TESTS usable.\n\nAn obvious way to do so would be\n\n Tests:\n \ttest_expect_success|failure '... some claim...' '\n+ \t\tCommands to establish a known precondition without\n+\t\tdepending on the state left by previous steps.\n \t\tCommands to test that claim.\n-\t\tCould do all sorts of things, as long as they do not\n-\t\tdisturb the invariants established by the\n-\t\tconstant_data and setup sections.\n \t'\n\nbut if done naively, the time it takes to re-establish a known\nprecondition for each and every test would add up to substantial\noverhead.\n"},{"id":"152850","messageId":"20101007062433.GF2285@burratino","threadId":"25282","inReplyTo":"1285719811-10871-9-git-send-email-johan@herland.net","subject":"Re: [PATCH 08/18] git notes merge: Initial implementation handling trivial merges only","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-10-07T06:24:33Z","receivedAt":"2010-10-07T06:24:33Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Johan Herland wrote:\n\n> This initial implementation of 'git notes merge' only handles the trivial\n> merge cases (i.e. where the merge is either a no-op, or a fast-forward).\n\nThis alone should already be a nice UI improvement: in the\nfast-forward case, with this patch applied, in place of\n\n\tgit fetch . refs/notes/x:refs/notes/commits\n\none could write\n\n\tgit notes merge x\n\nThis reminds me: it would be nice if non-builtin scripts could use\n\n\tgit notes --get-notes-ref $refarg\n\nto learn the configured notes ref.  In other words, shouldn't the\ndefault_notes_ref() exposed in patch 2 also be exposed to scripted\ncallers?  If someone else doesn't get around to it, I can mock\nsomething up in the next few days.\n\n[...]\n> +++ b/notes-merge.h\n> @@ -0,0 +1,30 @@\n> +#ifndef NOTES_MERGE_H\n> +#define NOTES_MERGE_H\n> +\n> +struct notes_merge_options {\n> +\tconst char *local_ref;\n> +\tconst char *remote_ref;\n> +\tint verbosity;\n> +};\n> +\n> +void init_notes_merge_options(struct notes_merge_options *o);\n[...]\n> +int notes_merge(struct notes_merge_options *o,\n> +\t\tunsigned char *result_sha1);\n\nSo the command is usable as-is from other builtins.  Nice.\n\n> +++ b/notes-merge.c\n> @@ -0,0 +1,103 @@\n[...]\n> +#include \"cache.h\"\n> +#include \"commit.h\"\n> +#include \"notes-merge.h\"\n> +\n> +void init_notes_merge_options(struct notes_merge_options *o)\n> +{\n> +\tmemset(o, 0, sizeof(struct notes_merge_options));\n> +\to->verbosity = 2;\n> +}\n> +\n> +static int show(struct notes_merge_options *o, int v)\n> +{\n> +\treturn (o->verbosity >= v) || o->verbosity >= 5;\n> +}\n\nShould the verbosities be of enum type?\n\n\tenum notes_merge_verbosity {\n\t\tDEFAULT_VERBOSITY = 2,\n\t\tMAX_VERBOSITY = 5,\n\t\tetc\n\t};\n\n> +\n> +#define OUTPUT(o, v, ...) \\\n> +\tdo { if (show((o), (v))) { printf(__VA_ARGS__); puts(\"\"); } } while (0)\n\nI would find it easier to read\n\n\tif (o->verbosity >= DEFAULT_VERBOSITY)\n\t\tfprintf(stderr, ...)\n\nunless there are going to be a huge number of messages.\n\n> +\n> +int notes_merge(struct notes_merge_options *o,\n> +\t\tunsigned char *result_sha1)\n> +{\n> +\tunsigned char local_sha1[20], remote_sha1[20];\n> +\tstruct commit *local, *remote;\n> +\tstruct commit_list *bases = NULL;\n> +\tconst unsigned char *base_sha1;\n> +\tint result = 0;\n> +\n> +\thashclr(result_sha1);\n> +\n> +\tOUTPUT(o, 5, \"notes_merge(o->local_ref = %s, o->remote_ref = %s)\",\n> +\t       o->local_ref, o->remote_ref);\n\nWould trace_printf be a good fit for messages like this one?  If not,\nany idea about how it could be made to fit some day?\n\n(It is especially nice to be able to direct the trace somewhere other\nthan stdout and stderr when running tests.)\n\n> +\n> +\tif (!o->local_ref || get_sha1(o->local_ref, local_sha1)) {\n> +\t\t/* empty notes ref => assume empty notes tree */\n\nCan an empty ref be distinguished from a missing ref?  It would be\nnice to error out when breakage is detected.\n\n[...]\n> +\t/* Find merge bases */\n> +\tbases = get_merge_bases(local, remote, 1);\n> +\tif (!bases) {\n> +\t\tbase_sha1 = null_sha1;\n> +\t\tOUTPUT(o, 4, \"No merge base found; doing history-less merge\");\n> +\t} else if (!bases->next) {\n> +\t\tbase_sha1 = bases->item->object.sha1;\n> +\t\tOUTPUT(o, 4, \"One merge base found (%.7s)\",\n> +\t\t       sha1_to_hex(base_sha1));\n> +\t} else {\n> +\t\t/* TODO: How to handle multiple merge-bases? */\n\nWith a recursive merge of the ancestors, of course. :)\n\nThe difficult part is what to do when a merge of ancestors results in\nconflicts.\n\n[...]\n> +++ b/builtin/notes.c\n> @@ -772,6 +779,50 @@ static int show(int argc, const char **argv, const char *prefix)\n>  \treturn retval;\n>  }\n>  \n> +static int merge(int argc, const char **argv, const char *prefix)\n> +{\n> +\tstruct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;\n> +\tunsigned char result_sha1[20];\n> +\tstruct notes_merge_options o;\n> +\tint verbosity = 0, result;\n> +\tstruct option options[] = {\n> +\t\tOPT__VERBOSITY(&verbosity),\n> +\t\tOPT_END()\n> +\t};\n> +\n> +\targc = parse_options(argc, argv, prefix, options,\n> +\t\t\t     git_notes_merge_usage, 0);\n> +\n> +\tif (argc != 1) {\n> +\t\terror(\"Must specify a notes ref to merge\");\n> +\t\tusage_with_options(git_notes_merge_usage, options);\n> +\t}\n> +\n> +\tinit_notes_merge_options(&o);\n> +\to.verbosity = verbosity + 2; // default verbosity level is 2\n\nMaybe\n\n\to.verbosity += verbosity;\n\nor\n\n\to.verbosity = DEFAULT_NOTES_MERGE_VERBOSITY + verbosity;\n\nto allow the default verbosity to be set in one place?\n\n> +\to.local_ref = default_notes_ref();\n> +\tstrbuf_addstr(&remote_ref, argv[0]);\n> +\texpand_notes_ref(&remote_ref);\n> +\to.remote_ref = remote_ref.buf;\n> +\n> +\tresult = notes_merge(&o, result_sha1);\n> +\n> +\tstrbuf_addf(&msg, \"notes: Merged notes from %s into %s\",\n> +\t\t    remote_ref.buf, default_notes_ref());\n> +\tif (result == 0) { /* Merge resulted (trivially) in result_sha1 */\n> +\t\t/* Update default notes ref with new commit */\n> +\t\tupdate_ref(msg.buf, default_notes_ref(), result_sha1, NULL,\n> +\t\t\t   0, DIE_ON_ERR);\n> +\t} else {\n> +\t\t/* TODO: */\n> +\t\tdie(\"'git notes merge' cannot yet handle non-trivial merges!\");\n\nMm.  In the long run, will (result != 0) mean \"merge conflict\"?\n\n> @@ -865,6 +916,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)\n>  \t\tresult = append_edit(argc, argv, prefix);\n>  \telse if (!strcmp(argv[0], \"show\"))\n>  \t\tresult = show(argc, argv, prefix);\n> +\telse if (!strcmp(argv[0], \"merge\"))\n> +\t\tresult = merge(argc, argv, prefix);\n\nThanks for a pleasant read.\n"},{"id":"152874","messageId":"201010071556.14555.johan@herland.net","threadId":"25282","inReplyTo":"20101005155053.GH12797@burratino","subject":"Re: [PATCH 07/18] builtin/notes.c: Split notes ref DWIMmery into a separate function","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-10-07T13:56:13Z","receivedAt":"2010-10-07T13:56:13Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Tuesday 5. October 2010 17.50.53 Jonathan Nieder wrote:\n> Johan Herland wrote:\n> > +static void expand_notes_ref(struct strbuf *sb)\n> > +{\n> > +\tif (!prefixcmp(sb->buf, \"refs/notes/\"))\n> > +\t\treturn; /* we're happy */\n> > +\telse if (!prefixcmp(sb->buf, \"notes/\"))\n> > +\t\tstrbuf_insert(sb, 0, \"refs/\", 5);\n> > +\telse\n> > +\t\tstrbuf_insert(sb, 0, \"refs/notes/\", 11);\n> > +}\n> \n> Aside: I'm not sure this is the most convenient rule to use after all.\n\nFTR, that rule is not introduced by this patch, the patch merely moves it into \na separate function.\n\n> Example:\n> \n>  $ git log --notes-ref=charon/notes/full\n>  fatal: unrecognized argument: --notes-ref=charon/notes/full\n>  $ git log --show-notes=charon/notes/full\n>  warning: notes ref refs/notes/charon/notes/full is invalid\n> ...\n>  $ git log --show-notes=remotes/charon/notes/full\n>  warning: notes ref refs/notes/remotes/charon/notes/full is invalid\n> ...\n>  $ git log --show-notes=refs/remotes/charon/notes/full -1\n>  commit 16461e8e5fc5b2dbe9176b9a8313c395e1e07304\n>  Merge: c3e5a06 79bd09f\n>  Author: Junio C Hamano <gitster@pobox.com>\n>  Date:   Thu Sep 30 16:02:27 2010 -0700\n> \n>      Merge branch 'il/remote-fd-ext' into pu\n> \n>      * il/remote-fd-ext:\n>        fixup! git-remote-fd\n> \n>  Notes (remotes/charon/notes/full):\n>      Pu-Overview:\n>          What's cooking in git.git (Sep 2010, #07; Wed, 29)\n>  $\n\nIndeed, if you keep notes refs outside refs/notes, the current behaviour is \nnot very user-friendly. So far, we've required all notes refs to be within \nrefs/notes. (See for example init_notes_check() in builtin/notes.c, which \nrefuses to work with notes refs outside 'refs/notes/', hence was probably the \nreason for adding the above code in the first place.)\n\nI guess this moves us towards the discussion of how to handle remote notes \nrefs and how to synchronize them on fetch/push. In short, if we want to keep \nnotes refs outside of refs/notes (e.g. in refs/remotes/foo/notes), we need to \nchange this part of the code.\n\n> And now that I think of it, the revision args parser uses its own code\n> for this...\n\nI assuming you're talking about the revision args parser's DWIMing of \"foo\" \ninto the first existing ref in the following list:\n\n1. $GIT_DIR/foo\n2. refs/foo\n3. refs/tags/foo\n4. refs/heads/foo\n5. refs/remotes/foo\n6. refs/remotes/foo/HEAD\n\nIf we want to reuse this DWIMery, we obviously want a different list of \"auto-\ncompletions\". Maybe something like:\n\n1. refs/notes/foo\n2. refs/remote-notes/foo (depends on how we organize remote notes refs)\n3. ?\n\n> Regardless, this patch is a step in the right direction imho.\n\nThanks, I expect future patches to change this code in order to deal with \nremote notes refs. For the purposes of the current patch series, I will assume \nthat all notes refs live under refs/notes.\n\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"153020","messageId":"201010090155.20858.johan@herland.net","threadId":"25282","inReplyTo":"20101007062433.GF2285@burratino","subject":"Re: [PATCH 08/18] git notes merge: Initial implementation handling trivial merges only","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-10-08T23:55:19Z","receivedAt":"2010-10-08T23:55:19Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Thursday 7. October 2010 08.24.33 Jonathan Nieder wrote:\n> Johan Herland wrote:\n> > This initial implementation of 'git notes merge' only handles the trivial\n> > merge cases (i.e. where the merge is either a no-op, or a fast-forward).\n> \n> [...]\n> \n> This reminds me: it would be nice if non-builtin scripts could use\n> \n> \tgit notes --get-notes-ref $refarg\n> \n> to learn the configured notes ref.  In other words, shouldn't the\n> default_notes_ref() exposed in patch 2 also be exposed to scripted\n> callers?\n\nI agree, but I'd like to name it 'git notes get-ref' instead, to stay\nconsistent with the current subcommand (instead of option) design.\n\n> If someone else doesn't get around to it, I can mock\n> something up in the next few days.\n\nThere is a patch implementing 'git notes get-ref' in the next iteration\nof this series. :)\n\n> > +++ b/notes-merge.h\n> > @@ -0,0 +1,30 @@\n> > +#ifndef NOTES_MERGE_H\n> > +#define NOTES_MERGE_H\n> > +\n> > +struct notes_merge_options {\n> > +\tconst char *local_ref;\n> > +\tconst char *remote_ref;\n> > +\tint verbosity;\n> > +};\n> > +\n> > +void init_notes_merge_options(struct notes_merge_options *o);\n> \n> [...]\n> \n> > +int notes_merge(struct notes_merge_options *o,\n> > +\t\tunsigned char *result_sha1);\n> \n> So the command is usable as-is from other builtins.  Nice.\n\nYep, that's the idea. notes-merge.h is really only an extension of\nnotes.h for providing the merge-related functionality.\n\n> > +++ b/notes-merge.c\n> > @@ -0,0 +1,103 @@\n> \n> [...]\n> \n> > +void init_notes_merge_options(struct notes_merge_options *o)\n> > +{\n> > +\tmemset(o, 0, sizeof(struct notes_merge_options));\n> > +\to->verbosity = 2;\n> > +}\n> > +\n> > +static int show(struct notes_merge_options *o, int v)\n> > +{\n> > +\treturn (o->verbosity >= v) || o->verbosity >= 5;\n> > +}\n> \n> Should the verbosities be of enum type?\n> \n> \tenum notes_merge_verbosity {\n> \t\tDEFAULT_VERBOSITY = 2,\n> \t\tMAX_VERBOSITY = 5,\n> \t\tetc\n> \t};\n\nMaybe. I've added these constants and used them where it made sense\n(to me) in the next iteration.\n\n> > +\n> > +#define OUTPUT(o, v, ...) \\\n> > +\tdo { if (show((o), (v))) { printf(__VA_ARGS__); puts(\"\"); } } while (0)\n> \n> I would find it easier to read\n> \n> \tif (o->verbosity >= DEFAULT_VERBOSITY)\n> \t\tfprintf(stderr, ...)\n> \n> unless there are going to be a huge number of messages.\n\nThe current version is modeled on the show() and output() functions in\nmerge-recursive.c. I think that works better in this situation.\nOr maybe you have a better solution for merge-recursive.c as well?\n\n> > +\n> > +int notes_merge(struct notes_merge_options *o,\n> > +\t\tunsigned char *result_sha1)\n> > +{\n> > +\tunsigned char local_sha1[20], remote_sha1[20];\n> > +\tstruct commit *local, *remote;\n> > +\tstruct commit_list *bases = NULL;\n> > +\tconst unsigned char *base_sha1;\n> > +\tint result = 0;\n> > +\n> > +\thashclr(result_sha1);\n> > +\n> > +\tOUTPUT(o, 5, \"notes_merge(o->local_ref = %s, o->remote_ref = %s)\",\n> > +\t       o->local_ref, o->remote_ref);\n> \n> Would trace_printf be a good fit for messages like this one?  If not,\n> any idea about how it could be made to fit some day?\n> \n> (It is especially nice to be able to direct the trace somewhere other\n> than stdout and stderr when running tests.)\n\nAgreed. The OUTPUT(o, 5, ...) instances should use trace_printf()\ninstead. Fixed in the next iteration.\n\n> > +\n> > +\tif (!o->local_ref || get_sha1(o->local_ref, local_sha1)) {\n> > +\t\t/* empty notes ref => assume empty notes tree */\n> \n> Can an empty ref be distinguished from a missing ref?  It would be\n> nice to error out when breakage is detected.\n\nI'm not sure when you think we should (or shouldn't) error out. FWIW,\nI've modeled the current code on what 'git merge' does. See [1] for\nmore details.\n\n> > +\t/* Find merge bases */\n> > +\tbases = get_merge_bases(local, remote, 1);\n> > +\tif (!bases) {\n> > +\t\tbase_sha1 = null_sha1;\n> > +\t\tOUTPUT(o, 4, \"No merge base found; doing history-less merge\");\n> > +\t} else if (!bases->next) {\n> > +\t\tbase_sha1 = bases->item->object.sha1;\n> > +\t\tOUTPUT(o, 4, \"One merge base found (%.7s)\",\n> > +\t\t       sha1_to_hex(base_sha1));\n> > +\t} else {\n> > +\t\t/* TODO: How to handle multiple merge-bases? */\n> \n> With a recursive merge of the ancestors, of course. :)\n> \n> The difficult part is what to do when a merge of ancestors results in\n> conflicts.\n\nExactly. I feel notes merge conflicts are confusing enough to the\naverage user, and I'd rather not expose them to resolving conflicts in\n_recursive_ notes merges...\n\nI guess this becomes a discussion of whether we should model notes\nmerges on the 'resolve' merge strategy or the 'recursive' merge\nstrategy. Without having studied each strategy in-depth, I don't know\nhow much \"better\" 'recursive' is than 'resolve', especially not from\nthe POV of notes merges. Are there features of 'recursive' that are\nuseful to notes merges?\n\n(All I know of the effectual differences between 'resolve' and\n'recursive' is the hand waving on the 'git merge' man page:\n  \"...has been reported to result in fewer merge conflicts without\n   causing mis-merges by tests done on actual merge commits taken from\n   Linux 2.6 kernel development history.\")\n\nIn conclusion, if someone can show that's it's worth implementing\nrecursive notes merging, then I'll gladly do it, but until then I'll\nstick with the simplicity of using the first merge base.\n\n> > +++ b/builtin/notes.c\n> > @@ -772,6 +779,50 @@ static int show(int argc, const char **argv, const\n> > +static int merge(int argc, const char **argv, const char *prefix)\n> > +{\n> > [...]\n> > +\to.verbosity = verbosity + 2; // default verbosity level is 2\n> \n> Maybe\n> \n> \to.verbosity += verbosity;\n> \n> or\n> \n> \to.verbosity = DEFAULT_NOTES_MERGE_VERBOSITY + verbosity;\n> \n> to allow the default verbosity to be set in one place?\n\nAgreed. Fixed in the next iteration.\n\n> > +\tresult = notes_merge(&o, result_sha1);\n> > +\n> > +\tstrbuf_addf(&msg, \"notes: Merged notes from %s into %s\",\n> > +\t\t    remote_ref.buf, default_notes_ref());\n> > +\tif (result == 0) { /* Merge resulted (trivially) in result_sha1 */\n> > +\t\t/* Update default notes ref with new commit */\n> > +\t\tupdate_ref(msg.buf, default_notes_ref(), result_sha1, NULL,\n> > +\t\t\t   0, DIE_ON_ERR);\n> > +\t} else {\n> > +\t\t/* TODO: */\n> > +\t\tdie(\"'git notes merge' cannot yet handle non-trivial merges!\");\n> \n> Mm.  In the long run, will (result != 0) mean \"merge conflict\"?\n\nAlmost. If you look at the notes_merge() docs in notes-merge.h by the\nend of this series, you'll see the following return values:\n\n0: Merge trivially succeeded in an existing commit (e.g. fast-forward).\n\n1: Merge successfully completes a merge commit (i.e. no conflicts).\n\n-1: Merge results is conflicts.\n\n> Thanks for a pleasant read.\n\nThanks for reading! :)\n\n\n...Johan\n\n\n[1]: Let's analyze the different cases here:\n\n1. local_ref (\"our\" side of the merge) doesn't exist\n(i.e. an unborn notes ref):\n\nIn this situation 'git merge' simply creates local_ref to point at\nremote_ref. 'git notes merge' does the same.\n\n\n2. local_ref is empty (.git/refs/notes/$local_ref is an empty file):\n\n'git merge' fails with\n  error: unable to resolve reference HEAD: No such file or directory\n  fatal: Cannot lock the ref 'HEAD'.\n\n'git notes merge' fails with\n  error: unable to resolve reference refs/notes/$local_ref: No such file or directory\n  fatal: Cannot lock the ref 'refs/notes/$local_ref'.\n\n3. local_ref refers to a non-existing commit:\n\n'git merge' fails with\n  error: Could not read $non_existing_sha1\n  error: Trying to write ref ORIG_HEAD with nonexistant object $non_existing_sha1\n  fatal: Cannot update the ref 'ORIG_HEAD'.\n  fatal: bad object HEAD\n\n'git notes merge' fails with\n  fatal: Failed to read notes tree referenced by refs/notes/$local_ref ($non_existing_sha1)\n\n4. local_ref refers to a non-commit (tree or blob):\n\n'git merge' fails with\n  error: Object $tree_sha1 is a tree, not a commit\n  error: Object $tree_sha1 is a tree, not a commit\n  Segmentation fault (someone might want to look into this...)\n\n'git notes merge' fails with\n  error: Object $tree_sha1 is a tree, not a commit\n  fatal: Could not parse commit 'refs/notes/$local_ref.\n\nLooking at remote_ref instead:\n\n5. remote_ref (\"their\" side of the merge) doesn't exist\n(i.e. an unborn notes ref):\n\n'git merge' fails with:\n  fatal: '$remote_ref' does not point to a commit\n\n'git notes merge' fails with:\n  fatal: Could not parse commit 'refs/notes/$remote_ref'.\n\n6. remote_ref is empty (.git/refs/notes/$remote_ref is an empty file):\n\nsame as #5\n\n7. remote_ref refers to a non-existing commit:\n\nsame as #5\n\n8. remote_ref refers to a non-commit (tree or blob):\n\n'git merge' fails with\n  error: $remote_ref: expected commit type, but the object dereferences to tree type\n  fatal: '$remote_ref' does not point to a commit\n\n'git notes merge' fails with\n  error: Object $tree_sha1 is a tree, not a commit\n  fatal: Could not parse commit '$remote_ref'.\n\nI believe all these outcomes make sense.\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"153065","messageId":"20101009172955.GB17799@burratino","threadId":"25282","inReplyTo":"201010090155.20858.johan@herland.net","subject":"Re: [PATCH 08/18] git notes merge: Initial implementation handling trivial merges only","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-10-09T17:29:55Z","receivedAt":"2010-10-09T17:29:55Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Johan Herland wrote:\n\n> I agree, but I'd like to name it 'git notes get-ref' instead, to stay\n> consistent with the current subcommand (instead of option) design.\n\nSounds good. :)\n\n> Jonathan Nieder wrote:\n\n>> I would find it easier to read\n>>\n>> \tif (o->verbosity >= DEFAULT_VERBOSITY)\n>> \t\tfprintf(stderr, ...)\n>>\n>> unless there are going to be a huge number of messages.\n>\n> The current version is modeled on the show() and output() functions in\n> merge-recursive.c. I think that works better in this situation.\n> Or maybe you have a better solution for merge-recursive.c as well?\n\nHmm --- isn't the point of output() that it indents to the appropriate\nlevel to portray a recursive merge?\n\nSimilarly, show() prevents those confusing messages from the internal\nmerge between ancestors from being printed when the user is not\ninterested.\n\nBut if you think they are important abstractions to maintain, I won't\nstop you. :)\n\n>>> +\n>>> +\tif (!o->local_ref || get_sha1(o->local_ref, local_sha1)) {\n>>> +\t\t/* empty notes ref => assume empty notes tree */\n[...]\n> I'm not sure when you think we should (or shouldn't) error out.\n\nget_sha1() can return -1 in the following cases:\n\n - starts with :/, regex does not match.\n - starts with :, entry not present in index\n - in form <rev>:<path>, <path> does not exist in <rev>\n - in form <rev>^, <rev> does not exist or that parent does not\n   exist\n - tag being used as commit points to a tree instead\n - et c.\n\nEspecially if the caller tries\n\n\tgit notes merge 'afjkdlsa^{gobbledeegook'\n\nI would not like the merge to succeed.\n\nSo as I see it, there are four cases:\n\n - get_sha1() succeeds and returns a commit ==> merge that rev\n - get_sha1() succeeds and returns a non-commit ==> fail\n - get_sha1() fails, but resolve_ref() indicates a ref valid\n   for writing ==> merge empty tree\n - get_sha1() fails, invalid refname ==> fail\n\nThe current code does not notice case 4, does it?\n\n> I guess this becomes a discussion of whether we should model notes\n> merges on the 'resolve' merge strategy or the 'recursive' merge\n> strategy. Without having studied each strategy in-depth, I don't know\n> how much \"better\" 'recursive' is than 'resolve', especially not from\n> the POV of notes merges.\n\nI think 'resolve' should be good enough for now.  We can always add\n'recursive' later.\n\nThe cases where 'recursive' might help are those in which both sides\nmade a lot of changes to the same notes.  It helps in two ways:\nsignaling conflicts that might have been otherwise missed and merging\ncleanly in cases that might otherwise produce spurious conflicts.\nIn theory, it makes the result less \"arbitrary\"; in practice, it seems\nto help avoid some conflicts in ugly cases like merging one week's pu\nwith the next week's pu.\n\n[...]\n> Almost. If you look at the notes_merge() docs in notes-merge.h by the\n> end of this series, you'll see the following return values:\n> \n> 0: Merge trivially succeeded in an existing commit (e.g. fast-forward).\n> \n> 1: Merge successfully completes a merge commit (i.e. no conflicts).\n> \n> -1: Merge results is conflicts.\n\nWhat kind of caller would care about the distinction between 1 and -1\nhere (just curious)?\n\nJonathan\n"},{"id":"153935","messageId":"201010210412.54383.johan@herland.net","threadId":"25282","inReplyTo":"20101009172955.GB17799@burratino","subject":"Re: [PATCH 08/18] git notes merge: Initial implementation handling trivial merges only","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-10-21T02:12:54Z","receivedAt":"2010-10-21T02:12:54Z","isPatch":true,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Saturday 09 October 2010, Jonathan Nieder wrote:\n> Johan Herland wrote:\n> > Jonathan Nieder wrote:\n> >> I would find it easier to read\n> >> \n> >> \tif (o->verbosity >= DEFAULT_VERBOSITY)\n> >> \t\n> >> \t\tfprintf(stderr, ...)\n> >> \n> >> unless there are going to be a huge number of messages.\n> > \n> > The current version is modeled on the show() and output() functions in\n> > merge-recursive.c. I think that works better in this situation.\n> > Or maybe you have a better solution for merge-recursive.c as well?\n> \n> Hmm --- isn't the point of output() that it indents to the appropriate\n> level to portray a recursive merge?\n> \n> Similarly, show() prevents those confusing messages from the internal\n> merge between ancestors from being printed when the user is not\n> interested.\n> \n> But if you think they are important abstractions to maintain, I won't\n> stop you. :)\n\nI see. I still find the OUTPUT() macro more readable, but I've amended\nthe patch to eliminate the extraneous show() function. Hence, you'll\nfind this in the next iteration:\n\n#define OUTPUT(o, v, ...) \\\n\tdo { \\\n\t\tif ((o)->verbosity >= (v)) { \\\n\t\t\tprintf(__VA_ARGS__); \\\n\t\t\tputs(\"\"); \\\n\t\t} \\\n\t} while (0)\n\n> >>> +\n> >>> +\tif (!o->local_ref || get_sha1(o->local_ref, local_sha1)) {\n> >>> +\t\t/* empty notes ref => assume empty notes tree */\n> \n> [...]\n> \n> > I'm not sure when you think we should (or shouldn't) error out.\n> \n> get_sha1() can return -1 in the following cases:\n> \n>  - starts with :/, regex does not match.\n>  - starts with :, entry not present in index\n>  - in form <rev>:<path>, <path> does not exist in <rev>\n>  - in form <rev>^, <rev> does not exist or that parent does not\n>    exist\n>  - tag being used as commit points to a tree instead\n>  - et c.\n\nI see. I didn't take these into account when I wrote the code.\n\n> Especially if the caller tries\n> \n> \tgit notes merge 'afjkdlsa^{gobbledeegook'\n> \n> I would not like the merge to succeed.\n\nAgreed, but I believe that command currently returns:\n\n  fatal: Could not parse commit 'refs/notes/afjkdlsa^{gobbledeegook'.\n\n(or using afjkdlsa^{gobbledeegook as \"our\" side of the merge):\n\n  fatal: Cannot lock the ref 'refs/notes/afjkdlsa^{gobbledeegook'.\n\n> So as I see it, there are four cases:\n> \n>  - get_sha1() succeeds and returns a commit ==> merge that rev\n>  - get_sha1() succeeds and returns a non-commit ==> fail\n>  - get_sha1() fails, but resolve_ref() indicates a ref valid\n>    for writing ==> merge empty tree\n>  - get_sha1() fails, invalid refname ==> fail\n> \n> The current code does not notice case 4, does it?\n\nMy tests indicate that case 4 does fail (correctly). However I also\nagree that this is not at all clear from the current code, so I've\nrewritten this code to something that is hopefully more straightforward\n(this is the next iteration):\n\n\t/* Dereference o->local_ref into local_sha1 */\n\tif (!resolve_ref(o->local_ref, local_sha1, 0, NULL))\n\t\tdie(\"Failed to resolve local notes ref '%s'\", o->local_ref);\n\telse if (!check_ref_format(o->local_ref) && is_null_sha1(local_sha1))\n\t\tlocal = NULL; /* local_sha1 == null_sha1 indicates unborn ref */\n\telse if (!(local = lookup_commit_reference(local_sha1)))\n\t\tdie(\"Could not parse local commit %s (%s)\",\n\t\t    sha1_to_hex(local_sha1), o->local_ref);\n\ttrace_printf(\"\\tlocal commit: %.7s\\n\", sha1_to_hex(local_sha1));\n\n\t/* Dereference o->remote_ref into remote_sha1 */\n\tif (get_sha1(o->remote_ref, remote_sha1)) {\n\t\t/*\n\t\t * Failed to get remote_sha1. If o->remote_ref looks like an\n\t\t * unborn ref, perform the merge using an empty notes tree.\n\t\t */\n\t\tif (!check_ref_format(o->remote_ref)) {\n\t\t\thashclr(remote_sha1);\n\t\t\tremote = NULL;\n\t\t}\n\t\telse\n\t\t\tdie(\"Failed to resolve remote notes ref '%s'\",\n\t\t\t    o->remote_ref);\n\t}\n\telse if (!(remote = lookup_commit_reference(remote_sha1)))\n\t\tdie(\"Could not parse remote commit %s (%s)\",\n\t\t    sha1_to_hex(remote_sha1), o->remote_ref);\n\ttrace_printf(\"\\tremote commit: %.7s\\n\", sha1_to_hex(remote_sha1));\n\nI've also added more testcases for expected failures.\n\n> > I guess this becomes a discussion of whether we should model notes\n> > merges on the 'resolve' merge strategy or the 'recursive' merge\n> > strategy. Without having studied each strategy in-depth, I don't know\n> > how much \"better\" 'recursive' is than 'resolve', especially not from\n> > the POV of notes merges.\n> \n> I think 'resolve' should be good enough for now.  We can always add\n> 'recursive' later.\n\nMy thoughts exactly.\n\n> > If you look at the notes_merge() docs in notes-merge.h by the\n> > end of this series, you'll see the following return values:\n> > \n> > 0: Merge trivially succeeded in an existing commit (e.g. fast-forward).\n> > \n> > 1: Merge successfully completes a merge commit (i.e. no conflicts).\n> > \n> > -1: Merge results is conflicts.\n> \n> What kind of caller would care about the distinction between 1 and -1\n> here (just curious)?\n\nAny caller who cares about the merge at all, I'd imagine\n\nIf 1 (or 0) the merge is complete, and there's nothing more to be done\n(expect updating the notes ref, of course).\n\nIf -1, there are conflicts checked out in .git/NOTES_MERGE_WORKTREE,\nand the user must be told to fix them and commit the conflict\nresolution.\n\nSee the last part of builtin/notes.c:merge() at the end of this series\nfor an example.\n\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"}]}