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