6 static const char *notnull(const char *arg, const char *name)
9 die("%s required", name);
13 static unsigned int arg_flags(const char *arg, const char *name)
15 return atoi(notnull(arg, name));
18 static const char **get_store(const char **argv, struct ref_store **refs)
23 die("ref store required");
24 } else if (!strcmp(argv[0], "main")) {
25 *refs = get_main_ref_store();
26 } else if (skip_prefix(argv[0], "submodule:", &gitdir)) {
27 struct strbuf sb = STRBUF_INIT;
30 ret = strbuf_git_path_submodule(&sb, gitdir, "objects/");
32 die("strbuf_git_path_submodule failed: %d", ret);
33 add_to_alternates_memory(sb.buf);
36 *refs = get_submodule_ref_store(gitdir);
37 } else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
38 struct worktree **p, **worktrees = get_worktrees(0);
40 for (p = worktrees; *p; p++) {
41 struct worktree *wt = *p;
44 /* special case for main worktree */
45 if (!strcmp(gitdir, "main"))
47 } else if (!strcmp(gitdir, wt->id))
51 die("no such worktree: %s", gitdir);
53 *refs = get_worktree_ref_store(*p);
55 die("unknown backend %s", argv[0]);
60 /* consume store-specific optional arguments if needed */
66 static int cmd_pack_refs(struct ref_store *refs, const char **argv)
68 unsigned int flags = arg_flags(*argv++, "flags");
70 return refs_pack_refs(refs, flags);
73 static int cmd_peel_ref(struct ref_store *refs, const char **argv)
75 const char *refname = notnull(*argv++, "refname");
79 ret = refs_peel_ref(refs, refname, &oid);
81 puts(oid_to_hex(&oid));
85 static int cmd_create_symref(struct ref_store *refs, const char **argv)
87 const char *refname = notnull(*argv++, "refname");
88 const char *target = notnull(*argv++, "target");
89 const char *logmsg = *argv++;
91 return refs_create_symref(refs, refname, target, logmsg);
94 static int cmd_delete_refs(struct ref_store *refs, const char **argv)
96 unsigned int flags = arg_flags(*argv++, "flags");
97 const char *msg = *argv++;
98 struct string_list refnames = STRING_LIST_INIT_NODUP;
101 string_list_append(&refnames, *argv++);
103 return refs_delete_refs(refs, msg, &refnames, flags);
106 static int cmd_rename_ref(struct ref_store *refs, const char **argv)
108 const char *oldref = notnull(*argv++, "oldref");
109 const char *newref = notnull(*argv++, "newref");
110 const char *logmsg = *argv++;
112 return refs_rename_ref(refs, oldref, newref, logmsg);
115 static int each_ref(const char *refname, const struct object_id *oid,
116 int flags, void *cb_data)
118 printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags);
122 static int cmd_for_each_ref(struct ref_store *refs, const char **argv)
124 const char *prefix = notnull(*argv++, "prefix");
126 return refs_for_each_ref_in(refs, prefix, each_ref, NULL);
129 static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
131 struct object_id oid;
132 const char *refname = notnull(*argv++, "refname");
133 int resolve_flags = arg_flags(*argv++, "resolve-flags");
137 ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
139 printf("%s %s 0x%x\n", oid_to_hex(&oid), ref ? ref : "(null)", flags);
143 static int cmd_verify_ref(struct ref_store *refs, const char **argv)
145 const char *refname = notnull(*argv++, "refname");
146 struct strbuf err = STRBUF_INIT;
149 ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);
155 static int cmd_for_each_reflog(struct ref_store *refs, const char **argv)
157 return refs_for_each_reflog(refs, each_ref, NULL);
160 static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
161 const char *committer, timestamp_t timestamp,
162 int tz, const char *msg, void *cb_data)
164 printf("%s %s %s %"PRItime" %d %s\n",
165 oid_to_hex(old_oid), oid_to_hex(new_oid),
166 committer, timestamp, tz, msg);
170 static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
172 const char *refname = notnull(*argv++, "refname");
174 return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
177 static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
179 const char *refname = notnull(*argv++, "refname");
181 return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
184 static int cmd_reflog_exists(struct ref_store *refs, const char **argv)
186 const char *refname = notnull(*argv++, "refname");
188 return !refs_reflog_exists(refs, refname);
191 static int cmd_create_reflog(struct ref_store *refs, const char **argv)
193 const char *refname = notnull(*argv++, "refname");
194 int force_create = arg_flags(*argv++, "force-create");
195 struct strbuf err = STRBUF_INIT;
198 ret = refs_create_reflog(refs, refname, force_create, &err);
204 static int cmd_delete_reflog(struct ref_store *refs, const char **argv)
206 const char *refname = notnull(*argv++, "refname");
208 return refs_delete_reflog(refs, refname);
211 static int cmd_reflog_expire(struct ref_store *refs, const char **argv)
213 die("not supported yet");
216 static int cmd_delete_ref(struct ref_store *refs, const char **argv)
218 const char *msg = notnull(*argv++, "msg");
219 const char *refname = notnull(*argv++, "refname");
220 const char *sha1_buf = notnull(*argv++, "old-sha1");
221 unsigned int flags = arg_flags(*argv++, "flags");
222 struct object_id old_oid;
224 if (get_oid_hex(sha1_buf, &old_oid))
227 return refs_delete_ref(refs, msg, refname, &old_oid, flags);
230 static int cmd_update_ref(struct ref_store *refs, const char **argv)
232 const char *msg = notnull(*argv++, "msg");
233 const char *refname = notnull(*argv++, "refname");
234 const char *new_sha1_buf = notnull(*argv++, "old-sha1");
235 const char *old_sha1_buf = notnull(*argv++, "old-sha1");
236 unsigned int flags = arg_flags(*argv++, "flags");
237 struct object_id old_oid;
238 struct object_id new_oid;
240 if (get_oid_hex(old_sha1_buf, &old_oid) ||
241 get_oid_hex(new_sha1_buf, &new_oid))
244 return refs_update_ref(refs, msg, refname,
246 flags, UPDATE_REFS_DIE_ON_ERR);
251 int (*func)(struct ref_store *refs, const char **argv);
254 static struct command commands[] = {
255 { "pack-refs", cmd_pack_refs },
256 { "peel-ref", cmd_peel_ref },
257 { "create-symref", cmd_create_symref },
258 { "delete-refs", cmd_delete_refs },
259 { "rename-ref", cmd_rename_ref },
260 { "for-each-ref", cmd_for_each_ref },
261 { "resolve-ref", cmd_resolve_ref },
262 { "verify-ref", cmd_verify_ref },
263 { "for-each-reflog", cmd_for_each_reflog },
264 { "for-each-reflog-ent", cmd_for_each_reflog_ent },
265 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse },
266 { "reflog-exists", cmd_reflog_exists },
267 { "create-reflog", cmd_create_reflog },
268 { "delete-reflog", cmd_delete_reflog },
269 { "reflog-expire", cmd_reflog_expire },
271 * backend transaction functions can't be tested separately
273 { "delete-ref", cmd_delete_ref },
274 { "update-ref", cmd_update_ref },
278 int cmd__ref_store(int argc, const char **argv)
280 struct ref_store *refs;
284 setup_git_directory();
286 argv = get_store(argv + 1, &refs);
290 die("ref function required");
291 for (cmd = commands; cmd->name; cmd++) {
292 if (!strcmp(func, cmd->name))
293 return cmd->func(refs, argv);
295 die("unknown function %s", func);