5 #include "object-store.h"
6 #include "repository.h"
8 struct flag_definition
{
18 static unsigned int parse_flags(const char *str
, struct flag_definition
*defs
)
20 struct string_list masks
= STRING_LIST_INIT_DUP
;
22 unsigned int result
= 0;
24 if (!strcmp(str
, "0"))
27 string_list_split(&masks
, str
, ',', 64);
28 for (; i
< masks
.nr
; i
++) {
29 const char *name
= masks
.items
[i
].string
;
30 struct flag_definition
*def
= defs
;
33 if (!strcmp(def
->name
, name
)) {
41 die("unknown flag \"%s\"", name
);
44 string_list_clear(&masks
, 0);
48 static struct flag_definition empty_flags
[] = { { NULL
, 0 } };
50 static const char *notnull(const char *arg
, const char *name
)
53 die("%s required", name
);
57 static unsigned int arg_flags(const char *arg
, const char *name
,
58 struct flag_definition
*defs
)
60 return parse_flags(notnull(arg
, name
), defs
);
63 static const char **get_store(const char **argv
, struct ref_store
**refs
)
68 die("ref store required");
69 } else if (!strcmp(argv
[0], "main")) {
70 *refs
= get_main_ref_store(the_repository
);
71 } else if (skip_prefix(argv
[0], "submodule:", &gitdir
)) {
72 struct strbuf sb
= STRBUF_INIT
;
75 ret
= strbuf_git_path_submodule(&sb
, gitdir
, "objects/");
77 die("strbuf_git_path_submodule failed: %d", ret
);
78 add_to_alternates_memory(sb
.buf
);
81 *refs
= get_submodule_ref_store(gitdir
);
82 } else if (skip_prefix(argv
[0], "worktree:", &gitdir
)) {
83 struct worktree
**p
, **worktrees
= get_worktrees();
85 for (p
= worktrees
; *p
; p
++) {
86 struct worktree
*wt
= *p
;
89 /* special case for main worktree */
90 if (!strcmp(gitdir
, "main"))
92 } else if (!strcmp(gitdir
, wt
->id
))
96 die("no such worktree: %s", gitdir
);
98 *refs
= get_worktree_ref_store(*p
);
100 die("unknown backend %s", argv
[0]);
105 /* consume store-specific optional arguments if needed */
110 static struct flag_definition pack_flags
[] = { FLAG_DEF(PACK_REFS_PRUNE
),
111 FLAG_DEF(PACK_REFS_ALL
),
114 static int cmd_pack_refs(struct ref_store
*refs
, const char **argv
)
116 unsigned int flags
= arg_flags(*argv
++, "flags", pack_flags
);
118 return refs_pack_refs(refs
, flags
);
121 static int cmd_create_symref(struct ref_store
*refs
, const char **argv
)
123 const char *refname
= notnull(*argv
++, "refname");
124 const char *target
= notnull(*argv
++, "target");
125 const char *logmsg
= *argv
++;
127 return refs_create_symref(refs
, refname
, target
, logmsg
);
130 static struct flag_definition transaction_flags
[] = {
131 FLAG_DEF(REF_NO_DEREF
),
132 FLAG_DEF(REF_FORCE_CREATE_REFLOG
),
133 FLAG_DEF(REF_SKIP_OID_VERIFICATION
),
134 FLAG_DEF(REF_SKIP_REFNAME_VERIFICATION
),
138 static int cmd_delete_refs(struct ref_store
*refs
, const char **argv
)
140 unsigned int flags
= arg_flags(*argv
++, "flags", transaction_flags
);
141 const char *msg
= *argv
++;
142 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
146 string_list_append(&refnames
, *argv
++);
148 result
= refs_delete_refs(refs
, msg
, &refnames
, flags
);
149 string_list_clear(&refnames
, 0);
153 static int cmd_rename_ref(struct ref_store
*refs
, const char **argv
)
155 const char *oldref
= notnull(*argv
++, "oldref");
156 const char *newref
= notnull(*argv
++, "newref");
157 const char *logmsg
= *argv
++;
159 return refs_rename_ref(refs
, oldref
, newref
, logmsg
);
162 static int each_ref(const char *refname
, const struct object_id
*oid
,
163 int flags
, void *cb_data
)
165 printf("%s %s 0x%x\n", oid_to_hex(oid
), refname
, flags
);
169 static int cmd_for_each_ref(struct ref_store
*refs
, const char **argv
)
171 const char *prefix
= notnull(*argv
++, "prefix");
173 return refs_for_each_ref_in(refs
, prefix
, each_ref
, NULL
);
176 static int cmd_resolve_ref(struct ref_store
*refs
, const char **argv
)
178 struct object_id oid
= *null_oid();
179 const char *refname
= notnull(*argv
++, "refname");
180 int resolve_flags
= arg_flags(*argv
++, "resolve-flags", empty_flags
);
184 ref
= refs_resolve_ref_unsafe(refs
, refname
, resolve_flags
,
186 printf("%s %s 0x%x\n", oid_to_hex(&oid
), ref
? ref
: "(null)", flags
);
190 static int cmd_verify_ref(struct ref_store
*refs
, const char **argv
)
192 const char *refname
= notnull(*argv
++, "refname");
193 struct strbuf err
= STRBUF_INIT
;
196 ret
= refs_verify_refname_available(refs
, refname
, NULL
, NULL
, &err
);
202 static int cmd_for_each_reflog(struct ref_store
*refs
, const char **argv
)
204 return refs_for_each_reflog(refs
, each_ref
, NULL
);
207 static int each_reflog(struct object_id
*old_oid
, struct object_id
*new_oid
,
208 const char *committer
, timestamp_t timestamp
,
209 int tz
, const char *msg
, void *cb_data
)
211 printf("%s %s %s %" PRItime
" %+05d%s%s", oid_to_hex(old_oid
),
212 oid_to_hex(new_oid
), committer
, timestamp
, tz
,
213 *msg
== '\n' ? "" : "\t", msg
);
217 static int cmd_for_each_reflog_ent(struct ref_store
*refs
, const char **argv
)
219 const char *refname
= notnull(*argv
++, "refname");
221 return refs_for_each_reflog_ent(refs
, refname
, each_reflog
, refs
);
224 static int cmd_for_each_reflog_ent_reverse(struct ref_store
*refs
, const char **argv
)
226 const char *refname
= notnull(*argv
++, "refname");
228 return refs_for_each_reflog_ent_reverse(refs
, refname
, each_reflog
, refs
);
231 static int cmd_reflog_exists(struct ref_store
*refs
, const char **argv
)
233 const char *refname
= notnull(*argv
++, "refname");
235 return !refs_reflog_exists(refs
, refname
);
238 static int cmd_create_reflog(struct ref_store
*refs
, const char **argv
)
240 const char *refname
= notnull(*argv
++, "refname");
241 struct strbuf err
= STRBUF_INIT
;
244 ret
= refs_create_reflog(refs
, refname
, &err
);
250 static int cmd_delete_reflog(struct ref_store
*refs
, const char **argv
)
252 const char *refname
= notnull(*argv
++, "refname");
254 return refs_delete_reflog(refs
, refname
);
257 static int cmd_reflog_expire(struct ref_store
*refs
, const char **argv
)
259 die("not supported yet");
262 static int cmd_delete_ref(struct ref_store
*refs
, const char **argv
)
264 const char *msg
= notnull(*argv
++, "msg");
265 const char *refname
= notnull(*argv
++, "refname");
266 const char *sha1_buf
= notnull(*argv
++, "old-sha1");
267 unsigned int flags
= arg_flags(*argv
++, "flags", transaction_flags
);
268 struct object_id old_oid
;
270 if (get_oid_hex(sha1_buf
, &old_oid
))
271 die("cannot parse %s as %s", sha1_buf
, the_hash_algo
->name
);
273 return refs_delete_ref(refs
, msg
, refname
, &old_oid
, flags
);
276 static int cmd_update_ref(struct ref_store
*refs
, const char **argv
)
278 const char *msg
= notnull(*argv
++, "msg");
279 const char *refname
= notnull(*argv
++, "refname");
280 const char *new_sha1_buf
= notnull(*argv
++, "new-sha1");
281 const char *old_sha1_buf
= notnull(*argv
++, "old-sha1");
282 unsigned int flags
= arg_flags(*argv
++, "flags", transaction_flags
);
283 struct object_id old_oid
;
284 struct object_id new_oid
;
286 if (get_oid_hex(old_sha1_buf
, &old_oid
))
287 die("cannot parse %s as %s", old_sha1_buf
, the_hash_algo
->name
);
288 if (get_oid_hex(new_sha1_buf
, &new_oid
))
289 die("cannot parse %s as %s", new_sha1_buf
, the_hash_algo
->name
);
291 return refs_update_ref(refs
, msg
, refname
,
293 flags
, UPDATE_REFS_DIE_ON_ERR
);
298 int (*func
)(struct ref_store
*refs
, const char **argv
);
301 static struct command commands
[] = {
302 { "pack-refs", cmd_pack_refs
},
303 { "create-symref", cmd_create_symref
},
304 { "delete-refs", cmd_delete_refs
},
305 { "rename-ref", cmd_rename_ref
},
306 { "for-each-ref", cmd_for_each_ref
},
307 { "resolve-ref", cmd_resolve_ref
},
308 { "verify-ref", cmd_verify_ref
},
309 { "for-each-reflog", cmd_for_each_reflog
},
310 { "for-each-reflog-ent", cmd_for_each_reflog_ent
},
311 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse
},
312 { "reflog-exists", cmd_reflog_exists
},
313 { "create-reflog", cmd_create_reflog
},
314 { "delete-reflog", cmd_delete_reflog
},
315 { "reflog-expire", cmd_reflog_expire
},
317 * backend transaction functions can't be tested separately
319 { "delete-ref", cmd_delete_ref
},
320 { "update-ref", cmd_update_ref
},
324 int cmd__ref_store(int argc
, const char **argv
)
326 struct ref_store
*refs
;
330 setup_git_directory();
332 argv
= get_store(argv
+ 1, &refs
);
336 die("ref function required");
337 for (cmd
= commands
; cmd
->name
; cmd
++) {
338 if (!strcmp(func
, cmd
->name
))
339 return cmd
->func(refs
, argv
);
341 die("unknown function %s", func
);