git

Форк
0
/
update-ref.c 
800 строк · 21.5 Кб
1
#include "builtin.h"
2
#include "config.h"
3
#include "gettext.h"
4
#include "hash.h"
5
#include "refs.h"
6
#include "object-name.h"
7
#include "parse-options.h"
8
#include "quote.h"
9
#include "repository.h"
10

11
static const char * const git_update_ref_usage[] = {
12
	N_("git update-ref [<options>] -d <refname> [<old-oid>]"),
13
	N_("git update-ref [<options>]    <refname> <new-oid> [<old-oid>]"),
14
	N_("git update-ref [<options>] --stdin [-z]"),
15
	NULL
16
};
17

18
static char line_termination = '\n';
19
static unsigned int update_flags;
20
static unsigned int default_flags;
21
static unsigned create_reflog_flag;
22
static const char *msg;
23

24
/*
25
 * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
26
 * and append the result to arg.  Return a pointer to the terminator.
27
 * Die if there is an error in how the argument is C-quoted.  This
28
 * function is only used if not -z.
29
 */
30
static const char *parse_arg(const char *next, struct strbuf *arg)
31
{
32
	if (*next == '"') {
33
		const char *orig = next;
34

35
		if (unquote_c_style(arg, next, &next))
36
			die("badly quoted argument: %s", orig);
37
		if (*next && !isspace(*next))
38
			die("unexpected character after quoted argument: %s", orig);
39
	} else {
40
		while (*next && !isspace(*next))
41
			strbuf_addch(arg, *next++);
42
	}
43

44
	return next;
45
}
46

47
/*
48
 * Parse the reference name immediately after "command SP".  If not
49
 * -z, then handle C-quoting.  Return a pointer to a newly allocated
50
 * string containing the name of the reference, or NULL if there was
51
 * an error.  Update *next to point at the character that terminates
52
 * the argument.  Die if C-quoting is malformed or the reference name
53
 * is invalid.
54
 */
55
static char *parse_refname(const char **next)
56
{
57
	struct strbuf ref = STRBUF_INIT;
58

59
	if (line_termination) {
60
		/* Without -z, use the next argument */
61
		*next = parse_arg(*next, &ref);
62
	} else {
63
		/* With -z, use everything up to the next NUL */
64
		strbuf_addstr(&ref, *next);
65
		*next += ref.len;
66
	}
67

68
	if (!ref.len) {
69
		strbuf_release(&ref);
70
		return NULL;
71
	}
72

73
	if (check_refname_format(ref.buf, REFNAME_ALLOW_ONELEVEL))
74
		die("invalid ref format: %s", ref.buf);
75

76
	return strbuf_detach(&ref, NULL);
77
}
78

79
/*
80
 * Wrapper around parse_refname which skips the next delimiter.
81
 */
82
static char *parse_next_refname(const char **next)
83
{
84
	if (line_termination) {
85
		/* Without -z, consume SP and use next argument */
86
		if (!**next || **next == line_termination)
87
			return NULL;
88
		if (**next != ' ')
89
			die("expected SP but got: %s", *next);
90
	} else {
91
		/* With -z, read the next NUL-terminated line */
92
		if (**next)
93
			return NULL;
94
	}
95
	/* Skip the delimiter */
96
	(*next)++;
97

98
	return parse_refname(next);
99
}
100

101
/*
102
 * Wrapper around parse_arg which skips the next delimiter.
103
 */
104
static char *parse_next_arg(const char **next)
105
{
106
	struct strbuf arg = STRBUF_INIT;
107

108
	if (line_termination) {
109
		/* Without -z, consume SP and use next argument */
110
		if (!**next || **next == line_termination)
111
			return NULL;
112
		if (**next != ' ')
113
			die("expected SP but got: %s", *next);
114
	} else {
115
		/* With -z, read the next NUL-terminated line */
116
		if (**next)
117
			return NULL;
118
	}
119
	/* Skip the delimiter */
120
	(*next)++;
121

122
	if (line_termination) {
123
		/* Without -z, use the next argument */
124
		*next = parse_arg(*next, &arg);
125
	} else {
126
		/* With -z, use everything up to the next NUL */
127
		strbuf_addstr(&arg, *next);
128
		*next += arg.len;
129
	}
130

131
	if (arg.len)
132
		return strbuf_detach(&arg, NULL);
133

134
	strbuf_release(&arg);
135
	return NULL;
136
}
137

138
/*
139
 * The value being parsed is <old-oid> (as opposed to <new-oid>; the
140
 * difference affects which error messages are generated):
141
 */
142
#define PARSE_SHA1_OLD 0x01
143

144
/*
145
 * For backwards compatibility, accept an empty string for update's
146
 * <new-oid> in binary mode to be equivalent to specifying zeros.
147
 */
148
#define PARSE_SHA1_ALLOW_EMPTY 0x02
149

150
/*
151
 * Parse an argument separator followed by the next argument, if any.
152
 * If there is an argument, convert it to a SHA-1, write it to sha1,
153
 * set *next to point at the character terminating the argument, and
154
 * return 0.  If there is no argument at all (not even the empty
155
 * string), return 1 and leave *next unchanged.  If the value is
156
 * provided but cannot be converted to a SHA-1, die.  flags can
157
 * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY.
158
 */
159
static int parse_next_oid(const char **next, const char *end,
160
			  struct object_id *oid,
161
			  const char *command, const char *refname,
162
			  int flags)
163
{
164
	struct strbuf arg = STRBUF_INIT;
165
	int ret = 0;
166

167
	if (*next == end)
168
		goto eof;
169

170
	if (line_termination) {
171
		/* Without -z, consume SP and use next argument */
172
		if (!**next || **next == line_termination)
173
			return 1;
174
		if (**next != ' ')
175
			die("%s %s: expected SP but got: %s",
176
			    command, refname, *next);
177
		(*next)++;
178
		*next = parse_arg(*next, &arg);
179
		if (arg.len) {
180
			if (repo_get_oid(the_repository, arg.buf, oid))
181
				goto invalid;
182
		} else {
183
			/* Without -z, an empty value means all zeros: */
184
			oidclr(oid, the_repository->hash_algo);
185
		}
186
	} else {
187
		/* With -z, read the next NUL-terminated line */
188
		if (**next)
189
			die("%s %s: expected NUL but got: %s",
190
			    command, refname, *next);
191
		(*next)++;
192
		if (*next == end)
193
			goto eof;
194
		strbuf_addstr(&arg, *next);
195
		*next += arg.len;
196

197
		if (arg.len) {
198
			if (repo_get_oid(the_repository, arg.buf, oid))
199
				goto invalid;
200
		} else if (flags & PARSE_SHA1_ALLOW_EMPTY) {
201
			/* With -z, treat an empty value as all zeros: */
202
			warning("%s %s: missing <new-oid>, treating as zero",
203
				command, refname);
204
			oidclr(oid, the_repository->hash_algo);
205
		} else {
206
			/*
207
			 * With -z, an empty non-required value means
208
			 * unspecified:
209
			 */
210
			ret = 1;
211
		}
212
	}
213

214
	strbuf_release(&arg);
215

216
	return ret;
217

218
 invalid:
219
	die(flags & PARSE_SHA1_OLD ?
220
	    "%s %s: invalid <old-oid>: %s" :
221
	    "%s %s: invalid <new-oid>: %s",
222
	    command, refname, arg.buf);
223

224
 eof:
225
	die(flags & PARSE_SHA1_OLD ?
226
	    "%s %s: unexpected end of input when reading <old-oid>" :
227
	    "%s %s: unexpected end of input when reading <new-oid>",
228
	    command, refname);
229
}
230

231

232
/*
233
 * The following five parse_cmd_*() functions parse the corresponding
234
 * command.  In each case, next points at the character following the
235
 * command name and the following space.  They each return a pointer
236
 * to the character terminating the command, and die with an
237
 * explanatory message if there are any parsing problems.  All of
238
 * these functions handle either text or binary format input,
239
 * depending on how line_termination is set.
240
 */
241

242
static void parse_cmd_update(struct ref_transaction *transaction,
243
			     const char *next, const char *end)
244
{
245
	struct strbuf err = STRBUF_INIT;
246
	char *refname;
247
	struct object_id new_oid, old_oid;
248
	int have_old;
249

250
	refname = parse_refname(&next);
251
	if (!refname)
252
		die("update: missing <ref>");
253

254
	if (parse_next_oid(&next, end, &new_oid, "update", refname,
255
			   PARSE_SHA1_ALLOW_EMPTY))
256
		die("update %s: missing <new-oid>", refname);
257

258
	have_old = !parse_next_oid(&next, end, &old_oid, "update", refname,
259
				   PARSE_SHA1_OLD);
260

261
	if (*next != line_termination)
262
		die("update %s: extra input: %s", refname, next);
263

264
	if (ref_transaction_update(transaction, refname,
265
				   &new_oid, have_old ? &old_oid : NULL,
266
				   NULL, NULL,
267
				   update_flags | create_reflog_flag,
268
				   msg, &err))
269
		die("%s", err.buf);
270

271
	update_flags = default_flags;
272
	free(refname);
273
	strbuf_release(&err);
274
}
275

276
static void parse_cmd_symref_update(struct ref_transaction *transaction,
277
				    const char *next, const char *end UNUSED)
278
{
279
	char *refname, *new_target, *old_arg;
280
	char *old_target = NULL;
281
	struct strbuf err = STRBUF_INIT;
282
	struct object_id old_oid;
283
	int have_old_oid = 0;
284

285
	refname = parse_refname(&next);
286
	if (!refname)
287
		die("symref-update: missing <ref>");
288

289
	new_target = parse_next_refname(&next);
290
	if (!new_target)
291
		die("symref-update %s: missing <new-target>", refname);
292

293
	old_arg = parse_next_arg(&next);
294
	if (old_arg) {
295
		old_target = parse_next_arg(&next);
296
		if (!old_target)
297
			die("symref-update %s: expected old value", refname);
298

299
		if (!strcmp(old_arg, "oid")) {
300
			if (repo_get_oid(the_repository, old_target, &old_oid))
301
				die("symref-update %s: invalid oid: %s", refname, old_target);
302

303
			have_old_oid = 1;
304
		} else if (!strcmp(old_arg, "ref")) {
305
			if (check_refname_format(old_target, REFNAME_ALLOW_ONELEVEL))
306
				die("symref-update %s: invalid ref: %s", refname, old_target);
307
		} else {
308
			die("symref-update %s: invalid arg '%s' for old value", refname, old_arg);
309
		}
310
	}
311

312
	if (*next != line_termination)
313
		die("symref-update %s: extra input: %s", refname, next);
314

315
	if (ref_transaction_update(transaction, refname, NULL,
316
				   have_old_oid ? &old_oid : NULL,
317
				   new_target,
318
				   have_old_oid ? NULL : old_target,
319
				   update_flags | create_reflog_flag,
320
				   msg, &err))
321
		die("%s", err.buf);
322

323
	update_flags = default_flags;
324
	free(refname);
325
	free(old_arg);
326
	free(old_target);
327
	free(new_target);
328
	strbuf_release(&err);
329
}
330

331
static void parse_cmd_create(struct ref_transaction *transaction,
332
			     const char *next, const char *end)
333
{
334
	struct strbuf err = STRBUF_INIT;
335
	char *refname;
336
	struct object_id new_oid;
337

338
	refname = parse_refname(&next);
339
	if (!refname)
340
		die("create: missing <ref>");
341

342
	if (parse_next_oid(&next, end, &new_oid, "create", refname, 0))
343
		die("create %s: missing <new-oid>", refname);
344

345
	if (is_null_oid(&new_oid))
346
		die("create %s: zero <new-oid>", refname);
347

348
	if (*next != line_termination)
349
		die("create %s: extra input: %s", refname, next);
350

351
	if (ref_transaction_create(transaction, refname, &new_oid, NULL,
352
				   update_flags | create_reflog_flag,
353
				   msg, &err))
354
		die("%s", err.buf);
355

356
	update_flags = default_flags;
357
	free(refname);
358
	strbuf_release(&err);
359
}
360

361

362
static void parse_cmd_symref_create(struct ref_transaction *transaction,
363
				    const char *next, const char *end UNUSED)
364
{
365
	struct strbuf err = STRBUF_INIT;
366
	char *refname, *new_target;
367

368
	refname = parse_refname(&next);
369
	if (!refname)
370
		die("symref-create: missing <ref>");
371

372
	new_target = parse_next_refname(&next);
373
	if (!new_target)
374
		die("symref-create %s: missing <new-target>", refname);
375

376
	if (*next != line_termination)
377
		die("symref-create %s: extra input: %s", refname, next);
378

379
	if (ref_transaction_create(transaction, refname, NULL, new_target,
380
				   update_flags | create_reflog_flag,
381
				   msg, &err))
382
		die("%s", err.buf);
383

384
	update_flags = default_flags;
385
	free(refname);
386
	free(new_target);
387
	strbuf_release(&err);
388
}
389

390
static void parse_cmd_delete(struct ref_transaction *transaction,
391
			     const char *next, const char *end)
392
{
393
	struct strbuf err = STRBUF_INIT;
394
	char *refname;
395
	struct object_id old_oid;
396
	int have_old;
397

398
	refname = parse_refname(&next);
399
	if (!refname)
400
		die("delete: missing <ref>");
401

402
	if (parse_next_oid(&next, end, &old_oid, "delete", refname,
403
			   PARSE_SHA1_OLD)) {
404
		have_old = 0;
405
	} else {
406
		if (is_null_oid(&old_oid))
407
			die("delete %s: zero <old-oid>", refname);
408
		have_old = 1;
409
	}
410

411
	if (*next != line_termination)
412
		die("delete %s: extra input: %s", refname, next);
413

414
	if (ref_transaction_delete(transaction, refname,
415
				   have_old ? &old_oid : NULL,
416
				   NULL, update_flags, msg, &err))
417
		die("%s", err.buf);
418

419
	update_flags = default_flags;
420
	free(refname);
421
	strbuf_release(&err);
422
}
423

424

425
static void parse_cmd_symref_delete(struct ref_transaction *transaction,
426
				    const char *next, const char *end UNUSED)
427
{
428
	struct strbuf err = STRBUF_INIT;
429
	char *refname, *old_target;
430

431
	if (!(update_flags & REF_NO_DEREF))
432
		die("symref-delete: cannot operate with deref mode");
433

434
	refname = parse_refname(&next);
435
	if (!refname)
436
		die("symref-delete: missing <ref>");
437

438
	old_target = parse_next_refname(&next);
439

440
	if (*next != line_termination)
441
		die("symref-delete %s: extra input: %s", refname, next);
442

443
	if (ref_transaction_delete(transaction, refname, NULL,
444
				   old_target, update_flags, msg, &err))
445
		die("%s", err.buf);
446

447
	update_flags = default_flags;
448
	free(refname);
449
	free(old_target);
450
	strbuf_release(&err);
451
}
452

453

454
static void parse_cmd_verify(struct ref_transaction *transaction,
455
			     const char *next, const char *end)
456
{
457
	struct strbuf err = STRBUF_INIT;
458
	char *refname;
459
	struct object_id old_oid;
460

461
	refname = parse_refname(&next);
462
	if (!refname)
463
		die("verify: missing <ref>");
464

465
	if (parse_next_oid(&next, end, &old_oid, "verify", refname,
466
			   PARSE_SHA1_OLD))
467
		oidclr(&old_oid, the_repository->hash_algo);
468

469
	if (*next != line_termination)
470
		die("verify %s: extra input: %s", refname, next);
471

472
	if (ref_transaction_verify(transaction, refname, &old_oid,
473
				   NULL, update_flags, &err))
474
		die("%s", err.buf);
475

476
	update_flags = default_flags;
477
	free(refname);
478
	strbuf_release(&err);
479
}
480

481
static void parse_cmd_symref_verify(struct ref_transaction *transaction,
482
				    const char *next, const char *end UNUSED)
483
{
484
	struct strbuf err = STRBUF_INIT;
485
	struct object_id old_oid;
486
	char *refname, *old_target;
487

488
	if (!(update_flags & REF_NO_DEREF))
489
		die("symref-verify: cannot operate with deref mode");
490

491
	refname = parse_refname(&next);
492
	if (!refname)
493
		die("symref-verify: missing <ref>");
494

495
	/*
496
	 * old_ref is optional, if not provided, we need to ensure that the
497
	 * ref doesn't exist.
498
	 */
499
	old_target = parse_next_refname(&next);
500
	if (!old_target)
501
		oidcpy(&old_oid, null_oid());
502

503
	if (*next != line_termination)
504
		die("symref-verify %s: extra input: %s", refname, next);
505

506
	if (ref_transaction_verify(transaction, refname,
507
				   old_target ? NULL : &old_oid,
508
				   old_target, update_flags, &err))
509
		die("%s", err.buf);
510

511
	update_flags = default_flags;
512
	free(refname);
513
	free(old_target);
514
	strbuf_release(&err);
515
}
516

517
static void report_ok(const char *command)
518
{
519
	fprintf(stdout, "%s: ok\n", command);
520
	fflush(stdout);
521
}
522

523
static void parse_cmd_option(struct ref_transaction *transaction UNUSED,
524
			     const char *next, const char *end UNUSED)
525
{
526
	const char *rest;
527
	if (skip_prefix(next, "no-deref", &rest) && *rest == line_termination)
528
		update_flags |= REF_NO_DEREF;
529
	else
530
		die("option unknown: %s", next);
531
}
532

533
static void parse_cmd_start(struct ref_transaction *transaction UNUSED,
534
			    const char *next, const char *end UNUSED)
535
{
536
	if (*next != line_termination)
537
		die("start: extra input: %s", next);
538
	report_ok("start");
539
}
540

541
static void parse_cmd_prepare(struct ref_transaction *transaction,
542
			      const char *next, const char *end UNUSED)
543
{
544
	struct strbuf error = STRBUF_INIT;
545
	if (*next != line_termination)
546
		die("prepare: extra input: %s", next);
547
	if (ref_transaction_prepare(transaction, &error))
548
		die("prepare: %s", error.buf);
549
	report_ok("prepare");
550
}
551

552
static void parse_cmd_abort(struct ref_transaction *transaction,
553
			    const char *next, const char *end UNUSED)
554
{
555
	struct strbuf error = STRBUF_INIT;
556
	if (*next != line_termination)
557
		die("abort: extra input: %s", next);
558
	if (ref_transaction_abort(transaction, &error))
559
		die("abort: %s", error.buf);
560
	report_ok("abort");
561
}
562

563
static void parse_cmd_commit(struct ref_transaction *transaction,
564
			     const char *next, const char *end UNUSED)
565
{
566
	struct strbuf error = STRBUF_INIT;
567
	if (*next != line_termination)
568
		die("commit: extra input: %s", next);
569
	if (ref_transaction_commit(transaction, &error))
570
		die("commit: %s", error.buf);
571
	report_ok("commit");
572
	ref_transaction_free(transaction);
573
}
574

575
enum update_refs_state {
576
	/* Non-transactional state open for updates. */
577
	UPDATE_REFS_OPEN,
578
	/* A transaction has been started. */
579
	UPDATE_REFS_STARTED,
580
	/* References are locked and ready for commit */
581
	UPDATE_REFS_PREPARED,
582
	/* Transaction has been committed or closed. */
583
	UPDATE_REFS_CLOSED,
584
};
585

586
static const struct parse_cmd {
587
	const char *prefix;
588
	void (*fn)(struct ref_transaction *, const char *, const char *);
589
	unsigned args;
590
	enum update_refs_state state;
591
} command[] = {
592
	{ "update",        parse_cmd_update,        3, UPDATE_REFS_OPEN },
593
	{ "create",        parse_cmd_create,        2, UPDATE_REFS_OPEN },
594
	{ "delete",        parse_cmd_delete,        2, UPDATE_REFS_OPEN },
595
	{ "verify",        parse_cmd_verify,        2, UPDATE_REFS_OPEN },
596
	{ "symref-update", parse_cmd_symref_update, 4, UPDATE_REFS_OPEN },
597
	{ "symref-create", parse_cmd_symref_create, 2, UPDATE_REFS_OPEN },
598
	{ "symref-delete", parse_cmd_symref_delete, 2, UPDATE_REFS_OPEN },
599
	{ "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN },
600
	{ "option",        parse_cmd_option,        1, UPDATE_REFS_OPEN },
601
	{ "start",         parse_cmd_start,         0, UPDATE_REFS_STARTED },
602
	{ "prepare",       parse_cmd_prepare,       0, UPDATE_REFS_PREPARED },
603
	{ "abort",         parse_cmd_abort,         0, UPDATE_REFS_CLOSED },
604
	{ "commit",        parse_cmd_commit,        0, UPDATE_REFS_CLOSED },
605
};
606

607
static void update_refs_stdin(void)
608
{
609
	struct strbuf input = STRBUF_INIT, err = STRBUF_INIT;
610
	enum update_refs_state state = UPDATE_REFS_OPEN;
611
	struct ref_transaction *transaction;
612
	int i, j;
613

614
	transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
615
						  &err);
616
	if (!transaction)
617
		die("%s", err.buf);
618

619
	/* Read each line dispatch its command */
620
	while (!strbuf_getwholeline(&input, stdin, line_termination)) {
621
		const struct parse_cmd *cmd = NULL;
622

623
		if (*input.buf == line_termination)
624
			die("empty command in input");
625
		else if (isspace(*input.buf))
626
			die("whitespace before command: %s", input.buf);
627

628
		for (i = 0; i < ARRAY_SIZE(command); i++) {
629
			const char *prefix = command[i].prefix;
630
			char c;
631

632
			if (!starts_with(input.buf, prefix))
633
				continue;
634

635
			/*
636
			 * If the command has arguments, verify that it's
637
			 * followed by a space. Otherwise, it shall be followed
638
			 * by a line terminator.
639
			 */
640
			c = command[i].args ? ' ' : line_termination;
641
			if (input.buf[strlen(prefix)] != c)
642
				continue;
643

644
			cmd = &command[i];
645
			break;
646
		}
647
		if (!cmd)
648
			die("unknown command: %s", input.buf);
649

650
		/*
651
		 * Read additional arguments if NUL-terminated. Do not raise an
652
		 * error in case there is an early EOF to let the command
653
		 * handle missing arguments with a proper error message.
654
		 */
655
		for (j = 1; line_termination == '\0' && j < cmd->args; j++)
656
			if (strbuf_appendwholeline(&input, stdin, line_termination))
657
				break;
658

659
		switch (state) {
660
		case UPDATE_REFS_OPEN:
661
		case UPDATE_REFS_STARTED:
662
			if (state == UPDATE_REFS_STARTED && cmd->state == UPDATE_REFS_STARTED)
663
				die("cannot restart ongoing transaction");
664
			/* Do not downgrade a transaction to a non-transaction. */
665
			if (cmd->state >= state)
666
				state = cmd->state;
667
			break;
668
		case UPDATE_REFS_PREPARED:
669
			if (cmd->state != UPDATE_REFS_CLOSED)
670
				die("prepared transactions can only be closed");
671
			state = cmd->state;
672
			break;
673
		case UPDATE_REFS_CLOSED:
674
			if (cmd->state != UPDATE_REFS_STARTED)
675
				die("transaction is closed");
676

677
			/*
678
			 * Open a new transaction if we're currently closed and
679
			 * get a "start".
680
			 */
681
			state = cmd->state;
682
			transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
683
								  &err);
684
			if (!transaction)
685
				die("%s", err.buf);
686

687
			break;
688
		}
689

690
		cmd->fn(transaction, input.buf + strlen(cmd->prefix) + !!cmd->args,
691
			input.buf + input.len);
692
	}
693

694
	switch (state) {
695
	case UPDATE_REFS_OPEN:
696
		/* Commit by default if no transaction was requested. */
697
		if (ref_transaction_commit(transaction, &err))
698
			die("%s", err.buf);
699
		ref_transaction_free(transaction);
700
		break;
701
	case UPDATE_REFS_STARTED:
702
	case UPDATE_REFS_PREPARED:
703
		/* If using a transaction, we want to abort it. */
704
		if (ref_transaction_abort(transaction, &err))
705
			die("%s", err.buf);
706
		break;
707
	case UPDATE_REFS_CLOSED:
708
		/* Otherwise no need to do anything, the transaction was closed already. */
709
		break;
710
	}
711

712
	strbuf_release(&err);
713
	strbuf_release(&input);
714
}
715

716
int cmd_update_ref(int argc, const char **argv, const char *prefix)
717
{
718
	const char *refname, *oldval;
719
	struct object_id oid, oldoid;
720
	int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0;
721
	int create_reflog = 0;
722
	struct option options[] = {
723
		OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
724
		OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
725
		OPT_BOOL( 0 , "no-deref", &no_deref,
726
					N_("update <refname> not the one it points to")),
727
		OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")),
728
		OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")),
729
		OPT_BOOL( 0 , "create-reflog", &create_reflog, N_("create a reflog")),
730
		OPT_END(),
731
	};
732

733
	git_config(git_default_config, NULL);
734
	argc = parse_options(argc, argv, prefix, options, git_update_ref_usage,
735
			     0);
736
	if (msg && !*msg)
737
		die("Refusing to perform update with empty message.");
738

739
	create_reflog_flag = create_reflog ? REF_FORCE_CREATE_REFLOG : 0;
740

741
	if (no_deref) {
742
		default_flags = REF_NO_DEREF;
743
		update_flags = default_flags;
744
	}
745

746
	if (read_stdin) {
747
		if (delete || argc > 0)
748
			usage_with_options(git_update_ref_usage, options);
749
		if (end_null)
750
			line_termination = '\0';
751
		update_refs_stdin();
752
		return 0;
753
	}
754

755
	if (end_null)
756
		usage_with_options(git_update_ref_usage, options);
757

758
	if (delete) {
759
		if (argc < 1 || argc > 2)
760
			usage_with_options(git_update_ref_usage, options);
761
		refname = argv[0];
762
		oldval = argv[1];
763
	} else {
764
		const char *value;
765
		if (argc < 2 || argc > 3)
766
			usage_with_options(git_update_ref_usage, options);
767
		refname = argv[0];
768
		value = argv[1];
769
		oldval = argv[2];
770
		if (repo_get_oid(the_repository, value, &oid))
771
			die("%s: not a valid SHA1", value);
772
	}
773

774
	if (oldval) {
775
		if (!*oldval)
776
			/*
777
			 * The empty string implies that the reference
778
			 * must not already exist:
779
			 */
780
			oidclr(&oldoid, the_repository->hash_algo);
781
		else if (repo_get_oid(the_repository, oldval, &oldoid))
782
			die("%s: not a valid old SHA1", oldval);
783
	}
784

785
	if (delete)
786
		/*
787
		 * For purposes of backwards compatibility, we treat
788
		 * NULL_SHA1 as "don't care" here:
789
		 */
790
		return refs_delete_ref(get_main_ref_store(the_repository),
791
				       msg, refname,
792
				       (oldval && !is_null_oid(&oldoid)) ? &oldoid : NULL,
793
				       default_flags);
794
	else
795
		return refs_update_ref(get_main_ref_store(the_repository),
796
				       msg, refname, &oid,
797
				       oldval ? &oldoid : NULL,
798
				       default_flags | create_reflog_flag,
799
				       UPDATE_REFS_DIE_ON_ERR);
800
}
801

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.