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