Merge branch 'hv/trailer-formatting'
[alt-git.git] / t / helper / test-ref-store.c
blobbba5f841c6ab00362aa81313fcf3ef3fa7ddfe9d
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "refs.h"
4 #include "worktree.h"
5 #include "object-store.h"
6 #include "repository.h"
8 static const char *notnull(const char *arg, const char *name)
10 if (!arg)
11 die("%s required", name);
12 return arg;
15 static unsigned int arg_flags(const char *arg, const char *name)
17 return atoi(notnull(arg, name));
20 static const char **get_store(const char **argv, struct ref_store **refs)
22 const char *gitdir;
24 if (!argv[0]) {
25 die("ref store required");
26 } else if (!strcmp(argv[0], "main")) {
27 *refs = get_main_ref_store(the_repository);
28 } else if (skip_prefix(argv[0], "submodule:", &gitdir)) {
29 struct strbuf sb = STRBUF_INIT;
30 int ret;
32 ret = strbuf_git_path_submodule(&sb, gitdir, "objects/");
33 if (ret)
34 die("strbuf_git_path_submodule failed: %d", ret);
35 add_to_alternates_memory(sb.buf);
36 strbuf_release(&sb);
38 *refs = get_submodule_ref_store(gitdir);
39 } else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
40 struct worktree **p, **worktrees = get_worktrees();
42 for (p = worktrees; *p; p++) {
43 struct worktree *wt = *p;
45 if (!wt->id) {
46 /* special case for main worktree */
47 if (!strcmp(gitdir, "main"))
48 break;
49 } else if (!strcmp(gitdir, wt->id))
50 break;
52 if (!*p)
53 die("no such worktree: %s", gitdir);
55 *refs = get_worktree_ref_store(*p);
56 } else
57 die("unknown backend %s", argv[0]);
59 if (!*refs)
60 die("no ref store");
62 /* consume store-specific optional arguments if needed */
64 return argv + 1;
68 static int cmd_pack_refs(struct ref_store *refs, const char **argv)
70 unsigned int flags = arg_flags(*argv++, "flags");
72 return refs_pack_refs(refs, flags);
75 static int cmd_create_symref(struct ref_store *refs, const char **argv)
77 const char *refname = notnull(*argv++, "refname");
78 const char *target = notnull(*argv++, "target");
79 const char *logmsg = *argv++;
81 return refs_create_symref(refs, refname, target, logmsg);
84 static int cmd_delete_refs(struct ref_store *refs, const char **argv)
86 unsigned int flags = arg_flags(*argv++, "flags");
87 const char *msg = *argv++;
88 struct string_list refnames = STRING_LIST_INIT_NODUP;
90 while (*argv)
91 string_list_append(&refnames, *argv++);
93 return refs_delete_refs(refs, msg, &refnames, flags);
96 static int cmd_rename_ref(struct ref_store *refs, const char **argv)
98 const char *oldref = notnull(*argv++, "oldref");
99 const char *newref = notnull(*argv++, "newref");
100 const char *logmsg = *argv++;
102 return refs_rename_ref(refs, oldref, newref, logmsg);
105 static int each_ref(const char *refname, const struct object_id *oid,
106 int flags, void *cb_data)
108 printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags);
109 return 0;
112 static int cmd_for_each_ref(struct ref_store *refs, const char **argv)
114 const char *prefix = notnull(*argv++, "prefix");
116 return refs_for_each_ref_in(refs, prefix, each_ref, NULL);
119 static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
121 struct object_id oid;
122 const char *refname = notnull(*argv++, "refname");
123 int resolve_flags = arg_flags(*argv++, "resolve-flags");
124 int flags;
125 const char *ref;
127 ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
128 &oid, &flags);
129 printf("%s %s 0x%x\n", oid_to_hex(&oid), ref ? ref : "(null)", flags);
130 return ref ? 0 : 1;
133 static int cmd_verify_ref(struct ref_store *refs, const char **argv)
135 const char *refname = notnull(*argv++, "refname");
136 struct strbuf err = STRBUF_INIT;
137 int ret;
139 ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);
140 if (err.len)
141 puts(err.buf);
142 return ret;
145 static int cmd_for_each_reflog(struct ref_store *refs, const char **argv)
147 return refs_for_each_reflog(refs, each_ref, NULL);
150 static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
151 const char *committer, timestamp_t timestamp,
152 int tz, const char *msg, void *cb_data)
154 printf("%s %s %s %"PRItime" %d %s\n",
155 oid_to_hex(old_oid), oid_to_hex(new_oid),
156 committer, timestamp, tz, msg);
157 return 0;
160 static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
162 const char *refname = notnull(*argv++, "refname");
164 return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
167 static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
169 const char *refname = notnull(*argv++, "refname");
171 return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
174 static int cmd_reflog_exists(struct ref_store *refs, const char **argv)
176 const char *refname = notnull(*argv++, "refname");
178 return !refs_reflog_exists(refs, refname);
181 static int cmd_create_reflog(struct ref_store *refs, const char **argv)
183 const char *refname = notnull(*argv++, "refname");
184 int force_create = arg_flags(*argv++, "force-create");
185 struct strbuf err = STRBUF_INIT;
186 int ret;
188 ret = refs_create_reflog(refs, refname, force_create, &err);
189 if (err.len)
190 puts(err.buf);
191 return ret;
194 static int cmd_delete_reflog(struct ref_store *refs, const char **argv)
196 const char *refname = notnull(*argv++, "refname");
198 return refs_delete_reflog(refs, refname);
201 static int cmd_reflog_expire(struct ref_store *refs, const char **argv)
203 die("not supported yet");
206 static int cmd_delete_ref(struct ref_store *refs, const char **argv)
208 const char *msg = notnull(*argv++, "msg");
209 const char *refname = notnull(*argv++, "refname");
210 const char *sha1_buf = notnull(*argv++, "old-sha1");
211 unsigned int flags = arg_flags(*argv++, "flags");
212 struct object_id old_oid;
214 if (get_oid_hex(sha1_buf, &old_oid))
215 die("not sha-1");
217 return refs_delete_ref(refs, msg, refname, &old_oid, flags);
220 static int cmd_update_ref(struct ref_store *refs, const char **argv)
222 const char *msg = notnull(*argv++, "msg");
223 const char *refname = notnull(*argv++, "refname");
224 const char *new_sha1_buf = notnull(*argv++, "new-sha1");
225 const char *old_sha1_buf = notnull(*argv++, "old-sha1");
226 unsigned int flags = arg_flags(*argv++, "flags");
227 struct object_id old_oid;
228 struct object_id new_oid;
230 if (get_oid_hex(old_sha1_buf, &old_oid) ||
231 get_oid_hex(new_sha1_buf, &new_oid))
232 die("not sha-1");
234 return refs_update_ref(refs, msg, refname,
235 &new_oid, &old_oid,
236 flags, UPDATE_REFS_DIE_ON_ERR);
239 struct command {
240 const char *name;
241 int (*func)(struct ref_store *refs, const char **argv);
244 static struct command commands[] = {
245 { "pack-refs", cmd_pack_refs },
246 { "create-symref", cmd_create_symref },
247 { "delete-refs", cmd_delete_refs },
248 { "rename-ref", cmd_rename_ref },
249 { "for-each-ref", cmd_for_each_ref },
250 { "resolve-ref", cmd_resolve_ref },
251 { "verify-ref", cmd_verify_ref },
252 { "for-each-reflog", cmd_for_each_reflog },
253 { "for-each-reflog-ent", cmd_for_each_reflog_ent },
254 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse },
255 { "reflog-exists", cmd_reflog_exists },
256 { "create-reflog", cmd_create_reflog },
257 { "delete-reflog", cmd_delete_reflog },
258 { "reflog-expire", cmd_reflog_expire },
260 * backend transaction functions can't be tested separately
262 { "delete-ref", cmd_delete_ref },
263 { "update-ref", cmd_update_ref },
264 { NULL, NULL }
267 int cmd__ref_store(int argc, const char **argv)
269 struct ref_store *refs;
270 const char *func;
271 struct command *cmd;
273 setup_git_directory();
275 argv = get_store(argv + 1, &refs);
277 func = *argv++;
278 if (!func)
279 die("ref function required");
280 for (cmd = commands; cmd->name; cmd++) {
281 if (!strcmp(func, cmd->name))
282 return cmd->func(refs, argv);
284 die("unknown function %s", func);
285 return 0;