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 const 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
;
65 static const char *parse_arg(const char *next
, struct strbuf
*arg
)
67 /* Parse SP-terminated, possibly C-quoted argument */
69 while (*next
&& !isspace(*next
))
70 strbuf_addch(arg
, *next
++);
71 else if (unquote_c_style(arg
, next
, &next
))
72 die("badly quoted argument: %s", next
);
74 /* Return position after the argument */
78 static const char *parse_first_arg(const char *next
, struct strbuf
*arg
)
80 /* Parse argument immediately after "command SP" */
82 if (line_termination
) {
83 /* Without -z, use the next argument */
84 next
= parse_arg(next
, arg
);
86 /* With -z, use rest of first NUL-terminated line */
87 strbuf_addstr(arg
, next
);
88 next
= next
+ arg
->len
;
93 static const char *parse_next_arg(const char *next
, struct strbuf
*arg
)
95 /* Parse next SP-terminated or NUL-terminated argument, if any */
97 if (line_termination
) {
98 /* Without -z, consume SP and use next argument */
102 die("expected SP but got: %s", next
);
103 next
= parse_arg(next
+ 1, arg
);
105 /* With -z, read the next NUL-terminated line */
107 die("expected NUL but got: %s", next
);
108 if (strbuf_getline(arg
, stdin
, '\0') == EOF
)
110 next
= arg
->buf
+ arg
->len
;
115 static void parse_cmd_update(const char *next
)
117 struct strbuf ref
= STRBUF_INIT
;
118 struct strbuf newvalue
= STRBUF_INIT
;
119 struct strbuf oldvalue
= STRBUF_INIT
;
120 struct ref_update
*update
;
122 update
= update_alloc();
124 if ((next
= parse_first_arg(next
, &ref
)) != NULL
&& ref
.buf
[0])
125 update_store_ref_name(update
, ref
.buf
);
127 die("update line missing <ref>");
129 if ((next
= parse_next_arg(next
, &newvalue
)) != NULL
)
130 update_store_new_sha1(update
, newvalue
.buf
);
132 die("update %s missing <newvalue>", ref
.buf
);
134 if ((next
= parse_next_arg(next
, &oldvalue
)) != NULL
)
135 update_store_old_sha1(update
, oldvalue
.buf
);
136 else if(!line_termination
)
137 die("update %s missing [<oldvalue>] NUL", ref
.buf
);
140 die("update %s has extra input: %s", ref
.buf
, next
);
143 static void parse_cmd_create(const char *next
)
145 struct strbuf ref
= STRBUF_INIT
;
146 struct strbuf newvalue
= STRBUF_INIT
;
147 struct ref_update
*update
;
149 update
= update_alloc();
150 update
->have_old
= 1;
152 if ((next
= parse_first_arg(next
, &ref
)) != NULL
&& ref
.buf
[0])
153 update_store_ref_name(update
, ref
.buf
);
155 die("create line missing <ref>");
157 if ((next
= parse_next_arg(next
, &newvalue
)) != NULL
)
158 update_store_new_sha1(update
, newvalue
.buf
);
160 die("create %s missing <newvalue>", ref
.buf
);
161 if (is_null_sha1(update
->new_sha1
))
162 die("create %s given zero new value", ref
.buf
);
165 die("create %s has extra input: %s", ref
.buf
, next
);
168 static void parse_cmd_delete(const char *next
)
170 struct strbuf ref
= STRBUF_INIT
;
171 struct strbuf oldvalue
= STRBUF_INIT
;
172 struct ref_update
*update
;
174 update
= update_alloc();
176 if ((next
= parse_first_arg(next
, &ref
)) != NULL
&& ref
.buf
[0])
177 update_store_ref_name(update
, ref
.buf
);
179 die("delete line missing <ref>");
181 if ((next
= parse_next_arg(next
, &oldvalue
)) != NULL
)
182 update_store_old_sha1(update
, oldvalue
.buf
);
183 else if(!line_termination
)
184 die("delete %s missing [<oldvalue>] NUL", ref
.buf
);
185 if (update
->have_old
&& is_null_sha1(update
->old_sha1
))
186 die("delete %s given zero old value", ref
.buf
);
189 die("delete %s has extra input: %s", ref
.buf
, next
);
192 static void parse_cmd_verify(const char *next
)
194 struct strbuf ref
= STRBUF_INIT
;
195 struct strbuf value
= STRBUF_INIT
;
196 struct ref_update
*update
;
198 update
= update_alloc();
200 if ((next
= parse_first_arg(next
, &ref
)) != NULL
&& ref
.buf
[0])
201 update_store_ref_name(update
, ref
.buf
);
203 die("verify line missing <ref>");
205 if ((next
= parse_next_arg(next
, &value
)) != NULL
) {
206 update_store_old_sha1(update
, value
.buf
);
207 update_store_new_sha1(update
, value
.buf
);
208 } else if(!line_termination
)
209 die("verify %s missing [<oldvalue>] NUL", ref
.buf
);
212 die("verify %s has extra input: %s", ref
.buf
, next
);
215 static void parse_cmd_option(const char *next
)
217 if (!strcmp(next
, "no-deref"))
218 update_flags
|= REF_NODEREF
;
220 die("option unknown: %s", next
);
223 static void update_refs_stdin(void)
225 struct strbuf cmd
= STRBUF_INIT
;
227 /* Read each line dispatch its command */
228 while (strbuf_getline(&cmd
, stdin
, line_termination
) != EOF
)
230 die("empty command in input");
231 else if (isspace(*cmd
.buf
))
232 die("whitespace before command: %s", cmd
.buf
);
233 else if (starts_with(cmd
.buf
, "update "))
234 parse_cmd_update(cmd
.buf
+ 7);
235 else if (starts_with(cmd
.buf
, "create "))
236 parse_cmd_create(cmd
.buf
+ 7);
237 else if (starts_with(cmd
.buf
, "delete "))
238 parse_cmd_delete(cmd
.buf
+ 7);
239 else if (starts_with(cmd
.buf
, "verify "))
240 parse_cmd_verify(cmd
.buf
+ 7);
241 else if (starts_with(cmd
.buf
, "option "))
242 parse_cmd_option(cmd
.buf
+ 7);
244 die("unknown command: %s", cmd
.buf
);
246 strbuf_release(&cmd
);
249 int cmd_update_ref(int argc
, const char **argv
, const char *prefix
)
251 const char *refname
, *oldval
, *msg
= NULL
;
252 unsigned char sha1
[20], oldsha1
[20];
253 int delete = 0, no_deref
= 0, read_stdin
= 0, end_null
= 0, flags
= 0;
254 struct option options
[] = {
255 OPT_STRING( 'm', NULL
, &msg
, N_("reason"), N_("reason of the update")),
256 OPT_BOOL('d', NULL
, &delete, N_("delete the reference")),
257 OPT_BOOL( 0 , "no-deref", &no_deref
,
258 N_("update <refname> not the one it points to")),
259 OPT_BOOL('z', NULL
, &end_null
, N_("stdin has NUL-terminated arguments")),
260 OPT_BOOL( 0 , "stdin", &read_stdin
, N_("read updates from stdin")),
264 git_config(git_default_config
, NULL
);
265 argc
= parse_options(argc
, argv
, prefix
, options
, git_update_ref_usage
,
268 die("Refusing to perform update with empty message.");
271 if (delete || no_deref
|| argc
> 0)
272 usage_with_options(git_update_ref_usage
, options
);
274 line_termination
= '\0';
276 return update_refs(msg
, updates
, updates_count
, DIE_ON_ERR
);
280 usage_with_options(git_update_ref_usage
, options
);
283 if (argc
< 1 || argc
> 2)
284 usage_with_options(git_update_ref_usage
, options
);
289 if (argc
< 2 || argc
> 3)
290 usage_with_options(git_update_ref_usage
, options
);
294 if (get_sha1(value
, sha1
))
295 die("%s: not a valid SHA1", value
);
298 hashclr(oldsha1
); /* all-zero hash in case oldval is the empty string */
299 if (oldval
&& *oldval
&& get_sha1(oldval
, oldsha1
))
300 die("%s: not a valid old SHA1", oldval
);
305 return delete_ref(refname
, oldval
? oldsha1
: NULL
, flags
);
307 return update_ref(msg
, refname
, sha1
, oldval
? oldsha1
: NULL
,