]>
Commit | Line | Data |
---|---|---|
66bf85a4 LT |
1 | #include "cache.h" |
2 | #include "refs.h" | |
854b4629 | 3 | #include "builtin.h" |
89942be6 | 4 | #include "parse-options.h" |
c750ba95 BK |
5 | #include "quote.h" |
6 | #include "argv-array.h" | |
66bf85a4 | 7 | |
89942be6 | 8 | static const char * const git_update_ref_usage[] = { |
82269505 NTND |
9 | N_("git update-ref [options] -d <refname> [<oldval>]"), |
10 | N_("git update-ref [options] <refname> <newval> [<oldval>]"), | |
c750ba95 | 11 | N_("git update-ref [options] --stdin [-z]"), |
89942be6 PH |
12 | NULL |
13 | }; | |
152da3df | 14 | |
c750ba95 BK |
15 | static int updates_alloc; |
16 | static int updates_count; | |
17 | static const struct ref_update **updates; | |
18 | ||
19 | static char line_termination = '\n'; | |
20 | static int update_flags; | |
21 | ||
22 | static struct ref_update *update_alloc(void) | |
23 | { | |
24 | struct ref_update *update; | |
25 | ||
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; | |
30 | ||
31 | /* Store and reset accumulated options */ | |
32 | update->flags = update_flags; | |
33 | update_flags = 0; | |
34 | ||
35 | return update; | |
36 | } | |
37 | ||
38 | static void update_store_ref_name(struct ref_update *update, | |
39 | const char *ref_name) | |
40 | { | |
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); | |
44 | } | |
45 | ||
46 | static void update_store_new_sha1(struct ref_update *update, | |
47 | const char *newvalue) | |
48 | { | |
49 | if (*newvalue && get_sha1(newvalue, update->new_sha1)) | |
50 | die("invalid new value for ref %s: %s", | |
51 | update->ref_name, newvalue); | |
52 | } | |
53 | ||
54 | static void update_store_old_sha1(struct ref_update *update, | |
55 | const char *oldvalue) | |
56 | { | |
57 | if (*oldvalue && get_sha1(oldvalue, update->old_sha1)) | |
58 | die("invalid old value for ref %s: %s", | |
59 | update->ref_name, oldvalue); | |
60 | ||
61 | /* We have an old value if non-empty, or if empty without -z */ | |
62 | update->have_old = *oldvalue || line_termination; | |
63 | } | |
64 | ||
697a4151 MH |
65 | /* |
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. | |
70 | */ | |
c750ba95 BK |
71 | static const char *parse_arg(const char *next, struct strbuf *arg) |
72 | { | |
697a4151 MH |
73 | if (*next == '"') { |
74 | const char *orig = next; | |
75 | ||
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); | |
80 | } else { | |
c750ba95 BK |
81 | while (*next && !isspace(*next)) |
82 | strbuf_addch(arg, *next++); | |
697a4151 | 83 | } |
c750ba95 | 84 | |
c750ba95 BK |
85 | return next; |
86 | } | |
87 | ||
88 | static const char *parse_first_arg(const char *next, struct strbuf *arg) | |
89 | { | |
90 | /* Parse argument immediately after "command SP" */ | |
91 | strbuf_reset(arg); | |
92 | if (line_termination) { | |
93 | /* Without -z, use the next argument */ | |
94 | next = parse_arg(next, arg); | |
95 | } else { | |
96 | /* With -z, use rest of first NUL-terminated line */ | |
97 | strbuf_addstr(arg, next); | |
98 | next = next + arg->len; | |
99 | } | |
100 | return next; | |
101 | } | |
102 | ||
103 | static const char *parse_next_arg(const char *next, struct strbuf *arg) | |
104 | { | |
105 | /* Parse next SP-terminated or NUL-terminated argument, if any */ | |
106 | strbuf_reset(arg); | |
107 | if (line_termination) { | |
108 | /* Without -z, consume SP and use next argument */ | |
109 | if (!*next) | |
110 | return NULL; | |
111 | if (*next != ' ') | |
112 | die("expected SP but got: %s", next); | |
113 | next = parse_arg(next + 1, arg); | |
114 | } else { | |
115 | /* With -z, read the next NUL-terminated line */ | |
116 | if (*next) | |
117 | die("expected NUL but got: %s", next); | |
118 | if (strbuf_getline(arg, stdin, '\0') == EOF) | |
119 | return NULL; | |
120 | next = arg->buf + arg->len; | |
121 | } | |
122 | return next; | |
123 | } | |
124 | ||
125 | static void parse_cmd_update(const char *next) | |
126 | { | |
127 | struct strbuf ref = STRBUF_INIT; | |
128 | struct strbuf newvalue = STRBUF_INIT; | |
129 | struct strbuf oldvalue = STRBUF_INIT; | |
130 | struct ref_update *update; | |
131 | ||
132 | update = update_alloc(); | |
133 | ||
134 | if ((next = parse_first_arg(next, &ref)) != NULL && ref.buf[0]) | |
135 | update_store_ref_name(update, ref.buf); | |
136 | else | |
137 | die("update line missing <ref>"); | |
138 | ||
139 | if ((next = parse_next_arg(next, &newvalue)) != NULL) | |
140 | update_store_new_sha1(update, newvalue.buf); | |
141 | else | |
142 | die("update %s missing <newvalue>", ref.buf); | |
143 | ||
144 | if ((next = parse_next_arg(next, &oldvalue)) != NULL) | |
145 | update_store_old_sha1(update, oldvalue.buf); | |
146 | else if(!line_termination) | |
147 | die("update %s missing [<oldvalue>] NUL", ref.buf); | |
148 | ||
149 | if (next && *next) | |
150 | die("update %s has extra input: %s", ref.buf, next); | |
151 | } | |
152 | ||
153 | static void parse_cmd_create(const char *next) | |
154 | { | |
155 | struct strbuf ref = STRBUF_INIT; | |
156 | struct strbuf newvalue = STRBUF_INIT; | |
157 | struct ref_update *update; | |
158 | ||
159 | update = update_alloc(); | |
160 | ||
161 | if ((next = parse_first_arg(next, &ref)) != NULL && ref.buf[0]) | |
162 | update_store_ref_name(update, ref.buf); | |
163 | else | |
164 | die("create line missing <ref>"); | |
165 | ||
166 | if ((next = parse_next_arg(next, &newvalue)) != NULL) | |
167 | update_store_new_sha1(update, newvalue.buf); | |
168 | else | |
169 | die("create %s missing <newvalue>", ref.buf); | |
170 | if (is_null_sha1(update->new_sha1)) | |
171 | die("create %s given zero new value", ref.buf); | |
172 | ||
173 | if (next && *next) | |
174 | die("create %s has extra input: %s", ref.buf, next); | |
175 | } | |
176 | ||
177 | static void parse_cmd_delete(const char *next) | |
178 | { | |
179 | struct strbuf ref = STRBUF_INIT; | |
180 | struct strbuf oldvalue = STRBUF_INIT; | |
181 | struct ref_update *update; | |
182 | ||
183 | update = update_alloc(); | |
184 | ||
185 | if ((next = parse_first_arg(next, &ref)) != NULL && ref.buf[0]) | |
186 | update_store_ref_name(update, ref.buf); | |
187 | else | |
188 | die("delete line missing <ref>"); | |
189 | ||
190 | if ((next = parse_next_arg(next, &oldvalue)) != NULL) | |
191 | update_store_old_sha1(update, oldvalue.buf); | |
192 | else if(!line_termination) | |
193 | die("delete %s missing [<oldvalue>] NUL", ref.buf); | |
194 | if (update->have_old && is_null_sha1(update->old_sha1)) | |
195 | die("delete %s given zero old value", ref.buf); | |
196 | ||
197 | if (next && *next) | |
198 | die("delete %s has extra input: %s", ref.buf, next); | |
199 | } | |
200 | ||
201 | static void parse_cmd_verify(const char *next) | |
202 | { | |
203 | struct strbuf ref = STRBUF_INIT; | |
204 | struct strbuf value = STRBUF_INIT; | |
205 | struct ref_update *update; | |
206 | ||
207 | update = update_alloc(); | |
208 | ||
209 | if ((next = parse_first_arg(next, &ref)) != NULL && ref.buf[0]) | |
210 | update_store_ref_name(update, ref.buf); | |
211 | else | |
212 | die("verify line missing <ref>"); | |
213 | ||
214 | if ((next = parse_next_arg(next, &value)) != NULL) { | |
215 | update_store_old_sha1(update, value.buf); | |
216 | update_store_new_sha1(update, value.buf); | |
217 | } else if(!line_termination) | |
218 | die("verify %s missing [<oldvalue>] NUL", ref.buf); | |
219 | ||
220 | if (next && *next) | |
221 | die("verify %s has extra input: %s", ref.buf, next); | |
222 | } | |
223 | ||
224 | static void parse_cmd_option(const char *next) | |
225 | { | |
226 | if (!strcmp(next, "no-deref")) | |
227 | update_flags |= REF_NODEREF; | |
228 | else | |
229 | die("option unknown: %s", next); | |
230 | } | |
231 | ||
232 | static void update_refs_stdin(void) | |
233 | { | |
234 | struct strbuf cmd = STRBUF_INIT; | |
235 | ||
236 | /* Read each line dispatch its command */ | |
237 | while (strbuf_getline(&cmd, stdin, line_termination) != EOF) | |
238 | if (!cmd.buf[0]) | |
239 | die("empty command in input"); | |
240 | else if (isspace(*cmd.buf)) | |
241 | die("whitespace before command: %s", cmd.buf); | |
59556548 | 242 | else if (starts_with(cmd.buf, "update ")) |
c750ba95 | 243 | parse_cmd_update(cmd.buf + 7); |
59556548 | 244 | else if (starts_with(cmd.buf, "create ")) |
c750ba95 | 245 | parse_cmd_create(cmd.buf + 7); |
59556548 | 246 | else if (starts_with(cmd.buf, "delete ")) |
c750ba95 | 247 | parse_cmd_delete(cmd.buf + 7); |
59556548 | 248 | else if (starts_with(cmd.buf, "verify ")) |
c750ba95 | 249 | parse_cmd_verify(cmd.buf + 7); |
59556548 | 250 | else if (starts_with(cmd.buf, "option ")) |
c750ba95 BK |
251 | parse_cmd_option(cmd.buf + 7); |
252 | else | |
253 | die("unknown command: %s", cmd.buf); | |
254 | ||
255 | strbuf_release(&cmd); | |
256 | } | |
257 | ||
a633fca0 | 258 | int cmd_update_ref(int argc, const char **argv, const char *prefix) |
66bf85a4 | 259 | { |
f877fd82 | 260 | const char *refname, *oldval, *msg = NULL; |
5b16b090 | 261 | unsigned char sha1[20], oldsha1[20]; |
c750ba95 | 262 | int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0; |
89942be6 | 263 | struct option options[] = { |
82269505 | 264 | OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")), |
d5d09d47 SB |
265 | OPT_BOOL('d', NULL, &delete, N_("delete the reference")), |
266 | OPT_BOOL( 0 , "no-deref", &no_deref, | |
82269505 | 267 | N_("update <refname> not the one it points to")), |
9a86b899 JH |
268 | OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")), |
269 | OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")), | |
89942be6 PH |
270 | OPT_END(), |
271 | }; | |
66bf85a4 | 272 | |
ef90d6d4 | 273 | git_config(git_default_config, NULL); |
37782920 SB |
274 | argc = parse_options(argc, argv, prefix, options, git_update_ref_usage, |
275 | 0); | |
89942be6 PH |
276 | if (msg && !*msg) |
277 | die("Refusing to perform update with empty message."); | |
5b16b090 | 278 | |
c750ba95 BK |
279 | if (read_stdin) { |
280 | if (delete || no_deref || argc > 0) | |
281 | usage_with_options(git_update_ref_usage, options); | |
282 | if (end_null) | |
283 | line_termination = '\0'; | |
284 | update_refs_stdin(); | |
285 | return update_refs(msg, updates, updates_count, DIE_ON_ERR); | |
286 | } | |
287 | ||
288 | if (end_null) | |
289 | usage_with_options(git_update_ref_usage, options); | |
290 | ||
ac5409e4 | 291 | if (delete) { |
3fe8dce6 | 292 | if (argc < 1 || argc > 2) |
973a70ea KW |
293 | usage_with_options(git_update_ref_usage, options); |
294 | refname = argv[0]; | |
295 | oldval = argv[1]; | |
296 | } else { | |
297 | const char *value; | |
298 | if (argc < 2 || argc > 3) | |
89942be6 | 299 | usage_with_options(git_update_ref_usage, options); |
973a70ea KW |
300 | refname = argv[0]; |
301 | value = argv[1]; | |
302 | oldval = argv[2]; | |
303 | if (get_sha1(value, sha1)) | |
304 | die("%s: not a valid SHA1", value); | |
ac5409e4 JH |
305 | } |
306 | ||
973a70ea | 307 | hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */ |
ac5409e4 | 308 | if (oldval && *oldval && get_sha1(oldval, oldsha1)) |
66bf85a4 LT |
309 | die("%s: not a valid old SHA1", oldval); |
310 | ||
569740bd MV |
311 | if (no_deref) |
312 | flags = REF_NODEREF; | |
973a70ea | 313 | if (delete) |
569740bd | 314 | return delete_ref(refname, oldval ? oldsha1 : NULL, flags); |
973a70ea KW |
315 | else |
316 | return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL, | |
569740bd | 317 | flags, DIE_ON_ERR); |
66bf85a4 | 318 | } |