Merge branch 'ah/log-decorate-default-to-auto'
[git/debian.git] / t / helper / test-ref-store.c
blobfba85e7da58fb124b409de502e9bb4f9a7d8f5b5
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 struct string_list refnames = STRING_LIST_INIT_NODUP;
98 while (*argv)
99 string_list_append(&refnames, *argv++);
101 return refs_delete_refs(refs, &refnames, flags);
104 static int cmd_rename_ref(struct ref_store *refs, const char **argv)
106 const char *oldref = notnull(*argv++, "oldref");
107 const char *newref = notnull(*argv++, "newref");
108 const char *logmsg = *argv++;
110 return refs_rename_ref(refs, oldref, newref, logmsg);
113 static int each_ref(const char *refname, const struct object_id *oid,
114 int flags, void *cb_data)
116 printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags);
117 return 0;
120 static int cmd_for_each_ref(struct ref_store *refs, const char **argv)
122 const char *prefix = notnull(*argv++, "prefix");
124 return refs_for_each_ref_in(refs, prefix, each_ref, NULL);
127 static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
129 unsigned char sha1[20];
130 const char *refname = notnull(*argv++, "refname");
131 int resolve_flags = arg_flags(*argv++, "resolve-flags");
132 int flags;
133 const char *ref;
135 ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
136 sha1, &flags);
137 printf("%s %s 0x%x\n", sha1_to_hex(sha1), ref, flags);
138 return ref ? 0 : 1;
141 static int cmd_verify_ref(struct ref_store *refs, const char **argv)
143 const char *refname = notnull(*argv++, "refname");
144 struct strbuf err = STRBUF_INIT;
145 int ret;
147 ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);
148 if (err.len)
149 puts(err.buf);
150 return ret;
153 static int cmd_for_each_reflog(struct ref_store *refs, const char **argv)
155 return refs_for_each_reflog(refs, each_ref, NULL);
158 static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
159 const char *committer, timestamp_t timestamp,
160 int tz, const char *msg, void *cb_data)
162 printf("%s %s %s %"PRItime" %d %s\n",
163 oid_to_hex(old_oid), oid_to_hex(new_oid),
164 committer, timestamp, tz, msg);
165 return 0;
168 static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
170 const char *refname = notnull(*argv++, "refname");
172 return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
175 static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
177 const char *refname = notnull(*argv++, "refname");
179 return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
182 static int cmd_reflog_exists(struct ref_store *refs, const char **argv)
184 const char *refname = notnull(*argv++, "refname");
186 return !refs_reflog_exists(refs, refname);
189 static int cmd_create_reflog(struct ref_store *refs, const char **argv)
191 const char *refname = notnull(*argv++, "refname");
192 int force_create = arg_flags(*argv++, "force-create");
193 struct strbuf err = STRBUF_INIT;
194 int ret;
196 ret = refs_create_reflog(refs, refname, force_create, &err);
197 if (err.len)
198 puts(err.buf);
199 return ret;
202 static int cmd_delete_reflog(struct ref_store *refs, const char **argv)
204 const char *refname = notnull(*argv++, "refname");
206 return refs_delete_reflog(refs, refname);
209 static int cmd_reflog_expire(struct ref_store *refs, const char **argv)
211 die("not supported yet");
214 static int cmd_delete_ref(struct ref_store *refs, const char **argv)
216 const char *msg = notnull(*argv++, "msg");
217 const char *refname = notnull(*argv++, "refname");
218 const char *sha1_buf = notnull(*argv++, "old-sha1");
219 unsigned int flags = arg_flags(*argv++, "flags");
220 unsigned char old_sha1[20];
222 if (get_sha1_hex(sha1_buf, old_sha1))
223 die("not sha-1");
225 return refs_delete_ref(refs, msg, refname, old_sha1, flags);
228 static int cmd_update_ref(struct ref_store *refs, const char **argv)
230 const char *msg = notnull(*argv++, "msg");
231 const char *refname = notnull(*argv++, "refname");
232 const char *new_sha1_buf = notnull(*argv++, "old-sha1");
233 const char *old_sha1_buf = notnull(*argv++, "old-sha1");
234 unsigned int flags = arg_flags(*argv++, "flags");
235 unsigned char old_sha1[20];
236 unsigned char new_sha1[20];
238 if (get_sha1_hex(old_sha1_buf, old_sha1) ||
239 get_sha1_hex(new_sha1_buf, new_sha1))
240 die("not sha-1");
242 return refs_update_ref(refs, msg, refname,
243 new_sha1, old_sha1,
244 flags, UPDATE_REFS_DIE_ON_ERR);
247 struct command {
248 const char *name;
249 int (*func)(struct ref_store *refs, const char **argv);
252 static struct command commands[] = {
253 { "pack-refs", cmd_pack_refs },
254 { "peel-ref", cmd_peel_ref },
255 { "create-symref", cmd_create_symref },
256 { "delete-refs", cmd_delete_refs },
257 { "rename-ref", cmd_rename_ref },
258 { "for-each-ref", cmd_for_each_ref },
259 { "resolve-ref", cmd_resolve_ref },
260 { "verify-ref", cmd_verify_ref },
261 { "for-each-reflog", cmd_for_each_reflog },
262 { "for-each-reflog-ent", cmd_for_each_reflog_ent },
263 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse },
264 { "reflog-exists", cmd_reflog_exists },
265 { "create-reflog", cmd_create_reflog },
266 { "delete-reflog", cmd_delete_reflog },
267 { "reflog-expire", cmd_reflog_expire },
269 * backend transaction functions can't be tested separately
271 { "delete-ref", cmd_delete_ref },
272 { "update-ref", cmd_update_ref },
273 { NULL, NULL }
276 int cmd_main(int argc, const char **argv)
278 struct ref_store *refs;
279 const char *func;
280 struct command *cmd;
282 setup_git_directory();
284 argv = get_store(argv + 1, &refs);
286 func = *argv++;
287 if (!func)
288 die("ref function required");
289 for (cmd = commands; cmd->name; cmd++) {
290 if (!strcmp(func, cmd->name))
291 return cmd->func(refs, argv);
293 die("unknown function %s", func);
294 return 0;