7 #include "object-store.h"
8 #include "repository.h"
10 struct flag_definition
{
20 static unsigned int parse_flags(const char *str
, struct flag_definition
*defs
)
22 struct string_list masks
= STRING_LIST_INIT_DUP
;
24 unsigned int result
= 0;
26 if (!strcmp(str
, "0"))
29 string_list_split(&masks
, str
, ',', 64);
30 for (; i
< masks
.nr
; i
++) {
31 const char *name
= masks
.items
[i
].string
;
32 struct flag_definition
*def
= defs
;
35 if (!strcmp(def
->name
, name
)) {
43 die("unknown flag \"%s\"", name
);
46 string_list_clear(&masks
, 0);
50 static struct flag_definition empty_flags
[] = { { NULL
, 0 } };
52 static const char *notnull(const char *arg
, const char *name
)
55 die("%s required", name
);
59 static unsigned int arg_flags(const char *arg
, const char *name
,
60 struct flag_definition
*defs
)
62 return parse_flags(notnull(arg
, name
), defs
);
65 static const char **get_store(const char **argv
, struct ref_store
**refs
)
70 die("ref store required");
71 } else if (!strcmp(argv
[0], "main")) {
72 *refs
= get_main_ref_store(the_repository
);
73 } else if (skip_prefix(argv
[0], "submodule:", &gitdir
)) {
74 struct strbuf sb
= STRBUF_INIT
;
77 ret
= strbuf_git_path_submodule(&sb
, gitdir
, "objects/");
79 die("strbuf_git_path_submodule failed: %d", ret
);
80 add_to_alternates_memory(sb
.buf
);
83 *refs
= get_submodule_ref_store(gitdir
);
84 } else if (skip_prefix(argv
[0], "worktree:", &gitdir
)) {
85 struct worktree
**p
, **worktrees
= get_worktrees();
87 for (p
= worktrees
; *p
; p
++) {
88 struct worktree
*wt
= *p
;
91 /* special case for main worktree */
92 if (!strcmp(gitdir
, "main"))
94 } else if (!strcmp(gitdir
, wt
->id
))
98 die("no such worktree: %s", gitdir
);
100 *refs
= get_worktree_ref_store(*p
);
101 free_worktrees(worktrees
);
103 die("unknown backend %s", argv
[0]);
108 /* consume store-specific optional arguments if needed */
113 static struct flag_definition pack_flags
[] = { FLAG_DEF(PACK_REFS_PRUNE
),
114 FLAG_DEF(PACK_REFS_ALL
),
117 static int cmd_pack_refs(struct ref_store
*refs
, const char **argv
)
119 unsigned int flags
= arg_flags(*argv
++, "flags", pack_flags
);
121 return refs_pack_refs(refs
, flags
);
124 static int cmd_create_symref(struct ref_store
*refs
, const char **argv
)
126 const char *refname
= notnull(*argv
++, "refname");
127 const char *target
= notnull(*argv
++, "target");
128 const char *logmsg
= *argv
++;
130 return refs_create_symref(refs
, refname
, target
, logmsg
);
133 static struct flag_definition transaction_flags
[] = {
134 FLAG_DEF(REF_NO_DEREF
),
135 FLAG_DEF(REF_FORCE_CREATE_REFLOG
),
136 FLAG_DEF(REF_SKIP_OID_VERIFICATION
),
137 FLAG_DEF(REF_SKIP_REFNAME_VERIFICATION
),
141 static int cmd_delete_refs(struct ref_store
*refs
, const char **argv
)
143 unsigned int flags
= arg_flags(*argv
++, "flags", transaction_flags
);
144 const char *msg
= *argv
++;
145 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
149 string_list_append(&refnames
, *argv
++);
151 result
= refs_delete_refs(refs
, msg
, &refnames
, flags
);
152 string_list_clear(&refnames
, 0);
156 static int cmd_rename_ref(struct ref_store
*refs
, const char **argv
)
158 const char *oldref
= notnull(*argv
++, "oldref");
159 const char *newref
= notnull(*argv
++, "newref");
160 const char *logmsg
= *argv
++;
162 return refs_rename_ref(refs
, oldref
, newref
, logmsg
);
165 static int each_ref(const char *refname
, const struct object_id
*oid
,
166 int flags
, void *cb_data UNUSED
)
168 printf("%s %s 0x%x\n", oid_to_hex(oid
), refname
, flags
);
172 static int cmd_for_each_ref(struct ref_store
*refs
, const char **argv
)
174 const char *prefix
= notnull(*argv
++, "prefix");
176 return refs_for_each_ref_in(refs
, prefix
, each_ref
, NULL
);
179 static int cmd_resolve_ref(struct ref_store
*refs
, const char **argv
)
181 struct object_id oid
= *null_oid();
182 const char *refname
= notnull(*argv
++, "refname");
183 int resolve_flags
= arg_flags(*argv
++, "resolve-flags", empty_flags
);
187 ref
= refs_resolve_ref_unsafe(refs
, refname
, resolve_flags
,
189 printf("%s %s 0x%x\n", oid_to_hex(&oid
), ref
? ref
: "(null)", flags
);
193 static int cmd_verify_ref(struct ref_store
*refs
, const char **argv
)
195 const char *refname
= notnull(*argv
++, "refname");
196 struct strbuf err
= STRBUF_INIT
;
199 ret
= refs_verify_refname_available(refs
, refname
, NULL
, NULL
, &err
);
205 static int cmd_for_each_reflog(struct ref_store
*refs
, const char **argv
)
207 return refs_for_each_reflog(refs
, each_ref
, NULL
);
210 static int each_reflog(struct object_id
*old_oid
, struct object_id
*new_oid
,
211 const char *committer
, timestamp_t timestamp
,
212 int tz
, const char *msg
, void *cb_data UNUSED
)
214 printf("%s %s %s %" PRItime
" %+05d%s%s", oid_to_hex(old_oid
),
215 oid_to_hex(new_oid
), committer
, timestamp
, tz
,
216 *msg
== '\n' ? "" : "\t", msg
);
220 static int cmd_for_each_reflog_ent(struct ref_store
*refs
, const char **argv
)
222 const char *refname
= notnull(*argv
++, "refname");
224 return refs_for_each_reflog_ent(refs
, refname
, each_reflog
, refs
);
227 static int cmd_for_each_reflog_ent_reverse(struct ref_store
*refs
, const char **argv
)
229 const char *refname
= notnull(*argv
++, "refname");
231 return refs_for_each_reflog_ent_reverse(refs
, refname
, each_reflog
, refs
);
234 static int cmd_reflog_exists(struct ref_store
*refs
, const char **argv
)
236 const char *refname
= notnull(*argv
++, "refname");
238 return !refs_reflog_exists(refs
, refname
);
241 static int cmd_create_reflog(struct ref_store
*refs
, const char **argv
)
243 const char *refname
= notnull(*argv
++, "refname");
244 struct strbuf err
= STRBUF_INIT
;
247 ret
= refs_create_reflog(refs
, refname
, &err
);
253 static int cmd_delete_reflog(struct ref_store
*refs
, const char **argv
)
255 const char *refname
= notnull(*argv
++, "refname");
257 return refs_delete_reflog(refs
, refname
);
260 static int cmd_reflog_expire(struct ref_store
*refs
, const char **argv
)
262 die("not supported yet");
265 static int cmd_delete_ref(struct ref_store
*refs
, const char **argv
)
267 const char *msg
= notnull(*argv
++, "msg");
268 const char *refname
= notnull(*argv
++, "refname");
269 const char *sha1_buf
= notnull(*argv
++, "old-sha1");
270 unsigned int flags
= arg_flags(*argv
++, "flags", transaction_flags
);
271 struct object_id old_oid
;
273 if (get_oid_hex(sha1_buf
, &old_oid
))
274 die("cannot parse %s as %s", sha1_buf
, the_hash_algo
->name
);
276 return refs_delete_ref(refs
, msg
, refname
, &old_oid
, flags
);
279 static int cmd_update_ref(struct ref_store
*refs
, const char **argv
)
281 const char *msg
= notnull(*argv
++, "msg");
282 const char *refname
= notnull(*argv
++, "refname");
283 const char *new_sha1_buf
= notnull(*argv
++, "new-sha1");
284 const char *old_sha1_buf
= notnull(*argv
++, "old-sha1");
285 unsigned int flags
= arg_flags(*argv
++, "flags", transaction_flags
);
286 struct object_id old_oid
;
287 struct object_id new_oid
;
289 if (get_oid_hex(old_sha1_buf
, &old_oid
))
290 die("cannot parse %s as %s", old_sha1_buf
, the_hash_algo
->name
);
291 if (get_oid_hex(new_sha1_buf
, &new_oid
))
292 die("cannot parse %s as %s", new_sha1_buf
, the_hash_algo
->name
);
294 return refs_update_ref(refs
, msg
, refname
,
296 flags
, UPDATE_REFS_DIE_ON_ERR
);
301 int (*func
)(struct ref_store
*refs
, const char **argv
);
304 static struct command commands
[] = {
305 { "pack-refs", cmd_pack_refs
},
306 { "create-symref", cmd_create_symref
},
307 { "delete-refs", cmd_delete_refs
},
308 { "rename-ref", cmd_rename_ref
},
309 { "for-each-ref", cmd_for_each_ref
},
310 { "resolve-ref", cmd_resolve_ref
},
311 { "verify-ref", cmd_verify_ref
},
312 { "for-each-reflog", cmd_for_each_reflog
},
313 { "for-each-reflog-ent", cmd_for_each_reflog_ent
},
314 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse
},
315 { "reflog-exists", cmd_reflog_exists
},
316 { "create-reflog", cmd_create_reflog
},
317 { "delete-reflog", cmd_delete_reflog
},
318 { "reflog-expire", cmd_reflog_expire
},
320 * backend transaction functions can't be tested separately
322 { "delete-ref", cmd_delete_ref
},
323 { "update-ref", cmd_update_ref
},
327 int cmd__ref_store(int argc
, const char **argv
)
329 struct ref_store
*refs
;
333 setup_git_directory();
335 argv
= get_store(argv
+ 1, &refs
);
339 die("ref function required");
340 for (cmd
= commands
; cmd
->name
; cmd
++) {
341 if (!strcmp(func
, cmd
->name
))
342 return cmd
->func(refs
, argv
);
344 die("unknown function %s", func
);