6 #include "object-store.h"
7 #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
);
120 static struct ref_exclusions exclusions
= REF_EXCLUSIONS_INIT
;
121 static struct string_list included_refs
= STRING_LIST_INIT_NODUP
;
122 struct pack_refs_opts pack_opts
= { .flags
= flags
,
123 .exclusions
= &exclusions
,
124 .includes
= &included_refs
};
126 if (pack_opts
.flags
& PACK_REFS_ALL
)
127 string_list_append(pack_opts
.includes
, "*");
129 return refs_pack_refs(refs
, &pack_opts
);
132 static int cmd_create_symref(struct ref_store
*refs
, const char **argv
)
134 const char *refname
= notnull(*argv
++, "refname");
135 const char *target
= notnull(*argv
++, "target");
136 const char *logmsg
= *argv
++;
138 return refs_create_symref(refs
, refname
, target
, logmsg
);
141 static struct flag_definition transaction_flags
[] = {
142 FLAG_DEF(REF_NO_DEREF
),
143 FLAG_DEF(REF_FORCE_CREATE_REFLOG
),
144 FLAG_DEF(REF_SKIP_OID_VERIFICATION
),
145 FLAG_DEF(REF_SKIP_REFNAME_VERIFICATION
),
149 static int cmd_delete_refs(struct ref_store
*refs
, const char **argv
)
151 unsigned int flags
= arg_flags(*argv
++, "flags", transaction_flags
);
152 const char *msg
= *argv
++;
153 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
157 string_list_append(&refnames
, *argv
++);
159 result
= refs_delete_refs(refs
, msg
, &refnames
, flags
);
160 string_list_clear(&refnames
, 0);
164 static int cmd_rename_ref(struct ref_store
*refs
, const char **argv
)
166 const char *oldref
= notnull(*argv
++, "oldref");
167 const char *newref
= notnull(*argv
++, "newref");
168 const char *logmsg
= *argv
++;
170 return refs_rename_ref(refs
, oldref
, newref
, logmsg
);
173 static int each_ref(const char *refname
, const struct object_id
*oid
,
174 int flags
, void *cb_data UNUSED
)
176 printf("%s %s 0x%x\n", oid_to_hex(oid
), refname
, flags
);
180 static int cmd_for_each_ref(struct ref_store
*refs
, const char **argv
)
182 const char *prefix
= notnull(*argv
++, "prefix");
184 return refs_for_each_ref_in(refs
, prefix
, each_ref
, NULL
);
187 static int cmd_resolve_ref(struct ref_store
*refs
, const char **argv
)
189 struct object_id oid
= *null_oid();
190 const char *refname
= notnull(*argv
++, "refname");
191 int resolve_flags
= arg_flags(*argv
++, "resolve-flags", empty_flags
);
195 ref
= refs_resolve_ref_unsafe(refs
, refname
, resolve_flags
,
197 printf("%s %s 0x%x\n", oid_to_hex(&oid
), ref
? ref
: "(null)", flags
);
201 static int cmd_verify_ref(struct ref_store
*refs
, const char **argv
)
203 const char *refname
= notnull(*argv
++, "refname");
204 struct strbuf err
= STRBUF_INIT
;
207 ret
= refs_verify_refname_available(refs
, refname
, NULL
, NULL
, &err
);
213 static int cmd_for_each_reflog(struct ref_store
*refs
,
214 const char **argv UNUSED
)
216 return refs_for_each_reflog(refs
, each_ref
, NULL
);
219 static int each_reflog(struct object_id
*old_oid
, struct object_id
*new_oid
,
220 const char *committer
, timestamp_t timestamp
,
221 int tz
, const char *msg
, void *cb_data UNUSED
)
223 printf("%s %s %s %" PRItime
" %+05d%s%s", oid_to_hex(old_oid
),
224 oid_to_hex(new_oid
), committer
, timestamp
, tz
,
225 *msg
== '\n' ? "" : "\t", msg
);
229 static int cmd_for_each_reflog_ent(struct ref_store
*refs
, const char **argv
)
231 const char *refname
= notnull(*argv
++, "refname");
233 return refs_for_each_reflog_ent(refs
, refname
, each_reflog
, refs
);
236 static int cmd_for_each_reflog_ent_reverse(struct ref_store
*refs
, const char **argv
)
238 const char *refname
= notnull(*argv
++, "refname");
240 return refs_for_each_reflog_ent_reverse(refs
, refname
, each_reflog
, refs
);
243 static int cmd_reflog_exists(struct ref_store
*refs
, const char **argv
)
245 const char *refname
= notnull(*argv
++, "refname");
247 return !refs_reflog_exists(refs
, refname
);
250 static int cmd_create_reflog(struct ref_store
*refs
, const char **argv
)
252 const char *refname
= notnull(*argv
++, "refname");
253 struct strbuf err
= STRBUF_INIT
;
256 ret
= refs_create_reflog(refs
, refname
, &err
);
262 static int cmd_delete_reflog(struct ref_store
*refs
, const char **argv
)
264 const char *refname
= notnull(*argv
++, "refname");
266 return refs_delete_reflog(refs
, refname
);
269 static int cmd_reflog_expire(struct ref_store
*refs
, const char **argv
)
271 die("not supported yet");
274 static int cmd_delete_ref(struct ref_store
*refs
, const char **argv
)
276 const char *msg
= notnull(*argv
++, "msg");
277 const char *refname
= notnull(*argv
++, "refname");
278 const char *sha1_buf
= notnull(*argv
++, "old-sha1");
279 unsigned int flags
= arg_flags(*argv
++, "flags", transaction_flags
);
280 struct object_id old_oid
;
282 if (get_oid_hex(sha1_buf
, &old_oid
))
283 die("cannot parse %s as %s", sha1_buf
, the_hash_algo
->name
);
285 return refs_delete_ref(refs
, msg
, refname
, &old_oid
, flags
);
288 static int cmd_update_ref(struct ref_store
*refs
, const char **argv
)
290 const char *msg
= notnull(*argv
++, "msg");
291 const char *refname
= notnull(*argv
++, "refname");
292 const char *new_sha1_buf
= notnull(*argv
++, "new-sha1");
293 const char *old_sha1_buf
= notnull(*argv
++, "old-sha1");
294 unsigned int flags
= arg_flags(*argv
++, "flags", transaction_flags
);
295 struct object_id old_oid
;
296 struct object_id new_oid
;
298 if (get_oid_hex(old_sha1_buf
, &old_oid
))
299 die("cannot parse %s as %s", old_sha1_buf
, the_hash_algo
->name
);
300 if (get_oid_hex(new_sha1_buf
, &new_oid
))
301 die("cannot parse %s as %s", new_sha1_buf
, the_hash_algo
->name
);
303 return refs_update_ref(refs
, msg
, refname
,
305 flags
, UPDATE_REFS_DIE_ON_ERR
);
310 int (*func
)(struct ref_store
*refs
, const char **argv
);
313 static struct command commands
[] = {
314 { "pack-refs", cmd_pack_refs
},
315 { "create-symref", cmd_create_symref
},
316 { "delete-refs", cmd_delete_refs
},
317 { "rename-ref", cmd_rename_ref
},
318 { "for-each-ref", cmd_for_each_ref
},
319 { "resolve-ref", cmd_resolve_ref
},
320 { "verify-ref", cmd_verify_ref
},
321 { "for-each-reflog", cmd_for_each_reflog
},
322 { "for-each-reflog-ent", cmd_for_each_reflog_ent
},
323 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse
},
324 { "reflog-exists", cmd_reflog_exists
},
325 { "create-reflog", cmd_create_reflog
},
326 { "delete-reflog", cmd_delete_reflog
},
327 { "reflog-expire", cmd_reflog_expire
},
329 * backend transaction functions can't be tested separately
331 { "delete-ref", cmd_delete_ref
},
332 { "update-ref", cmd_update_ref
},
336 int cmd__ref_store(int argc UNUSED
, const char **argv
)
338 struct ref_store
*refs
;
342 setup_git_directory();
344 argv
= get_store(argv
+ 1, &refs
);
348 die("ref function required");
349 for (cmd
= commands
; cmd
->name
; cmd
++) {
350 if (!strcmp(func
, cmd
->name
))
351 return cmd
->func(refs
, argv
);
353 die("unknown function %s", func
);