notes: add notes.mergeStrategy option to select default strategy

Teach git-notes about "notes.mergeStrategy" to select a general strategy for all notes merges. This enables a user to always get expected merge strategy such as "cat_sort_uniq" without having to pass the "-s" option manually. Signed-off-by: Jacob Keller <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jacob Keller committed Aug 17, 2015 at 14:33 UTC d2d68d9975da4a15dfca0e6e59001948e5bf57f9
4 files changed +76 -3
Documentation/config.txt
+6
@@ -1886,6 +1886,12 @@ mergetool.writeToTemp::
1886 mergetool.prompt::
1887 Prompt before each invocation of the merge resolution program.
1888
1889 +notes.mergeStrategy::
1890 + Which merge strategy to choose by default when resolving notes
1891 + conflicts. Must be one of `manual`, `ours`, `theirs`, `union`, or
1892 + `cat_sort_uniq`. Defaults to `manual`. See "NOTES MERGE STRATEGIES"
1893 + section of linkgit:git-notes[1] for more information on each strategy.
1894 +
1895 notes.displayRef::
1896 The (fully qualified) refname from which to show notes when
1897 showing commit messages. The value of this variable can be set
Documentation/git-notes.txt
+13 -1
@@ -101,7 +101,7 @@ merge::
101 any) into the current notes ref (called "local").
102 +
103 If conflicts arise and a strategy for automatically resolving
104 -conflicting notes (see the -s/--strategy option) is not given,
104 +conflicting notes (see the "NOTES MERGE STRATEGIES" section) is not given,
105 the "manual" resolver is used. This resolver checks out the
106 conflicting notes in a special worktree (`.git/NOTES_MERGE_WORKTREE`),
107 and instructs the user to manually resolve the conflicts there.
@@ -183,6 +183,7 @@ OPTIONS
183 When merging notes, resolve notes conflicts using the given
184 strategy. The following strategies are recognized: "manual"
185 (default), "ours", "theirs", "union" and "cat_sort_uniq".
186 + This option overrides the "notes.mergeStrategy" configuration setting.
187 See the "NOTES MERGE STRATEGIES" section below for more
188 information on each notes merge strategy.
189
@@ -247,6 +248,9 @@ When done, the user can either finalize the merge with
248 'git notes merge --commit', or abort the merge with
249 'git notes merge --abort'.
250
251 +Users may select an automated merge strategy from among the following using
252 +either -s/--strategy option or configuring notes.mergeStrategy accordingly:
253 +
254 "ours" automatically resolves conflicting notes in favor of the local
255 version (i.e. the current notes ref).
256
@@ -310,6 +314,14 @@ core.notesRef::
314 This setting can be overridden through the environment and
315 command line.
316
317 +notes.mergeStrategy::
318 + Which merge strategy to choose by default when resolving notes
319 + conflicts. Must be one of `manual`, `ours`, `theirs`, `union`, or
320 + `cat_sort_uniq`. Defaults to `manual`. See "NOTES MERGE STRATEGIES"
321 + section above for more information on each strategy.
322 ++
323 +This setting can be overridden by passing the `--strategy` option.
324 +
325 notes.displayRef::
326 Which ref (or refs, if a glob or specified more than once), in
327 addition to the default set by `core.notesRef` or
builtin/notes.c
+17 -2
@@ -737,6 +737,19 @@ static int merge_commit(struct notes_merge_options *o)
737 return ret;
738 }
739
740 +static int git_config_get_notes_strategy(const char *key,
741 + enum notes_merge_strategy *strategy)
742 +{
743 + const char *value;
744 +
745 + if (git_config_get_string_const(key, &value))
746 + return 1;
747 + if (parse_notes_merge_strategy(value, strategy))
748 + git_die_config(key, "unknown notes merge strategy %s", value);
749 +
750 + return 0;
751 +}
752 +
753 static int merge(int argc, const char **argv, const char *prefix)
754 {
755 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
@@ -795,15 +808,17 @@ static int merge(int argc, const char **argv, const char *prefix)
808 expand_notes_ref(&remote_ref);
809 o.remote_ref = remote_ref.buf;
810
811 + t = init_notes_check("merge");
812 +
813 if (strategy) {
814 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
815 error("Unknown -s/--strategy: %s", strategy);
816 usage_with_options(git_notes_merge_usage, options);
817 }
818 + } else {
819 + git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
820 }
821
805 - t = init_notes_check("merge");
806 -
822 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
823 remote_ref.buf, default_notes_ref());
824 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
t/t3309-notes-merge-auto-resolve.sh
+40
@@ -298,6 +298,13 @@ test_expect_success 'merge z into y with invalid strategy => Fail/No changes' '
298 verify_notes y y
299 '
300
301 +test_expect_success 'merge z into y with invalid configuration option => Fail/No changes' '
302 + git config core.notesRef refs/notes/y &&
303 + test_must_fail git -c notes.mergeStrategy="foo" notes merge z &&
304 + # Verify no changes (y)
305 + verify_notes y y
306 +'
307 +
308 cat <<EOF | sort >expect_notes_ours
309 68b8630d25516028bed862719855b3d6768d7833 $commit_sha15
310 5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14
@@ -365,6 +372,17 @@ test_expect_success 'reset to pre-merge state (y)' '
372 verify_notes y y
373 '
374
375 +test_expect_success 'merge z into y with "ours" configuration option => Non-conflicting 3-way merge' '
376 + git -c notes.mergeStrategy="ours" notes merge z &&
377 + verify_notes y ours
378 +'
379 +
380 +test_expect_success 'reset to pre-merge state (y)' '
381 + git update-ref refs/notes/y refs/notes/y^1 &&
382 + # Verify pre-merge state
383 + verify_notes y y
384 +'
385 +
386 cat <<EOF | sort >expect_notes_theirs
387 9b4b2c61f0615412da3c10f98ff85b57c04ec765 $commit_sha15
388 5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14
@@ -432,6 +450,17 @@ test_expect_success 'reset to pre-merge state (y)' '
450 verify_notes y y
451 '
452
453 +test_expect_success 'merge z into y with "theirs" strategy overriding configuration option "ours" => Non-conflicting 3-way merge' '
454 + git -c notes.mergeStrategy="ours" notes merge --strategy=theirs z &&
455 + verify_notes y theirs
456 +'
457 +
458 +test_expect_success 'reset to pre-merge state (y)' '
459 + git update-ref refs/notes/y refs/notes/y^1 &&
460 + # Verify pre-merge state
461 + verify_notes y y
462 +'
463 +
464 cat <<EOF | sort >expect_notes_union
465 7c4e546efd0fe939f876beb262ece02797880b54 $commit_sha15
466 5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14
@@ -644,4 +673,15 @@ test_expect_success 'merge y into z with "cat_sort_uniq" strategy => Non-conflic
673 verify_notes z cat_sort_uniq
674 '
675
676 +test_expect_success 'reset to pre-merge state (z)' '
677 + git update-ref refs/notes/z refs/notes/z^1 &&
678 + # Verify pre-merge state
679 + verify_notes z z
680 +'
681 +
682 +test_expect_success 'merge y into z with "cat_sort_uniq" strategy configuration option => Non-conflicting 3-way merge' '
683 + git -c notes.mergeStrategy="cat_sort_uniq" notes merge y &&
684 + verify_notes z cat_sort_uniq
685 +'
686 +
687 test_done