interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / helper / test-ref-store.c

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