]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/update-ref.c
Merge branch 'ob/t9001-indent-fix'
[thirdparty/git.git] / builtin / update-ref.c
CommitLineData
bc5c5ec0 1#include "builtin.h"
b2141fc1 2#include "config.h"
f394e093 3#include "gettext.h"
d1cbe1e6 4#include "hash.h"
66bf85a4 5#include "refs.h"
dabab1d6 6#include "object-name.h"
89942be6 7#include "parse-options.h"
c750ba95 8#include "quote.h"
d1cbe1e6 9#include "repository.h"
dbbcd44f 10#include "strvec.h"
66bf85a4 11
89942be6 12static const char * const git_update_ref_usage[] = {
9c9b4f2f
AH
13 N_("git update-ref [<options>] -d <refname> [<old-val>]"),
14 N_("git update-ref [<options>] <refname> <new-val> [<old-val>]"),
15 N_("git update-ref [<options>] --stdin [-z]"),
89942be6
PH
16 NULL
17};
152da3df 18
c750ba95 19static char line_termination = '\n';
e4c34855 20static unsigned int update_flags;
d345e9fb 21static unsigned int default_flags;
144c76fa 22static unsigned create_reflog_flag;
db7516ab 23static const char *msg;
c750ba95 24
697a4151
MH
25/*
26 * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
27 * and append the result to arg. Return a pointer to the terminator.
28 * Die if there is an error in how the argument is C-quoted. This
29 * function is only used if not -z.
30 */
c750ba95
BK
31static const char *parse_arg(const char *next, struct strbuf *arg)
32{
697a4151
MH
33 if (*next == '"') {
34 const char *orig = next;
35
36 if (unquote_c_style(arg, next, &next))
37 die("badly quoted argument: %s", orig);
38 if (*next && !isspace(*next))
39 die("unexpected character after quoted argument: %s", orig);
40 } else {
c750ba95
BK
41 while (*next && !isspace(*next))
42 strbuf_addch(arg, *next++);
697a4151 43 }
c750ba95 44
c750ba95
BK
45 return next;
46}
47
e23d8435 48/*
ed410e61
MH
49 * Parse the reference name immediately after "command SP". If not
50 * -z, then handle C-quoting. Return a pointer to a newly allocated
51 * string containing the name of the reference, or NULL if there was
52 * an error. Update *next to point at the character that terminates
53 * the argument. Die if C-quoting is malformed or the reference name
54 * is invalid.
e23d8435 55 */
5ae6c5a7 56static char *parse_refname(const char **next)
c750ba95 57{
ed410e61
MH
58 struct strbuf ref = STRBUF_INIT;
59
c750ba95
BK
60 if (line_termination) {
61 /* Without -z, use the next argument */
ed410e61 62 *next = parse_arg(*next, &ref);
c750ba95 63 } else {
e23d8435 64 /* With -z, use everything up to the next NUL */
ed410e61
MH
65 strbuf_addstr(&ref, *next);
66 *next += ref.len;
67 }
68
69 if (!ref.len) {
70 strbuf_release(&ref);
71 return NULL;
c750ba95 72 }
ed410e61
MH
73
74 if (check_refname_format(ref.buf, REFNAME_ALLOW_ONELEVEL))
75 die("invalid ref format: %s", ref.buf);
76
77 return strbuf_detach(&ref, NULL);
c750ba95
BK
78}
79
e23d8435 80/*
3afcc463
MH
81 * The value being parsed is <oldvalue> (as opposed to <newvalue>; the
82 * difference affects which error messages are generated):
83 */
84#define PARSE_SHA1_OLD 0x01
85
86/*
87 * For backwards compatibility, accept an empty string for update's
88 * <newvalue> in binary mode to be equivalent to specifying zeros.
89 */
90#define PARSE_SHA1_ALLOW_EMPTY 0x02
91
92/*
93 * Parse an argument separator followed by the next argument, if any.
94 * If there is an argument, convert it to a SHA-1, write it to sha1,
95 * set *next to point at the character terminating the argument, and
e23d8435 96 * return 0. If there is no argument at all (not even the empty
3afcc463
MH
97 * string), return 1 and leave *next unchanged. If the value is
98 * provided but cannot be converted to a SHA-1, die. flags can
99 * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY.
e23d8435 100 */
804dba54 101static int parse_next_oid(const char **next, const char *end,
a0bb5535 102 struct object_id *oid,
103 const char *command, const char *refname,
104 int flags)
c750ba95 105{
3afcc463
MH
106 struct strbuf arg = STRBUF_INIT;
107 int ret = 0;
108
804dba54 109 if (*next == end)
3afcc463
MH
110 goto eof;
111
c750ba95
BK
112 if (line_termination) {
113 /* Without -z, consume SP and use next argument */
e23d8435 114 if (!**next || **next == line_termination)
3afcc463 115 return 1;
e23d8435 116 if (**next != ' ')
3afcc463
MH
117 die("%s %s: expected SP but got: %s",
118 command, refname, *next);
e23d8435 119 (*next)++;
3afcc463
MH
120 *next = parse_arg(*next, &arg);
121 if (arg.len) {
d850b7a5 122 if (repo_get_oid(the_repository, arg.buf, oid))
3afcc463
MH
123 goto invalid;
124 } else {
125 /* Without -z, an empty value means all zeros: */
a0bb5535 126 oidclr(oid);
3afcc463 127 }
c750ba95
BK
128 } else {
129 /* With -z, read the next NUL-terminated line */
e23d8435 130 if (**next)
3afcc463
MH
131 die("%s %s: expected NUL but got: %s",
132 command, refname, *next);
e23d8435 133 (*next)++;
804dba54 134 if (*next == end)
3afcc463
MH
135 goto eof;
136 strbuf_addstr(&arg, *next);
137 *next += arg.len;
138
139 if (arg.len) {
d850b7a5 140 if (repo_get_oid(the_repository, arg.buf, oid))
3afcc463
MH
141 goto invalid;
142 } else if (flags & PARSE_SHA1_ALLOW_EMPTY) {
143 /* With -z, treat an empty value as all zeros: */
1fbd5049
MH
144 warning("%s %s: missing <newvalue>, treating as zero",
145 command, refname);
a0bb5535 146 oidclr(oid);
3afcc463
MH
147 } else {
148 /*
149 * With -z, an empty non-required value means
150 * unspecified:
151 */
152 ret = 1;
153 }
c750ba95 154 }
3afcc463
MH
155
156 strbuf_release(&arg);
157
158 return ret;
159
160 invalid:
161 die(flags & PARSE_SHA1_OLD ?
162 "%s %s: invalid <oldvalue>: %s" :
163 "%s %s: invalid <newvalue>: %s",
164 command, refname, arg.buf);
165
166 eof:
167 die(flags & PARSE_SHA1_OLD ?
726f6916
MH
168 "%s %s: unexpected end of input when reading <oldvalue>" :
169 "%s %s: unexpected end of input when reading <newvalue>",
3afcc463 170 command, refname);
c750ba95
BK
171}
172
e23d8435
MH
173
174/*
175 * The following five parse_cmd_*() functions parse the corresponding
176 * command. In each case, next points at the character following the
177 * command name and the following space. They each return a pointer
178 * to the character terminating the command, and die with an
179 * explanatory message if there are any parsing problems. All of
180 * these functions handle either text or binary format input,
181 * depending on how line_termination is set.
182 */
183
94fd491a
PS
184static void parse_cmd_update(struct ref_transaction *transaction,
185 const char *next, const char *end)
c750ba95 186{
ab5ac957 187 struct strbuf err = STRBUF_INIT;
aebfc133 188 char *refname;
a0bb5535 189 struct object_id new_oid, old_oid;
aebfc133 190 int have_old;
c750ba95 191
5ae6c5a7 192 refname = parse_refname(&next);
aebfc133 193 if (!refname)
f11b09fb 194 die("update: missing <ref>");
c750ba95 195
804dba54 196 if (parse_next_oid(&next, end, &new_oid, "update", refname,
a0bb5535 197 PARSE_SHA1_ALLOW_EMPTY))
aebfc133 198 die("update %s: missing <newvalue>", refname);
c750ba95 199
804dba54 200 have_old = !parse_next_oid(&next, end, &old_oid, "update", refname,
a0bb5535 201 PARSE_SHA1_OLD);
3afcc463
MH
202
203 if (*next != line_termination)
aebfc133
MH
204 die("update %s: extra input: %s", refname, next);
205
1d147bdf 206 if (ref_transaction_update(transaction, refname,
89f3bbdd 207 &new_oid, have_old ? &old_oid : NULL,
144c76fa
DT
208 update_flags | create_reflog_flag,
209 msg, &err))
8e34800e 210 die("%s", err.buf);
aebfc133 211
d345e9fb 212 update_flags = default_flags;
aebfc133 213 free(refname);
ab5ac957 214 strbuf_release(&err);
c750ba95
BK
215}
216
94fd491a
PS
217static void parse_cmd_create(struct ref_transaction *transaction,
218 const char *next, const char *end)
c750ba95 219{
ab5ac957 220 struct strbuf err = STRBUF_INIT;
aebfc133 221 char *refname;
a0bb5535 222 struct object_id new_oid;
c750ba95 223
5ae6c5a7 224 refname = parse_refname(&next);
aebfc133 225 if (!refname)
f11b09fb 226 die("create: missing <ref>");
c750ba95 227
804dba54 228 if (parse_next_oid(&next, end, &new_oid, "create", refname, 0))
aebfc133 229 die("create %s: missing <newvalue>", refname);
e23d8435 230
a0bb5535 231 if (is_null_oid(&new_oid))
aebfc133 232 die("create %s: zero <newvalue>", refname);
c750ba95 233
e23d8435 234 if (*next != line_termination)
aebfc133
MH
235 die("create %s: extra input: %s", refname, next);
236
89f3bbdd 237 if (ref_transaction_create(transaction, refname, &new_oid,
144c76fa
DT
238 update_flags | create_reflog_flag,
239 msg, &err))
b416af5b 240 die("%s", err.buf);
aebfc133 241
d345e9fb 242 update_flags = default_flags;
aebfc133 243 free(refname);
ab5ac957 244 strbuf_release(&err);
c750ba95
BK
245}
246
94fd491a
PS
247static void parse_cmd_delete(struct ref_transaction *transaction,
248 const char *next, const char *end)
c750ba95 249{
ab5ac957 250 struct strbuf err = STRBUF_INIT;
aebfc133 251 char *refname;
a0bb5535 252 struct object_id old_oid;
aebfc133 253 int have_old;
c750ba95 254
5ae6c5a7 255 refname = parse_refname(&next);
aebfc133 256 if (!refname)
f11b09fb 257 die("delete: missing <ref>");
c750ba95 258
804dba54 259 if (parse_next_oid(&next, end, &old_oid, "delete", refname,
a0bb5535 260 PARSE_SHA1_OLD)) {
aebfc133 261 have_old = 0;
3afcc463 262 } else {
a0bb5535 263 if (is_null_oid(&old_oid))
aebfc133
MH
264 die("delete %s: zero <oldvalue>", refname);
265 have_old = 1;
3afcc463 266 }
c750ba95 267
e23d8435 268 if (*next != line_termination)
aebfc133
MH
269 die("delete %s: extra input: %s", refname, next);
270
fb5a6bb6 271 if (ref_transaction_delete(transaction, refname,
89f3bbdd 272 have_old ? &old_oid : NULL,
fb5a6bb6 273 update_flags, msg, &err))
8c8bdc0d 274 die("%s", err.buf);
aebfc133 275
d345e9fb 276 update_flags = default_flags;
aebfc133 277 free(refname);
ab5ac957 278 strbuf_release(&err);
c750ba95
BK
279}
280
94fd491a
PS
281static void parse_cmd_verify(struct ref_transaction *transaction,
282 const char *next, const char *end)
c750ba95 283{
ab5ac957 284 struct strbuf err = STRBUF_INIT;
aebfc133 285 char *refname;
a0bb5535 286 struct object_id old_oid;
c750ba95 287
5ae6c5a7 288 refname = parse_refname(&next);
aebfc133 289 if (!refname)
f11b09fb 290 die("verify: missing <ref>");
c750ba95 291
804dba54 292 if (parse_next_oid(&next, end, &old_oid, "verify", refname,
a0bb5535 293 PARSE_SHA1_OLD))
294 oidclr(&old_oid);
0e729c7e 295
e23d8435 296 if (*next != line_termination)
aebfc133
MH
297 die("verify %s: extra input: %s", refname, next);
298
89f3bbdd 299 if (ref_transaction_verify(transaction, refname, &old_oid,
16180334 300 update_flags, &err))
8e34800e 301 die("%s", err.buf);
aebfc133 302
d345e9fb 303 update_flags = default_flags;
aebfc133 304 free(refname);
ab5ac957 305 strbuf_release(&err);
c750ba95
BK
306}
307
efa3d64c
PS
308static void report_ok(const char *command)
309{
310 fprintf(stdout, "%s: ok\n", command);
311 fflush(stdout);
312}
313
94fd491a
PS
314static void parse_cmd_option(struct ref_transaction *transaction,
315 const char *next, const char *end)
c750ba95 316{
aee9be2e
SG
317 const char *rest;
318 if (skip_prefix(next, "no-deref", &rest) && *rest == line_termination)
91774afc 319 update_flags |= REF_NO_DEREF;
c750ba95
BK
320 else
321 die("option unknown: %s", next);
322}
323
e48cf33b
PS
324static void parse_cmd_start(struct ref_transaction *transaction,
325 const char *next, const char *end)
326{
327 if (*next != line_termination)
328 die("start: extra input: %s", next);
efa3d64c 329 report_ok("start");
e48cf33b
PS
330}
331
332static void parse_cmd_prepare(struct ref_transaction *transaction,
333 const char *next, const char *end)
334{
335 struct strbuf error = STRBUF_INIT;
336 if (*next != line_termination)
337 die("prepare: extra input: %s", next);
338 if (ref_transaction_prepare(transaction, &error))
339 die("prepare: %s", error.buf);
efa3d64c 340 report_ok("prepare");
e48cf33b
PS
341}
342
343static void parse_cmd_abort(struct ref_transaction *transaction,
344 const char *next, const char *end)
345{
346 struct strbuf error = STRBUF_INIT;
347 if (*next != line_termination)
348 die("abort: extra input: %s", next);
349 if (ref_transaction_abort(transaction, &error))
350 die("abort: %s", error.buf);
efa3d64c 351 report_ok("abort");
e48cf33b
PS
352}
353
354static void parse_cmd_commit(struct ref_transaction *transaction,
355 const char *next, const char *end)
356{
357 struct strbuf error = STRBUF_INIT;
358 if (*next != line_termination)
359 die("commit: extra input: %s", next);
360 if (ref_transaction_commit(transaction, &error))
361 die("commit: %s", error.buf);
efa3d64c 362 report_ok("commit");
e48cf33b
PS
363 ref_transaction_free(transaction);
364}
365
366enum update_refs_state {
367 /* Non-transactional state open for updates. */
368 UPDATE_REFS_OPEN,
369 /* A transaction has been started. */
370 UPDATE_REFS_STARTED,
371 /* References are locked and ready for commit */
372 UPDATE_REFS_PREPARED,
373 /* Transaction has been committed or closed. */
374 UPDATE_REFS_CLOSED,
375};
376
a65b8ac2
PS
377static const struct parse_cmd {
378 const char *prefix;
94fd491a
PS
379 void (*fn)(struct ref_transaction *, const char *, const char *);
380 unsigned args;
e48cf33b 381 enum update_refs_state state;
a65b8ac2 382} command[] = {
e48cf33b
PS
383 { "update", parse_cmd_update, 3, UPDATE_REFS_OPEN },
384 { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN },
385 { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN },
386 { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN },
387 { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN },
388 { "start", parse_cmd_start, 0, UPDATE_REFS_STARTED },
389 { "prepare", parse_cmd_prepare, 0, UPDATE_REFS_PREPARED },
390 { "abort", parse_cmd_abort, 0, UPDATE_REFS_CLOSED },
391 { "commit", parse_cmd_commit, 0, UPDATE_REFS_CLOSED },
a65b8ac2
PS
392};
393
de0e0d65 394static void update_refs_stdin(void)
c750ba95 395{
de0e0d65 396 struct strbuf input = STRBUF_INIT, err = STRBUF_INIT;
e48cf33b 397 enum update_refs_state state = UPDATE_REFS_OPEN;
de0e0d65 398 struct ref_transaction *transaction;
94fd491a 399 int i, j;
de0e0d65
PS
400
401 transaction = ref_transaction_begin(&err);
402 if (!transaction)
403 die("%s", err.buf);
404
c750ba95 405 /* Read each line dispatch its command */
94fd491a 406 while (!strbuf_getwholeline(&input, stdin, line_termination)) {
a65b8ac2
PS
407 const struct parse_cmd *cmd = NULL;
408
94fd491a 409 if (*input.buf == line_termination)
c750ba95 410 die("empty command in input");
94fd491a
PS
411 else if (isspace(*input.buf))
412 die("whitespace before command: %s", input.buf);
a65b8ac2
PS
413
414 for (i = 0; i < ARRAY_SIZE(command); i++) {
415 const char *prefix = command[i].prefix;
94fd491a 416 char c;
a65b8ac2 417
94fd491a
PS
418 if (!starts_with(input.buf, prefix))
419 continue;
420
421 /*
422 * If the command has arguments, verify that it's
423 * followed by a space. Otherwise, it shall be followed
424 * by a line terminator.
425 */
426 c = command[i].args ? ' ' : line_termination;
427 if (input.buf[strlen(prefix)] != c)
a65b8ac2
PS
428 continue;
429
430 cmd = &command[i];
431 break;
432 }
433 if (!cmd)
94fd491a
PS
434 die("unknown command: %s", input.buf);
435
436 /*
437 * Read additional arguments if NUL-terminated. Do not raise an
438 * error in case there is an early EOF to let the command
439 * handle missing arguments with a proper error message.
440 */
441 for (j = 1; line_termination == '\0' && j < cmd->args; j++)
442 if (strbuf_appendwholeline(&input, stdin, line_termination))
443 break;
444
e48cf33b
PS
445 switch (state) {
446 case UPDATE_REFS_OPEN:
447 case UPDATE_REFS_STARTED:
8c4417f1
PS
448 if (state == UPDATE_REFS_STARTED && cmd->state == UPDATE_REFS_STARTED)
449 die("cannot restart ongoing transaction");
e48cf33b
PS
450 /* Do not downgrade a transaction to a non-transaction. */
451 if (cmd->state >= state)
452 state = cmd->state;
453 break;
454 case UPDATE_REFS_PREPARED:
455 if (cmd->state != UPDATE_REFS_CLOSED)
456 die("prepared transactions can only be closed");
457 state = cmd->state;
458 break;
459 case UPDATE_REFS_CLOSED:
262a4d28
PS
460 if (cmd->state != UPDATE_REFS_STARTED)
461 die("transaction is closed");
462
463 /*
464 * Open a new transaction if we're currently closed and
465 * get a "start".
466 */
467 state = cmd->state;
468 transaction = ref_transaction_begin(&err);
469 if (!transaction)
470 die("%s", err.buf);
471
e48cf33b
PS
472 break;
473 }
474
94fd491a
PS
475 cmd->fn(transaction, input.buf + strlen(cmd->prefix) + !!cmd->args,
476 input.buf + input.len);
e23d8435 477 }
c750ba95 478
e48cf33b
PS
479 switch (state) {
480 case UPDATE_REFS_OPEN:
481 /* Commit by default if no transaction was requested. */
482 if (ref_transaction_commit(transaction, &err))
483 die("%s", err.buf);
484 ref_transaction_free(transaction);
485 break;
486 case UPDATE_REFS_STARTED:
487 case UPDATE_REFS_PREPARED:
488 /* If using a transaction, we want to abort it. */
489 if (ref_transaction_abort(transaction, &err))
490 die("%s", err.buf);
491 break;
492 case UPDATE_REFS_CLOSED:
493 /* Otherwise no need to do anything, the transaction was closed already. */
494 break;
495 }
de0e0d65 496
de0e0d65 497 strbuf_release(&err);
e23d8435 498 strbuf_release(&input);
c750ba95
BK
499}
500
a633fca0 501int cmd_update_ref(int argc, const char **argv, const char *prefix)
66bf85a4 502{
db7516ab 503 const char *refname, *oldval;
a0bb5535 504 struct object_id oid, oldoid;
fec14ec3 505 int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0;
144c76fa 506 int create_reflog = 0;
89942be6 507 struct option options[] = {
82269505 508 OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
d5d09d47
SB
509 OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
510 OPT_BOOL( 0 , "no-deref", &no_deref,
82269505 511 N_("update <refname> not the one it points to")),
9a86b899
JH
512 OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")),
513 OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")),
98c32bd8 514 OPT_BOOL( 0 , "create-reflog", &create_reflog, N_("create a reflog")),
89942be6
PH
515 OPT_END(),
516 };
66bf85a4 517
ef90d6d4 518 git_config(git_default_config, NULL);
37782920
SB
519 argc = parse_options(argc, argv, prefix, options, git_update_ref_usage,
520 0);
89942be6
PH
521 if (msg && !*msg)
522 die("Refusing to perform update with empty message.");
5b16b090 523
144c76fa
DT
524 create_reflog_flag = create_reflog ? REF_FORCE_CREATE_REFLOG : 0;
525
d345e9fb
EN
526 if (no_deref) {
527 default_flags = REF_NO_DEREF;
528 update_flags = default_flags;
529 }
530
c750ba95 531 if (read_stdin) {
d345e9fb 532 if (delete || argc > 0)
c750ba95
BK
533 usage_with_options(git_update_ref_usage, options);
534 if (end_null)
535 line_termination = '\0';
de0e0d65 536 update_refs_stdin();
8bcd3748 537 return 0;
c750ba95
BK
538 }
539
540 if (end_null)
541 usage_with_options(git_update_ref_usage, options);
542
ac5409e4 543 if (delete) {
3fe8dce6 544 if (argc < 1 || argc > 2)
973a70ea
KW
545 usage_with_options(git_update_ref_usage, options);
546 refname = argv[0];
547 oldval = argv[1];
548 } else {
549 const char *value;
550 if (argc < 2 || argc > 3)
89942be6 551 usage_with_options(git_update_ref_usage, options);
973a70ea
KW
552 refname = argv[0];
553 value = argv[1];
554 oldval = argv[2];
d850b7a5 555 if (repo_get_oid(the_repository, value, &oid))
973a70ea 556 die("%s: not a valid SHA1", value);
ac5409e4
JH
557 }
558
e2991c80
MH
559 if (oldval) {
560 if (!*oldval)
561 /*
562 * The empty string implies that the reference
563 * must not already exist:
564 */
a0bb5535 565 oidclr(&oldoid);
d850b7a5 566 else if (repo_get_oid(the_repository, oldval, &oldoid))
e2991c80
MH
567 die("%s: not a valid old SHA1", oldval);
568 }
66bf85a4 569
973a70ea 570 if (delete)
1c03c4d3
MH
571 /*
572 * For purposes of backwards compatibility, we treat
573 * NULL_SHA1 as "don't care" here:
574 */
de922669 575 return delete_ref(msg, refname,
2616a5e5 576 (oldval && !is_null_oid(&oldoid)) ? &oldoid : NULL,
d345e9fb 577 default_flags);
973a70ea 578 else
ae077771 579 return update_ref(msg, refname, &oid, oldval ? &oldoid : NULL,
d345e9fb 580 default_flags | create_reflog_flag,
144c76fa 581 UPDATE_REFS_DIE_ON_ERR);
66bf85a4 582}