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