5 #include "object-store.h"
6 #include "repository.h"
8 static const char *notnull(const char *arg, const char *name)
11 die("%s required", name);
15 static unsigned int arg_flags(const char *arg, const char *name)
17 return atoi(notnull(arg, name));
20 static const char **get_store(const char **argv, struct ref_store **refs)
25 die("ref store required");
26 } else if (!strcmp(argv[0], "main")) {
27 *refs = get_main_ref_store(the_repository);
28 } else if (skip_prefix(argv[0], "submodule:", &gitdir)) {
29 struct strbuf sb = STRBUF_INIT;
32 ret = strbuf_git_path_submodule(&sb, gitdir, "objects/");
34 die("strbuf_git_path_submodule failed: %d", ret);
35 add_to_alternates_memory(sb.buf);
38 *refs = get_submodule_ref_store(gitdir);
39 } else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
40 struct worktree **p, **worktrees = get_worktrees();
42 for (p = worktrees; *p; p++) {
43 struct worktree *wt = *p;
46 /* special case for main worktree */
47 if (!strcmp(gitdir, "main"))
49 } else if (!strcmp(gitdir, wt->id))
53 die("no such worktree: %s", gitdir);
55 *refs = get_worktree_ref_store(*p);
57 die("unknown backend %s", argv[0]);
62 /* consume store-specific optional arguments if needed */
68 static int cmd_pack_refs(struct ref_store *refs, const char **argv)
70 unsigned int flags = arg_flags(*argv++, "flags");
72 return refs_pack_refs(refs, flags);
75 static int cmd_peel_ref(struct ref_store *refs, const char **argv)
77 const char *refname = notnull(*argv++, "refname");
81 ret = refs_peel_ref(refs, refname, &oid);
83 puts(oid_to_hex(&oid));
87 static int cmd_create_symref(struct ref_store *refs, const char **argv)
89 const char *refname = notnull(*argv++, "refname");
90 const char *target = notnull(*argv++, "target");
91 const char *logmsg = *argv++;
93 return refs_create_symref(refs, refname, target, logmsg);
96 static int cmd_delete_refs(struct ref_store *refs, const char **argv)
98 unsigned int flags = arg_flags(*argv++, "flags");
99 const char *msg = *argv++;
100 struct string_list refnames = STRING_LIST_INIT_NODUP;
103 string_list_append(&refnames, *argv++);
105 return refs_delete_refs(refs, msg, &refnames, flags);
108 static int cmd_rename_ref(struct ref_store *refs, const char **argv)
110 const char *oldref = notnull(*argv++, "oldref");
111 const char *newref = notnull(*argv++, "newref");
112 const char *logmsg = *argv++;
114 return refs_rename_ref(refs, oldref, newref, logmsg);
117 static int each_ref(const char *refname, const struct object_id *oid,
118 int flags, void *cb_data)
120 printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags);
124 static int cmd_for_each_ref(struct ref_store *refs, const char **argv)
126 const char *prefix = notnull(*argv++, "prefix");
128 return refs_for_each_ref_in(refs, prefix, each_ref, NULL);
131 static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
133 struct object_id oid;
134 const char *refname = notnull(*argv++, "refname");
135 int resolve_flags = arg_flags(*argv++, "resolve-flags");
139 ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
141 printf("%s %s 0x%x\n", oid_to_hex(&oid), ref ? ref : "(null)", flags);
145 static int cmd_verify_ref(struct ref_store *refs, const char **argv)
147 const char *refname = notnull(*argv++, "refname");
148 struct strbuf err = STRBUF_INIT;
151 ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);
157 static int cmd_for_each_reflog(struct ref_store *refs, const char **argv)
159 return refs_for_each_reflog(refs, each_ref, NULL);
162 static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
163 const char *committer, timestamp_t timestamp,
164 int tz, const char *msg, void *cb_data)
166 printf("%s %s %s %"PRItime" %d %s\n",
167 oid_to_hex(old_oid), oid_to_hex(new_oid),
168 committer, timestamp, tz, msg);
172 static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
174 const char *refname = notnull(*argv++, "refname");
176 return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
179 static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
181 const char *refname = notnull(*argv++, "refname");
183 return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
186 static int cmd_reflog_exists(struct ref_store *refs, const char **argv)
188 const char *refname = notnull(*argv++, "refname");
190 return !refs_reflog_exists(refs, refname);
193 static int cmd_create_reflog(struct ref_store *refs, const char **argv)
195 const char *refname = notnull(*argv++, "refname");
196 int force_create = arg_flags(*argv++, "force-create");
197 struct strbuf err = STRBUF_INIT;
200 ret = refs_create_reflog(refs, refname, force_create, &err);
206 static int cmd_delete_reflog(struct ref_store *refs, const char **argv)
208 const char *refname = notnull(*argv++, "refname");
210 return refs_delete_reflog(refs, refname);
213 static int cmd_reflog_expire(struct ref_store *refs, const char **argv)
215 die("not supported yet");
218 static int cmd_delete_ref(struct ref_store *refs, const char **argv)
220 const char *msg = notnull(*argv++, "msg");
221 const char *refname = notnull(*argv++, "refname");
222 const char *sha1_buf = notnull(*argv++, "old-sha1");
223 unsigned int flags = arg_flags(*argv++, "flags");
224 struct object_id old_oid;
226 if (get_oid_hex(sha1_buf, &old_oid))
229 return refs_delete_ref(refs, msg, refname, &old_oid, flags);
232 static int cmd_update_ref(struct ref_store *refs, const char **argv)
234 const char *msg = notnull(*argv++, "msg");
235 const char *refname = notnull(*argv++, "refname");
236 const char *new_sha1_buf = notnull(*argv++, "new-sha1");
237 const char *old_sha1_buf = notnull(*argv++, "old-sha1");
238 unsigned int flags = arg_flags(*argv++, "flags");
239 struct object_id old_oid;
240 struct object_id new_oid;
242 if (get_oid_hex(old_sha1_buf, &old_oid) ||
243 get_oid_hex(new_sha1_buf, &new_oid))
246 return refs_update_ref(refs, msg, refname,
248 flags, UPDATE_REFS_DIE_ON_ERR);
253 int (*func)(struct ref_store *refs, const char **argv);
256 static struct command commands[] = {
257 { "pack-refs", cmd_pack_refs },
258 { "peel-ref", cmd_peel_ref },
259 { "create-symref", cmd_create_symref },
260 { "delete-refs", cmd_delete_refs },
261 { "rename-ref", cmd_rename_ref },
262 { "for-each-ref", cmd_for_each_ref },
263 { "resolve-ref", cmd_resolve_ref },
264 { "verify-ref", cmd_verify_ref },
265 { "for-each-reflog", cmd_for_each_reflog },
266 { "for-each-reflog-ent", cmd_for_each_reflog_ent },
267 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse },
268 { "reflog-exists", cmd_reflog_exists },
269 { "create-reflog", cmd_create_reflog },
270 { "delete-reflog", cmd_delete_reflog },
271 { "reflog-expire", cmd_reflog_expire },
273 * backend transaction functions can't be tested separately
275 { "delete-ref", cmd_delete_ref },
276 { "update-ref", cmd_update_ref },
280 int cmd__ref_store(int argc, const char **argv)
282 struct ref_store *refs;
286 setup_git_directory();
288 argv = get_store(argv + 1, &refs);
292 die("ref function required");
293 for (cmd = commands; cmd->name; cmd++) {
294 if (!strcmp(func, cmd->name))
295 return cmd->func(refs, argv);
297 die("unknown function %s", func);