]>
Commit | Line | Data |
---|---|---|
65370d81 | 1 | #include "test-tool.h" |
80f2a609 NTND |
2 | #include "cache.h" |
3 | #include "refs.h" | |
fa099d23 | 4 | #include "worktree.h" |
0d4a1321 | 5 | #include "object-store.h" |
23a3f0cb | 6 | #include "repository.h" |
80f2a609 | 7 | |
cd2d40fb HWN |
8 | struct flag_definition { |
9 | const char *name; | |
10 | uint64_t mask; | |
11 | }; | |
12 | ||
13 | #define FLAG_DEF(x) \ | |
14 | { \ | |
15 | #x, (x) \ | |
16 | } | |
17 | ||
18 | static unsigned int parse_flags(const char *str, struct flag_definition *defs) | |
19 | { | |
20 | struct string_list masks = STRING_LIST_INIT_DUP; | |
21 | int i = 0; | |
22 | unsigned int result = 0; | |
23 | ||
24 | if (!strcmp(str, "0")) | |
25 | return 0; | |
26 | ||
27 | string_list_split(&masks, str, ',', 64); | |
28 | for (; i < masks.nr; i++) { | |
29 | const char *name = masks.items[i].string; | |
30 | struct flag_definition *def = defs; | |
31 | int found = 0; | |
32 | while (def->name) { | |
33 | if (!strcmp(def->name, name)) { | |
34 | result |= def->mask; | |
35 | found = 1; | |
36 | break; | |
37 | } | |
38 | def++; | |
39 | } | |
40 | if (!found) | |
41 | die("unknown flag \"%s\"", name); | |
42 | } | |
43 | ||
44 | string_list_clear(&masks, 0); | |
45 | return result; | |
46 | } | |
47 | ||
48 | static struct flag_definition empty_flags[] = { { NULL, 0 } }; | |
49 | ||
80f2a609 NTND |
50 | static const char *notnull(const char *arg, const char *name) |
51 | { | |
52 | if (!arg) | |
53 | die("%s required", name); | |
54 | return arg; | |
55 | } | |
56 | ||
cd2d40fb HWN |
57 | static unsigned int arg_flags(const char *arg, const char *name, |
58 | struct flag_definition *defs) | |
80f2a609 | 59 | { |
cd2d40fb | 60 | return parse_flags(notnull(arg, name), defs); |
80f2a609 NTND |
61 | } |
62 | ||
63 | static const char **get_store(const char **argv, struct ref_store **refs) | |
64 | { | |
65 | const char *gitdir; | |
66 | ||
67 | if (!argv[0]) { | |
68 | die("ref store required"); | |
69 | } else if (!strcmp(argv[0], "main")) { | |
23a3f0cb | 70 | *refs = get_main_ref_store(the_repository); |
80f2a609 NTND |
71 | } else if (skip_prefix(argv[0], "submodule:", &gitdir)) { |
72 | struct strbuf sb = STRBUF_INIT; | |
73 | int ret; | |
74 | ||
75 | ret = strbuf_git_path_submodule(&sb, gitdir, "objects/"); | |
76 | if (ret) | |
77 | die("strbuf_git_path_submodule failed: %d", ret); | |
78 | add_to_alternates_memory(sb.buf); | |
79 | strbuf_release(&sb); | |
80 | ||
81 | *refs = get_submodule_ref_store(gitdir); | |
fa099d23 | 82 | } else if (skip_prefix(argv[0], "worktree:", &gitdir)) { |
03f2465b | 83 | struct worktree **p, **worktrees = get_worktrees(); |
fa099d23 NTND |
84 | |
85 | for (p = worktrees; *p; p++) { | |
86 | struct worktree *wt = *p; | |
87 | ||
88 | if (!wt->id) { | |
89 | /* special case for main worktree */ | |
90 | if (!strcmp(gitdir, "main")) | |
91 | break; | |
92 | } else if (!strcmp(gitdir, wt->id)) | |
93 | break; | |
94 | } | |
95 | if (!*p) | |
96 | die("no such worktree: %s", gitdir); | |
97 | ||
98 | *refs = get_worktree_ref_store(*p); | |
34e69128 | 99 | free_worktrees(worktrees); |
80f2a609 NTND |
100 | } else |
101 | die("unknown backend %s", argv[0]); | |
102 | ||
103 | if (!*refs) | |
104 | die("no ref store"); | |
105 | ||
106 | /* consume store-specific optional arguments if needed */ | |
107 | ||
108 | return argv + 1; | |
109 | } | |
110 | ||
cd2d40fb HWN |
111 | static struct flag_definition pack_flags[] = { FLAG_DEF(PACK_REFS_PRUNE), |
112 | FLAG_DEF(PACK_REFS_ALL), | |
113 | { NULL, 0 } }; | |
80f2a609 NTND |
114 | |
115 | static int cmd_pack_refs(struct ref_store *refs, const char **argv) | |
116 | { | |
cd2d40fb | 117 | unsigned int flags = arg_flags(*argv++, "flags", pack_flags); |
80f2a609 NTND |
118 | |
119 | return refs_pack_refs(refs, flags); | |
120 | } | |
121 | ||
80f2a609 NTND |
122 | static int cmd_create_symref(struct ref_store *refs, const char **argv) |
123 | { | |
124 | const char *refname = notnull(*argv++, "refname"); | |
125 | const char *target = notnull(*argv++, "target"); | |
126 | const char *logmsg = *argv++; | |
127 | ||
128 | return refs_create_symref(refs, refname, target, logmsg); | |
129 | } | |
130 | ||
cd2d40fb HWN |
131 | static struct flag_definition transaction_flags[] = { |
132 | FLAG_DEF(REF_NO_DEREF), | |
133 | FLAG_DEF(REF_FORCE_CREATE_REFLOG), | |
e9706a18 | 134 | FLAG_DEF(REF_SKIP_OID_VERIFICATION), |
3c966c7b | 135 | FLAG_DEF(REF_SKIP_REFNAME_VERIFICATION), |
cd2d40fb HWN |
136 | { NULL, 0 } |
137 | }; | |
138 | ||
80f2a609 NTND |
139 | static int cmd_delete_refs(struct ref_store *refs, const char **argv) |
140 | { | |
cd2d40fb | 141 | unsigned int flags = arg_flags(*argv++, "flags", transaction_flags); |
64da4199 | 142 | const char *msg = *argv++; |
80f2a609 | 143 | struct string_list refnames = STRING_LIST_INIT_NODUP; |
df25a19d | 144 | int result; |
80f2a609 NTND |
145 | |
146 | while (*argv) | |
147 | string_list_append(&refnames, *argv++); | |
148 | ||
df25a19d HWN |
149 | result = refs_delete_refs(refs, msg, &refnames, flags); |
150 | string_list_clear(&refnames, 0); | |
151 | return result; | |
80f2a609 NTND |
152 | } |
153 | ||
154 | static int cmd_rename_ref(struct ref_store *refs, const char **argv) | |
155 | { | |
156 | const char *oldref = notnull(*argv++, "oldref"); | |
157 | const char *newref = notnull(*argv++, "newref"); | |
158 | const char *logmsg = *argv++; | |
159 | ||
160 | return refs_rename_ref(refs, oldref, newref, logmsg); | |
161 | } | |
162 | ||
163 | static int each_ref(const char *refname, const struct object_id *oid, | |
5cf88fd8 | 164 | int flags, void *cb_data UNUSED) |
80f2a609 NTND |
165 | { |
166 | printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags); | |
167 | return 0; | |
168 | } | |
169 | ||
170 | static int cmd_for_each_ref(struct ref_store *refs, const char **argv) | |
171 | { | |
172 | const char *prefix = notnull(*argv++, "prefix"); | |
173 | ||
174 | return refs_for_each_ref_in(refs, prefix, each_ref, NULL); | |
175 | } | |
176 | ||
177 | static int cmd_resolve_ref(struct ref_store *refs, const char **argv) | |
178 | { | |
0221eb86 | 179 | struct object_id oid = *null_oid(); |
80f2a609 | 180 | const char *refname = notnull(*argv++, "refname"); |
cd2d40fb | 181 | int resolve_flags = arg_flags(*argv++, "resolve-flags", empty_flags); |
80f2a609 NTND |
182 | int flags; |
183 | const char *ref; | |
184 | ||
f1da24ca | 185 | ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags, |
ce14de03 | 186 | &oid, &flags); |
e7e456f5 | 187 | printf("%s %s 0x%x\n", oid_to_hex(&oid), ref ? ref : "(null)", flags); |
80f2a609 NTND |
188 | return ref ? 0 : 1; |
189 | } | |
190 | ||
191 | static int cmd_verify_ref(struct ref_store *refs, const char **argv) | |
192 | { | |
193 | const char *refname = notnull(*argv++, "refname"); | |
194 | struct strbuf err = STRBUF_INIT; | |
195 | int ret; | |
196 | ||
197 | ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err); | |
198 | if (err.len) | |
199 | puts(err.buf); | |
200 | return ret; | |
201 | } | |
202 | ||
203 | static int cmd_for_each_reflog(struct ref_store *refs, const char **argv) | |
204 | { | |
205 | return refs_for_each_reflog(refs, each_ref, NULL); | |
206 | } | |
207 | ||
208 | static int each_reflog(struct object_id *old_oid, struct object_id *new_oid, | |
dddbad72 | 209 | const char *committer, timestamp_t timestamp, |
5cf88fd8 | 210 | int tz, const char *msg, void *cb_data UNUSED) |
80f2a609 | 211 | { |
3474b602 HWN |
212 | printf("%s %s %s %" PRItime " %+05d%s%s", oid_to_hex(old_oid), |
213 | oid_to_hex(new_oid), committer, timestamp, tz, | |
214 | *msg == '\n' ? "" : "\t", msg); | |
80f2a609 NTND |
215 | return 0; |
216 | } | |
217 | ||
218 | static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv) | |
219 | { | |
220 | const char *refname = notnull(*argv++, "refname"); | |
221 | ||
222 | return refs_for_each_reflog_ent(refs, refname, each_reflog, refs); | |
223 | } | |
224 | ||
225 | static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv) | |
226 | { | |
227 | const char *refname = notnull(*argv++, "refname"); | |
228 | ||
229 | return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs); | |
230 | } | |
231 | ||
232 | static int cmd_reflog_exists(struct ref_store *refs, const char **argv) | |
233 | { | |
234 | const char *refname = notnull(*argv++, "refname"); | |
235 | ||
236 | return !refs_reflog_exists(refs, refname); | |
237 | } | |
238 | ||
239 | static int cmd_create_reflog(struct ref_store *refs, const char **argv) | |
240 | { | |
241 | const char *refname = notnull(*argv++, "refname"); | |
80f2a609 NTND |
242 | struct strbuf err = STRBUF_INIT; |
243 | int ret; | |
244 | ||
7b089120 | 245 | ret = refs_create_reflog(refs, refname, &err); |
80f2a609 NTND |
246 | if (err.len) |
247 | puts(err.buf); | |
248 | return ret; | |
249 | } | |
250 | ||
251 | static int cmd_delete_reflog(struct ref_store *refs, const char **argv) | |
252 | { | |
253 | const char *refname = notnull(*argv++, "refname"); | |
254 | ||
255 | return refs_delete_reflog(refs, refname); | |
256 | } | |
257 | ||
258 | static int cmd_reflog_expire(struct ref_store *refs, const char **argv) | |
259 | { | |
260 | die("not supported yet"); | |
261 | } | |
262 | ||
263 | static int cmd_delete_ref(struct ref_store *refs, const char **argv) | |
264 | { | |
265 | const char *msg = notnull(*argv++, "msg"); | |
266 | const char *refname = notnull(*argv++, "refname"); | |
267 | const char *sha1_buf = notnull(*argv++, "old-sha1"); | |
cd2d40fb | 268 | unsigned int flags = arg_flags(*argv++, "flags", transaction_flags); |
2616a5e5 | 269 | struct object_id old_oid; |
80f2a609 | 270 | |
2616a5e5 | 271 | if (get_oid_hex(sha1_buf, &old_oid)) |
57eb3681 | 272 | die("cannot parse %s as %s", sha1_buf, the_hash_algo->name); |
80f2a609 | 273 | |
2616a5e5 | 274 | return refs_delete_ref(refs, msg, refname, &old_oid, flags); |
80f2a609 NTND |
275 | } |
276 | ||
277 | static int cmd_update_ref(struct ref_store *refs, const char **argv) | |
278 | { | |
279 | const char *msg = notnull(*argv++, "msg"); | |
280 | const char *refname = notnull(*argv++, "refname"); | |
3c27e2e0 | 281 | const char *new_sha1_buf = notnull(*argv++, "new-sha1"); |
80f2a609 | 282 | const char *old_sha1_buf = notnull(*argv++, "old-sha1"); |
cd2d40fb | 283 | unsigned int flags = arg_flags(*argv++, "flags", transaction_flags); |
ae077771 | 284 | struct object_id old_oid; |
285 | struct object_id new_oid; | |
80f2a609 | 286 | |
57eb3681 HWN |
287 | if (get_oid_hex(old_sha1_buf, &old_oid)) |
288 | die("cannot parse %s as %s", old_sha1_buf, the_hash_algo->name); | |
289 | if (get_oid_hex(new_sha1_buf, &new_oid)) | |
290 | die("cannot parse %s as %s", new_sha1_buf, the_hash_algo->name); | |
80f2a609 NTND |
291 | |
292 | return refs_update_ref(refs, msg, refname, | |
ae077771 | 293 | &new_oid, &old_oid, |
80f2a609 NTND |
294 | flags, UPDATE_REFS_DIE_ON_ERR); |
295 | } | |
296 | ||
297 | struct command { | |
298 | const char *name; | |
299 | int (*func)(struct ref_store *refs, const char **argv); | |
300 | }; | |
301 | ||
302 | static struct command commands[] = { | |
303 | { "pack-refs", cmd_pack_refs }, | |
80f2a609 NTND |
304 | { "create-symref", cmd_create_symref }, |
305 | { "delete-refs", cmd_delete_refs }, | |
306 | { "rename-ref", cmd_rename_ref }, | |
307 | { "for-each-ref", cmd_for_each_ref }, | |
308 | { "resolve-ref", cmd_resolve_ref }, | |
309 | { "verify-ref", cmd_verify_ref }, | |
310 | { "for-each-reflog", cmd_for_each_reflog }, | |
311 | { "for-each-reflog-ent", cmd_for_each_reflog_ent }, | |
312 | { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse }, | |
313 | { "reflog-exists", cmd_reflog_exists }, | |
314 | { "create-reflog", cmd_create_reflog }, | |
315 | { "delete-reflog", cmd_delete_reflog }, | |
316 | { "reflog-expire", cmd_reflog_expire }, | |
317 | /* | |
318 | * backend transaction functions can't be tested separately | |
319 | */ | |
320 | { "delete-ref", cmd_delete_ref }, | |
321 | { "update-ref", cmd_update_ref }, | |
322 | { NULL, NULL } | |
323 | }; | |
324 | ||
65370d81 | 325 | int cmd__ref_store(int argc, const char **argv) |
80f2a609 NTND |
326 | { |
327 | struct ref_store *refs; | |
328 | const char *func; | |
329 | struct command *cmd; | |
330 | ||
331 | setup_git_directory(); | |
332 | ||
333 | argv = get_store(argv + 1, &refs); | |
334 | ||
335 | func = *argv++; | |
336 | if (!func) | |
337 | die("ref function required"); | |
338 | for (cmd = commands; cmd->name; cmd++) { | |
339 | if (!strcmp(func, cmd->name)) | |
340 | return cmd->func(refs, argv); | |
341 | } | |
342 | die("unknown function %s", func); | |
343 | return 0; | |
344 | } |