blob: 52aa9af74be8d47780d4762185c5cbe09624b382 [file] [log] [blame]
Johan Herlandcd067d32010-02-13 22:28:20 +01001/*
2 * Builtin "git notes"
3 *
4 * Copyright (c) 2010 Johan Herland <johan@herland.net>
5 *
6 * Based on git-notes.sh by Johannes Schindelin,
Phil Hord09b7e222013-06-18 13:44:58 -04007 * and builtin/tag.c by Kristian Høgsberg and Carlos Rica.
Johan Herlandcd067d32010-02-13 22:28:20 +01008 */
9
10#include "cache.h"
11#include "builtin.h"
12#include "notes.h"
13#include "blob.h"
14#include "commit.h"
15#include "refs.h"
16#include "exec_cmd.h"
17#include "run-command.h"
18#include "parse-options.h"
Thomas Rast6956f852010-03-12 18:04:32 +010019#include "string-list.h"
Johan Herland75ef3f42010-11-09 22:49:46 +010020#include "notes-merge.h"
Johan Herland49c24702013-06-12 02:13:00 +020021#include "notes-utils.h"
Michael Rappazzoac6c5612015-10-02 07:55:31 -040022#include "worktree.h"
Junio C Hamanof50fee42012-09-15 13:56:07 -070023
Johan Herlandcd067d32010-02-13 22:28:20 +010024static const char * const git_notes_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070025 N_("git notes [--ref <notes-ref>] [list [<object>]]"),
26 N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
27 N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
28 N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
29 N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
30 N_("git notes [--ref <notes-ref>] show [<object>]"),
31 N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070032 N_("git notes merge --commit [-v | -q]"),
33 N_("git notes merge --abort [-v | -q]"),
Alex Henrie9c9b4f22015-01-13 00:44:47 -070034 N_("git notes [--ref <notes-ref>] remove [<object>...]"),
35 N_("git notes [--ref <notes-ref>] prune [-n | -v]"),
36 N_("git notes [--ref <notes-ref>] get-ref"),
Stephen Boyd74884b52010-02-27 00:59:22 -080037 NULL
38};
39
40static const char * const git_notes_list_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070041 N_("git notes [list [<object>]]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080042 NULL
43};
44
45static const char * const git_notes_add_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070046 N_("git notes add [<options>] [<object>]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080047 NULL
48};
49
50static const char * const git_notes_copy_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070051 N_("git notes copy [<options>] <from-object> <to-object>"),
52 N_("git notes copy --stdin [<from-object> <to-object>]..."),
Stephen Boyd74884b52010-02-27 00:59:22 -080053 NULL
54};
55
56static const char * const git_notes_append_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070057 N_("git notes append [<options>] [<object>]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080058 NULL
59};
60
61static const char * const git_notes_edit_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070062 N_("git notes edit [<object>]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080063 NULL
64};
65
66static const char * const git_notes_show_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070067 N_("git notes show [<object>]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080068 NULL
69};
70
Johan Herland75ef3f42010-11-09 22:49:46 +010071static const char * const git_notes_merge_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070072 N_("git notes merge [<options>] <notes-ref>"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070073 N_("git notes merge --commit [<options>]"),
74 N_("git notes merge --abort [<options>]"),
Johan Herland75ef3f42010-11-09 22:49:46 +010075 NULL
76};
77
Stephen Boyd74884b52010-02-27 00:59:22 -080078static const char * const git_notes_remove_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070079 N_("git notes remove [<object>]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080080 NULL
81};
82
83static const char * const git_notes_prune_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070084 N_("git notes prune [<options>]"),
Johan Herlandcd067d32010-02-13 22:28:20 +010085 NULL
86};
87
Johan Herland618cd752010-11-09 22:49:57 +010088static const char * const git_notes_get_ref_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070089 N_("git notes get-ref"),
Johan Herland618cd752010-11-09 22:49:57 +010090 NULL
91};
92
Johan Herlandcd067d32010-02-13 22:28:20 +010093static const char note_template[] =
Junio C Hamanoeff80a92013-01-16 20:18:48 +010094 "\nWrite/edit the notes for the following object:\n";
Johan Herlandcd067d32010-02-13 22:28:20 +010095
Johan Herlandbebf5c02014-11-09 13:30:49 +010096struct note_data {
Johan Herland348f1992010-02-13 22:28:35 +010097 int given;
Johan Herland0691cff2010-02-13 22:28:36 +010098 int use_editor;
Johan Herland4282af02014-11-09 13:30:50 +010099 char *edit_path;
Johan Herland348f1992010-02-13 22:28:35 +0100100 struct strbuf buf;
101};
102
Johan Herland4282af02014-11-09 13:30:50 +0100103static void free_note_data(struct note_data *d)
104{
105 if (d->edit_path) {
106 unlink_or_warn(d->edit_path);
107 free(d->edit_path);
108 }
109 strbuf_release(&d->buf);
110}
111
Johan Herlande3974212010-02-13 22:28:30 +0100112static int list_each_note(const unsigned char *object_sha1,
113 const unsigned char *note_sha1, char *note_path,
114 void *cb_data)
115{
116 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
117 return 0;
118}
119
Johan Herlandbebf5c02014-11-09 13:30:49 +0100120static void copy_obj_to_fd(int fd, const unsigned char *sha1)
Johan Herlandcd067d32010-02-13 22:28:20 +0100121{
122 unsigned long size;
123 enum object_type type;
124 char *buf = read_sha1_file(sha1, &type, &size);
125 if (buf) {
126 if (size)
127 write_or_die(fd, buf, size);
128 free(buf);
129 }
130}
131
132static void write_commented_object(int fd, const unsigned char *object)
133{
134 const char *show_args[5] =
135 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
René Scharfed3180272014-08-19 21:09:35 +0200136 struct child_process show = CHILD_PROCESS_INIT;
Johan Herlandcd067d32010-02-13 22:28:20 +0100137 struct strbuf buf = STRBUF_INIT;
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100138 struct strbuf cbuf = STRBUF_INIT;
Johan Herlandcd067d32010-02-13 22:28:20 +0100139
140 /* Invoke "git show --stat --no-notes $object" */
Johan Herlandcd067d32010-02-13 22:28:20 +0100141 show.argv = show_args;
142 show.no_stdin = 1;
143 show.out = -1;
144 show.err = 0;
145 show.git_cmd = 1;
146 if (start_command(&show))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000147 die(_("unable to start 'show' for object '%s'"),
Johan Herlandcd067d32010-02-13 22:28:20 +0100148 sha1_to_hex(object));
149
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100150 if (strbuf_read(&buf, show.out, 0) < 0)
151 die_errno(_("could not read 'show' output"));
152 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
153 write_or_die(fd, cbuf.buf, cbuf.len);
Johan Herlandcd067d32010-02-13 22:28:20 +0100154
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100155 strbuf_release(&cbuf);
Johan Herlandcd067d32010-02-13 22:28:20 +0100156 strbuf_release(&buf);
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100157
Johan Herlandcd067d32010-02-13 22:28:20 +0100158 if (finish_command(&show))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000159 die(_("failed to finish 'show' for object '%s'"),
Johan Herlandcd067d32010-02-13 22:28:20 +0100160 sha1_to_hex(object));
161}
162
Johan Herland52694cd2014-11-12 01:40:13 +0100163static void prepare_note_data(const unsigned char *object, struct note_data *d,
164 const unsigned char *old_note)
Johan Herlandcd067d32010-02-13 22:28:20 +0100165{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100166 if (d->use_editor || !d->given) {
Johan Herlandcd067d32010-02-13 22:28:20 +0100167 int fd;
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100168 struct strbuf buf = STRBUF_INIT;
Johan Herlandcd067d32010-02-13 22:28:20 +0100169
170 /* write the template message before editing: */
Johan Herland4282af02014-11-09 13:30:50 +0100171 d->edit_path = git_pathdup("NOTES_EDITMSG");
172 fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
Johan Herlandcd067d32010-02-13 22:28:20 +0100173 if (fd < 0)
Johan Herland4282af02014-11-09 13:30:50 +0100174 die_errno(_("could not create file '%s'"), d->edit_path);
Johan Herlandcd067d32010-02-13 22:28:20 +0100175
Johan Herlandbebf5c02014-11-09 13:30:49 +0100176 if (d->given)
177 write_or_die(fd, d->buf.buf, d->buf.len);
Johan Herland52694cd2014-11-12 01:40:13 +0100178 else if (old_note)
179 copy_obj_to_fd(fd, old_note);
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100180
181 strbuf_addch(&buf, '\n');
182 strbuf_add_commented_lines(&buf, note_template, strlen(note_template));
183 strbuf_addch(&buf, '\n');
184 write_or_die(fd, buf.buf, buf.len);
Johan Herlandcd067d32010-02-13 22:28:20 +0100185
186 write_commented_object(fd, object);
187
188 close(fd);
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100189 strbuf_release(&buf);
Johan Herlandbebf5c02014-11-09 13:30:49 +0100190 strbuf_reset(&d->buf);
Johan Herlandcd067d32010-02-13 22:28:20 +0100191
Johan Herland4282af02014-11-09 13:30:50 +0100192 if (launch_editor(d->edit_path, &d->buf, NULL)) {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100193 die(_("Please supply the note contents using either -m or -F option"));
Johan Herlandcd067d32010-02-13 22:28:20 +0100194 }
Tobias Klauser63af4a82015-10-16 17:16:42 +0200195 strbuf_stripspace(&d->buf, 1);
Johan Herlandcd067d32010-02-13 22:28:20 +0100196 }
Johan Herland52694cd2014-11-12 01:40:13 +0100197}
Johan Herlandcd067d32010-02-13 22:28:20 +0100198
Johan Herland52694cd2014-11-12 01:40:13 +0100199static void write_note_data(struct note_data *d, unsigned char *sha1)
200{
201 if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) {
202 error(_("unable to write note object"));
203 if (d->edit_path)
204 error(_("The note contents have been left in %s"),
205 d->edit_path);
206 exit(128);
Johan Herlandcd067d32010-02-13 22:28:20 +0100207 }
Johan Herlandcd067d32010-02-13 22:28:20 +0100208}
209
Johan Herlandcd067d32010-02-13 22:28:20 +0100210static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
211{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100212 struct note_data *d = opt->value;
Johan Herlandcd067d32010-02-13 22:28:20 +0100213
Johan Herlandbebf5c02014-11-09 13:30:49 +0100214 strbuf_grow(&d->buf, strlen(arg) + 2);
215 if (d->buf.len)
216 strbuf_addch(&d->buf, '\n');
217 strbuf_addstr(&d->buf, arg);
Tobias Klauser63af4a82015-10-16 17:16:42 +0200218 strbuf_stripspace(&d->buf, 0);
Johan Herland348f1992010-02-13 22:28:35 +0100219
Johan Herlandbebf5c02014-11-09 13:30:49 +0100220 d->given = 1;
Johan Herland348f1992010-02-13 22:28:35 +0100221 return 0;
222}
223
224static int parse_file_arg(const struct option *opt, const char *arg, int unset)
225{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100226 struct note_data *d = opt->value;
Johan Herland348f1992010-02-13 22:28:35 +0100227
Johan Herlandbebf5c02014-11-09 13:30:49 +0100228 if (d->buf.len)
229 strbuf_addch(&d->buf, '\n');
Johan Herland348f1992010-02-13 22:28:35 +0100230 if (!strcmp(arg, "-")) {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100231 if (strbuf_read(&d->buf, 0, 1024) < 0)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000232 die_errno(_("cannot read '%s'"), arg);
Johan Herlandbebf5c02014-11-09 13:30:49 +0100233 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000234 die_errno(_("could not open or read '%s'"), arg);
Tobias Klauser63af4a82015-10-16 17:16:42 +0200235 strbuf_stripspace(&d->buf, 0);
Johan Herland348f1992010-02-13 22:28:35 +0100236
Johan Herlandbebf5c02014-11-09 13:30:49 +0100237 d->given = 1;
Johan Herlandcd067d32010-02-13 22:28:20 +0100238 return 0;
239}
240
Johan Herland0691cff2010-02-13 22:28:36 +0100241static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
242{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100243 struct note_data *d = opt->value;
Johan Herland0691cff2010-02-13 22:28:36 +0100244 char *buf;
245 unsigned char object[20];
246 enum object_type type;
247 unsigned long len;
248
Johan Herlandbebf5c02014-11-09 13:30:49 +0100249 if (d->buf.len)
250 strbuf_addch(&d->buf, '\n');
Johan Herland0691cff2010-02-13 22:28:36 +0100251
252 if (get_sha1(arg, object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000253 die(_("Failed to resolve '%s' as a valid ref."), arg);
Johan Herland511726e2014-11-09 13:30:47 +0100254 if (!(buf = read_sha1_file(object, &type, &len))) {
Johan Herland0691cff2010-02-13 22:28:36 +0100255 free(buf);
Johan Herlandce8daa12014-02-12 10:54:16 +0100256 die(_("Failed to read object '%s'."), arg);
257 }
258 if (type != OBJ_BLOB) {
259 free(buf);
260 die(_("Cannot read note data from non-blob object '%s'."), arg);
Johan Herland0691cff2010-02-13 22:28:36 +0100261 }
Johan Herlandbebf5c02014-11-09 13:30:49 +0100262 strbuf_add(&d->buf, buf, len);
Johan Herland0691cff2010-02-13 22:28:36 +0100263 free(buf);
264
Johan Herlandbebf5c02014-11-09 13:30:49 +0100265 d->given = 1;
Johan Herland0691cff2010-02-13 22:28:36 +0100266 return 0;
267}
268
269static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
270{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100271 struct note_data *d = opt->value;
272 d->use_editor = 1;
Johan Herland0691cff2010-02-13 22:28:36 +0100273 return parse_reuse_arg(opt, arg, unset);
274}
275
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700276static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
Thomas Rast160baa02010-03-12 18:04:31 +0100277{
278 struct strbuf buf = STRBUF_INIT;
Thomas Rast6956f852010-03-12 18:04:32 +0100279 struct notes_rewrite_cfg *c = NULL;
Ævar Arnfjörð Bjarmasonef7a8e32010-06-14 23:40:05 +0000280 struct notes_tree *t = NULL;
Thomas Rast160baa02010-03-12 18:04:31 +0100281 int ret = 0;
Johan Herland80a14662013-06-12 02:12:59 +0200282 const char *msg = "Notes added by 'git notes copy'";
Thomas Rast160baa02010-03-12 18:04:31 +0100283
Thomas Rast6956f852010-03-12 18:04:32 +0100284 if (rewrite_cmd) {
285 c = init_copy_notes_for_rewrite(rewrite_cmd);
286 if (!c)
287 return 0;
288 } else {
289 init_notes(NULL, NULL, NULL, 0);
290 t = &default_notes_tree;
291 }
Thomas Rast160baa02010-03-12 18:04:31 +0100292
293 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
294 unsigned char from_obj[20], to_obj[20];
295 struct strbuf **split;
296 int err;
297
298 split = strbuf_split(&buf, ' ');
299 if (!split[0] || !split[1])
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000300 die(_("Malformed input line: '%s'."), buf.buf);
Thomas Rast160baa02010-03-12 18:04:31 +0100301 strbuf_rtrim(split[0]);
302 strbuf_rtrim(split[1]);
303 if (get_sha1(split[0]->buf, from_obj))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000304 die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
Thomas Rast160baa02010-03-12 18:04:31 +0100305 if (get_sha1(split[1]->buf, to_obj))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000306 die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
Thomas Rast160baa02010-03-12 18:04:31 +0100307
Thomas Rast6956f852010-03-12 18:04:32 +0100308 if (rewrite_cmd)
309 err = copy_note_for_rewrite(c, from_obj, to_obj);
310 else
311 err = copy_note(t, from_obj, to_obj, force,
312 combine_notes_overwrite);
Thomas Rast160baa02010-03-12 18:04:31 +0100313
314 if (err) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000315 error(_("Failed to copy notes from '%s' to '%s'"),
Thomas Rast160baa02010-03-12 18:04:31 +0100316 split[0]->buf, split[1]->buf);
317 ret = 1;
318 }
319
320 strbuf_list_free(split);
321 }
322
Thomas Rast6956f852010-03-12 18:04:32 +0100323 if (!rewrite_cmd) {
Johan Herland80a14662013-06-12 02:12:59 +0200324 commit_notes(t, msg);
Thomas Rast6956f852010-03-12 18:04:32 +0100325 free_notes(t);
326 } else {
Johan Herland80a14662013-06-12 02:12:59 +0200327 finish_copy_notes_for_rewrite(c, msg);
Thomas Rast6956f852010-03-12 18:04:32 +0100328 }
Thomas Rast160baa02010-03-12 18:04:31 +0100329 return ret;
330}
331
Stephen Boyd74884b52010-02-27 00:59:22 -0800332static struct notes_tree *init_notes_check(const char *subcommand)
Johan Herlandcd067d32010-02-13 22:28:20 +0100333{
Johan Herlandcd067d32010-02-13 22:28:20 +0100334 struct notes_tree *t;
Stephen Boyd74884b52010-02-27 00:59:22 -0800335 init_notes(NULL, NULL, NULL, 0);
336 t = &default_notes_tree;
Johan Herlanda0b4dfa2010-02-13 22:28:24 +0100337
Christian Couder59556542013-11-30 21:55:40 +0100338 if (!starts_with(t->ref, "refs/notes/"))
Stephen Boyd74884b52010-02-27 00:59:22 -0800339 die("Refusing to %s notes in %s (outside of refs/notes/)",
340 subcommand, t->ref);
341 return t;
342}
343
344static int list(int argc, const char **argv, const char *prefix)
345{
346 struct notes_tree *t;
347 unsigned char object[20];
348 const unsigned char *note;
349 int retval = -1;
Johan Herlandcd067d32010-02-13 22:28:20 +0100350 struct option options[] = {
Stephen Boyd74884b52010-02-27 00:59:22 -0800351 OPT_END()
352 };
353
354 if (argc)
355 argc = parse_options(argc, argv, prefix, options,
356 git_notes_list_usage, 0);
357
358 if (1 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000359 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800360 usage_with_options(git_notes_list_usage, options);
361 }
362
363 t = init_notes_check("list");
364 if (argc) {
365 if (get_sha1(argv[0], object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000366 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
Stephen Boyd74884b52010-02-27 00:59:22 -0800367 note = get_note(t, object);
368 if (note) {
369 puts(sha1_to_hex(note));
370 retval = 0;
371 } else
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000372 retval = error(_("No note found for object %s."),
Stephen Boyd74884b52010-02-27 00:59:22 -0800373 sha1_to_hex(object));
374 } else
375 retval = for_each_note(t, 0, list_each_note, NULL);
376
377 free_notes(t);
378 return retval;
379}
380
Johan Herland84a7e352011-03-30 02:02:55 +0200381static int append_edit(int argc, const char **argv, const char *prefix);
382
Stephen Boyd74884b52010-02-27 00:59:22 -0800383static int add(int argc, const char **argv, const char *prefix)
384{
Johan Herlandd73a5b92014-11-12 01:40:14 +0100385 int force = 0, allow_empty = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800386 const char *object_ref;
387 struct notes_tree *t;
388 unsigned char object[20], new_note[20];
Stephen Boyd74884b52010-02-27 00:59:22 -0800389 const unsigned char *note;
Johan Herland4282af02014-11-09 13:30:50 +0100390 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
Stephen Boyd74884b52010-02-27 00:59:22 -0800391 struct option options[] = {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100392 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700393 N_("note contents as a string"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100394 parse_msg_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100395 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700396 N_("note contents in a file"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100397 parse_file_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100398 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700399 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100400 parse_reedit_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100401 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700402 N_("reuse specified note object"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100403 parse_reuse_arg},
Johan Herlandd73a5b92014-11-12 01:40:14 +0100404 OPT_BOOL(0, "allow-empty", &allow_empty,
405 N_("allow storing empty note")),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700406 OPT__FORCE(&force, N_("replace existing notes")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800407 OPT_END()
408 };
409
410 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
Johan Herland84a7e352011-03-30 02:02:55 +0200411 PARSE_OPT_KEEP_ARGV0);
Stephen Boyd74884b52010-02-27 00:59:22 -0800412
Johan Herland84a7e352011-03-30 02:02:55 +0200413 if (2 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000414 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800415 usage_with_options(git_notes_add_usage, options);
416 }
417
Johan Herland84a7e352011-03-30 02:02:55 +0200418 object_ref = argc > 1 ? argv[1] : "HEAD";
Stephen Boyd74884b52010-02-27 00:59:22 -0800419
420 if (get_sha1(object_ref, object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000421 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800422
423 t = init_notes_check("add");
424 note = get_note(t, object);
425
426 if (note) {
427 if (!force) {
Johan Herlandb0de56c2014-11-12 01:40:12 +0100428 free_notes(t);
429 if (d.given) {
Johan Herland4282af02014-11-09 13:30:50 +0100430 free_note_data(&d);
Johan Herlandb0de56c2014-11-12 01:40:12 +0100431 return error(_("Cannot add notes. "
432 "Found existing notes for object %s. "
433 "Use '-f' to overwrite existing notes"),
434 sha1_to_hex(object));
Johan Herland84a7e352011-03-30 02:02:55 +0200435 }
Johan Herlandb0de56c2014-11-12 01:40:12 +0100436 /*
437 * Redirect to "edit" subcommand.
438 *
439 * We only end up here if none of -m/-F/-c/-C or -f are
440 * given. The original args are therefore still in
441 * argv[0-1].
442 */
443 argv[0] = "edit";
444 return append_edit(argc, argv, prefix);
Stephen Boyd74884b52010-02-27 00:59:22 -0800445 }
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000446 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800447 sha1_to_hex(object));
448 }
449
Johan Herland52694cd2014-11-12 01:40:13 +0100450 prepare_note_data(object, &d, note);
Johan Herlandd73a5b92014-11-12 01:40:14 +0100451 if (d.buf.len || allow_empty) {
Johan Herland52694cd2014-11-12 01:40:13 +0100452 write_note_data(&d, new_note);
453 if (add_note(t, object, new_note, combine_notes_overwrite))
454 die("BUG: combine_notes_overwrite failed");
455 commit_notes(t, "Notes added by 'git notes add'");
456 } else {
457 fprintf(stderr, _("Removing note for object %s\n"),
458 sha1_to_hex(object));
Stephen Boyd74884b52010-02-27 00:59:22 -0800459 remove_note(t, object);
Johan Herland52694cd2014-11-12 01:40:13 +0100460 commit_notes(t, "Notes removed by 'git notes add'");
461 }
Stephen Boyd74884b52010-02-27 00:59:22 -0800462
Johan Herland52694cd2014-11-12 01:40:13 +0100463 free_note_data(&d);
Stephen Boyd74884b52010-02-27 00:59:22 -0800464 free_notes(t);
Johan Herlandb0de56c2014-11-12 01:40:12 +0100465 return 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800466}
467
468static int copy(int argc, const char **argv, const char *prefix)
469{
470 int retval = 0, force = 0, from_stdin = 0;
471 const unsigned char *from_note, *note;
472 const char *object_ref;
473 unsigned char object[20], from_obj[20];
474 struct notes_tree *t;
475 const char *rewrite_cmd = NULL;
476 struct option options[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700477 OPT__FORCE(&force, N_("replace existing notes")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200478 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700479 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
480 N_("load rewriting config for <command> (implies "
481 "--stdin)")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800482 OPT_END()
483 };
484
485 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
486 0);
487
488 if (from_stdin || rewrite_cmd) {
489 if (argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000490 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800491 usage_with_options(git_notes_copy_usage, options);
492 } else {
493 return notes_copy_from_stdin(force, rewrite_cmd);
494 }
495 }
496
Jeff Kingbbb1b8a2010-06-28 04:59:07 -0400497 if (argc < 2) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000498 error(_("too few parameters"));
Jeff Kingbbb1b8a2010-06-28 04:59:07 -0400499 usage_with_options(git_notes_copy_usage, options);
500 }
Stephen Boyd74884b52010-02-27 00:59:22 -0800501 if (2 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000502 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800503 usage_with_options(git_notes_copy_usage, options);
504 }
505
506 if (get_sha1(argv[0], from_obj))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000507 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
Stephen Boyd74884b52010-02-27 00:59:22 -0800508
509 object_ref = 1 < argc ? argv[1] : "HEAD";
510
511 if (get_sha1(object_ref, object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000512 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800513
514 t = init_notes_check("copy");
515 note = get_note(t, object);
516
517 if (note) {
518 if (!force) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000519 retval = error(_("Cannot copy notes. Found existing "
Stephen Boyd74884b52010-02-27 00:59:22 -0800520 "notes for object %s. Use '-f' to "
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000521 "overwrite existing notes"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800522 sha1_to_hex(object));
523 goto out;
524 }
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000525 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800526 sha1_to_hex(object));
527 }
528
529 from_note = get_note(t, from_obj);
530 if (!from_note) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000531 retval = error(_("Missing notes on source object %s. Cannot "
532 "copy."), sha1_to_hex(from_obj));
Stephen Boyd74884b52010-02-27 00:59:22 -0800533 goto out;
534 }
535
Johan Herland180619a2010-11-15 00:52:26 +0100536 if (add_note(t, object, from_note, combine_notes_overwrite))
537 die("BUG: combine_notes_overwrite failed");
Stephen Boyd74884b52010-02-27 00:59:22 -0800538 commit_notes(t, "Notes added by 'git notes copy'");
539out:
540 free_notes(t);
541 return retval;
542}
543
544static int append_edit(int argc, const char **argv, const char *prefix)
545{
Johan Herlandd73a5b92014-11-12 01:40:14 +0100546 int allow_empty = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800547 const char *object_ref;
548 struct notes_tree *t;
549 unsigned char object[20], new_note[20];
550 const unsigned char *note;
551 char logmsg[100];
552 const char * const *usage;
Johan Herland4282af02014-11-09 13:30:50 +0100553 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
Stephen Boyd74884b52010-02-27 00:59:22 -0800554 struct option options[] = {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100555 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700556 N_("note contents as a string"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800557 parse_msg_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100558 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700559 N_("note contents in a file"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800560 parse_file_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100561 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700562 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800563 parse_reedit_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100564 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700565 N_("reuse specified note object"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800566 parse_reuse_arg},
Johan Herlandd73a5b92014-11-12 01:40:14 +0100567 OPT_BOOL(0, "allow-empty", &allow_empty,
568 N_("allow storing empty note")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800569 OPT_END()
570 };
571 int edit = !strcmp(argv[0], "edit");
572
573 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
574 argc = parse_options(argc, argv, prefix, options, usage,
575 PARSE_OPT_KEEP_ARGV0);
576
577 if (2 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000578 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800579 usage_with_options(usage, options);
580 }
581
Johan Herlandbebf5c02014-11-09 13:30:49 +0100582 if (d.given && edit)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000583 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
Stephen Boyd74884b52010-02-27 00:59:22 -0800584 "for the 'edit' subcommand.\n"
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000585 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800586
587 object_ref = 1 < argc ? argv[1] : "HEAD";
588
589 if (get_sha1(object_ref, object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000590 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800591
592 t = init_notes_check(argv[0]);
593 note = get_note(t, object);
594
Johan Herland52694cd2014-11-12 01:40:13 +0100595 prepare_note_data(object, &d, edit ? note : NULL);
Stephen Boyd74884b52010-02-27 00:59:22 -0800596
Johan Herland52694cd2014-11-12 01:40:13 +0100597 if (note && !edit) {
598 /* Append buf to previous note contents */
599 unsigned long size;
600 enum object_type type;
601 char *prev_buf = read_sha1_file(note, &type, &size);
602
603 strbuf_grow(&d.buf, size + 1);
604 if (d.buf.len && prev_buf && size)
605 strbuf_insert(&d.buf, 0, "\n", 1);
606 if (prev_buf && size)
607 strbuf_insert(&d.buf, 0, prev_buf, size);
608 free(prev_buf);
609 }
610
Johan Herlandd73a5b92014-11-12 01:40:14 +0100611 if (d.buf.len || allow_empty) {
Johan Herland52694cd2014-11-12 01:40:13 +0100612 write_note_data(&d, new_note);
613 if (add_note(t, object, new_note, combine_notes_overwrite))
614 die("BUG: combine_notes_overwrite failed");
615 snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'",
616 argv[0]);
617 } else {
618 fprintf(stderr, _("Removing note for object %s\n"),
619 sha1_to_hex(object));
Stephen Boyd74884b52010-02-27 00:59:22 -0800620 remove_note(t, object);
Johan Herland52694cd2014-11-12 01:40:13 +0100621 snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'",
622 argv[0]);
623 }
Stephen Boyd74884b52010-02-27 00:59:22 -0800624 commit_notes(t, logmsg);
Johan Herland52694cd2014-11-12 01:40:13 +0100625
626 free_note_data(&d);
Stephen Boyd74884b52010-02-27 00:59:22 -0800627 free_notes(t);
Stephen Boyd74884b52010-02-27 00:59:22 -0800628 return 0;
629}
630
631static int show(int argc, const char **argv, const char *prefix)
632{
633 const char *object_ref;
634 struct notes_tree *t;
635 unsigned char object[20];
636 const unsigned char *note;
637 int retval;
638 struct option options[] = {
639 OPT_END()
640 };
641
642 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
643 0);
644
645 if (1 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000646 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800647 usage_with_options(git_notes_show_usage, options);
648 }
649
650 object_ref = argc ? argv[0] : "HEAD";
651
652 if (get_sha1(object_ref, object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000653 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800654
655 t = init_notes_check("show");
656 note = get_note(t, object);
657
658 if (!note)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000659 retval = error(_("No note found for object %s."),
Stephen Boyd74884b52010-02-27 00:59:22 -0800660 sha1_to_hex(object));
661 else {
662 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
663 retval = execv_git_cmd(show_args);
664 }
665 free_notes(t);
666 return retval;
667}
668
Johan Herland6abb3652010-11-09 22:49:52 +0100669static int merge_abort(struct notes_merge_options *o)
670{
671 int ret = 0;
672
673 /*
674 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
675 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
676 */
677
678 if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
679 ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
680 if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
681 ret += error("Failed to delete ref NOTES_MERGE_REF");
682 if (notes_merge_abort(o))
683 ret += error("Failed to remove 'git notes merge' worktree");
684 return ret;
685}
686
687static int merge_commit(struct notes_merge_options *o)
688{
689 struct strbuf msg = STRBUF_INIT;
Johan Herland6cfd6a92010-11-09 22:49:54 +0100690 unsigned char sha1[20], parent_sha1[20];
Johan Herland6abb3652010-11-09 22:49:52 +0100691 struct notes_tree *t;
692 struct commit *partial;
693 struct pretty_print_context pretty_ctx;
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700694 void *local_ref_to_free;
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +0700695 int ret;
Johan Herland6abb3652010-11-09 22:49:52 +0100696
697 /*
698 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
699 * and target notes ref from .git/NOTES_MERGE_REF.
700 */
701
702 if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
703 die("Failed to read ref NOTES_MERGE_PARTIAL");
704 else if (!(partial = lookup_commit_reference(sha1)))
705 die("Could not find commit from NOTES_MERGE_PARTIAL.");
706 else if (parse_commit(partial))
707 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
708
Johan Herland6cfd6a92010-11-09 22:49:54 +0100709 if (partial->parents)
brian m. carlsoned1c9972015-11-10 02:22:29 +0000710 hashcpy(parent_sha1, partial->parents->item->object.oid.hash);
Johan Herland6cfd6a92010-11-09 22:49:54 +0100711 else
712 hashclr(parent_sha1);
713
Johan Herland6abb3652010-11-09 22:49:52 +0100714 t = xcalloc(1, sizeof(struct notes_tree));
715 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
716
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700717 o->local_ref = local_ref_to_free =
Ronnie Sahlberg7695d112014-07-15 12:59:36 -0700718 resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL);
Johan Herland6abb3652010-11-09 22:49:52 +0100719 if (!o->local_ref)
720 die("Failed to resolve NOTES_MERGE_REF");
721
722 if (notes_merge_commit(o, t, partial, sha1))
723 die("Failed to finalize notes merge");
724
725 /* Reuse existing commit message in reflog message */
726 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
727 format_commit_message(partial, "%s", &msg, &pretty_ctx);
728 strbuf_trim(&msg);
729 strbuf_insert(&msg, 0, "notes: ", 7);
Johan Herland6cfd6a92010-11-09 22:49:54 +0100730 update_ref(msg.buf, o->local_ref, sha1,
731 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
Michael Haggertyf4124112014-04-07 15:47:56 +0200732 0, UPDATE_REFS_DIE_ON_ERR);
Johan Herland6abb3652010-11-09 22:49:52 +0100733
734 free_notes(t);
735 strbuf_release(&msg);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +0700736 ret = merge_abort(o);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700737 free(local_ref_to_free);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +0700738 return ret;
Johan Herland6abb3652010-11-09 22:49:52 +0100739}
740
Jacob Kellerd2d68d92015-08-17 14:33:33 -0700741static int git_config_get_notes_strategy(const char *key,
742 enum notes_merge_strategy *strategy)
743{
744 const char *value;
745
746 if (git_config_get_string_const(key, &value))
747 return 1;
748 if (parse_notes_merge_strategy(value, strategy))
749 git_die_config(key, "unknown notes merge strategy %s", value);
750
751 return 0;
752}
753
Johan Herland75ef3f42010-11-09 22:49:46 +0100754static int merge(int argc, const char **argv, const char *prefix)
755{
756 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
757 unsigned char result_sha1[20];
Johan Herland2085b162010-11-15 00:54:11 +0100758 struct notes_tree *t;
Johan Herland75ef3f42010-11-09 22:49:46 +0100759 struct notes_merge_options o;
Johan Herland6abb3652010-11-09 22:49:52 +0100760 int do_merge = 0, do_commit = 0, do_abort = 0;
Johan Herland75ef3f42010-11-09 22:49:46 +0100761 int verbosity = 0, result;
Johan Herland3228e672010-11-15 00:55:12 +0100762 const char *strategy = NULL;
Johan Herland75ef3f42010-11-09 22:49:46 +0100763 struct option options[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700764 OPT_GROUP(N_("General options")),
Johan Herland75ef3f42010-11-09 22:49:46 +0100765 OPT__VERBOSITY(&verbosity),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700766 OPT_GROUP(N_("Merge options")),
767 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
768 N_("resolve notes conflicts using the given strategy "
769 "(manual/ours/theirs/union/cat_sort_uniq)")),
770 OPT_GROUP(N_("Committing unmerged notes")),
Stefan Beller4741edd2013-08-03 13:51:18 +0200771 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700772 N_("finalize notes merge by committing unmerged notes"),
Stefan Beller4741edd2013-08-03 13:51:18 +0200773 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700774 OPT_GROUP(N_("Aborting notes merge resolution")),
Stefan Beller4741edd2013-08-03 13:51:18 +0200775 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700776 N_("abort notes merge"),
Stefan Beller4741edd2013-08-03 13:51:18 +0200777 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
Johan Herland75ef3f42010-11-09 22:49:46 +0100778 OPT_END()
779 };
780
781 argc = parse_options(argc, argv, prefix, options,
782 git_notes_merge_usage, 0);
783
Johan Herland6abb3652010-11-09 22:49:52 +0100784 if (strategy || do_commit + do_abort == 0)
785 do_merge = 1;
786 if (do_merge + do_commit + do_abort != 1) {
787 error("cannot mix --commit, --abort or -s/--strategy");
788 usage_with_options(git_notes_merge_usage, options);
789 }
790
791 if (do_merge && argc != 1) {
Johan Herland75ef3f42010-11-09 22:49:46 +0100792 error("Must specify a notes ref to merge");
793 usage_with_options(git_notes_merge_usage, options);
Johan Herland6abb3652010-11-09 22:49:52 +0100794 } else if (!do_merge && argc) {
795 error("too many parameters");
796 usage_with_options(git_notes_merge_usage, options);
Johan Herland75ef3f42010-11-09 22:49:46 +0100797 }
798
799 init_notes_merge_options(&o);
800 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
801
Johan Herland6abb3652010-11-09 22:49:52 +0100802 if (do_abort)
803 return merge_abort(&o);
804 if (do_commit)
805 return merge_commit(&o);
806
Johan Herland75ef3f42010-11-09 22:49:46 +0100807 o.local_ref = default_notes_ref();
808 strbuf_addstr(&remote_ref, argv[0]);
809 expand_notes_ref(&remote_ref);
810 o.remote_ref = remote_ref.buf;
811
Jacob Kellerd2d68d92015-08-17 14:33:33 -0700812 t = init_notes_check("merge");
813
Johan Herland3228e672010-11-15 00:55:12 +0100814 if (strategy) {
Jacob Keller93efcad2015-08-17 14:33:31 -0700815 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
Johan Herland3228e672010-11-15 00:55:12 +0100816 error("Unknown -s/--strategy: %s", strategy);
817 usage_with_options(git_notes_merge_usage, options);
818 }
Jacob Kellerd2d68d92015-08-17 14:33:33 -0700819 } else {
Jacob Keller4f655e22015-08-17 14:33:34 -0700820 struct strbuf merge_key = STRBUF_INIT;
821 const char *short_ref = NULL;
Johan Herland3228e672010-11-15 00:55:12 +0100822
Jacob Keller4f655e22015-08-17 14:33:34 -0700823 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
824 die("BUG: local ref %s is outside of refs/notes/",
825 o.local_ref);
826
827 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
828
829 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
830 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
831
832 strbuf_release(&merge_key);
Johan Herland3228e672010-11-15 00:55:12 +0100833 }
Johan Herland75ef3f42010-11-09 22:49:46 +0100834
835 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
836 remote_ref.buf, default_notes_ref());
Johan Herland443259c2010-11-09 22:49:53 +0100837 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
Johan Herland2085b162010-11-15 00:54:11 +0100838
839 result = notes_merge(&o, t, result_sha1);
840
841 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
Johan Herland75ef3f42010-11-09 22:49:46 +0100842 /* Update default notes ref with new commit */
843 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
Michael Haggertyf4124112014-04-07 15:47:56 +0200844 0, UPDATE_REFS_DIE_ON_ERR);
Johan Herland6abb3652010-11-09 22:49:52 +0100845 else { /* Merge has unresolved conflicts */
David Turnerb02e8592015-08-10 13:52:45 -0400846 char *existing;
Johan Herland6abb3652010-11-09 22:49:52 +0100847 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
848 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
Michael Haggertyf4124112014-04-07 15:47:56 +0200849 0, UPDATE_REFS_DIE_ON_ERR);
Johan Herland6abb3652010-11-09 22:49:52 +0100850 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
David Turnerb02e8592015-08-10 13:52:45 -0400851 existing = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
852 if (existing)
853 die(_("A notes merge into %s is already in-progress at %s"),
854 default_notes_ref(), existing);
Johan Herland6abb3652010-11-09 22:49:52 +0100855 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
856 die("Failed to store link to current notes ref (%s)",
857 default_notes_ref());
858 printf("Automatic notes merge failed. Fix conflicts in %s and "
859 "commit the result with 'git notes merge --commit', or "
860 "abort the merge with 'git notes merge --abort'.\n",
Johan Herland809f38c2010-11-09 22:49:51 +0100861 git_path(NOTES_MERGE_WORKTREE));
Johan Herland6abb3652010-11-09 22:49:52 +0100862 }
Johan Herland75ef3f42010-11-09 22:49:46 +0100863
Johan Herland2085b162010-11-15 00:54:11 +0100864 free_notes(t);
Johan Herland75ef3f42010-11-09 22:49:46 +0100865 strbuf_release(&remote_ref);
866 strbuf_release(&msg);
Johan Herland809f38c2010-11-09 22:49:51 +0100867 return result < 0; /* return non-zero on conflicts */
Johan Herland75ef3f42010-11-09 22:49:46 +0100868}
869
Junio C Hamano46538012011-05-18 16:44:30 -0700870#define IGNORE_MISSING 1
Junio C Hamano2d370d22011-05-18 16:02:58 -0700871
872static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700873{
874 int status;
875 unsigned char sha1[20];
876 if (get_sha1(name, sha1))
877 return error(_("Failed to resolve '%s' as a valid ref."), name);
878 status = remove_note(t, sha1);
879 if (status)
880 fprintf(stderr, _("Object %s has no note\n"), name);
881 else
882 fprintf(stderr, _("Removing note for object %s\n"), name);
Junio C Hamano46538012011-05-18 16:44:30 -0700883 return (flag & IGNORE_MISSING) ? 0 : status;
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700884}
885
Stephen Boyd74884b52010-02-27 00:59:22 -0800886static int remove_cmd(int argc, const char **argv, const char *prefix)
887{
Junio C Hamano2d370d22011-05-18 16:02:58 -0700888 unsigned flag = 0;
Junio C Hamano46538012011-05-18 16:44:30 -0700889 int from_stdin = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800890 struct option options[] = {
Junio C Hamano2d370d22011-05-18 16:02:58 -0700891 OPT_BIT(0, "ignore-missing", &flag,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700892 N_("attempt to remove non-existent note is not an error"),
Junio C Hamano46538012011-05-18 16:44:30 -0700893 IGNORE_MISSING),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200894 OPT_BOOL(0, "stdin", &from_stdin,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700895 N_("read object names from the standard input")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800896 OPT_END()
897 };
Stephen Boyd74884b52010-02-27 00:59:22 -0800898 struct notes_tree *t;
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700899 int retval = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800900
901 argc = parse_options(argc, argv, prefix, options,
902 git_notes_remove_usage, 0);
903
Stephen Boyd74884b52010-02-27 00:59:22 -0800904 t = init_notes_check("remove");
905
Junio C Hamano46538012011-05-18 16:44:30 -0700906 if (!argc && !from_stdin) {
Junio C Hamano2d370d22011-05-18 16:02:58 -0700907 retval = remove_one_note(t, "HEAD", flag);
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700908 } else {
909 while (*argv) {
Junio C Hamano2d370d22011-05-18 16:02:58 -0700910 retval |= remove_one_note(t, *argv, flag);
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700911 argv++;
912 }
Johan Herland1ee1e432010-08-31 17:56:50 +0200913 }
Junio C Hamano46538012011-05-18 16:44:30 -0700914 if (from_stdin) {
915 struct strbuf sb = STRBUF_INIT;
916 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
917 strbuf_rtrim(&sb);
918 retval |= remove_one_note(t, sb.buf, flag);
919 }
920 strbuf_release(&sb);
921 }
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700922 if (!retval)
923 commit_notes(t, "Notes removed by 'git notes remove'");
Stephen Boyd74884b52010-02-27 00:59:22 -0800924 free_notes(t);
Johan Herland1ee1e432010-08-31 17:56:50 +0200925 return retval;
Stephen Boyd74884b52010-02-27 00:59:22 -0800926}
927
928static int prune(int argc, const char **argv, const char *prefix)
929{
930 struct notes_tree *t;
Michael J Grubera9f2adf2010-05-14 23:42:07 +0200931 int show_only = 0, verbose = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800932 struct option options[] = {
René Scharfee21adb82010-11-08 18:58:51 +0100933 OPT__DRY_RUN(&show_only, "do not remove, show only"),
René Scharfefd038812010-11-08 18:56:39 +0100934 OPT__VERBOSE(&verbose, "report pruned notes"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800935 OPT_END()
936 };
937
938 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
939 0);
940
941 if (argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000942 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800943 usage_with_options(git_notes_prune_usage, options);
944 }
945
946 t = init_notes_check("prune");
947
Michael J Grubera9f2adf2010-05-14 23:42:07 +0200948 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
949 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
950 if (!show_only)
951 commit_notes(t, "Notes removed by 'git notes prune'");
Stephen Boyd74884b52010-02-27 00:59:22 -0800952 free_notes(t);
953 return 0;
954}
955
Johan Herland618cd752010-11-09 22:49:57 +0100956static int get_ref(int argc, const char **argv, const char *prefix)
957{
958 struct option options[] = { OPT_END() };
959 argc = parse_options(argc, argv, prefix, options,
960 git_notes_get_ref_usage, 0);
961
962 if (argc) {
963 error("too many parameters");
964 usage_with_options(git_notes_get_ref_usage, options);
965 }
966
967 puts(default_notes_ref());
968 return 0;
969}
970
Stephen Boyd74884b52010-02-27 00:59:22 -0800971int cmd_notes(int argc, const char **argv, const char *prefix)
972{
973 int result;
974 const char *override_notes_ref = NULL;
975 struct option options[] = {
Junio C Hamanoe703d712014-03-23 15:58:12 -0700976 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700977 N_("use notes from <notes-ref>")),
Johan Herlandcd067d32010-02-13 22:28:20 +0100978 OPT_END()
979 };
980
981 git_config(git_default_config, NULL);
Stephen Boyd74884b52010-02-27 00:59:22 -0800982 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
983 PARSE_OPT_STOP_AT_NON_OPTION);
Johan Herlandcd067d32010-02-13 22:28:20 +0100984
Thomas Rastdcf783a2010-03-12 18:04:35 +0100985 if (override_notes_ref) {
986 struct strbuf sb = STRBUF_INIT;
Thomas Rastdcf783a2010-03-12 18:04:35 +0100987 strbuf_addstr(&sb, override_notes_ref);
Johan Herland8ef313e2010-11-09 22:49:45 +0100988 expand_notes_ref(&sb);
Thomas Rastdcf783a2010-03-12 18:04:35 +0100989 setenv("GIT_NOTES_REF", sb.buf, 1);
990 strbuf_release(&sb);
991 }
992
Stephen Boyd74884b52010-02-27 00:59:22 -0800993 if (argc < 1 || !strcmp(argv[0], "list"))
994 result = list(argc, argv, prefix);
995 else if (!strcmp(argv[0], "add"))
996 result = add(argc, argv, prefix);
997 else if (!strcmp(argv[0], "copy"))
998 result = copy(argc, argv, prefix);
999 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1000 result = append_edit(argc, argv, prefix);
1001 else if (!strcmp(argv[0], "show"))
1002 result = show(argc, argv, prefix);
Johan Herland75ef3f42010-11-09 22:49:46 +01001003 else if (!strcmp(argv[0], "merge"))
1004 result = merge(argc, argv, prefix);
Stephen Boyd74884b52010-02-27 00:59:22 -08001005 else if (!strcmp(argv[0], "remove"))
1006 result = remove_cmd(argc, argv, prefix);
1007 else if (!strcmp(argv[0], "prune"))
1008 result = prune(argc, argv, prefix);
Johan Herland618cd752010-11-09 22:49:57 +01001009 else if (!strcmp(argv[0], "get-ref"))
1010 result = get_ref(argc, argv, prefix);
Stephen Boyd74884b52010-02-27 00:59:22 -08001011 else {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +00001012 result = error(_("Unknown subcommand: %s"), argv[0]);
Johan Herland92b33852010-02-13 22:28:25 +01001013 usage_with_options(git_notes_usage, options);
1014 }
1015
Stephen Boyd74884b52010-02-27 00:59:22 -08001016 return result ? 1 : 0;
Johan Herlandcd067d32010-02-13 22:28:20 +01001017}