5 static const char *notnull(const char *arg, const char *name)
8 die("%s required", name);
12 static unsigned int arg_flags(const char *arg, const char *name)
14 return atoi(notnull(arg, name));
17 static const char **get_store(const char **argv, struct ref_store **refs)
22 die("ref store required");
23 } else if (!strcmp(argv[0], "main")) {
24 *refs = get_main_ref_store();
25 } else if (skip_prefix(argv[0], "submodule:", &gitdir)) {
26 struct strbuf sb = STRBUF_INIT;
29 ret = strbuf_git_path_submodule(&sb, gitdir, "objects/");
31 die("strbuf_git_path_submodule failed: %d", ret);
32 add_to_alternates_memory(sb.buf);
35 *refs = get_submodule_ref_store(gitdir);
36 } else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
37 struct worktree **p, **worktrees = get_worktrees(0);
39 for (p = worktrees; *p; p++) {
40 struct worktree *wt = *p;
43 /* special case for main worktree */
44 if (!strcmp(gitdir, "main"))
46 } else if (!strcmp(gitdir, wt->id))
50 die("no such worktree: %s", gitdir);
52 *refs = get_worktree_ref_store(*p);
54 die("unknown backend %s", argv[0]);
59 /* consume store-specific optional arguments if needed */
65 static int cmd_pack_refs(struct ref_store *refs, const char **argv)
67 unsigned int flags = arg_flags(*argv++, "flags");
69 return refs_pack_refs(refs, flags);
72 static int cmd_peel_ref(struct ref_store *refs, const char **argv)
74 const char *refname = notnull(*argv++, "refname");
75 unsigned char sha1[20];
78 ret = refs_peel_ref(refs, refname, sha1);
80 puts(sha1_to_hex(sha1));
84 static int cmd_create_symref(struct ref_store *refs, const char **argv)
86 const char *refname = notnull(*argv++, "refname");
87 const char *target = notnull(*argv++, "target");
88 const char *logmsg = *argv++;
90 return refs_create_symref(refs, refname, target, logmsg);
93 static int cmd_delete_refs(struct ref_store *refs, const char **argv)
95 unsigned int flags = arg_flags(*argv++, "flags");
96 const char *msg = *argv++;
97 struct string_list refnames = STRING_LIST_INIT_NODUP;
100 string_list_append(&refnames, *argv++);
102 return refs_delete_refs(refs, msg, &refnames, flags);
105 static int cmd_rename_ref(struct ref_store *refs, const char **argv)
107 const char *oldref = notnull(*argv++, "oldref");
108 const char *newref = notnull(*argv++, "newref");
109 const char *logmsg = *argv++;
111 return refs_rename_ref(refs, oldref, newref, logmsg);
114 static int each_ref(const char *refname, const struct object_id *oid,
115 int flags, void *cb_data)
117 printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags);
121 static int cmd_for_each_ref(struct ref_store *refs, const char **argv)
123 const char *prefix = notnull(*argv++, "prefix");
125 return refs_for_each_ref_in(refs, prefix, each_ref, NULL);
128 static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
130 unsigned char sha1[20];
131 const char *refname = notnull(*argv++, "refname");
132 int resolve_flags = arg_flags(*argv++, "resolve-flags");
136 ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
138 printf("%s %s 0x%x\n", sha1_to_hex(sha1), ref, flags);
142 static int cmd_verify_ref(struct ref_store *refs, const char **argv)
144 const char *refname = notnull(*argv++, "refname");
145 struct strbuf err = STRBUF_INIT;
148 ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);
154 static int cmd_for_each_reflog(struct ref_store *refs, const char **argv)
156 return refs_for_each_reflog(refs, each_ref, NULL);
159 static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
160 const char *committer, timestamp_t timestamp,
161 int tz, const char *msg, void *cb_data)
163 printf("%s %s %s %"PRItime" %d %s\n",
164 oid_to_hex(old_oid), oid_to_hex(new_oid),
165 committer, timestamp, tz, msg);
169 static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
171 const char *refname = notnull(*argv++, "refname");
173 return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
176 static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
178 const char *refname = notnull(*argv++, "refname");
180 return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
183 static int cmd_reflog_exists(struct ref_store *refs, const char **argv)
185 const char *refname = notnull(*argv++, "refname");
187 return !refs_reflog_exists(refs, refname);
190 static int cmd_create_reflog(struct ref_store *refs, const char **argv)
192 const char *refname = notnull(*argv++, "refname");
193 int force_create = arg_flags(*argv++, "force-create");
194 struct strbuf err = STRBUF_INIT;
197 ret = refs_create_reflog(refs, refname, force_create, &err);
203 static int cmd_delete_reflog(struct ref_store *refs, const char **argv)
205 const char *refname = notnull(*argv++, "refname");
207 return refs_delete_reflog(refs, refname);
210 static int cmd_reflog_expire(struct ref_store *refs, const char **argv)
212 die("not supported yet");
215 static int cmd_delete_ref(struct ref_store *refs, const char **argv)
217 const char *msg = notnull(*argv++, "msg");
218 const char *refname = notnull(*argv++, "refname");
219 const char *sha1_buf = notnull(*argv++, "old-sha1");
220 unsigned int flags = arg_flags(*argv++, "flags");
221 unsigned char old_sha1[20];
223 if (get_sha1_hex(sha1_buf, old_sha1))
226 return refs_delete_ref(refs, msg, refname, old_sha1, flags);
229 static int cmd_update_ref(struct ref_store *refs, const char **argv)
231 const char *msg = notnull(*argv++, "msg");
232 const char *refname = notnull(*argv++, "refname");
233 const char *new_sha1_buf = notnull(*argv++, "old-sha1");
234 const char *old_sha1_buf = notnull(*argv++, "old-sha1");
235 unsigned int flags = arg_flags(*argv++, "flags");
236 unsigned char old_sha1[20];
237 unsigned char new_sha1[20];
239 if (get_sha1_hex(old_sha1_buf, old_sha1) ||
240 get_sha1_hex(new_sha1_buf, new_sha1))
243 return refs_update_ref(refs, msg, refname,
245 flags, UPDATE_REFS_DIE_ON_ERR);
250 int (*func)(struct ref_store *refs, const char **argv);
253 static struct command commands[] = {
254 { "pack-refs", cmd_pack_refs },
255 { "peel-ref", cmd_peel_ref },
256 { "create-symref", cmd_create_symref },
257 { "delete-refs", cmd_delete_refs },
258 { "rename-ref", cmd_rename_ref },
259 { "for-each-ref", cmd_for_each_ref },
260 { "resolve-ref", cmd_resolve_ref },
261 { "verify-ref", cmd_verify_ref },
262 { "for-each-reflog", cmd_for_each_reflog },
263 { "for-each-reflog-ent", cmd_for_each_reflog_ent },
264 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse },
265 { "reflog-exists", cmd_reflog_exists },
266 { "create-reflog", cmd_create_reflog },
267 { "delete-reflog", cmd_delete_reflog },
268 { "reflog-expire", cmd_reflog_expire },
270 * backend transaction functions can't be tested separately
272 { "delete-ref", cmd_delete_ref },
273 { "update-ref", cmd_update_ref },
277 int cmd_main(int argc, const char **argv)
279 struct ref_store *refs;
283 setup_git_directory();
285 argv = get_store(argv + 1, &refs);
289 die("ref function required");
290 for (cmd = commands; cmd->name; cmd++) {
291 if (!strcmp(func, cmd->name))
292 return cmd->func(refs, argv);
294 die("unknown function %s", func);