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