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