1#include "cache.h"
2#include "refs.h"
3#include "worktree.h"
4
5static const char *notnull(const char *arg, const char *name)
6{
7 if (!arg)
8 die("%s required", name);
9 return arg;
10}
11
12static unsigned int arg_flags(const char *arg, const char *name)
13{
14 return atoi(notnull(arg, name));
15}
16
17static const char **get_store(const char **argv, struct ref_store **refs)
18{
19 const char *gitdir;
20
21 if (!argv[0]) {
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;
27 int ret;
28
29 ret = strbuf_git_path_submodule(&sb, gitdir, "objects/");
30 if (ret)
31 die("strbuf_git_path_submodule failed: %d", ret);
32 add_to_alternates_memory(sb.buf);
33 strbuf_release(&sb);
34
35 *refs = get_submodule_ref_store(gitdir);
36 } else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
37 struct worktree **p, **worktrees = get_worktrees(0);
38
39 for (p = worktrees; *p; p++) {
40 struct worktree *wt = *p;
41
42 if (!wt->id) {
43 /* special case for main worktree */
44 if (!strcmp(gitdir, "main"))
45 break;
46 } else if (!strcmp(gitdir, wt->id))
47 break;
48 }
49 if (!*p)
50 die("no such worktree: %s", gitdir);
51
52 *refs = get_worktree_ref_store(*p);
53 } else
54 die("unknown backend %s", argv[0]);
55
56 if (!*refs)
57 die("no ref store");
58
59 /* consume store-specific optional arguments if needed */
60
61 return argv + 1;
62}
63
64
65static int cmd_pack_refs(struct ref_store *refs, const char **argv)
66{
67 unsigned int flags = arg_flags(*argv++, "flags");
68
69 return refs_pack_refs(refs, flags);
70}
71
72static int cmd_peel_ref(struct ref_store *refs, const char **argv)
73{
74 const char *refname = notnull(*argv++, "refname");
75 unsigned char sha1[20];
76 int ret;
77
78 ret = refs_peel_ref(refs, refname, sha1);
79 if (!ret)
80 puts(sha1_to_hex(sha1));
81 return ret;
82}
83
84static int cmd_create_symref(struct ref_store *refs, const char **argv)
85{
86 const char *refname = notnull(*argv++, "refname");
87 const char *target = notnull(*argv++, "target");
88 const char *logmsg = *argv++;
89
90 return refs_create_symref(refs, refname, target, logmsg);
91}
92
93static int cmd_delete_refs(struct ref_store *refs, const char **argv)
94{
95 unsigned int flags = arg_flags(*argv++, "flags");
96 const char *msg = *argv++;
97 struct string_list refnames = STRING_LIST_INIT_NODUP;
98
99 while (*argv)
100 string_list_append(&refnames, *argv++);
101
102 return refs_delete_refs(refs, msg, &refnames, flags);
103}
104
105static int cmd_rename_ref(struct ref_store *refs, const char **argv)
106{
107 const char *oldref = notnull(*argv++, "oldref");
108 const char *newref = notnull(*argv++, "newref");
109 const char *logmsg = *argv++;
110
111 return refs_rename_ref(refs, oldref, newref, logmsg);
112}
113
114static int each_ref(const char *refname, const struct object_id *oid,
115 int flags, void *cb_data)
116{
117 printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags);
118 return 0;
119}
120
121static int cmd_for_each_ref(struct ref_store *refs, const char **argv)
122{
123 const char *prefix = notnull(*argv++, "prefix");
124
125 return refs_for_each_ref_in(refs, prefix, each_ref, NULL);
126}
127
128static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
129{
130 unsigned char sha1[20];
131 const char *refname = notnull(*argv++, "refname");
132 int resolve_flags = arg_flags(*argv++, "resolve-flags");
133 int flags;
134 const char *ref;
135
136 ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
137 sha1, &flags);
138 printf("%s %s 0x%x\n", sha1_to_hex(sha1), ref, flags);
139 return ref ? 0 : 1;
140}
141
142static int cmd_verify_ref(struct ref_store *refs, const char **argv)
143{
144 const char *refname = notnull(*argv++, "refname");
145 struct strbuf err = STRBUF_INIT;
146 int ret;
147
148 ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);
149 if (err.len)
150 puts(err.buf);
151 return ret;
152}
153
154static int cmd_for_each_reflog(struct ref_store *refs, const char **argv)
155{
156 return refs_for_each_reflog(refs, each_ref, NULL);
157}
158
159static 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)
162{
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);
166 return 0;
167}
168
169static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
170{
171 const char *refname = notnull(*argv++, "refname");
172
173 return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
174}
175
176static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
177{
178 const char *refname = notnull(*argv++, "refname");
179
180 return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
181}
182
183static int cmd_reflog_exists(struct ref_store *refs, const char **argv)
184{
185 const char *refname = notnull(*argv++, "refname");
186
187 return !refs_reflog_exists(refs, refname);
188}
189
190static int cmd_create_reflog(struct ref_store *refs, const char **argv)
191{
192 const char *refname = notnull(*argv++, "refname");
193 int force_create = arg_flags(*argv++, "force-create");
194 struct strbuf err = STRBUF_INIT;
195 int ret;
196
197 ret = refs_create_reflog(refs, refname, force_create, &err);
198 if (err.len)
199 puts(err.buf);
200 return ret;
201}
202
203static int cmd_delete_reflog(struct ref_store *refs, const char **argv)
204{
205 const char *refname = notnull(*argv++, "refname");
206
207 return refs_delete_reflog(refs, refname);
208}
209
210static int cmd_reflog_expire(struct ref_store *refs, const char **argv)
211{
212 die("not supported yet");
213}
214
215static int cmd_delete_ref(struct ref_store *refs, const char **argv)
216{
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];
222
223 if (get_sha1_hex(sha1_buf, old_sha1))
224 die("not sha-1");
225
226 return refs_delete_ref(refs, msg, refname, old_sha1, flags);
227}
228
229static int cmd_update_ref(struct ref_store *refs, const char **argv)
230{
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];
238
239 if (get_sha1_hex(old_sha1_buf, old_sha1) ||
240 get_sha1_hex(new_sha1_buf, new_sha1))
241 die("not sha-1");
242
243 return refs_update_ref(refs, msg, refname,
244 new_sha1, old_sha1,
245 flags, UPDATE_REFS_DIE_ON_ERR);
246}
247
248struct command {
249 const char *name;
250 int (*func)(struct ref_store *refs, const char **argv);
251};
252
253static 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 },
269 /*
270 * backend transaction functions can't be tested separately
271 */
272 { "delete-ref", cmd_delete_ref },
273 { "update-ref", cmd_update_ref },
274 { NULL, NULL }
275};
276
277int cmd_main(int argc, const char **argv)
278{
279 struct ref_store *refs;
280 const char *func;
281 struct command *cmd;
282
283 setup_git_directory();
284
285 argv = get_store(argv + 1, &refs);
286
287 func = *argv++;
288 if (!func)
289 die("ref function required");
290 for (cmd = commands; cmd->name; cmd++) {
291 if (!strcmp(func, cmd->name))
292 return cmd->func(refs, argv);
293 }
294 die("unknown function %s", func);
295 return 0;
296}