Skip to content

Commit d2d68d9

Browse files
jacob-kellergitster
authored andcommitted
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>
1 parent 11dd2b2 commit d2d68d9

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,12 @@ mergetool.writeToTemp::
18861886
mergetool.prompt::
18871887
Prompt before each invocation of the merge resolution program.
18881888

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+
18891895
notes.displayRef::
18901896
The (fully qualified) refname from which to show notes when
18911897
showing commit messages. The value of this variable can be set

Documentation/git-notes.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ merge::
101101
any) into the current notes ref (called "local").
102102
+
103103
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,
105105
the "manual" resolver is used. This resolver checks out the
106106
conflicting notes in a special worktree (`.git/NOTES_MERGE_WORKTREE`),
107107
and instructs the user to manually resolve the conflicts there.
@@ -183,6 +183,7 @@ OPTIONS
183183
When merging notes, resolve notes conflicts using the given
184184
strategy. The following strategies are recognized: "manual"
185185
(default), "ours", "theirs", "union" and "cat_sort_uniq".
186+
This option overrides the "notes.mergeStrategy" configuration setting.
186187
See the "NOTES MERGE STRATEGIES" section below for more
187188
information on each notes merge strategy.
188189

@@ -247,6 +248,9 @@ When done, the user can either finalize the merge with
247248
'git notes merge --commit', or abort the merge with
248249
'git notes merge --abort'.
249250

251+
Users may select an automated merge strategy from among the following using
252+
either -s/--strategy option or configuring notes.mergeStrategy accordingly:
253+
250254
"ours" automatically resolves conflicting notes in favor of the local
251255
version (i.e. the current notes ref).
252256

@@ -310,6 +314,14 @@ core.notesRef::
310314
This setting can be overridden through the environment and
311315
command line.
312316

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+
313325
notes.displayRef::
314326
Which ref (or refs, if a glob or specified more than once), in
315327
addition to the default set by `core.notesRef` or

builtin/notes.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,19 @@ static int merge_commit(struct notes_merge_options *o)
737737
return ret;
738738
}
739739

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+
740753
static int merge(int argc, const char **argv, const char *prefix)
741754
{
742755
struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
@@ -795,15 +808,17 @@ static int merge(int argc, const char **argv, const char *prefix)
795808
expand_notes_ref(&remote_ref);
796809
o.remote_ref = remote_ref.buf;
797810

811+
t = init_notes_check("merge");
812+
798813
if (strategy) {
799814
if (parse_notes_merge_strategy(strategy, &o.strategy)) {
800815
error("Unknown -s/--strategy: %s", strategy);
801816
usage_with_options(git_notes_merge_usage, options);
802817
}
818+
} else {
819+
git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
803820
}
804821

805-
t = init_notes_check("merge");
806-
807822
strbuf_addf(&msg, "notes: Merged notes from %s into %s",
808823
remote_ref.buf, default_notes_ref());
809824
strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */

t/t3309-notes-merge-auto-resolve.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@ test_expect_success 'merge z into y with invalid strategy => Fail/No changes' '
298298
verify_notes y y
299299
'
300300

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+
301308
cat <<EOF | sort >expect_notes_ours
302309
68b8630d25516028bed862719855b3d6768d7833 $commit_sha15
303310
5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14
@@ -365,6 +372,17 @@ test_expect_success 'reset to pre-merge state (y)' '
365372
verify_notes y y
366373
'
367374

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+
368386
cat <<EOF | sort >expect_notes_theirs
369387
9b4b2c61f0615412da3c10f98ff85b57c04ec765 $commit_sha15
370388
5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14
@@ -432,6 +450,17 @@ test_expect_success 'reset to pre-merge state (y)' '
432450
verify_notes y y
433451
'
434452

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+
435464
cat <<EOF | sort >expect_notes_union
436465
7c4e546efd0fe939f876beb262ece02797880b54 $commit_sha15
437466
5de7ea7ad4f47e7ff91989fb82234634730f75df $commit_sha14
@@ -644,4 +673,15 @@ test_expect_success 'merge y into z with "cat_sort_uniq" strategy => Non-conflic
644673
verify_notes z cat_sort_uniq
645674
'
646675

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+
647687
test_done

0 commit comments

Comments
 (0)