]>
Commit | Line | Data |
---|---|---|
49c24704 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
49c24704 | 3 | #include "commit.h" |
f394e093 | 4 | #include "gettext.h" |
49c24704 JH |
5 | #include "refs.h" |
6 | #include "notes-utils.h" | |
c1f5eb49 | 7 | #include "repository.h" |
bf9a05ba | 8 | |
1d18d758 NTND |
9 | void create_notes_commit(struct repository *r, |
10 | struct notes_tree *t, | |
11 | struct commit_list *parents, | |
3ffefb54 | 12 | const char *msg, size_t msg_len, |
5078f344 | 13 | struct object_id *result_oid) |
bf9a05ba | 14 | { |
18b74e51 | 15 | struct object_id tree_oid; |
bf9a05ba JH |
16 | |
17 | assert(t->initialized); | |
18 | ||
bbca96d5 | 19 | if (write_notes_tree(t, &tree_oid)) |
bf9a05ba JH |
20 | die("Failed to write notes tree to database"); |
21 | ||
22 | if (!parents) { | |
23 | /* Deduce parent commit from t->ref */ | |
18b74e51 | 24 | struct object_id parent_oid; |
34c290a6 | 25 | if (!read_ref(t->ref, &parent_oid)) { |
1d18d758 | 26 | struct commit *parent = lookup_commit(r, &parent_oid); |
5e7d4d3e | 27 | if (parse_commit(parent)) |
bf9a05ba JH |
28 | die("Failed to find/parse commit %s", t->ref); |
29 | commit_list_insert(parent, &parents); | |
30 | } | |
31 | /* else: t->ref points to nothing, assume root/orphan commit */ | |
32 | } | |
33 | ||
5078f344 PO |
34 | if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL, |
35 | NULL)) | |
bf9a05ba JH |
36 | die("Failed to commit notes tree to database"); |
37 | } | |
49c24704 | 38 | |
1d18d758 | 39 | void commit_notes(struct repository *r, struct notes_tree *t, const char *msg) |
49c24704 JH |
40 | { |
41 | struct strbuf buf = STRBUF_INIT; | |
18b74e51 | 42 | struct object_id commit_oid; |
49c24704 JH |
43 | |
44 | if (!t) | |
45 | t = &default_notes_tree; | |
ee76f92f | 46 | if (!t->initialized || !t->update_ref || !*t->update_ref) |
49c24704 JH |
47 | die(_("Cannot commit uninitialized/unreferenced notes tree")); |
48 | if (!t->dirty) | |
49 | return; /* don't have to commit an unchanged tree */ | |
50 | ||
51 | /* Prepare commit message and reflog message */ | |
52 | strbuf_addstr(&buf, msg); | |
a0d4923d | 53 | strbuf_complete_line(&buf); |
49c24704 | 54 | |
1d18d758 | 55 | create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid); |
a91cc7fa | 56 | strbuf_insertstr(&buf, 0, "notes: "); |
ae077771 | 57 | update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0, |
f4124112 | 58 | UPDATE_REFS_DIE_ON_ERR); |
49c24704 JH |
59 | |
60 | strbuf_release(&buf); | |
61 | } | |
62 | ||
93efcad3 JK |
63 | int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s) |
64 | { | |
65 | if (!strcmp(v, "manual")) | |
66 | *s = NOTES_MERGE_RESOLVE_MANUAL; | |
67 | else if (!strcmp(v, "ours")) | |
68 | *s = NOTES_MERGE_RESOLVE_OURS; | |
69 | else if (!strcmp(v, "theirs")) | |
70 | *s = NOTES_MERGE_RESOLVE_THEIRS; | |
71 | else if (!strcmp(v, "union")) | |
72 | *s = NOTES_MERGE_RESOLVE_UNION; | |
73 | else if (!strcmp(v, "cat_sort_uniq")) | |
74 | *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ; | |
75 | else | |
76 | return -1; | |
77 | ||
78 | return 0; | |
79 | } | |
80 | ||
49c24704 JH |
81 | static combine_notes_fn parse_combine_notes_fn(const char *v) |
82 | { | |
83 | if (!strcasecmp(v, "overwrite")) | |
84 | return combine_notes_overwrite; | |
85 | else if (!strcasecmp(v, "ignore")) | |
86 | return combine_notes_ignore; | |
87 | else if (!strcasecmp(v, "concatenate")) | |
88 | return combine_notes_concatenate; | |
89 | else if (!strcasecmp(v, "cat_sort_uniq")) | |
90 | return combine_notes_cat_sort_uniq; | |
91 | else | |
92 | return NULL; | |
93 | } | |
94 | ||
95 | static int notes_rewrite_config(const char *k, const char *v, void *cb) | |
96 | { | |
97 | struct notes_rewrite_cfg *c = cb; | |
59556548 | 98 | if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) { |
49c24704 JH |
99 | c->enabled = git_config_bool(k, v); |
100 | return 0; | |
101 | } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) { | |
102 | if (!v) | |
aa012e90 | 103 | return config_error_nonbool(k); |
49c24704 JH |
104 | c->combine = parse_combine_notes_fn(v); |
105 | if (!c->combine) { | |
106 | error(_("Bad notes.rewriteMode value: '%s'"), v); | |
107 | return 1; | |
108 | } | |
109 | return 0; | |
110 | } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) { | |
111 | /* note that a refs/ prefix is implied in the | |
112 | * underlying for_each_glob_ref */ | |
59556548 | 113 | if (starts_with(v, "refs/notes/")) |
49c24704 JH |
114 | string_list_add_refs_by_glob(c->refs, v); |
115 | else | |
116 | warning(_("Refusing to rewrite notes in %s" | |
117 | " (outside of refs/notes/)"), v); | |
118 | return 0; | |
119 | } | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
124 | ||
125 | struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd) | |
126 | { | |
127 | struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg)); | |
128 | const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT); | |
129 | const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT); | |
130 | c->cmd = cmd; | |
131 | c->enabled = 1; | |
132 | c->combine = combine_notes_concatenate; | |
ca56dadb | 133 | CALLOC_ARRAY(c->refs, 1); |
49c24704 JH |
134 | c->refs->strdup_strings = 1; |
135 | c->refs_from_env = 0; | |
136 | c->mode_from_env = 0; | |
137 | if (rewrite_mode_env) { | |
138 | c->mode_from_env = 1; | |
139 | c->combine = parse_combine_notes_fn(rewrite_mode_env); | |
140 | if (!c->combine) | |
66f5f6dc ÆAB |
141 | /* |
142 | * TRANSLATORS: The first %s is the name of | |
143 | * the environment variable, the second %s is | |
144 | * its value. | |
145 | */ | |
49c24704 JH |
146 | error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT, |
147 | rewrite_mode_env); | |
148 | } | |
149 | if (rewrite_refs_env) { | |
150 | c->refs_from_env = 1; | |
151 | string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env); | |
152 | } | |
153 | git_config(notes_rewrite_config, c); | |
154 | if (!c->enabled || !c->refs->nr) { | |
155 | string_list_clear(c->refs, 0); | |
156 | free(c->refs); | |
157 | free(c); | |
158 | return NULL; | |
159 | } | |
ee76f92f | 160 | c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE); |
49c24704 JH |
161 | string_list_clear(c->refs, 0); |
162 | free(c->refs); | |
163 | return c; | |
164 | } | |
165 | ||
166 | int copy_note_for_rewrite(struct notes_rewrite_cfg *c, | |
bb7e4739 | 167 | const struct object_id *from_obj, const struct object_id *to_obj) |
49c24704 JH |
168 | { |
169 | int ret = 0; | |
170 | int i; | |
171 | for (i = 0; c->trees[i]; i++) | |
172 | ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret; | |
173 | return ret; | |
174 | } | |
175 | ||
1d18d758 NTND |
176 | void finish_copy_notes_for_rewrite(struct repository *r, |
177 | struct notes_rewrite_cfg *c, | |
178 | const char *msg) | |
49c24704 JH |
179 | { |
180 | int i; | |
181 | for (i = 0; c->trees[i]; i++) { | |
1d18d758 | 182 | commit_notes(r, c->trees[i], msg); |
49c24704 JH |
183 | free_notes(c->trees[i]); |
184 | } | |
185 | free(c->trees); | |
186 | free(c); | |
187 | } |