git

Форк
0
/
notes.c 
1139 строк · 31.6 Кб
1
/*
2
 * Builtin "git notes"
3
 *
4
 * Copyright (c) 2010 Johan Herland <johan@herland.net>
5
 *
6
 * Based on git-notes.sh by Johannes Schindelin,
7
 * and builtin/tag.c by Kristian Høgsberg and Carlos Rica.
8
 */
9

10
#include "builtin.h"
11
#include "config.h"
12
#include "editor.h"
13
#include "environment.h"
14
#include "gettext.h"
15
#include "hex.h"
16
#include "notes.h"
17
#include "object-name.h"
18
#include "object-store-ll.h"
19
#include "path.h"
20
#include "repository.h"
21
#include "pretty.h"
22
#include "refs.h"
23
#include "exec-cmd.h"
24
#include "run-command.h"
25
#include "parse-options.h"
26
#include "string-list.h"
27
#include "notes-merge.h"
28
#include "notes-utils.h"
29
#include "worktree.h"
30
#include "write-or-die.h"
31

32
static const char *separator = "\n";
33
static const char * const git_notes_usage[] = {
34
	N_("git notes [--ref <notes-ref>] [list [<object>]]"),
35
	N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
36
	N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
37
	N_("git notes [--ref <notes-ref>] append [--allow-empty] [--[no-]separator|--separator=<paragraph-break>] [--[no-]stripspace] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
38
	N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
39
	N_("git notes [--ref <notes-ref>] show [<object>]"),
40
	N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
41
	"git notes merge --commit [-v | -q]",
42
	"git notes merge --abort [-v | -q]",
43
	N_("git notes [--ref <notes-ref>] remove [<object>...]"),
44
	N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
45
	N_("git notes [--ref <notes-ref>] get-ref"),
46
	NULL
47
};
48

49
static const char * const git_notes_list_usage[] = {
50
	N_("git notes [list [<object>]]"),
51
	NULL
52
};
53

54
static const char * const git_notes_add_usage[] = {
55
	N_("git notes add [<options>] [<object>]"),
56
	NULL
57
};
58

59
static const char * const git_notes_copy_usage[] = {
60
	N_("git notes copy [<options>] <from-object> <to-object>"),
61
	N_("git notes copy --stdin [<from-object> <to-object>]..."),
62
	NULL
63
};
64

65
static const char * const git_notes_append_usage[] = {
66
	N_("git notes append [<options>] [<object>]"),
67
	NULL
68
};
69

70
static const char * const git_notes_edit_usage[] = {
71
	N_("git notes edit [<object>]"),
72
	NULL
73
};
74

75
static const char * const git_notes_show_usage[] = {
76
	N_("git notes show [<object>]"),
77
	NULL
78
};
79

80
static const char * const git_notes_merge_usage[] = {
81
	N_("git notes merge [<options>] <notes-ref>"),
82
	N_("git notes merge --commit [<options>]"),
83
	N_("git notes merge --abort [<options>]"),
84
	NULL
85
};
86

87
static const char * const git_notes_remove_usage[] = {
88
	N_("git notes remove [<object>]"),
89
	NULL
90
};
91

92
static const char * const git_notes_prune_usage[] = {
93
	N_("git notes prune [<options>]"),
94
	NULL
95
};
96

97
static const char * const git_notes_get_ref_usage[] = {
98
	"git notes get-ref",
99
	NULL
100
};
101

102
static const char note_template[] =
103
	N_("Write/edit the notes for the following object:");
104

105
enum notes_stripspace {
106
	UNSPECIFIED = -1,
107
	NO_STRIPSPACE = 0,
108
	STRIPSPACE = 1,
109
};
110

111
struct note_msg {
112
	enum notes_stripspace stripspace;
113
	struct strbuf buf;
114
};
115

116
struct note_data {
117
	int use_editor;
118
	int stripspace;
119
	char *edit_path;
120
	struct strbuf buf;
121
	struct note_msg **messages;
122
	size_t msg_nr;
123
	size_t msg_alloc;
124
};
125

126
static void free_note_data(struct note_data *d)
127
{
128
	if (d->edit_path) {
129
		unlink_or_warn(d->edit_path);
130
		free(d->edit_path);
131
	}
132
	strbuf_release(&d->buf);
133

134
	while (d->msg_nr--) {
135
		strbuf_release(&d->messages[d->msg_nr]->buf);
136
		free(d->messages[d->msg_nr]);
137
	}
138
	free(d->messages);
139
}
140

141
static int list_each_note(const struct object_id *object_oid,
142
			  const struct object_id *note_oid,
143
			  char *note_path UNUSED,
144
			  void *cb_data UNUSED)
145
{
146
	printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
147
	return 0;
148
}
149

150
static void copy_obj_to_fd(int fd, const struct object_id *oid)
151
{
152
	unsigned long size;
153
	enum object_type type;
154
	char *buf = repo_read_object_file(the_repository, oid, &type, &size);
155
	if (buf) {
156
		if (size)
157
			write_or_die(fd, buf, size);
158
		free(buf);
159
	}
160
}
161

162
static void write_commented_object(int fd, const struct object_id *object)
163
{
164
	struct child_process show = CHILD_PROCESS_INIT;
165
	struct strbuf buf = STRBUF_INIT;
166
	struct strbuf cbuf = STRBUF_INIT;
167

168
	/* Invoke "git show --stat --no-notes $object" */
169
	strvec_pushl(&show.args, "show", "--stat", "--no-notes",
170
		     oid_to_hex(object), NULL);
171
	show.no_stdin = 1;
172
	show.out = -1;
173
	show.err = 0;
174
	show.git_cmd = 1;
175
	if (start_command(&show))
176
		die(_("unable to start 'show' for object '%s'"),
177
		    oid_to_hex(object));
178

179
	if (strbuf_read(&buf, show.out, 0) < 0)
180
		die_errno(_("could not read 'show' output"));
181
	strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_str);
182
	write_or_die(fd, cbuf.buf, cbuf.len);
183

184
	strbuf_release(&cbuf);
185
	strbuf_release(&buf);
186

187
	if (finish_command(&show))
188
		die(_("failed to finish 'show' for object '%s'"),
189
		    oid_to_hex(object));
190
}
191

192
static void prepare_note_data(const struct object_id *object, struct note_data *d,
193
		const struct object_id *old_note)
194
{
195
	if (d->use_editor || !d->msg_nr) {
196
		int fd;
197
		struct strbuf buf = STRBUF_INIT;
198

199
		/* write the template message before editing: */
200
		d->edit_path = git_pathdup("NOTES_EDITMSG");
201
		fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
202

203
		if (d->msg_nr)
204
			write_or_die(fd, d->buf.buf, d->buf.len);
205
		else if (old_note)
206
			copy_obj_to_fd(fd, old_note);
207

208
		strbuf_addch(&buf, '\n');
209
		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
210
		strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
211
					   comment_line_str);
212
		strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_str);
213
		write_or_die(fd, buf.buf, buf.len);
214

215
		write_commented_object(fd, object);
216

217
		close(fd);
218
		strbuf_release(&buf);
219
		strbuf_reset(&d->buf);
220

221
		if (launch_editor(d->edit_path, &d->buf, NULL)) {
222
			die(_("please supply the note contents using either -m or -F option"));
223
		}
224
		if (d->stripspace)
225
			strbuf_stripspace(&d->buf, comment_line_str);
226
	}
227
}
228

229
static void write_note_data(struct note_data *d, struct object_id *oid)
230
{
231
	if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) {
232
		int status = die_message(_("unable to write note object"));
233

234
		if (d->edit_path)
235
			die_message(_("the note contents have been left in %s"),
236
				    d->edit_path);
237
		exit(status);
238
	}
239
}
240

241
static void append_separator(struct strbuf *message)
242
{
243
	size_t sep_len = 0;
244

245
	if (!separator)
246
		return;
247
	else if ((sep_len = strlen(separator)) && separator[sep_len - 1] == '\n')
248
		strbuf_addstr(message, separator);
249
	else
250
		strbuf_addf(message, "%s%s", separator, "\n");
251
}
252

253
static void concat_messages(struct note_data *d)
254
{
255
	struct strbuf msg = STRBUF_INIT;
256
	size_t i;
257

258
	for (i = 0; i < d->msg_nr ; i++) {
259
		if (d->buf.len)
260
			append_separator(&d->buf);
261
		strbuf_add(&msg, d->messages[i]->buf.buf, d->messages[i]->buf.len);
262
		strbuf_addbuf(&d->buf, &msg);
263
		if ((d->stripspace == UNSPECIFIED &&
264
		     d->messages[i]->stripspace == STRIPSPACE) ||
265
		    d->stripspace == STRIPSPACE)
266
			strbuf_stripspace(&d->buf, NULL);
267
		strbuf_reset(&msg);
268
	}
269
	strbuf_release(&msg);
270
}
271

272
static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
273
{
274
	struct note_data *d = opt->value;
275
	struct note_msg *msg = xmalloc(sizeof(*msg));
276

277
	BUG_ON_OPT_NEG(unset);
278

279
	strbuf_init(&msg->buf, strlen(arg));
280
	strbuf_addstr(&msg->buf, arg);
281
	ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
282
	d->messages[d->msg_nr - 1] = msg;
283
	msg->stripspace = STRIPSPACE;
284
	return 0;
285
}
286

287
static int parse_file_arg(const struct option *opt, const char *arg, int unset)
288
{
289
	struct note_data *d = opt->value;
290
	struct note_msg *msg = xmalloc(sizeof(*msg));
291

292
	BUG_ON_OPT_NEG(unset);
293

294
	strbuf_init(&msg->buf , 0);
295
	if (!strcmp(arg, "-")) {
296
		if (strbuf_read(&msg->buf, 0, 1024) < 0)
297
			die_errno(_("cannot read '%s'"), arg);
298
	} else if (strbuf_read_file(&msg->buf, arg, 1024) < 0)
299
		die_errno(_("could not open or read '%s'"), arg);
300

301
	ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
302
	d->messages[d->msg_nr - 1] = msg;
303
	msg->stripspace = STRIPSPACE;
304
	return 0;
305
}
306

307
static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
308
{
309
	struct note_data *d = opt->value;
310
	struct note_msg *msg = xmalloc(sizeof(*msg));
311
	char *value;
312
	struct object_id object;
313
	enum object_type type;
314
	unsigned long len;
315

316
	BUG_ON_OPT_NEG(unset);
317

318
	strbuf_init(&msg->buf, 0);
319
	if (repo_get_oid(the_repository, arg, &object))
320
		die(_("failed to resolve '%s' as a valid ref."), arg);
321
	if (!(value = repo_read_object_file(the_repository, &object, &type, &len)))
322
		die(_("failed to read object '%s'."), arg);
323
	if (type != OBJ_BLOB) {
324
		strbuf_release(&msg->buf);
325
		free(value);
326
		free(msg);
327
		die(_("cannot read note data from non-blob object '%s'."), arg);
328
	}
329

330
	strbuf_add(&msg->buf, value, len);
331
	free(value);
332

333
	msg->buf.len = len;
334
	ALLOC_GROW_BY(d->messages, d->msg_nr, 1, d->msg_alloc);
335
	d->messages[d->msg_nr - 1] = msg;
336
	msg->stripspace = NO_STRIPSPACE;
337
	return 0;
338
}
339

340
static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
341
{
342
	struct note_data *d = opt->value;
343
	BUG_ON_OPT_NEG(unset);
344
	d->use_editor = 1;
345
	return parse_reuse_arg(opt, arg, unset);
346
}
347

348
static int parse_separator_arg(const struct option *opt, const char *arg,
349
			       int unset)
350
{
351
	if (unset)
352
		*(const char **)opt->value = NULL;
353
	else
354
		*(const char **)opt->value = arg ? arg : "\n";
355
	return 0;
356
}
357

358
static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
359
{
360
	struct strbuf buf = STRBUF_INIT;
361
	struct notes_rewrite_cfg *c = NULL;
362
	struct notes_tree *t = NULL;
363
	int ret = 0;
364
	const char *msg = "Notes added by 'git notes copy'";
365

366
	if (rewrite_cmd) {
367
		c = init_copy_notes_for_rewrite(rewrite_cmd);
368
		if (!c)
369
			return 0;
370
	} else {
371
		init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
372
		t = &default_notes_tree;
373
	}
374

375
	while (strbuf_getline_lf(&buf, stdin) != EOF) {
376
		struct object_id from_obj, to_obj;
377
		struct strbuf **split;
378
		int err;
379

380
		split = strbuf_split(&buf, ' ');
381
		if (!split[0] || !split[1])
382
			die(_("malformed input line: '%s'."), buf.buf);
383
		strbuf_rtrim(split[0]);
384
		strbuf_rtrim(split[1]);
385
		if (repo_get_oid(the_repository, split[0]->buf, &from_obj))
386
			die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
387
		if (repo_get_oid(the_repository, split[1]->buf, &to_obj))
388
			die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
389

390
		if (rewrite_cmd)
391
			err = copy_note_for_rewrite(c, &from_obj, &to_obj);
392
		else
393
			err = copy_note(t, &from_obj, &to_obj, force,
394
					combine_notes_overwrite);
395

396
		if (err) {
397
			error(_("failed to copy notes from '%s' to '%s'"),
398
			      split[0]->buf, split[1]->buf);
399
			ret = 1;
400
		}
401

402
		strbuf_list_free(split);
403
	}
404

405
	if (!rewrite_cmd) {
406
		commit_notes(the_repository, t, msg);
407
		free_notes(t);
408
	} else {
409
		finish_copy_notes_for_rewrite(the_repository, c, msg);
410
	}
411
	strbuf_release(&buf);
412
	return ret;
413
}
414

415
static struct notes_tree *init_notes_check(const char *subcommand,
416
					   int flags)
417
{
418
	struct notes_tree *t;
419
	const char *ref;
420
	init_notes(NULL, NULL, NULL, flags);
421
	t = &default_notes_tree;
422

423
	ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
424
	if (!starts_with(ref, "refs/notes/"))
425
		/*
426
		 * TRANSLATORS: the first %s will be replaced by a git
427
		 * notes command: 'add', 'merge', 'remove', etc.
428
		 */
429
		die(_("refusing to %s notes in %s (outside of refs/notes/)"),
430
		    subcommand, ref);
431
	return t;
432
}
433

434
static int list(int argc, const char **argv, const char *prefix)
435
{
436
	struct notes_tree *t;
437
	struct object_id object;
438
	const struct object_id *note;
439
	int retval = -1;
440
	struct option options[] = {
441
		OPT_END()
442
	};
443

444
	if (argc)
445
		argc = parse_options(argc, argv, prefix, options,
446
				     git_notes_list_usage, 0);
447

448
	if (1 < argc) {
449
		error(_("too many arguments"));
450
		usage_with_options(git_notes_list_usage, options);
451
	}
452

453
	t = init_notes_check("list", 0);
454
	if (argc) {
455
		if (repo_get_oid(the_repository, argv[0], &object))
456
			die(_("failed to resolve '%s' as a valid ref."), argv[0]);
457
		note = get_note(t, &object);
458
		if (note) {
459
			puts(oid_to_hex(note));
460
			retval = 0;
461
		} else
462
			retval = error(_("no note found for object %s."),
463
				       oid_to_hex(&object));
464
	} else
465
		retval = for_each_note(t, 0, list_each_note, NULL);
466

467
	free_notes(t);
468
	return retval;
469
}
470

471
static int append_edit(int argc, const char **argv, const char *prefix);
472

473
static int add(int argc, const char **argv, const char *prefix)
474
{
475
	int force = 0, allow_empty = 0;
476
	const char *object_ref;
477
	struct notes_tree *t;
478
	struct object_id object, new_note;
479
	const struct object_id *note;
480
	struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
481

482
	struct option options[] = {
483
		OPT_CALLBACK_F('m', "message", &d, N_("message"),
484
			N_("note contents as a string"), PARSE_OPT_NONEG,
485
			parse_msg_arg),
486
		OPT_CALLBACK_F('F', "file", &d, N_("file"),
487
			N_("note contents in a file"), PARSE_OPT_NONEG,
488
			parse_file_arg),
489
		OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
490
			N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
491
			parse_reedit_arg),
492
		OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
493
			N_("reuse specified note object"), PARSE_OPT_NONEG,
494
			parse_reuse_arg),
495
		OPT_BOOL(0, "allow-empty", &allow_empty,
496
			N_("allow storing empty note")),
497
		OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
498
		OPT_CALLBACK_F(0, "separator", &separator,
499
			N_("<paragraph-break>"),
500
			N_("insert <paragraph-break> between paragraphs"),
501
			PARSE_OPT_OPTARG, parse_separator_arg),
502
		OPT_BOOL(0, "stripspace", &d.stripspace,
503
			N_("remove unnecessary whitespace")),
504
		OPT_END()
505
	};
506

507
	argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
508
			     PARSE_OPT_KEEP_ARGV0);
509

510
	if (2 < argc) {
511
		error(_("too many arguments"));
512
		usage_with_options(git_notes_add_usage, options);
513
	}
514

515
	if (d.msg_nr)
516
		concat_messages(&d);
517

518
	object_ref = argc > 1 ? argv[1] : "HEAD";
519

520
	if (repo_get_oid(the_repository, object_ref, &object))
521
		die(_("failed to resolve '%s' as a valid ref."), object_ref);
522

523
	t = init_notes_check("add", NOTES_INIT_WRITABLE);
524
	note = get_note(t, &object);
525

526
	if (note) {
527
		if (!force) {
528
			free_notes(t);
529
			if (d.msg_nr) {
530
				free_note_data(&d);
531
				return error(_("Cannot add notes. "
532
					"Found existing notes for object %s. "
533
					"Use '-f' to overwrite existing notes"),
534
					oid_to_hex(&object));
535
			}
536
			/*
537
			 * Redirect to "edit" subcommand.
538
			 *
539
			 * We only end up here if none of -m/-F/-c/-C or -f are
540
			 * given. The original args are therefore still in
541
			 * argv[0-1].
542
			 */
543
			argv[0] = "edit";
544
			return append_edit(argc, argv, prefix);
545
		}
546
		fprintf(stderr, _("Overwriting existing notes for object %s\n"),
547
			oid_to_hex(&object));
548
	}
549

550
	prepare_note_data(&object, &d, note);
551
	if (d.buf.len || allow_empty) {
552
		write_note_data(&d, &new_note);
553
		if (add_note(t, &object, &new_note, combine_notes_overwrite))
554
			BUG("combine_notes_overwrite failed");
555
		commit_notes(the_repository, t,
556
			     "Notes added by 'git notes add'");
557
	} else {
558
		fprintf(stderr, _("Removing note for object %s\n"),
559
			oid_to_hex(&object));
560
		remove_note(t, object.hash);
561
		commit_notes(the_repository, t,
562
			     "Notes removed by 'git notes add'");
563
	}
564

565
	free_note_data(&d);
566
	free_notes(t);
567
	return 0;
568
}
569

570
static int copy(int argc, const char **argv, const char *prefix)
571
{
572
	int retval = 0, force = 0, from_stdin = 0;
573
	const struct object_id *from_note, *note;
574
	const char *object_ref;
575
	struct object_id object, from_obj;
576
	struct notes_tree *t;
577
	const char *rewrite_cmd = NULL;
578
	struct option options[] = {
579
		OPT__FORCE(&force, N_("replace existing notes"), PARSE_OPT_NOCOMPLETE),
580
		OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
581
		OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
582
			   N_("load rewriting config for <command> (implies "
583
			      "--stdin)")),
584
		OPT_END()
585
	};
586

587
	argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
588
			     0);
589

590
	if (from_stdin || rewrite_cmd) {
591
		if (argc) {
592
			error(_("too many arguments"));
593
			usage_with_options(git_notes_copy_usage, options);
594
		} else {
595
			return notes_copy_from_stdin(force, rewrite_cmd);
596
		}
597
	}
598

599
	if (argc < 1) {
600
		error(_("too few arguments"));
601
		usage_with_options(git_notes_copy_usage, options);
602
	}
603
	if (2 < argc) {
604
		error(_("too many arguments"));
605
		usage_with_options(git_notes_copy_usage, options);
606
	}
607

608
	if (repo_get_oid(the_repository, argv[0], &from_obj))
609
		die(_("failed to resolve '%s' as a valid ref."), argv[0]);
610

611
	object_ref = 1 < argc ? argv[1] : "HEAD";
612

613
	if (repo_get_oid(the_repository, object_ref, &object))
614
		die(_("failed to resolve '%s' as a valid ref."), object_ref);
615

616
	t = init_notes_check("copy", NOTES_INIT_WRITABLE);
617
	note = get_note(t, &object);
618

619
	if (note) {
620
		if (!force) {
621
			retval = error(_("Cannot copy notes. Found existing "
622
				       "notes for object %s. Use '-f' to "
623
				       "overwrite existing notes"),
624
				       oid_to_hex(&object));
625
			goto out;
626
		}
627
		fprintf(stderr, _("Overwriting existing notes for object %s\n"),
628
			oid_to_hex(&object));
629
	}
630

631
	from_note = get_note(t, &from_obj);
632
	if (!from_note) {
633
		retval = error(_("missing notes on source object %s. Cannot "
634
			       "copy."), oid_to_hex(&from_obj));
635
		goto out;
636
	}
637

638
	if (add_note(t, &object, from_note, combine_notes_overwrite))
639
		BUG("combine_notes_overwrite failed");
640
	commit_notes(the_repository, t,
641
		     "Notes added by 'git notes copy'");
642
out:
643
	free_notes(t);
644
	return retval;
645
}
646

647
static int append_edit(int argc, const char **argv, const char *prefix)
648
{
649
	int allow_empty = 0;
650
	const char *object_ref;
651
	struct notes_tree *t;
652
	struct object_id object, new_note;
653
	const struct object_id *note;
654
	char *logmsg;
655
	const char * const *usage;
656
	struct note_data d = { .buf = STRBUF_INIT, .stripspace = UNSPECIFIED };
657
	struct option options[] = {
658
		OPT_CALLBACK_F('m', "message", &d, N_("message"),
659
			N_("note contents as a string"), PARSE_OPT_NONEG,
660
			parse_msg_arg),
661
		OPT_CALLBACK_F('F', "file", &d, N_("file"),
662
			N_("note contents in a file"), PARSE_OPT_NONEG,
663
			parse_file_arg),
664
		OPT_CALLBACK_F('c', "reedit-message", &d, N_("object"),
665
			N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
666
			parse_reedit_arg),
667
		OPT_CALLBACK_F('C', "reuse-message", &d, N_("object"),
668
			N_("reuse specified note object"), PARSE_OPT_NONEG,
669
			parse_reuse_arg),
670
		OPT_BOOL(0, "allow-empty", &allow_empty,
671
			N_("allow storing empty note")),
672
		OPT_CALLBACK_F(0, "separator", &separator,
673
			N_("<paragraph-break>"),
674
			N_("insert <paragraph-break> between paragraphs"),
675
			PARSE_OPT_OPTARG, parse_separator_arg),
676
		OPT_BOOL(0, "stripspace", &d.stripspace,
677
			N_("remove unnecessary whitespace")),
678
		OPT_END()
679
	};
680
	int edit = !strcmp(argv[0], "edit");
681

682
	usage = edit ? git_notes_edit_usage : git_notes_append_usage;
683
	argc = parse_options(argc, argv, prefix, options, usage,
684
			     PARSE_OPT_KEEP_ARGV0);
685

686
	if (2 < argc) {
687
		error(_("too many arguments"));
688
		usage_with_options(usage, options);
689
	}
690

691
	if (d.msg_nr) {
692
		concat_messages(&d);
693
		if (edit)
694
			fprintf(stderr, _("The -m/-F/-c/-C options have been "
695
				"deprecated for the 'edit' subcommand.\n"
696
				"Please use 'git notes add -f -m/-F/-c/-C' "
697
				"instead.\n"));
698
	}
699

700
	object_ref = 1 < argc ? argv[1] : "HEAD";
701

702
	if (repo_get_oid(the_repository, object_ref, &object))
703
		die(_("failed to resolve '%s' as a valid ref."), object_ref);
704

705
	t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
706
	note = get_note(t, &object);
707

708
	prepare_note_data(&object, &d, edit && note ? note : NULL);
709

710
	if (note && !edit) {
711
		/* Append buf to previous note contents */
712
		unsigned long size;
713
		enum object_type type;
714
		struct strbuf buf = STRBUF_INIT;
715
		char *prev_buf = repo_read_object_file(the_repository, note, &type, &size);
716

717
		if (!prev_buf)
718
			die(_("unable to read %s"), oid_to_hex(note));
719
		if (size)
720
			strbuf_add(&buf, prev_buf, size);
721
		if (d.buf.len && size)
722
			append_separator(&buf);
723
		strbuf_insert(&d.buf, 0, buf.buf, buf.len);
724

725
		free(prev_buf);
726
		strbuf_release(&buf);
727
	}
728

729
	if (d.buf.len || allow_empty) {
730
		write_note_data(&d, &new_note);
731
		if (add_note(t, &object, &new_note, combine_notes_overwrite))
732
			BUG("combine_notes_overwrite failed");
733
		logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
734
	} else {
735
		fprintf(stderr, _("Removing note for object %s\n"),
736
			oid_to_hex(&object));
737
		remove_note(t, object.hash);
738
		logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
739
	}
740
	commit_notes(the_repository, t, logmsg);
741

742
	free(logmsg);
743
	free_note_data(&d);
744
	free_notes(t);
745
	return 0;
746
}
747

748
static int show(int argc, const char **argv, const char *prefix)
749
{
750
	const char *object_ref;
751
	struct notes_tree *t;
752
	struct object_id object;
753
	const struct object_id *note;
754
	int retval;
755
	struct option options[] = {
756
		OPT_END()
757
	};
758

759
	argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
760
			     0);
761

762
	if (1 < argc) {
763
		error(_("too many arguments"));
764
		usage_with_options(git_notes_show_usage, options);
765
	}
766

767
	object_ref = argc ? argv[0] : "HEAD";
768

769
	if (repo_get_oid(the_repository, object_ref, &object))
770
		die(_("failed to resolve '%s' as a valid ref."), object_ref);
771

772
	t = init_notes_check("show", 0);
773
	note = get_note(t, &object);
774

775
	if (!note)
776
		retval = error(_("no note found for object %s."),
777
			       oid_to_hex(&object));
778
	else {
779
		const char *show_args[3] = {"show", oid_to_hex(note), NULL};
780
		retval = execv_git_cmd(show_args);
781
	}
782
	free_notes(t);
783
	return retval;
784
}
785

786
static int merge_abort(struct notes_merge_options *o)
787
{
788
	int ret = 0;
789

790
	/*
791
	 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
792
	 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
793
	 */
794

795
	if (refs_delete_ref(get_main_ref_store(the_repository), NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
796
		ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
797
	if (refs_delete_ref(get_main_ref_store(the_repository), NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF))
798
		ret += error(_("failed to delete ref NOTES_MERGE_REF"));
799
	if (notes_merge_abort(o))
800
		ret += error(_("failed to remove 'git notes merge' worktree"));
801
	return ret;
802
}
803

804
static int merge_commit(struct notes_merge_options *o)
805
{
806
	struct strbuf msg = STRBUF_INIT;
807
	struct object_id oid, parent_oid;
808
	struct notes_tree t = {0};
809
	struct commit *partial;
810
	struct pretty_print_context pretty_ctx;
811
	void *local_ref_to_free;
812
	int ret;
813

814
	/*
815
	 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
816
	 * and target notes ref from .git/NOTES_MERGE_REF.
817
	 */
818

819
	if (repo_get_oid(the_repository, "NOTES_MERGE_PARTIAL", &oid))
820
		die(_("failed to read ref NOTES_MERGE_PARTIAL"));
821
	else if (!(partial = lookup_commit_reference(the_repository, &oid)))
822
		die(_("could not find commit from NOTES_MERGE_PARTIAL."));
823
	else if (repo_parse_commit(the_repository, partial))
824
		die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
825

826
	if (partial->parents)
827
		oidcpy(&parent_oid, &partial->parents->item->object.oid);
828
	else
829
		oidclr(&parent_oid, the_repository->hash_algo);
830

831
	init_notes(&t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
832

833
	o->local_ref = local_ref_to_free =
834
		refs_resolve_refdup(get_main_ref_store(the_repository),
835
				    "NOTES_MERGE_REF", 0, &oid, NULL);
836
	if (!o->local_ref)
837
		die(_("failed to resolve NOTES_MERGE_REF"));
838

839
	if (notes_merge_commit(o, &t, partial, &oid))
840
		die(_("failed to finalize notes merge"));
841

842
	/* Reuse existing commit message in reflog message */
843
	memset(&pretty_ctx, 0, sizeof(pretty_ctx));
844
	repo_format_commit_message(the_repository, partial, "%s", &msg,
845
				   &pretty_ctx);
846
	strbuf_trim(&msg);
847
	strbuf_insertstr(&msg, 0, "notes: ");
848
	refs_update_ref(get_main_ref_store(the_repository), msg.buf,
849
			o->local_ref, &oid,
850
			is_null_oid(&parent_oid) ? NULL : &parent_oid,
851
			0, UPDATE_REFS_DIE_ON_ERR);
852

853
	free_notes(&t);
854
	strbuf_release(&msg);
855
	ret = merge_abort(o);
856
	free(local_ref_to_free);
857
	return ret;
858
}
859

860
static int git_config_get_notes_strategy(const char *key,
861
					 enum notes_merge_strategy *strategy)
862
{
863
	char *value;
864

865
	if (git_config_get_string(key, &value))
866
		return 1;
867
	if (parse_notes_merge_strategy(value, strategy))
868
		git_die_config(the_repository, key, _("unknown notes merge strategy %s"), value);
869

870
	free(value);
871
	return 0;
872
}
873

874
static int merge(int argc, const char **argv, const char *prefix)
875
{
876
	struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
877
	struct object_id result_oid;
878
	struct notes_tree *t;
879
	struct notes_merge_options o;
880
	int do_merge = 0, do_commit = 0, do_abort = 0;
881
	int verbosity = 0, result;
882
	const char *strategy = NULL;
883
	struct option options[] = {
884
		OPT_GROUP(N_("General options")),
885
		OPT__VERBOSITY(&verbosity),
886
		OPT_GROUP(N_("Merge options")),
887
		OPT_STRING('s', "strategy", &strategy, N_("strategy"),
888
			   N_("resolve notes conflicts using the given strategy "
889
			      "(manual/ours/theirs/union/cat_sort_uniq)")),
890
		OPT_GROUP(N_("Committing unmerged notes")),
891
		OPT_SET_INT_F(0, "commit", &do_commit,
892
			      N_("finalize notes merge by committing unmerged notes"),
893
			      1, PARSE_OPT_NONEG),
894
		OPT_GROUP(N_("Aborting notes merge resolution")),
895
		OPT_SET_INT_F(0, "abort", &do_abort,
896
			      N_("abort notes merge"),
897
			      1, PARSE_OPT_NONEG),
898
		OPT_END()
899
	};
900

901
	argc = parse_options(argc, argv, prefix, options,
902
			     git_notes_merge_usage, 0);
903

904
	if (strategy || do_commit + do_abort == 0)
905
		do_merge = 1;
906
	if (do_merge + do_commit + do_abort != 1) {
907
		error(_("cannot mix --commit, --abort or -s/--strategy"));
908
		usage_with_options(git_notes_merge_usage, options);
909
	}
910

911
	if (do_merge && argc != 1) {
912
		error(_("must specify a notes ref to merge"));
913
		usage_with_options(git_notes_merge_usage, options);
914
	} else if (!do_merge && argc) {
915
		error(_("too many arguments"));
916
		usage_with_options(git_notes_merge_usage, options);
917
	}
918

919
	init_notes_merge_options(the_repository, &o);
920
	o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
921

922
	if (do_abort)
923
		return merge_abort(&o);
924
	if (do_commit)
925
		return merge_commit(&o);
926

927
	o.local_ref = default_notes_ref();
928
	strbuf_addstr(&remote_ref, argv[0]);
929
	expand_loose_notes_ref(&remote_ref);
930
	o.remote_ref = remote_ref.buf;
931

932
	t = init_notes_check("merge", NOTES_INIT_WRITABLE);
933

934
	if (strategy) {
935
		if (parse_notes_merge_strategy(strategy, &o.strategy)) {
936
			error(_("unknown -s/--strategy: %s"), strategy);
937
			usage_with_options(git_notes_merge_usage, options);
938
		}
939
	} else {
940
		struct strbuf merge_key = STRBUF_INIT;
941
		const char *short_ref = NULL;
942

943
		if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
944
			BUG("local ref %s is outside of refs/notes/",
945
			    o.local_ref);
946

947
		strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
948

949
		if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
950
			git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
951

952
		strbuf_release(&merge_key);
953
	}
954

955
	strbuf_addf(&msg, "notes: Merged notes from %s into %s",
956
		    remote_ref.buf, default_notes_ref());
957
	strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
958

959
	result = notes_merge(&o, t, &result_oid);
960

961
	if (result >= 0) /* Merge resulted (trivially) in result_oid */
962
		/* Update default notes ref with new commit */
963
		refs_update_ref(get_main_ref_store(the_repository), msg.buf,
964
				default_notes_ref(), &result_oid, NULL, 0,
965
				UPDATE_REFS_DIE_ON_ERR);
966
	else { /* Merge has unresolved conflicts */
967
		struct worktree **worktrees;
968
		const struct worktree *wt;
969
		/* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
970
		refs_update_ref(get_main_ref_store(the_repository), msg.buf,
971
				"NOTES_MERGE_PARTIAL", &result_oid, NULL,
972
				0, UPDATE_REFS_DIE_ON_ERR);
973
		/* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
974
		worktrees = get_worktrees();
975
		wt = find_shared_symref(worktrees, "NOTES_MERGE_REF",
976
					default_notes_ref());
977
		if (wt)
978
			die(_("a notes merge into %s is already in-progress at %s"),
979
			    default_notes_ref(), wt->path);
980
		free_worktrees(worktrees);
981
		if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", default_notes_ref(), NULL))
982
			die(_("failed to store link to current notes ref (%s)"),
983
			    default_notes_ref());
984
		fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
985
				  "and commit the result with 'git notes merge --commit', "
986
				  "or abort the merge with 'git notes merge --abort'.\n"),
987
			git_path(NOTES_MERGE_WORKTREE));
988
	}
989

990
	free_notes(t);
991
	strbuf_release(&remote_ref);
992
	strbuf_release(&msg);
993
	return result < 0; /* return non-zero on conflicts */
994
}
995

996
#define IGNORE_MISSING 1
997

998
static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
999
{
1000
	int status;
1001
	struct object_id oid;
1002
	if (repo_get_oid(the_repository, name, &oid))
1003
		return error(_("Failed to resolve '%s' as a valid ref."), name);
1004
	status = remove_note(t, oid.hash);
1005
	if (status)
1006
		fprintf(stderr, _("Object %s has no note\n"), name);
1007
	else
1008
		fprintf(stderr, _("Removing note for object %s\n"), name);
1009
	return (flag & IGNORE_MISSING) ? 0 : status;
1010
}
1011

1012
static int remove_cmd(int argc, const char **argv, const char *prefix)
1013
{
1014
	unsigned flag = 0;
1015
	int from_stdin = 0;
1016
	struct option options[] = {
1017
		OPT_BIT(0, "ignore-missing", &flag,
1018
			N_("attempt to remove non-existent note is not an error"),
1019
			IGNORE_MISSING),
1020
		OPT_BOOL(0, "stdin", &from_stdin,
1021
			    N_("read object names from the standard input")),
1022
		OPT_END()
1023
	};
1024
	struct notes_tree *t;
1025
	int retval = 0;
1026

1027
	argc = parse_options(argc, argv, prefix, options,
1028
			     git_notes_remove_usage, 0);
1029

1030
	t = init_notes_check("remove", NOTES_INIT_WRITABLE);
1031

1032
	if (!argc && !from_stdin) {
1033
		retval = remove_one_note(t, "HEAD", flag);
1034
	} else {
1035
		while (*argv) {
1036
			retval |= remove_one_note(t, *argv, flag);
1037
			argv++;
1038
		}
1039
	}
1040
	if (from_stdin) {
1041
		struct strbuf sb = STRBUF_INIT;
1042
		while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1043
			strbuf_rtrim(&sb);
1044
			retval |= remove_one_note(t, sb.buf, flag);
1045
		}
1046
		strbuf_release(&sb);
1047
	}
1048
	if (!retval)
1049
		commit_notes(the_repository, t,
1050
			     "Notes removed by 'git notes remove'");
1051
	free_notes(t);
1052
	return retval;
1053
}
1054

1055
static int prune(int argc, const char **argv, const char *prefix)
1056
{
1057
	struct notes_tree *t;
1058
	int show_only = 0, verbose = 0;
1059
	struct option options[] = {
1060
		OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
1061
		OPT__VERBOSE(&verbose, N_("report pruned notes")),
1062
		OPT_END()
1063
	};
1064

1065
	argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
1066
			     0);
1067

1068
	if (argc) {
1069
		error(_("too many arguments"));
1070
		usage_with_options(git_notes_prune_usage, options);
1071
	}
1072

1073
	t = init_notes_check("prune", NOTES_INIT_WRITABLE);
1074

1075
	prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
1076
		(show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
1077
	if (!show_only)
1078
		commit_notes(the_repository, t,
1079
			     "Notes removed by 'git notes prune'");
1080
	free_notes(t);
1081
	return 0;
1082
}
1083

1084
static int get_ref(int argc, const char **argv, const char *prefix)
1085
{
1086
	struct option options[] = { OPT_END() };
1087
	argc = parse_options(argc, argv, prefix, options,
1088
			     git_notes_get_ref_usage, 0);
1089

1090
	if (argc) {
1091
		error(_("too many arguments"));
1092
		usage_with_options(git_notes_get_ref_usage, options);
1093
	}
1094

1095
	puts(default_notes_ref());
1096
	return 0;
1097
}
1098

1099
int cmd_notes(int argc, const char **argv, const char *prefix)
1100
{
1101
	const char *override_notes_ref = NULL;
1102
	parse_opt_subcommand_fn *fn = NULL;
1103
	struct option options[] = {
1104
		OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
1105
			   N_("use notes from <notes-ref>")),
1106
		OPT_SUBCOMMAND("list", &fn, list),
1107
		OPT_SUBCOMMAND("add", &fn, add),
1108
		OPT_SUBCOMMAND("copy", &fn, copy),
1109
		OPT_SUBCOMMAND("append", &fn, append_edit),
1110
		OPT_SUBCOMMAND("edit", &fn, append_edit),
1111
		OPT_SUBCOMMAND("show", &fn, show),
1112
		OPT_SUBCOMMAND("merge", &fn, merge),
1113
		OPT_SUBCOMMAND("remove", &fn, remove_cmd),
1114
		OPT_SUBCOMMAND("prune", &fn, prune),
1115
		OPT_SUBCOMMAND("get-ref", &fn, get_ref),
1116
		OPT_END()
1117
	};
1118

1119
	git_config(git_default_config, NULL);
1120
	argc = parse_options(argc, argv, prefix, options, git_notes_usage,
1121
			     PARSE_OPT_SUBCOMMAND_OPTIONAL);
1122
	if (!fn) {
1123
		if (argc) {
1124
			error(_("unknown subcommand: `%s'"), argv[0]);
1125
			usage_with_options(git_notes_usage, options);
1126
		}
1127
		fn = list;
1128
	}
1129

1130
	if (override_notes_ref) {
1131
		struct strbuf sb = STRBUF_INIT;
1132
		strbuf_addstr(&sb, override_notes_ref);
1133
		expand_notes_ref(&sb);
1134
		setenv("GIT_NOTES_REF", sb.buf, 1);
1135
		strbuf_release(&sb);
1136
	}
1137

1138
	return !!fn(argc, argv, prefix);
1139
}
1140

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

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

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

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