4 static const char *notnull(const char *arg
, const char *name
)
7 die("%s required", name
);
11 static unsigned int arg_flags(const char *arg
, const char *name
)
13 return atoi(notnull(arg
, name
));
16 static const char **get_store(const char **argv
, struct ref_store
**refs
)
21 die("ref store required");
22 } else if (!strcmp(argv
[0], "main")) {
23 *refs
= get_main_ref_store();
24 } else if (skip_prefix(argv
[0], "submodule:", &gitdir
)) {
25 struct strbuf sb
= STRBUF_INIT
;
28 ret
= strbuf_git_path_submodule(&sb
, gitdir
, "objects/");
30 die("strbuf_git_path_submodule failed: %d", ret
);
31 add_to_alternates_memory(sb
.buf
);
34 *refs
= get_submodule_ref_store(gitdir
);
36 die("unknown backend %s", argv
[0]);
41 /* consume store-specific optional arguments if needed */
47 static int cmd_pack_refs(struct ref_store
*refs
, const char **argv
)
49 unsigned int flags
= arg_flags(*argv
++, "flags");
51 return refs_pack_refs(refs
, flags
);
54 static int cmd_peel_ref(struct ref_store
*refs
, const char **argv
)
56 const char *refname
= notnull(*argv
++, "refname");
57 unsigned char sha1
[20];
60 ret
= refs_peel_ref(refs
, refname
, sha1
);
62 puts(sha1_to_hex(sha1
));
66 static int cmd_create_symref(struct ref_store
*refs
, const char **argv
)
68 const char *refname
= notnull(*argv
++, "refname");
69 const char *target
= notnull(*argv
++, "target");
70 const char *logmsg
= *argv
++;
72 return refs_create_symref(refs
, refname
, target
, logmsg
);
75 static int cmd_delete_refs(struct ref_store
*refs
, const char **argv
)
77 unsigned int flags
= arg_flags(*argv
++, "flags");
78 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
81 string_list_append(&refnames
, *argv
++);
83 return refs_delete_refs(refs
, &refnames
, flags
);
86 static int cmd_rename_ref(struct ref_store
*refs
, const char **argv
)
88 const char *oldref
= notnull(*argv
++, "oldref");
89 const char *newref
= notnull(*argv
++, "newref");
90 const char *logmsg
= *argv
++;
92 return refs_rename_ref(refs
, oldref
, newref
, logmsg
);
95 static int each_ref(const char *refname
, const struct object_id
*oid
,
96 int flags
, void *cb_data
)
98 printf("%s %s 0x%x\n", oid_to_hex(oid
), refname
, flags
);
102 static int cmd_for_each_ref(struct ref_store
*refs
, const char **argv
)
104 const char *prefix
= notnull(*argv
++, "prefix");
106 return refs_for_each_ref_in(refs
, prefix
, each_ref
, NULL
);
109 static int cmd_resolve_ref(struct ref_store
*refs
, const char **argv
)
111 unsigned char sha1
[20];
112 const char *refname
= notnull(*argv
++, "refname");
113 int resolve_flags
= arg_flags(*argv
++, "resolve-flags");
117 ref
= refs_resolve_ref_unsafe(refs
, refname
, resolve_flags
,
119 printf("%s %s 0x%x\n", sha1_to_hex(sha1
), ref
, flags
);
123 static int cmd_verify_ref(struct ref_store
*refs
, const char **argv
)
125 const char *refname
= notnull(*argv
++, "refname");
126 struct strbuf err
= STRBUF_INIT
;
129 ret
= refs_verify_refname_available(refs
, refname
, NULL
, NULL
, &err
);
135 static int cmd_for_each_reflog(struct ref_store
*refs
, const char **argv
)
137 return refs_for_each_reflog(refs
, each_ref
, NULL
);
140 static int each_reflog(struct object_id
*old_oid
, struct object_id
*new_oid
,
141 const char *committer
, unsigned long timestamp
,
142 int tz
, const char *msg
, void *cb_data
)
144 printf("%s %s %s %lu %d %s\n",
145 oid_to_hex(old_oid
), oid_to_hex(new_oid
),
146 committer
, timestamp
, tz
, msg
);
150 static int cmd_for_each_reflog_ent(struct ref_store
*refs
, const char **argv
)
152 const char *refname
= notnull(*argv
++, "refname");
154 return refs_for_each_reflog_ent(refs
, refname
, each_reflog
, refs
);
157 static int cmd_for_each_reflog_ent_reverse(struct ref_store
*refs
, const char **argv
)
159 const char *refname
= notnull(*argv
++, "refname");
161 return refs_for_each_reflog_ent_reverse(refs
, refname
, each_reflog
, refs
);
164 static int cmd_reflog_exists(struct ref_store
*refs
, const char **argv
)
166 const char *refname
= notnull(*argv
++, "refname");
168 return !refs_reflog_exists(refs
, refname
);
171 static int cmd_create_reflog(struct ref_store
*refs
, const char **argv
)
173 const char *refname
= notnull(*argv
++, "refname");
174 int force_create
= arg_flags(*argv
++, "force-create");
175 struct strbuf err
= STRBUF_INIT
;
178 ret
= refs_create_reflog(refs
, refname
, force_create
, &err
);
184 static int cmd_delete_reflog(struct ref_store
*refs
, const char **argv
)
186 const char *refname
= notnull(*argv
++, "refname");
188 return refs_delete_reflog(refs
, refname
);
191 static int cmd_reflog_expire(struct ref_store
*refs
, const char **argv
)
193 die("not supported yet");
196 static int cmd_delete_ref(struct ref_store
*refs
, const char **argv
)
198 const char *msg
= notnull(*argv
++, "msg");
199 const char *refname
= notnull(*argv
++, "refname");
200 const char *sha1_buf
= notnull(*argv
++, "old-sha1");
201 unsigned int flags
= arg_flags(*argv
++, "flags");
202 unsigned char old_sha1
[20];
204 if (get_sha1_hex(sha1_buf
, old_sha1
))
207 return refs_delete_ref(refs
, msg
, refname
, old_sha1
, flags
);
210 static int cmd_update_ref(struct ref_store
*refs
, const char **argv
)
212 const char *msg
= notnull(*argv
++, "msg");
213 const char *refname
= notnull(*argv
++, "refname");
214 const char *new_sha1_buf
= notnull(*argv
++, "old-sha1");
215 const char *old_sha1_buf
= notnull(*argv
++, "old-sha1");
216 unsigned int flags
= arg_flags(*argv
++, "flags");
217 unsigned char old_sha1
[20];
218 unsigned char new_sha1
[20];
220 if (get_sha1_hex(old_sha1_buf
, old_sha1
) ||
221 get_sha1_hex(new_sha1_buf
, new_sha1
))
224 return refs_update_ref(refs
, msg
, refname
,
226 flags
, UPDATE_REFS_DIE_ON_ERR
);
231 int (*func
)(struct ref_store
*refs
, const char **argv
);
234 static struct command commands
[] = {
235 { "pack-refs", cmd_pack_refs
},
236 { "peel-ref", cmd_peel_ref
},
237 { "create-symref", cmd_create_symref
},
238 { "delete-refs", cmd_delete_refs
},
239 { "rename-ref", cmd_rename_ref
},
240 { "for-each-ref", cmd_for_each_ref
},
241 { "resolve-ref", cmd_resolve_ref
},
242 { "verify-ref", cmd_verify_ref
},
243 { "for-each-reflog", cmd_for_each_reflog
},
244 { "for-each-reflog-ent", cmd_for_each_reflog_ent
},
245 { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse
},
246 { "reflog-exists", cmd_reflog_exists
},
247 { "create-reflog", cmd_create_reflog
},
248 { "delete-reflog", cmd_delete_reflog
},
249 { "reflog-expire", cmd_reflog_expire
},
251 * backend transaction functions can't be tested separately
253 { "delete-ref", cmd_delete_ref
},
254 { "update-ref", cmd_update_ref
},
258 int cmd_main(int argc
, const char **argv
)
260 struct ref_store
*refs
;
264 setup_git_directory();
266 argv
= get_store(argv
+ 1, &refs
);
270 die("ref function required");
271 for (cmd
= commands
; cmd
->name
; cmd
++) {
272 if (!strcmp(func
, cmd
->name
))
273 return cmd
->func(refs
, argv
);
275 die("unknown function %s", func
);