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