packed_ref_store: move `packed_refs_path` here
[git.git] / t / helper / test-ref-store.c
blob05d8c4d8af093ad42d855b17d243fe07b8893bfb
1 #include "cache.h"
2 #include "refs.h"
3 #include "worktree.h"
5 static const char *notnull(const char *arg, const char *name)
7 if (!arg)
8 die("%s required", name);
9 return arg;
12 static unsigned int arg_flags(const char *arg, const char *name)
14 return atoi(notnull(arg, name));
17 static const char **get_store(const char **argv, struct ref_store **refs)
19 const char *gitdir;
21 if (!argv[0]) {
22 die("ref store required");
23 } else if (!strcmp(argv[0], "main")) {
24 *refs = get_main_ref_store();
25 } else if (skip_prefix(argv[0], "submodule:", &gitdir)) {
26 struct strbuf sb = STRBUF_INIT;
27 int ret;
29 ret = strbuf_git_path_submodule(&sb, gitdir, "objects/");
30 if (ret)
31 die("strbuf_git_path_submodule failed: %d", ret);
32 add_to_alternates_memory(sb.buf);
33 strbuf_release(&sb);
35 *refs = get_submodule_ref_store(gitdir);
36 } else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
37 struct worktree **p, **worktrees = get_worktrees(0);
39 for (p = worktrees; *p; p++) {
40 struct worktree *wt = *p;
42 if (!wt->id) {
43 /* special case for main worktree */
44 if (!strcmp(gitdir, "main"))
45 break;
46 } else if (!strcmp(gitdir, wt->id))
47 break;
49 if (!*p)
50 die("no such worktree: %s", gitdir);
52 *refs = get_worktree_ref_store(*p);
53 } else
54 die("unknown backend %s", argv[0]);
56 if (!*refs)
57 die("no ref store");
59 /* consume store-specific optional arguments if needed */
61 return argv + 1;
65 static int cmd_pack_refs(struct ref_store *refs, const char **argv)
67 unsigned int flags = arg_flags(*argv++, "flags");
69 return refs_pack_refs(refs, flags);
72 static int cmd_peel_ref(struct ref_store *refs, const char **argv)
74 const char *refname = notnull(*argv++, "refname");
75 unsigned char sha1[20];
76 int ret;
78 ret = refs_peel_ref(refs, refname, sha1);
79 if (!ret)
80 puts(sha1_to_hex(sha1));
81 return ret;
84 static int cmd_create_symref(struct ref_store *refs, const char **argv)
86 const char *refname = notnull(*argv++, "refname");
87 const char *target = notnull(*argv++, "target");
88 const char *logmsg = *argv++;
90 return refs_create_symref(refs, refname, target, logmsg);
93 static int cmd_delete_refs(struct ref_store *refs, const char **argv)
95 unsigned int flags = arg_flags(*argv++, "flags");
96 const char *msg = *argv++;
97 struct string_list refnames = STRING_LIST_INIT_NODUP;
99 while (*argv)
100 string_list_append(&refnames, *argv++);
102 return refs_delete_refs(refs, msg, &refnames, flags);
105 static int cmd_rename_ref(struct ref_store *refs, const char **argv)
107 const char *oldref = notnull(*argv++, "oldref");
108 const char *newref = notnull(*argv++, "newref");
109 const char *logmsg = *argv++;
111 return refs_rename_ref(refs, oldref, newref, logmsg);
114 static int each_ref(const char *refname, const struct object_id *oid,
115 int flags, void *cb_data)
117 printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags);
118 return 0;
121 static int cmd_for_each_ref(struct ref_store *refs, const char **argv)
123 const char *prefix = notnull(*argv++, "prefix");
125 return refs_for_each_ref_in(refs, prefix, each_ref, NULL);
128 static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
130 unsigned char sha1[20];
131 const char *refname = notnull(*argv++, "refname");
132 int resolve_flags = arg_flags(*argv++, "resolve-flags");
133 int flags;
134 const char *ref;
136 ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
137 sha1, &flags);
138 printf("%s %s 0x%x\n", sha1_to_hex(sha1), ref, flags);
139 return ref ? 0 : 1;
142 static int cmd_verify_ref(struct ref_store *refs, const char **argv)
144 const char *refname = notnull(*argv++, "refname");
145 struct strbuf err = STRBUF_INIT;
146 int ret;
148 ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);
149 if (err.len)
150 puts(err.buf);
151 return ret;
154 static int cmd_for_each_reflog(struct ref_store *refs, const char **argv)
156 return refs_for_each_reflog(refs, each_ref, NULL);
159 static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
160 const char *committer, timestamp_t timestamp,
161 int tz, const char *msg, void *cb_data)
163 printf("%s %s %s %"PRItime" %d %s\n",
164 oid_to_hex(old_oid), oid_to_hex(new_oid),
165 committer, timestamp, tz, msg);
166 return 0;
169 static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
171 const char *refname = notnull(*argv++, "refname");
173 return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
176 static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
178 const char *refname = notnull(*argv++, "refname");
180 return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
183 static int cmd_reflog_exists(struct ref_store *refs, const char **argv)
185 const char *refname = notnull(*argv++, "refname");
187 return !refs_reflog_exists(refs, refname);
190 static int cmd_create_reflog(struct ref_store *refs, const char **argv)
192 const char *refname = notnull(*argv++, "refname");
193 int force_create = arg_flags(*argv++, "force-create");
194 struct strbuf err = STRBUF_INIT;
195 int ret;
197 ret = refs_create_reflog(refs, refname, force_create, &err);
198 if (err.len)
199 puts(err.buf);
200 return ret;
203 static int cmd_delete_reflog(struct ref_store *refs, const char **argv)
205 const char *refname = notnull(*argv++, "refname");
207 return refs_delete_reflog(refs, refname);
210 static int cmd_reflog_expire(struct ref_store *refs, const char **argv)
212 die("not supported yet");
215 static int cmd_delete_ref(struct ref_store *refs, const char **argv)
217 const char *msg = notnull(*argv++, "msg");
218 const char *refname = notnull(*argv++, "refname");
219 const char *sha1_buf = notnull(*argv++, "old-sha1");
220 unsigned int flags = arg_flags(*argv++, "flags");
221 unsigned char old_sha1[20];
223 if (get_sha1_hex(sha1_buf, old_sha1))
224 die("not sha-1");
226 return refs_delete_ref(refs, msg, refname, old_sha1, flags);
229 static int cmd_update_ref(struct ref_store *refs, const char **argv)
231 const char *msg = notnull(*argv++, "msg");
232 const char *refname = notnull(*argv++, "refname");
233 const char *new_sha1_buf = notnull(*argv++, "old-sha1");
234 const char *old_sha1_buf = notnull(*argv++, "old-sha1");
235 unsigned int flags = arg_flags(*argv++, "flags");
236 unsigned char old_sha1[20];
237 unsigned char new_sha1[20];
239 if (get_sha1_hex(old_sha1_buf, old_sha1) ||
240 get_sha1_hex(new_sha1_buf, new_sha1))
241 die("not sha-1");
243 return refs_update_ref(refs, msg, refname,
244 new_sha1, old_sha1,
245 flags, UPDATE_REFS_DIE_ON_ERR);
248 struct command {
249 const char *name;
250 int (*func)(struct ref_store *refs, const char **argv);
253 static struct command commands[] = {
254 { "pack-refs", cmd_pack_refs },
255 { "peel-ref", cmd_peel_ref },
256 { "create-symref", cmd_create_symref },
257 { "delete-refs", cmd_delete_refs },
258 { "rename-ref", cmd_rename_ref },
259 { "for-each-ref", cmd_for_each_ref },
260 { "resolve-ref", cmd_resolve_ref },
261 { "verify-ref", cmd_verify_ref },
262 { "for-each-reflog", cmd_for_each_reflog },
263 { "for-each-reflog-ent", cmd_for_each_reflog_ent },
264 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse },
265 { "reflog-exists", cmd_reflog_exists },
266 { "create-reflog", cmd_create_reflog },
267 { "delete-reflog", cmd_delete_reflog },
268 { "reflog-expire", cmd_reflog_expire },
270 * backend transaction functions can't be tested separately
272 { "delete-ref", cmd_delete_ref },
273 { "update-ref", cmd_update_ref },
274 { NULL, NULL }
277 int cmd_main(int argc, const char **argv)
279 struct ref_store *refs;
280 const char *func;
281 struct command *cmd;
283 setup_git_directory();
285 argv = get_store(argv + 1, &refs);
287 func = *argv++;
288 if (!func)
289 die("ref function required");
290 for (cmd = commands; cmd->name; cmd++) {
291 if (!strcmp(func, cmd->name))
292 return cmd->func(refs, argv);
294 die("unknown function %s", func);
295 return 0;