@matheusbsbs / matheus-bagliano / commits / 75ef3f4a5c
Fork 0

git notes merge: Initial implementation handling trivial merges only

This initial implementation of 'git notes merge' only handles the trivial merge cases (i.e. where the merge is either a no-op, or a fast-forward). The patch includes testcases for these trivial merge cases. Future patches will extend the functionality of 'git notes merge'. This patch has been improved by the following contributions: - Stephen Boyd: Simplify argc logic - Stephen Boyd: Use test_commit - Ævar Arnfjörð Bjarmason: Don't use C99 comments. - Jonathan Nieder: Add constants for common verbosity values - Jonathan Nieder: Use trace_printf(...) instead of OUTPUT(o, 5, ...) - Jonathan Nieder: Remove extraneous show() function - Jonathan Nieder: Clarify handling of empty/missing notes ref in notes_merge() - Junio C Hamano: fixup minor style issues Thanks-to: Stephen Boyd <bebarino@gmail.com> Thanks-to: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Thanks-to: Jonathan Nieder <jrnieder@gmail.com> Thanks-to: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Nov 9, 2010 at 22:49 UTC 75ef3f4a5cc69b21bc825ed0e739030d77a4f077
5 files changed +392
Makefile
+2
@@ -503,6 +503,7 @@ LIB_H += mailmap.h
503 LIB_H += merge-recursive.h
504 LIB_H += notes.h
505 LIB_H += notes-cache.h
506 +LIB_H += notes-merge.h
507 LIB_H += object.h
508 LIB_H += pack.h
509 LIB_H += pack-refs.h
@@ -593,6 +594,7 @@ LIB_OBJS += merge-recursive.o
594 LIB_OBJS += name-hash.o
595 LIB_OBJS += notes.o
596 LIB_OBJS += notes-cache.o
597 +LIB_OBJS += notes-merge.o
598 LIB_OBJS += object.o
599 LIB_OBJS += pack-check.o
600 LIB_OBJS += pack-refs.o
builtin/notes.c
+54
@@ -17,6 +17,7 @@
17 #include "run-command.h"
18 #include "parse-options.h"
19 #include "string-list.h"
20 +#include "notes-merge.h"
21
22 static const char * const git_notes_usage[] = {
23 "git notes [--ref <notes_ref>] [list [<object>]]",
@@ -25,6 +26,7 @@ static const char * const git_notes_usage[] = {
26 "git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
27 "git notes [--ref <notes_ref>] edit [<object>]",
28 "git notes [--ref <notes_ref>] show [<object>]",
29 + "git notes [--ref <notes_ref>] merge [-v | -q] <notes_ref>",
30 "git notes [--ref <notes_ref>] remove [<object>]",
31 "git notes [--ref <notes_ref>] prune [-n | -v]",
32 NULL
@@ -61,6 +63,11 @@ static const char * const git_notes_show_usage[] = {
63 NULL
64 };
65
66 +static const char * const git_notes_merge_usage[] = {
67 + "git notes merge [<options>] <notes_ref>",
68 + NULL
69 +};
70 +
71 static const char * const git_notes_remove_usage[] = {
72 "git notes remove [<object>]",
73 NULL
@@ -772,6 +779,51 @@ static int show(int argc, const char **argv, const char *prefix)
779 return retval;
780 }
781
782 +static int merge(int argc, const char **argv, const char *prefix)
783 +{
784 + struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
785 + unsigned char result_sha1[20];
786 + struct notes_merge_options o;
787 + int verbosity = 0, result;
788 + struct option options[] = {
789 + OPT__VERBOSITY(&verbosity),
790 + OPT_END()
791 + };
792 +
793 + argc = parse_options(argc, argv, prefix, options,
794 + git_notes_merge_usage, 0);
795 +
796 + if (argc != 1) {
797 + error("Must specify a notes ref to merge");
798 + usage_with_options(git_notes_merge_usage, options);
799 + }
800 +
801 + init_notes_merge_options(&o);
802 + o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
803 +
804 + o.local_ref = default_notes_ref();
805 + strbuf_addstr(&remote_ref, argv[0]);
806 + expand_notes_ref(&remote_ref);
807 + o.remote_ref = remote_ref.buf;
808 +
809 + result = notes_merge(&o, result_sha1);
810 +
811 + strbuf_addf(&msg, "notes: Merged notes from %s into %s",
812 + remote_ref.buf, default_notes_ref());
813 + if (result == 0) { /* Merge resulted (trivially) in result_sha1 */
814 + /* Update default notes ref with new commit */
815 + update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
816 + 0, DIE_ON_ERR);
817 + } else {
818 + /* TODO: */
819 + die("'git notes merge' cannot yet handle non-trivial merges!");
820 + }
821 +
822 + strbuf_release(&remote_ref);
823 + strbuf_release(&msg);
824 + return 0;
825 +}
826 +
827 static int remove_cmd(int argc, const char **argv, const char *prefix)
828 {
829 struct option options[] = {
@@ -865,6 +917,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
917 result = append_edit(argc, argv, prefix);
918 else if (!strcmp(argv[0], "show"))
919 result = show(argc, argv, prefix);
920 + else if (!strcmp(argv[0], "merge"))
921 + result = merge(argc, argv, prefix);
922 else if (!strcmp(argv[0], "remove"))
923 result = remove_cmd(argc, argv, prefix);
924 else if (!strcmp(argv[0], "prune"))
notes-merge.c new
+120
@@ -0,0 +1,120 @@
1 +#include "cache.h"
2 +#include "commit.h"
3 +#include "refs.h"
4 +#include "notes-merge.h"
5 +
6 +void init_notes_merge_options(struct notes_merge_options *o)
7 +{
8 + memset(o, 0, sizeof(struct notes_merge_options));
9 + o->verbosity = NOTES_MERGE_VERBOSITY_DEFAULT;
10 +}
11 +
12 +#define OUTPUT(o, v, ...) \
13 + do { \
14 + if ((o)->verbosity >= (v)) { \
15 + printf(__VA_ARGS__); \
16 + puts(""); \
17 + } \
18 + } while (0)
19 +
20 +int notes_merge(struct notes_merge_options *o,
21 + unsigned char *result_sha1)
22 +{
23 + unsigned char local_sha1[20], remote_sha1[20];
24 + struct commit *local, *remote;
25 + struct commit_list *bases = NULL;
26 + const unsigned char *base_sha1;
27 + int result = 0;
28 +
29 + assert(o->local_ref && o->remote_ref);
30 + hashclr(result_sha1);
31 +
32 + trace_printf("notes_merge(o->local_ref = %s, o->remote_ref = %s)\n",
33 + o->local_ref, o->remote_ref);
34 +
35 + /* Dereference o->local_ref into local_sha1 */
36 + if (!resolve_ref(o->local_ref, local_sha1, 0, NULL))
37 + die("Failed to resolve local notes ref '%s'", o->local_ref);
38 + else if (!check_ref_format(o->local_ref) && is_null_sha1(local_sha1))
39 + local = NULL; /* local_sha1 == null_sha1 indicates unborn ref */
40 + else if (!(local = lookup_commit_reference(local_sha1)))
41 + die("Could not parse local commit %s (%s)",
42 + sha1_to_hex(local_sha1), o->local_ref);
43 + trace_printf("\tlocal commit: %.7s\n", sha1_to_hex(local_sha1));
44 +
45 + /* Dereference o->remote_ref into remote_sha1 */
46 + if (get_sha1(o->remote_ref, remote_sha1)) {
47 + /*
48 + * Failed to get remote_sha1. If o->remote_ref looks like an
49 + * unborn ref, perform the merge using an empty notes tree.
50 + */
51 + if (!check_ref_format(o->remote_ref)) {
52 + hashclr(remote_sha1);
53 + remote = NULL;
54 + } else {
55 + die("Failed to resolve remote notes ref '%s'",
56 + o->remote_ref);
57 + }
58 + } else if (!(remote = lookup_commit_reference(remote_sha1))) {
59 + die("Could not parse remote commit %s (%s)",
60 + sha1_to_hex(remote_sha1), o->remote_ref);
61 + }
62 + trace_printf("\tremote commit: %.7s\n", sha1_to_hex(remote_sha1));
63 +
64 + if (!local && !remote)
65 + die("Cannot merge empty notes ref (%s) into empty notes ref "
66 + "(%s)", o->remote_ref, o->local_ref);
67 + if (!local) {
68 + /* result == remote commit */
69 + hashcpy(result_sha1, remote_sha1);
70 + goto found_result;
71 + }
72 + if (!remote) {
73 + /* result == local commit */
74 + hashcpy(result_sha1, local_sha1);
75 + goto found_result;
76 + }
77 + assert(local && remote);
78 +
79 + /* Find merge bases */
80 + bases = get_merge_bases(local, remote, 1);
81 + if (!bases) {
82 + base_sha1 = null_sha1;
83 + OUTPUT(o, 4, "No merge base found; doing history-less merge");
84 + } else if (!bases->next) {
85 + base_sha1 = bases->item->object.sha1;
86 + OUTPUT(o, 4, "One merge base found (%.7s)",
87 + sha1_to_hex(base_sha1));
88 + } else {
89 + /* TODO: How to handle multiple merge-bases? */
90 + base_sha1 = bases->item->object.sha1;
91 + OUTPUT(o, 3, "Multiple merge bases found. Using the first "
92 + "(%.7s)", sha1_to_hex(base_sha1));
93 + }
94 +
95 + OUTPUT(o, 4, "Merging remote commit %.7s into local commit %.7s with "
96 + "merge-base %.7s", sha1_to_hex(remote->object.sha1),
97 + sha1_to_hex(local->object.sha1), sha1_to_hex(base_sha1));
98 +
99 + if (!hashcmp(remote->object.sha1, base_sha1)) {
100 + /* Already merged; result == local commit */
101 + OUTPUT(o, 2, "Already up-to-date!");
102 + hashcpy(result_sha1, local->object.sha1);
103 + goto found_result;
104 + }
105 + if (!hashcmp(local->object.sha1, base_sha1)) {
106 + /* Fast-forward; result == remote commit */
107 + OUTPUT(o, 2, "Fast-forward");
108 + hashcpy(result_sha1, remote->object.sha1);
109 + goto found_result;
110 + }
111 +
112 + /* TODO: */
113 + result = error("notes_merge() cannot yet handle real merges.");
114 +
115 +found_result:
116 + free_commit_list(bases);
117 + trace_printf("notes_merge(): result = %i, result_sha1 = %.7s\n",
118 + result, sha1_to_hex(result_sha1));
119 + return result;
120 +}
notes-merge.h new
+36
@@ -0,0 +1,36 @@
1 +#ifndef NOTES_MERGE_H
2 +#define NOTES_MERGE_H
3 +
4 +enum notes_merge_verbosity {
5 + NOTES_MERGE_VERBOSITY_DEFAULT = 2,
6 + NOTES_MERGE_VERBOSITY_MAX = 5
7 +};
8 +
9 +struct notes_merge_options {
10 + const char *local_ref;
11 + const char *remote_ref;
12 + int verbosity;
13 +};
14 +
15 +void init_notes_merge_options(struct notes_merge_options *o);
16 +
17 +/*
18 + * Merge notes from o->remote_ref into o->local_ref
19 + *
20 + * The commits given by the two refs are merged, producing one of the following
21 + * outcomes:
22 + *
23 + * 1. The merge trivially results in an existing commit (e.g. fast-forward or
24 + * already-up-to-date). The SHA1 of the result is written into 'result_sha1'
25 + * and 0 is returned.
26 + * 2. The merge fails. result_sha1 is set to null_sha1, and non-zero returned.
27 + *
28 + * Both o->local_ref and o->remote_ref must be given (non-NULL), but either ref
29 + * (although not both) may refer to a non-existing notes ref, in which case
30 + * that notes ref is interpreted as an empty notes tree, and the merge
31 + * trivially results in what the other ref points to.
32 + */
33 +int notes_merge(struct notes_merge_options *o,
34 + unsigned char *result_sha1);
35 +
36 +#endif
t/t3308-notes-merge.sh new
+180
@@ -0,0 +1,180 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2010 Johan Herland
4 +#
5 +
6 +test_description='Test merging of notes trees'
7 +
8 +. ./test-lib.sh
9 +
10 +test_expect_success setup '
11 + test_commit 1st &&
12 + test_commit 2nd &&
13 + test_commit 3rd &&
14 + test_commit 4th &&
15 + test_commit 5th &&
16 + # Create notes on 4 first commits
17 + git config core.notesRef refs/notes/x &&
18 + git notes add -m "Notes on 1st commit" 1st &&
19 + git notes add -m "Notes on 2nd commit" 2nd &&
20 + git notes add -m "Notes on 3rd commit" 3rd &&
21 + git notes add -m "Notes on 4th commit" 4th
22 +'
23 +
24 +commit_sha1=$(git rev-parse 1st^{commit})
25 +commit_sha2=$(git rev-parse 2nd^{commit})
26 +commit_sha3=$(git rev-parse 3rd^{commit})
27 +commit_sha4=$(git rev-parse 4th^{commit})
28 +commit_sha5=$(git rev-parse 5th^{commit})
29 +
30 +verify_notes () {
31 + notes_ref="$1"
32 + git -c core.notesRef="refs/notes/$notes_ref" notes |
33 + sort >"output_notes_$notes_ref" &&
34 + test_cmp "expect_notes_$notes_ref" "output_notes_$notes_ref" &&
35 + git -c core.notesRef="refs/notes/$notes_ref" log --format="%H %s%n%N" \
36 + >"output_log_$notes_ref" &&
37 + test_cmp "expect_log_$notes_ref" "output_log_$notes_ref"
38 +}
39 +
40 +cat <<EOF | sort >expect_notes_x
41 +5e93d24084d32e1cb61f7070505b9d2530cca987 $commit_sha4
42 +8366731eeee53787d2bdf8fc1eff7d94757e8da0 $commit_sha3
43 +eede89064cd42441590d6afec6c37b321ada3389 $commit_sha2
44 +daa55ffad6cb99bf64226532147ffcaf5ce8bdd1 $commit_sha1
45 +EOF
46 +
47 +cat >expect_log_x <<EOF
48 +$commit_sha5 5th
49 +
50 +$commit_sha4 4th
51 +Notes on 4th commit
52 +
53 +$commit_sha3 3rd
54 +Notes on 3rd commit
55 +
56 +$commit_sha2 2nd
57 +Notes on 2nd commit
58 +
59 +$commit_sha1 1st
60 +Notes on 1st commit
61 +
62 +EOF
63 +
64 +test_expect_success 'verify initial notes (x)' '
65 + verify_notes x
66 +'
67 +
68 +cp expect_notes_x expect_notes_y
69 +cp expect_log_x expect_log_y
70 +
71 +test_expect_success 'fail to merge empty notes ref into empty notes ref (z => y)' '
72 + test_must_fail git -c "core.notesRef=refs/notes/y" notes merge z
73 +'
74 +
75 +test_expect_success 'fail to merge into various non-notes refs' '
76 + test_must_fail git -c "core.notesRef=refs/notes" notes merge x &&
77 + test_must_fail git -c "core.notesRef=refs/notes/" notes merge x &&
78 + mkdir -p .git/refs/notes/dir &&
79 + test_must_fail git -c "core.notesRef=refs/notes/dir" notes merge x &&
80 + test_must_fail git -c "core.notesRef=refs/notes/dir/" notes merge x &&
81 + test_must_fail git -c "core.notesRef=refs/heads/master" notes merge x &&
82 + test_must_fail git -c "core.notesRef=refs/notes/y:" notes merge x &&
83 + test_must_fail git -c "core.notesRef=refs/notes/y:foo" notes merge x &&
84 + test_must_fail git -c "core.notesRef=refs/notes/foo^{bar" notes merge x
85 +'
86 +
87 +test_expect_success 'fail to merge various non-note-trees' '
88 + git config core.notesRef refs/notes/y &&
89 + test_must_fail git notes merge refs/notes &&
90 + test_must_fail git notes merge refs/notes/ &&
91 + test_must_fail git notes merge refs/notes/dir &&
92 + test_must_fail git notes merge refs/notes/dir/ &&
93 + test_must_fail git notes merge refs/heads/master &&
94 + test_must_fail git notes merge x: &&
95 + test_must_fail git notes merge x:foo &&
96 + test_must_fail git notes merge foo^{bar
97 +'
98 +
99 +test_expect_success 'merge notes into empty notes ref (x => y)' '
100 + git config core.notesRef refs/notes/y &&
101 + git notes merge x &&
102 + verify_notes y &&
103 + # x and y should point to the same notes commit
104 + test "$(git rev-parse refs/notes/x)" = "$(git rev-parse refs/notes/y)"
105 +'
106 +
107 +test_expect_success 'merge empty notes ref (z => y)' '
108 + git notes merge z &&
109 + # y should not change (still == x)
110 + test "$(git rev-parse refs/notes/x)" = "$(git rev-parse refs/notes/y)"
111 +'
112 +
113 +test_expect_success 'change notes on other notes ref (y)' '
114 + # Not touching notes to 1st commit
115 + git notes remove 2nd &&
116 + git notes append -m "More notes on 3rd commit" 3rd &&
117 + git notes add -f -m "New notes on 4th commit" 4th &&
118 + git notes add -m "Notes on 5th commit" 5th
119 +'
120 +
121 +test_expect_success 'merge previous notes commit (y^ => y) => No-op' '
122 + pre_state="$(git rev-parse refs/notes/y)" &&
123 + git notes merge y^ &&
124 + # y should not move
125 + test "$pre_state" = "$(git rev-parse refs/notes/y)"
126 +'
127 +
128 +cat <<EOF | sort >expect_notes_y
129 +0f2efbd00262f2fd41dfae33df8765618eeacd99 $commit_sha5
130 +dec2502dac3ea161543f71930044deff93fa945c $commit_sha4
131 +4069cdb399fd45463ec6eef8e051a16a03592d91 $commit_sha3
132 +daa55ffad6cb99bf64226532147ffcaf5ce8bdd1 $commit_sha1
133 +EOF
134 +
135 +cat >expect_log_y <<EOF
136 +$commit_sha5 5th
137 +Notes on 5th commit
138 +
139 +$commit_sha4 4th
140 +New notes on 4th commit
141 +
142 +$commit_sha3 3rd
143 +Notes on 3rd commit
144 +
145 +More notes on 3rd commit
146 +
147 +$commit_sha2 2nd
148 +
149 +$commit_sha1 1st
150 +Notes on 1st commit
151 +
152 +EOF
153 +
154 +test_expect_success 'verify changed notes on other notes ref (y)' '
155 + verify_notes y
156 +'
157 +
158 +test_expect_success 'verify unchanged notes on original notes ref (x)' '
159 + verify_notes x
160 +'
161 +
162 +test_expect_success 'merge original notes (x) into changed notes (y) => No-op' '
163 + git notes merge -vvv x &&
164 + verify_notes y &&
165 + verify_notes x
166 +'
167 +
168 +cp expect_notes_y expect_notes_x
169 +cp expect_log_y expect_log_x
170 +
171 +test_expect_success 'merge changed (y) into original (x) => Fast-forward' '
172 + git config core.notesRef refs/notes/x &&
173 + git notes merge y &&
174 + verify_notes x &&
175 + verify_notes y &&
176 + # x and y should point to same the notes commit
177 + test "$(git rev-parse refs/notes/x)" = "$(git rev-parse refs/notes/y)"
178 +'
179 +
180 +test_done