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