4 #include "parse-options.h"
6 #include "argv-array.h"
8 static const char * const git_update_ref_usage
[] = {
9 N_("git update-ref [options] -d <refname> [<oldval>]"),
10 N_("git update-ref [options] <refname> <newval> [<oldval>]"),
11 N_("git update-ref [options] --stdin [-z]"),
15 static int updates_alloc
;
16 static int updates_count
;
17 static struct ref_update
**updates
;
19 static char line_termination
= '\n';
20 static int update_flags
;
22 static struct ref_update
*update_alloc(void)
24 struct ref_update
*update
;
26 /* Allocate and zero-init a struct ref_update */
27 update
= xcalloc(1, sizeof(*update
));
28 ALLOC_GROW(updates
, updates_count
+ 1, updates_alloc
);
29 updates
[updates_count
++] = update
;
31 /* Store and reset accumulated options */
32 update
->flags
= update_flags
;
38 static void update_store_ref_name(struct ref_update
*update
,
41 if (check_refname_format(ref_name
, REFNAME_ALLOW_ONELEVEL
))
42 die("invalid ref format: %s", ref_name
);
43 update
->ref_name
= xstrdup(ref_name
);
46 static void update_store_new_sha1(struct ref_update
*update
,
49 if (*newvalue
&& get_sha1(newvalue
, update
->new_sha1
))
50 die("invalid new value for ref %s: %s",
51 update
->ref_name
, newvalue
);
54 static void update_store_old_sha1(struct ref_update
*update
,
57 if (*oldvalue
&& get_sha1(oldvalue
, update
->old_sha1
))
58 die("invalid old value for ref %s: %s",
59 update
->ref_name
, oldvalue
);
61 /* We have an old value if non-empty, or if empty without -z */
62 update
->have_old
= *oldvalue
|| line_termination
;
66 * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
67 * and append the result to arg. Return a pointer to the terminator.
68 * Die if there is an error in how the argument is C-quoted. This
69 * function is only used if not -z.
71 static const char *parse_arg(const char *next
, struct strbuf
*arg
)
74 const char *orig
= next
;
76 if (unquote_c_style(arg
, next
, &next
))
77 die("badly quoted argument: %s", orig
);
78 if (*next
&& !isspace(*next
))
79 die("unexpected character after quoted argument: %s", orig
);
81 while (*next
&& !isspace(*next
))
82 strbuf_addch(arg
, *next
++);
89 * Parse the argument immediately after "command SP". If not -z, then
90 * handle C-quoting. Write the argument to arg. Set *next to point
91 * at the character that terminates the argument. Die if C-quoting is
94 static void parse_first_arg(struct strbuf
*input
, const char **next
,
98 if (line_termination
) {
99 /* Without -z, use the next argument */
100 *next
= parse_arg(*next
, arg
);
102 /* With -z, use everything up to the next NUL */
103 strbuf_addstr(arg
, *next
);
109 * Parse a SP/NUL separator followed by the next SP- or NUL-terminated
110 * argument, if any. If there is an argument, write it to arg, set
111 * *next to point at the character terminating the argument, and
112 * return 0. If there is no argument at all (not even the empty
113 * string), return a non-zero result and leave *next unchanged.
115 static int parse_next_arg(struct strbuf
*input
, const char **next
,
119 if (line_termination
) {
120 /* Without -z, consume SP and use next argument */
121 if (!**next
|| **next
== line_termination
)
124 die("expected SP but got: %s", *next
);
126 *next
= parse_arg(*next
, arg
);
128 /* With -z, read the next NUL-terminated line */
130 die("expected NUL but got: %s", *next
);
132 if (*next
== input
->buf
+ input
->len
)
134 strbuf_addstr(arg
, *next
);
142 * The following five parse_cmd_*() functions parse the corresponding
143 * command. In each case, next points at the character following the
144 * command name and the following space. They each return a pointer
145 * to the character terminating the command, and die with an
146 * explanatory message if there are any parsing problems. All of
147 * these functions handle either text or binary format input,
148 * depending on how line_termination is set.
151 static const char *parse_cmd_update(struct strbuf
*input
, const char *next
)
153 struct strbuf ref
= STRBUF_INIT
;
154 struct strbuf newvalue
= STRBUF_INIT
;
155 struct strbuf oldvalue
= STRBUF_INIT
;
156 struct ref_update
*update
;
158 update
= update_alloc();
160 parse_first_arg(input
, &next
, &ref
);
162 update_store_ref_name(update
, ref
.buf
);
164 die("update line missing <ref>");
166 if (!parse_next_arg(input
, &next
, &newvalue
))
167 update_store_new_sha1(update
, newvalue
.buf
);
169 die("update %s missing <newvalue>", ref
.buf
);
171 if (!parse_next_arg(input
, &next
, &oldvalue
)) {
172 update_store_old_sha1(update
, oldvalue
.buf
);
173 if (*next
!= line_termination
)
174 die("update %s has extra input: %s", ref
.buf
, next
);
175 } else if (!line_termination
)
176 die("update %s missing [<oldvalue>] NUL", ref
.buf
);
181 static const char *parse_cmd_create(struct strbuf
*input
, const char *next
)
183 struct strbuf ref
= STRBUF_INIT
;
184 struct strbuf newvalue
= STRBUF_INIT
;
185 struct ref_update
*update
;
187 update
= update_alloc();
189 parse_first_arg(input
, &next
, &ref
);
191 update_store_ref_name(update
, ref
.buf
);
193 die("create line missing <ref>");
195 if (!parse_next_arg(input
, &next
, &newvalue
))
196 update_store_new_sha1(update
, newvalue
.buf
);
198 die("create %s missing <newvalue>", ref
.buf
);
200 if (is_null_sha1(update
->new_sha1
))
201 die("create %s given zero new value", ref
.buf
);
203 if (*next
!= line_termination
)
204 die("create %s has extra input: %s", ref
.buf
, next
);
209 static const char *parse_cmd_delete(struct strbuf
*input
, const char *next
)
211 struct strbuf ref
= STRBUF_INIT
;
212 struct strbuf oldvalue
= STRBUF_INIT
;
213 struct ref_update
*update
;
215 update
= update_alloc();
217 parse_first_arg(input
, &next
, &ref
);
219 update_store_ref_name(update
, ref
.buf
);
221 die("delete line missing <ref>");
223 if (!parse_next_arg(input
, &next
, &oldvalue
)) {
224 update_store_old_sha1(update
, oldvalue
.buf
);
225 if (update
->have_old
&& is_null_sha1(update
->old_sha1
))
226 die("delete %s given zero old value", ref
.buf
);
227 } else if (!line_termination
)
228 die("delete %s missing [<oldvalue>] NUL", ref
.buf
);
230 if (*next
!= line_termination
)
231 die("delete %s has extra input: %s", ref
.buf
, next
);
236 static const char *parse_cmd_verify(struct strbuf
*input
, const char *next
)
238 struct strbuf ref
= STRBUF_INIT
;
239 struct strbuf value
= STRBUF_INIT
;
240 struct ref_update
*update
;
242 update
= update_alloc();
244 parse_first_arg(input
, &next
, &ref
);
246 update_store_ref_name(update
, ref
.buf
);
248 die("verify line missing <ref>");
250 if (!parse_next_arg(input
, &next
, &value
)) {
251 update_store_old_sha1(update
, value
.buf
);
252 hashcpy(update
->new_sha1
, update
->old_sha1
);
253 } else if (!line_termination
)
254 die("verify %s missing [<oldvalue>] NUL", ref
.buf
);
256 if (*next
!= line_termination
)
257 die("verify %s has extra input: %s", ref
.buf
, next
);
262 static const char *parse_cmd_option(struct strbuf
*input
, const char *next
)
264 if (!strncmp(next
, "no-deref", 8) && next
[8] == line_termination
)
265 update_flags
|= REF_NODEREF
;
267 die("option unknown: %s", next
);
271 static void update_refs_stdin(void)
273 struct strbuf input
= STRBUF_INIT
;
276 if (strbuf_read(&input
, 0, 1000) < 0)
277 die_errno("could not read from stdin");
279 /* Read each line dispatch its command */
280 while (next
< input
.buf
+ input
.len
) {
281 if (*next
== line_termination
)
282 die("empty command in input");
283 else if (isspace(*next
))
284 die("whitespace before command: %s", next
);
285 else if (starts_with(next
, "update "))
286 next
= parse_cmd_update(&input
, next
+ 7);
287 else if (starts_with(next
, "create "))
288 next
= parse_cmd_create(&input
, next
+ 7);
289 else if (starts_with(next
, "delete "))
290 next
= parse_cmd_delete(&input
, next
+ 7);
291 else if (starts_with(next
, "verify "))
292 next
= parse_cmd_verify(&input
, next
+ 7);
293 else if (starts_with(next
, "option "))
294 next
= parse_cmd_option(&input
, next
+ 7);
296 die("unknown command: %s", next
);
301 strbuf_release(&input
);
304 int cmd_update_ref(int argc
, const char **argv
, const char *prefix
)
306 const char *refname
, *oldval
, *msg
= NULL
;
307 unsigned char sha1
[20], oldsha1
[20];
308 int delete = 0, no_deref
= 0, read_stdin
= 0, end_null
= 0, flags
= 0;
309 struct option options
[] = {
310 OPT_STRING( 'm', NULL
, &msg
, N_("reason"), N_("reason of the update")),
311 OPT_BOOL('d', NULL
, &delete, N_("delete the reference")),
312 OPT_BOOL( 0 , "no-deref", &no_deref
,
313 N_("update <refname> not the one it points to")),
314 OPT_BOOL('z', NULL
, &end_null
, N_("stdin has NUL-terminated arguments")),
315 OPT_BOOL( 0 , "stdin", &read_stdin
, N_("read updates from stdin")),
319 git_config(git_default_config
, NULL
);
320 argc
= parse_options(argc
, argv
, prefix
, options
, git_update_ref_usage
,
323 die("Refusing to perform update with empty message.");
326 if (delete || no_deref
|| argc
> 0)
327 usage_with_options(git_update_ref_usage
, options
);
329 line_termination
= '\0';
331 return update_refs(msg
, updates
, updates_count
,
332 UPDATE_REFS_DIE_ON_ERR
);
336 usage_with_options(git_update_ref_usage
, options
);
339 if (argc
< 1 || argc
> 2)
340 usage_with_options(git_update_ref_usage
, options
);
345 if (argc
< 2 || argc
> 3)
346 usage_with_options(git_update_ref_usage
, options
);
350 if (get_sha1(value
, sha1
))
351 die("%s: not a valid SHA1", value
);
354 hashclr(oldsha1
); /* all-zero hash in case oldval is the empty string */
355 if (oldval
&& *oldval
&& get_sha1(oldval
, oldsha1
))
356 die("%s: not a valid old SHA1", oldval
);
361 return delete_ref(refname
, oldval
? oldsha1
: NULL
, flags
);
363 return update_ref(msg
, refname
, sha1
, oldval
? oldsha1
: NULL
,
364 flags
, UPDATE_REFS_DIE_ON_ERR
);