]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-ref-store.c
refs: drop force_create argument of create_reflog API
[thirdparty/git.git] / t / helper / test-ref-store.c
CommitLineData
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
NTND
7
8static const char *notnull(const char *arg, const char *name)
9{
10 if (!arg)
11 die("%s required", name);
12 return arg;
13}
14
15static unsigned int arg_flags(const char *arg, const char *name)
16{
17 return atoi(notnull(arg, name));
18}
19
20static const char **get_store(const char **argv, struct ref_store **refs)
21{
22 const char *gitdir;
23
24 if (!argv[0]) {
25 die("ref store required");
26 } else if (!strcmp(argv[0], "main")) {
23a3f0cb 27 *refs = get_main_ref_store(the_repository);
80f2a609
NTND
28 } else if (skip_prefix(argv[0], "submodule:", &gitdir)) {
29 struct strbuf sb = STRBUF_INIT;
30 int ret;
31
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);
37
38 *refs = get_submodule_ref_store(gitdir);
fa099d23 39 } else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
03f2465b 40 struct worktree **p, **worktrees = get_worktrees();
fa099d23
NTND
41
42 for (p = worktrees; *p; p++) {
43 struct worktree *wt = *p;
44
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;
51 }
52 if (!*p)
53 die("no such worktree: %s", gitdir);
54
55 *refs = get_worktree_ref_store(*p);
80f2a609
NTND
56 } else
57 die("unknown backend %s", argv[0]);
58
59 if (!*refs)
60 die("no ref store");
61
62 /* consume store-specific optional arguments if needed */
63
64 return argv + 1;
65}
66
67
68static int cmd_pack_refs(struct ref_store *refs, const char **argv)
69{
70 unsigned int flags = arg_flags(*argv++, "flags");
71
72 return refs_pack_refs(refs, flags);
73}
74
80f2a609
NTND
75static int cmd_create_symref(struct ref_store *refs, const char **argv)
76{
77 const char *refname = notnull(*argv++, "refname");
78 const char *target = notnull(*argv++, "target");
79 const char *logmsg = *argv++;
80
81 return refs_create_symref(refs, refname, target, logmsg);
82}
83
84static int cmd_delete_refs(struct ref_store *refs, const char **argv)
85{
86 unsigned int flags = arg_flags(*argv++, "flags");
64da4199 87 const char *msg = *argv++;
80f2a609
NTND
88 struct string_list refnames = STRING_LIST_INIT_NODUP;
89
90 while (*argv)
91 string_list_append(&refnames, *argv++);
92
64da4199 93 return refs_delete_refs(refs, msg, &refnames, flags);
80f2a609
NTND
94}
95
96static int cmd_rename_ref(struct ref_store *refs, const char **argv)
97{
98 const char *oldref = notnull(*argv++, "oldref");
99 const char *newref = notnull(*argv++, "newref");
100 const char *logmsg = *argv++;
101
102 return refs_rename_ref(refs, oldref, newref, logmsg);
103}
104
105static int each_ref(const char *refname, const struct object_id *oid,
106 int flags, void *cb_data)
107{
108 printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags);
109 return 0;
110}
111
112static int cmd_for_each_ref(struct ref_store *refs, const char **argv)
113{
114 const char *prefix = notnull(*argv++, "prefix");
115
116 return refs_for_each_ref_in(refs, prefix, each_ref, NULL);
117}
118
119static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
120{
0221eb86 121 struct object_id oid = *null_oid();
80f2a609
NTND
122 const char *refname = notnull(*argv++, "refname");
123 int resolve_flags = arg_flags(*argv++, "resolve-flags");
124 int flags;
125 const char *ref;
126
127 ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
49e61479 128 &oid, &flags);
e7e456f5 129 printf("%s %s 0x%x\n", oid_to_hex(&oid), ref ? ref : "(null)", flags);
80f2a609
NTND
130 return ref ? 0 : 1;
131}
132
133static int cmd_verify_ref(struct ref_store *refs, const char **argv)
134{
135 const char *refname = notnull(*argv++, "refname");
136 struct strbuf err = STRBUF_INIT;
137 int ret;
138
139 ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);
140 if (err.len)
141 puts(err.buf);
142 return ret;
143}
144
145static int cmd_for_each_reflog(struct ref_store *refs, const char **argv)
146{
147 return refs_for_each_reflog(refs, each_ref, NULL);
148}
149
150static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
dddbad72 151 const char *committer, timestamp_t timestamp,
80f2a609
NTND
152 int tz, const char *msg, void *cb_data)
153{
cb71f8bd 154 printf("%s %s %s %"PRItime" %d %s\n",
80f2a609
NTND
155 oid_to_hex(old_oid), oid_to_hex(new_oid),
156 committer, timestamp, tz, msg);
157 return 0;
158}
159
160static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
161{
162 const char *refname = notnull(*argv++, "refname");
163
164 return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
165}
166
167static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
168{
169 const char *refname = notnull(*argv++, "refname");
170
171 return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
172}
173
174static int cmd_reflog_exists(struct ref_store *refs, const char **argv)
175{
176 const char *refname = notnull(*argv++, "refname");
177
178 return !refs_reflog_exists(refs, refname);
179}
180
181static int cmd_create_reflog(struct ref_store *refs, const char **argv)
182{
183 const char *refname = notnull(*argv++, "refname");
80f2a609
NTND
184 struct strbuf err = STRBUF_INIT;
185 int ret;
186
7b089120 187 ret = refs_create_reflog(refs, refname, &err);
80f2a609
NTND
188 if (err.len)
189 puts(err.buf);
190 return ret;
191}
192
193static int cmd_delete_reflog(struct ref_store *refs, const char **argv)
194{
195 const char *refname = notnull(*argv++, "refname");
196
197 return refs_delete_reflog(refs, refname);
198}
199
200static int cmd_reflog_expire(struct ref_store *refs, const char **argv)
201{
202 die("not supported yet");
203}
204
205static int cmd_delete_ref(struct ref_store *refs, const char **argv)
206{
207 const char *msg = notnull(*argv++, "msg");
208 const char *refname = notnull(*argv++, "refname");
209 const char *sha1_buf = notnull(*argv++, "old-sha1");
210 unsigned int flags = arg_flags(*argv++, "flags");
2616a5e5 211 struct object_id old_oid;
80f2a609 212
2616a5e5 213 if (get_oid_hex(sha1_buf, &old_oid))
80f2a609
NTND
214 die("not sha-1");
215
2616a5e5 216 return refs_delete_ref(refs, msg, refname, &old_oid, flags);
80f2a609
NTND
217}
218
219static int cmd_update_ref(struct ref_store *refs, const char **argv)
220{
221 const char *msg = notnull(*argv++, "msg");
222 const char *refname = notnull(*argv++, "refname");
3c27e2e0 223 const char *new_sha1_buf = notnull(*argv++, "new-sha1");
80f2a609
NTND
224 const char *old_sha1_buf = notnull(*argv++, "old-sha1");
225 unsigned int flags = arg_flags(*argv++, "flags");
ae077771 226 struct object_id old_oid;
227 struct object_id new_oid;
80f2a609 228
ae077771 229 if (get_oid_hex(old_sha1_buf, &old_oid) ||
230 get_oid_hex(new_sha1_buf, &new_oid))
80f2a609
NTND
231 die("not sha-1");
232
233 return refs_update_ref(refs, msg, refname,
ae077771 234 &new_oid, &old_oid,
80f2a609
NTND
235 flags, UPDATE_REFS_DIE_ON_ERR);
236}
237
238struct command {
239 const char *name;
240 int (*func)(struct ref_store *refs, const char **argv);
241};
242
243static struct command commands[] = {
244 { "pack-refs", cmd_pack_refs },
80f2a609
NTND
245 { "create-symref", cmd_create_symref },
246 { "delete-refs", cmd_delete_refs },
247 { "rename-ref", cmd_rename_ref },
248 { "for-each-ref", cmd_for_each_ref },
249 { "resolve-ref", cmd_resolve_ref },
250 { "verify-ref", cmd_verify_ref },
251 { "for-each-reflog", cmd_for_each_reflog },
252 { "for-each-reflog-ent", cmd_for_each_reflog_ent },
253 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse },
254 { "reflog-exists", cmd_reflog_exists },
255 { "create-reflog", cmd_create_reflog },
256 { "delete-reflog", cmd_delete_reflog },
257 { "reflog-expire", cmd_reflog_expire },
258 /*
259 * backend transaction functions can't be tested separately
260 */
261 { "delete-ref", cmd_delete_ref },
262 { "update-ref", cmd_update_ref },
263 { NULL, NULL }
264};
265
65370d81 266int cmd__ref_store(int argc, const char **argv)
80f2a609
NTND
267{
268 struct ref_store *refs;
269 const char *func;
270 struct command *cmd;
271
272 setup_git_directory();
273
274 argv = get_store(argv + 1, &refs);
275
276 func = *argv++;
277 if (!func)
278 die("ref function required");
279 for (cmd = commands; cmd->name; cmd++) {
280 if (!strcmp(func, cmd->name))
281 return cmd->func(refs, argv);
282 }
283 die("unknown function %s", func);
284 return 0;
285}