blob: 101747243a081a18687a60a5071c6ca208362e4c [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"
Junio C Hamanof50fee42012-09-15 13:56:07 -070022
Johan Herlandcd067d32010-02-13 22:28:20 +010023static const char * const git_notes_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070024 N_("git notes [--ref <notes_ref>] [list [<object>]]"),
25 N_("git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
26 N_("git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>"),
27 N_("git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
28 N_("git notes [--ref <notes_ref>] edit [<object>]"),
29 N_("git notes [--ref <notes_ref>] show [<object>]"),
30 N_("git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>"),
31 N_("git notes merge --commit [-v | -q]"),
32 N_("git notes merge --abort [-v | -q]"),
33 N_("git notes [--ref <notes_ref>] remove [<object>...]"),
34 N_("git notes [--ref <notes_ref>] prune [-n | -v]"),
35 N_("git notes [--ref <notes_ref>] get-ref"),
Stephen Boyd74884b52010-02-27 00:59:22 -080036 NULL
37};
38
39static const char * const git_notes_list_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070040 N_("git notes [list [<object>]]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080041 NULL
42};
43
44static const char * const git_notes_add_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070045 N_("git notes add [<options>] [<object>]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080046 NULL
47};
48
49static const char * const git_notes_copy_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070050 N_("git notes copy [<options>] <from-object> <to-object>"),
51 N_("git notes copy --stdin [<from-object> <to-object>]..."),
Stephen Boyd74884b52010-02-27 00:59:22 -080052 NULL
53};
54
55static const char * const git_notes_append_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070056 N_("git notes append [<options>] [<object>]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080057 NULL
58};
59
60static const char * const git_notes_edit_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070061 N_("git notes edit [<object>]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080062 NULL
63};
64
65static const char * const git_notes_show_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070066 N_("git notes show [<object>]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080067 NULL
68};
69
Johan Herland75ef3f42010-11-09 22:49:46 +010070static const char * const git_notes_merge_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070071 N_("git notes merge [<options>] <notes_ref>"),
72 N_("git notes merge --commit [<options>]"),
73 N_("git notes merge --abort [<options>]"),
Johan Herland75ef3f42010-11-09 22:49:46 +010074 NULL
75};
76
Stephen Boyd74884b52010-02-27 00:59:22 -080077static const char * const git_notes_remove_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070078 N_("git notes remove [<object>]"),
Stephen Boyd74884b52010-02-27 00:59:22 -080079 NULL
80};
81
82static const char * const git_notes_prune_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070083 N_("git notes prune [<options>]"),
Johan Herlandcd067d32010-02-13 22:28:20 +010084 NULL
85};
86
Johan Herland618cd752010-11-09 22:49:57 +010087static const char * const git_notes_get_ref_usage[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +070088 N_("git notes get-ref"),
Johan Herland618cd752010-11-09 22:49:57 +010089 NULL
90};
91
Johan Herlandcd067d32010-02-13 22:28:20 +010092static const char note_template[] =
Junio C Hamanoeff80a92013-01-16 20:18:48 +010093 "\nWrite/edit the notes for the following object:\n";
Johan Herlandcd067d32010-02-13 22:28:20 +010094
Johan Herlandbebf5c02014-11-09 13:30:49 +010095struct note_data {
Johan Herland348f1992010-02-13 22:28:35 +010096 int given;
Johan Herland0691cff2010-02-13 22:28:36 +010097 int use_editor;
Johan Herland4282af02014-11-09 13:30:50 +010098 char *edit_path;
Johan Herland348f1992010-02-13 22:28:35 +010099 struct strbuf buf;
100};
101
Johan Herland4282af02014-11-09 13:30:50 +0100102static void free_note_data(struct note_data *d)
103{
104 if (d->edit_path) {
105 unlink_or_warn(d->edit_path);
106 free(d->edit_path);
107 }
108 strbuf_release(&d->buf);
109}
110
Johan Herlande3974212010-02-13 22:28:30 +0100111static int list_each_note(const unsigned char *object_sha1,
112 const unsigned char *note_sha1, char *note_path,
113 void *cb_data)
114{
115 printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
116 return 0;
117}
118
Johan Herlandbebf5c02014-11-09 13:30:49 +0100119static void copy_obj_to_fd(int fd, const unsigned char *sha1)
Johan Herlandcd067d32010-02-13 22:28:20 +0100120{
121 unsigned long size;
122 enum object_type type;
123 char *buf = read_sha1_file(sha1, &type, &size);
124 if (buf) {
125 if (size)
126 write_or_die(fd, buf, size);
127 free(buf);
128 }
129}
130
131static void write_commented_object(int fd, const unsigned char *object)
132{
133 const char *show_args[5] =
134 {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
René Scharfed3180272014-08-19 21:09:35 +0200135 struct child_process show = CHILD_PROCESS_INIT;
Johan Herlandcd067d32010-02-13 22:28:20 +0100136 struct strbuf buf = STRBUF_INIT;
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100137 struct strbuf cbuf = STRBUF_INIT;
Johan Herlandcd067d32010-02-13 22:28:20 +0100138
139 /* Invoke "git show --stat --no-notes $object" */
Johan Herlandcd067d32010-02-13 22:28:20 +0100140 show.argv = show_args;
141 show.no_stdin = 1;
142 show.out = -1;
143 show.err = 0;
144 show.git_cmd = 1;
145 if (start_command(&show))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000146 die(_("unable to start 'show' for object '%s'"),
Johan Herlandcd067d32010-02-13 22:28:20 +0100147 sha1_to_hex(object));
148
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100149 if (strbuf_read(&buf, show.out, 0) < 0)
150 die_errno(_("could not read 'show' output"));
151 strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
152 write_or_die(fd, cbuf.buf, cbuf.len);
Johan Herlandcd067d32010-02-13 22:28:20 +0100153
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100154 strbuf_release(&cbuf);
Johan Herlandcd067d32010-02-13 22:28:20 +0100155 strbuf_release(&buf);
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100156
Johan Herlandcd067d32010-02-13 22:28:20 +0100157 if (finish_command(&show))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000158 die(_("failed to finish 'show' for object '%s'"),
Johan Herlandcd067d32010-02-13 22:28:20 +0100159 sha1_to_hex(object));
160}
161
Johan Herlandbebf5c02014-11-09 13:30:49 +0100162static void create_note(const unsigned char *object, struct note_data *d,
Johan Herland348f1992010-02-13 22:28:35 +0100163 int append_only, const unsigned char *prev,
Johan Herlandcd067d32010-02-13 22:28:20 +0100164 unsigned char *result)
165{
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 Herland0691cff2010-02-13 22:28:36 +0100178 else if (prev && !append_only)
Johan Herlandbebf5c02014-11-09 13:30:49 +0100179 copy_obj_to_fd(fd, prev);
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 }
Johan Herlandbebf5c02014-11-09 13:30:49 +0100195 stripspace(&d->buf, 1);
Johan Herlandcd067d32010-02-13 22:28:20 +0100196 }
197
Johan Herland2347fae2010-02-13 22:28:33 +0100198 if (prev && append_only) {
199 /* Append buf to previous note contents */
200 unsigned long size;
201 enum object_type type;
202 char *prev_buf = read_sha1_file(prev, &type, &size);
203
Johan Herlandbebf5c02014-11-09 13:30:49 +0100204 strbuf_grow(&d->buf, size + 1);
205 if (d->buf.len && prev_buf && size)
206 strbuf_insert(&d->buf, 0, "\n", 1);
Johan Herland2347fae2010-02-13 22:28:33 +0100207 if (prev_buf && size)
Johan Herlandbebf5c02014-11-09 13:30:49 +0100208 strbuf_insert(&d->buf, 0, prev_buf, size);
Johan Herland2347fae2010-02-13 22:28:33 +0100209 free(prev_buf);
210 }
211
Johan Herlandbebf5c02014-11-09 13:30:49 +0100212 if (!d->buf.len) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000213 fprintf(stderr, _("Removing note for object %s\n"),
Johan Herlandcd067d32010-02-13 22:28:20 +0100214 sha1_to_hex(object));
215 hashclr(result);
216 } else {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100217 if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, result)) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000218 error(_("unable to write note object"));
Johan Herland4282af02014-11-09 13:30:50 +0100219 if (d->edit_path)
Alex Henriead5fe372014-08-30 13:56:01 -0600220 error(_("The note contents have been left in %s"),
Johan Herland4282af02014-11-09 13:30:50 +0100221 d->edit_path);
Johan Herlandcd067d32010-02-13 22:28:20 +0100222 exit(128);
223 }
224 }
Johan Herlandcd067d32010-02-13 22:28:20 +0100225}
226
Johan Herlandcd067d32010-02-13 22:28:20 +0100227static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
228{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100229 struct note_data *d = opt->value;
Johan Herlandcd067d32010-02-13 22:28:20 +0100230
Johan Herlandbebf5c02014-11-09 13:30:49 +0100231 strbuf_grow(&d->buf, strlen(arg) + 2);
232 if (d->buf.len)
233 strbuf_addch(&d->buf, '\n');
234 strbuf_addstr(&d->buf, arg);
235 stripspace(&d->buf, 0);
Johan Herland348f1992010-02-13 22:28:35 +0100236
Johan Herlandbebf5c02014-11-09 13:30:49 +0100237 d->given = 1;
Johan Herland348f1992010-02-13 22:28:35 +0100238 return 0;
239}
240
241static int parse_file_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 Herland348f1992010-02-13 22:28:35 +0100244
Johan Herlandbebf5c02014-11-09 13:30:49 +0100245 if (d->buf.len)
246 strbuf_addch(&d->buf, '\n');
Johan Herland348f1992010-02-13 22:28:35 +0100247 if (!strcmp(arg, "-")) {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100248 if (strbuf_read(&d->buf, 0, 1024) < 0)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000249 die_errno(_("cannot read '%s'"), arg);
Johan Herlandbebf5c02014-11-09 13:30:49 +0100250 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000251 die_errno(_("could not open or read '%s'"), arg);
Johan Herlandbebf5c02014-11-09 13:30:49 +0100252 stripspace(&d->buf, 0);
Johan Herland348f1992010-02-13 22:28:35 +0100253
Johan Herlandbebf5c02014-11-09 13:30:49 +0100254 d->given = 1;
Johan Herlandcd067d32010-02-13 22:28:20 +0100255 return 0;
256}
257
Johan Herland0691cff2010-02-13 22:28:36 +0100258static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
259{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100260 struct note_data *d = opt->value;
Johan Herland0691cff2010-02-13 22:28:36 +0100261 char *buf;
262 unsigned char object[20];
263 enum object_type type;
264 unsigned long len;
265
Johan Herlandbebf5c02014-11-09 13:30:49 +0100266 if (d->buf.len)
267 strbuf_addch(&d->buf, '\n');
Johan Herland0691cff2010-02-13 22:28:36 +0100268
269 if (get_sha1(arg, object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000270 die(_("Failed to resolve '%s' as a valid ref."), arg);
Johan Herland511726e2014-11-09 13:30:47 +0100271 if (!(buf = read_sha1_file(object, &type, &len))) {
Johan Herland0691cff2010-02-13 22:28:36 +0100272 free(buf);
Johan Herlandce8daa12014-02-12 10:54:16 +0100273 die(_("Failed to read object '%s'."), arg);
274 }
275 if (type != OBJ_BLOB) {
276 free(buf);
277 die(_("Cannot read note data from non-blob object '%s'."), arg);
Johan Herland0691cff2010-02-13 22:28:36 +0100278 }
Johan Herlandbebf5c02014-11-09 13:30:49 +0100279 strbuf_add(&d->buf, buf, len);
Johan Herland0691cff2010-02-13 22:28:36 +0100280 free(buf);
281
Johan Herlandbebf5c02014-11-09 13:30:49 +0100282 d->given = 1;
Johan Herland0691cff2010-02-13 22:28:36 +0100283 return 0;
284}
285
286static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
287{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100288 struct note_data *d = opt->value;
289 d->use_editor = 1;
Johan Herland0691cff2010-02-13 22:28:36 +0100290 return parse_reuse_arg(opt, arg, unset);
291}
292
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700293static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
Thomas Rast160baa02010-03-12 18:04:31 +0100294{
295 struct strbuf buf = STRBUF_INIT;
Thomas Rast6956f852010-03-12 18:04:32 +0100296 struct notes_rewrite_cfg *c = NULL;
Ævar Arnfjörð Bjarmasonef7a8e32010-06-14 23:40:05 +0000297 struct notes_tree *t = NULL;
Thomas Rast160baa02010-03-12 18:04:31 +0100298 int ret = 0;
Johan Herland80a14662013-06-12 02:12:59 +0200299 const char *msg = "Notes added by 'git notes copy'";
Thomas Rast160baa02010-03-12 18:04:31 +0100300
Thomas Rast6956f852010-03-12 18:04:32 +0100301 if (rewrite_cmd) {
302 c = init_copy_notes_for_rewrite(rewrite_cmd);
303 if (!c)
304 return 0;
305 } else {
306 init_notes(NULL, NULL, NULL, 0);
307 t = &default_notes_tree;
308 }
Thomas Rast160baa02010-03-12 18:04:31 +0100309
310 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
311 unsigned char from_obj[20], to_obj[20];
312 struct strbuf **split;
313 int err;
314
315 split = strbuf_split(&buf, ' ');
316 if (!split[0] || !split[1])
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000317 die(_("Malformed input line: '%s'."), buf.buf);
Thomas Rast160baa02010-03-12 18:04:31 +0100318 strbuf_rtrim(split[0]);
319 strbuf_rtrim(split[1]);
320 if (get_sha1(split[0]->buf, from_obj))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000321 die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf);
Thomas Rast160baa02010-03-12 18:04:31 +0100322 if (get_sha1(split[1]->buf, to_obj))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000323 die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf);
Thomas Rast160baa02010-03-12 18:04:31 +0100324
Thomas Rast6956f852010-03-12 18:04:32 +0100325 if (rewrite_cmd)
326 err = copy_note_for_rewrite(c, from_obj, to_obj);
327 else
328 err = copy_note(t, from_obj, to_obj, force,
329 combine_notes_overwrite);
Thomas Rast160baa02010-03-12 18:04:31 +0100330
331 if (err) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000332 error(_("Failed to copy notes from '%s' to '%s'"),
Thomas Rast160baa02010-03-12 18:04:31 +0100333 split[0]->buf, split[1]->buf);
334 ret = 1;
335 }
336
337 strbuf_list_free(split);
338 }
339
Thomas Rast6956f852010-03-12 18:04:32 +0100340 if (!rewrite_cmd) {
Johan Herland80a14662013-06-12 02:12:59 +0200341 commit_notes(t, msg);
Thomas Rast6956f852010-03-12 18:04:32 +0100342 free_notes(t);
343 } else {
Johan Herland80a14662013-06-12 02:12:59 +0200344 finish_copy_notes_for_rewrite(c, msg);
Thomas Rast6956f852010-03-12 18:04:32 +0100345 }
Thomas Rast160baa02010-03-12 18:04:31 +0100346 return ret;
347}
348
Stephen Boyd74884b52010-02-27 00:59:22 -0800349static struct notes_tree *init_notes_check(const char *subcommand)
Johan Herlandcd067d32010-02-13 22:28:20 +0100350{
Johan Herlandcd067d32010-02-13 22:28:20 +0100351 struct notes_tree *t;
Stephen Boyd74884b52010-02-27 00:59:22 -0800352 init_notes(NULL, NULL, NULL, 0);
353 t = &default_notes_tree;
Johan Herlanda0b4dfa2010-02-13 22:28:24 +0100354
Christian Couder59556542013-11-30 21:55:40 +0100355 if (!starts_with(t->ref, "refs/notes/"))
Stephen Boyd74884b52010-02-27 00:59:22 -0800356 die("Refusing to %s notes in %s (outside of refs/notes/)",
357 subcommand, t->ref);
358 return t;
359}
360
361static int list(int argc, const char **argv, const char *prefix)
362{
363 struct notes_tree *t;
364 unsigned char object[20];
365 const unsigned char *note;
366 int retval = -1;
Johan Herlandcd067d32010-02-13 22:28:20 +0100367 struct option options[] = {
Stephen Boyd74884b52010-02-27 00:59:22 -0800368 OPT_END()
369 };
370
371 if (argc)
372 argc = parse_options(argc, argv, prefix, options,
373 git_notes_list_usage, 0);
374
375 if (1 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000376 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800377 usage_with_options(git_notes_list_usage, options);
378 }
379
380 t = init_notes_check("list");
381 if (argc) {
382 if (get_sha1(argv[0], object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000383 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
Stephen Boyd74884b52010-02-27 00:59:22 -0800384 note = get_note(t, object);
385 if (note) {
386 puts(sha1_to_hex(note));
387 retval = 0;
388 } else
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000389 retval = error(_("No note found for object %s."),
Stephen Boyd74884b52010-02-27 00:59:22 -0800390 sha1_to_hex(object));
391 } else
392 retval = for_each_note(t, 0, list_each_note, NULL);
393
394 free_notes(t);
395 return retval;
396}
397
Johan Herland84a7e352011-03-30 02:02:55 +0200398static int append_edit(int argc, const char **argv, const char *prefix);
399
Stephen Boyd74884b52010-02-27 00:59:22 -0800400static int add(int argc, const char **argv, const char *prefix)
401{
402 int retval = 0, force = 0;
403 const char *object_ref;
404 struct notes_tree *t;
405 unsigned char object[20], new_note[20];
406 char logmsg[100];
407 const unsigned char *note;
Johan Herland4282af02014-11-09 13:30:50 +0100408 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
Stephen Boyd74884b52010-02-27 00:59:22 -0800409 struct option options[] = {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100410 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700411 N_("note contents as a string"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100412 parse_msg_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100413 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700414 N_("note contents in a file"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100415 parse_file_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100416 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700417 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100418 parse_reedit_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100419 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700420 N_("reuse specified note object"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100421 parse_reuse_arg},
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700422 OPT__FORCE(&force, N_("replace existing notes")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800423 OPT_END()
424 };
425
426 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
Johan Herland84a7e352011-03-30 02:02:55 +0200427 PARSE_OPT_KEEP_ARGV0);
Stephen Boyd74884b52010-02-27 00:59:22 -0800428
Johan Herland84a7e352011-03-30 02:02:55 +0200429 if (2 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000430 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800431 usage_with_options(git_notes_add_usage, options);
432 }
433
Johan Herland84a7e352011-03-30 02:02:55 +0200434 object_ref = argc > 1 ? argv[1] : "HEAD";
Stephen Boyd74884b52010-02-27 00:59:22 -0800435
436 if (get_sha1(object_ref, object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000437 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800438
439 t = init_notes_check("add");
440 note = get_note(t, object);
441
442 if (note) {
443 if (!force) {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100444 if (!d.given) {
Johan Herland84a7e352011-03-30 02:02:55 +0200445 /*
446 * Redirect to "edit" subcommand.
447 *
448 * We only end up here if none of -m/-F/-c/-C
449 * or -f are given. The original args are
450 * therefore still in argv[0-1].
451 */
452 argv[0] = "edit";
Johan Herland4282af02014-11-09 13:30:50 +0100453 free_note_data(&d);
Johan Herland84a7e352011-03-30 02:02:55 +0200454 free_notes(t);
455 return append_edit(argc, argv, prefix);
456 }
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000457 retval = error(_("Cannot add notes. Found existing notes "
Stephen Boyd74884b52010-02-27 00:59:22 -0800458 "for object %s. Use '-f' to overwrite "
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000459 "existing notes"), sha1_to_hex(object));
Stephen Boyd74884b52010-02-27 00:59:22 -0800460 goto out;
461 }
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000462 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800463 sha1_to_hex(object));
464 }
465
Johan Herlandbebf5c02014-11-09 13:30:49 +0100466 create_note(object, &d, 0, note, new_note);
Johan Herland4282af02014-11-09 13:30:50 +0100467 free_note_data(&d);
Stephen Boyd74884b52010-02-27 00:59:22 -0800468
469 if (is_null_sha1(new_note))
470 remove_note(t, object);
Johan Herland180619a2010-11-15 00:52:26 +0100471 else if (add_note(t, object, new_note, combine_notes_overwrite))
472 die("BUG: combine_notes_overwrite failed");
Stephen Boyd74884b52010-02-27 00:59:22 -0800473
474 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
475 is_null_sha1(new_note) ? "removed" : "added", "add");
476 commit_notes(t, logmsg);
477out:
478 free_notes(t);
Stephen Boyd74884b52010-02-27 00:59:22 -0800479 return retval;
480}
481
482static int copy(int argc, const char **argv, const char *prefix)
483{
484 int retval = 0, force = 0, from_stdin = 0;
485 const unsigned char *from_note, *note;
486 const char *object_ref;
487 unsigned char object[20], from_obj[20];
488 struct notes_tree *t;
489 const char *rewrite_cmd = NULL;
490 struct option options[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700491 OPT__FORCE(&force, N_("replace existing notes")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200492 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700493 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
494 N_("load rewriting config for <command> (implies "
495 "--stdin)")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800496 OPT_END()
497 };
498
499 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
500 0);
501
502 if (from_stdin || rewrite_cmd) {
503 if (argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000504 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800505 usage_with_options(git_notes_copy_usage, options);
506 } else {
507 return notes_copy_from_stdin(force, rewrite_cmd);
508 }
509 }
510
Jeff Kingbbb1b8a2010-06-28 04:59:07 -0400511 if (argc < 2) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000512 error(_("too few parameters"));
Jeff Kingbbb1b8a2010-06-28 04:59:07 -0400513 usage_with_options(git_notes_copy_usage, options);
514 }
Stephen Boyd74884b52010-02-27 00:59:22 -0800515 if (2 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000516 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800517 usage_with_options(git_notes_copy_usage, options);
518 }
519
520 if (get_sha1(argv[0], from_obj))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000521 die(_("Failed to resolve '%s' as a valid ref."), argv[0]);
Stephen Boyd74884b52010-02-27 00:59:22 -0800522
523 object_ref = 1 < argc ? argv[1] : "HEAD";
524
525 if (get_sha1(object_ref, object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000526 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800527
528 t = init_notes_check("copy");
529 note = get_note(t, object);
530
531 if (note) {
532 if (!force) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000533 retval = error(_("Cannot copy notes. Found existing "
Stephen Boyd74884b52010-02-27 00:59:22 -0800534 "notes for object %s. Use '-f' to "
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000535 "overwrite existing notes"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800536 sha1_to_hex(object));
537 goto out;
538 }
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000539 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800540 sha1_to_hex(object));
541 }
542
543 from_note = get_note(t, from_obj);
544 if (!from_note) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000545 retval = error(_("Missing notes on source object %s. Cannot "
546 "copy."), sha1_to_hex(from_obj));
Stephen Boyd74884b52010-02-27 00:59:22 -0800547 goto out;
548 }
549
Johan Herland180619a2010-11-15 00:52:26 +0100550 if (add_note(t, object, from_note, combine_notes_overwrite))
551 die("BUG: combine_notes_overwrite failed");
Stephen Boyd74884b52010-02-27 00:59:22 -0800552 commit_notes(t, "Notes added by 'git notes copy'");
553out:
554 free_notes(t);
555 return retval;
556}
557
558static int append_edit(int argc, const char **argv, const char *prefix)
559{
560 const char *object_ref;
561 struct notes_tree *t;
562 unsigned char object[20], new_note[20];
563 const unsigned char *note;
564 char logmsg[100];
565 const char * const *usage;
Johan Herland4282af02014-11-09 13:30:50 +0100566 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
Stephen Boyd74884b52010-02-27 00:59:22 -0800567 struct option options[] = {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100568 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700569 N_("note contents as a string"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800570 parse_msg_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100571 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700572 N_("note contents in a file"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800573 parse_file_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100574 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700575 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800576 parse_reedit_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100577 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700578 N_("reuse specified note object"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800579 parse_reuse_arg},
580 OPT_END()
581 };
582 int edit = !strcmp(argv[0], "edit");
583
584 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
585 argc = parse_options(argc, argv, prefix, options, usage,
586 PARSE_OPT_KEEP_ARGV0);
587
588 if (2 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000589 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800590 usage_with_options(usage, options);
591 }
592
Johan Herlandbebf5c02014-11-09 13:30:49 +0100593 if (d.given && edit)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000594 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
Stephen Boyd74884b52010-02-27 00:59:22 -0800595 "for the 'edit' subcommand.\n"
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000596 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800597
598 object_ref = 1 < argc ? argv[1] : "HEAD";
599
600 if (get_sha1(object_ref, object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000601 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800602
603 t = init_notes_check(argv[0]);
604 note = get_note(t, object);
605
Johan Herlandbebf5c02014-11-09 13:30:49 +0100606 create_note(object, &d, !edit, note, new_note);
Johan Herland4282af02014-11-09 13:30:50 +0100607 free_note_data(&d);
Stephen Boyd74884b52010-02-27 00:59:22 -0800608
609 if (is_null_sha1(new_note))
610 remove_note(t, object);
Johan Herland180619a2010-11-15 00:52:26 +0100611 else if (add_note(t, object, new_note, combine_notes_overwrite))
612 die("BUG: combine_notes_overwrite failed");
Stephen Boyd74884b52010-02-27 00:59:22 -0800613
614 snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
615 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
616 commit_notes(t, logmsg);
617 free_notes(t);
Stephen Boyd74884b52010-02-27 00:59:22 -0800618 return 0;
619}
620
621static int show(int argc, const char **argv, const char *prefix)
622{
623 const char *object_ref;
624 struct notes_tree *t;
625 unsigned char object[20];
626 const unsigned char *note;
627 int retval;
628 struct option options[] = {
629 OPT_END()
630 };
631
632 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
633 0);
634
635 if (1 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000636 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800637 usage_with_options(git_notes_show_usage, options);
638 }
639
640 object_ref = argc ? argv[0] : "HEAD";
641
642 if (get_sha1(object_ref, object))
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000643 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800644
645 t = init_notes_check("show");
646 note = get_note(t, object);
647
648 if (!note)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000649 retval = error(_("No note found for object %s."),
Stephen Boyd74884b52010-02-27 00:59:22 -0800650 sha1_to_hex(object));
651 else {
652 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
653 retval = execv_git_cmd(show_args);
654 }
655 free_notes(t);
656 return retval;
657}
658
Johan Herland6abb3652010-11-09 22:49:52 +0100659static int merge_abort(struct notes_merge_options *o)
660{
661 int ret = 0;
662
663 /*
664 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
665 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
666 */
667
668 if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
669 ret += error("Failed to delete ref NOTES_MERGE_PARTIAL");
670 if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
671 ret += error("Failed to delete ref NOTES_MERGE_REF");
672 if (notes_merge_abort(o))
673 ret += error("Failed to remove 'git notes merge' worktree");
674 return ret;
675}
676
677static int merge_commit(struct notes_merge_options *o)
678{
679 struct strbuf msg = STRBUF_INIT;
Johan Herland6cfd6a92010-11-09 22:49:54 +0100680 unsigned char sha1[20], parent_sha1[20];
Johan Herland6abb3652010-11-09 22:49:52 +0100681 struct notes_tree *t;
682 struct commit *partial;
683 struct pretty_print_context pretty_ctx;
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700684 void *local_ref_to_free;
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +0700685 int ret;
Johan Herland6abb3652010-11-09 22:49:52 +0100686
687 /*
688 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
689 * and target notes ref from .git/NOTES_MERGE_REF.
690 */
691
692 if (get_sha1("NOTES_MERGE_PARTIAL", sha1))
693 die("Failed to read ref NOTES_MERGE_PARTIAL");
694 else if (!(partial = lookup_commit_reference(sha1)))
695 die("Could not find commit from NOTES_MERGE_PARTIAL.");
696 else if (parse_commit(partial))
697 die("Could not parse commit from NOTES_MERGE_PARTIAL.");
698
Johan Herland6cfd6a92010-11-09 22:49:54 +0100699 if (partial->parents)
700 hashcpy(parent_sha1, partial->parents->item->object.sha1);
701 else
702 hashclr(parent_sha1);
703
Johan Herland6abb3652010-11-09 22:49:52 +0100704 t = xcalloc(1, sizeof(struct notes_tree));
705 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
706
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700707 o->local_ref = local_ref_to_free =
Ronnie Sahlberg7695d112014-07-15 12:59:36 -0700708 resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL);
Johan Herland6abb3652010-11-09 22:49:52 +0100709 if (!o->local_ref)
710 die("Failed to resolve NOTES_MERGE_REF");
711
712 if (notes_merge_commit(o, t, partial, sha1))
713 die("Failed to finalize notes merge");
714
715 /* Reuse existing commit message in reflog message */
716 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
717 format_commit_message(partial, "%s", &msg, &pretty_ctx);
718 strbuf_trim(&msg);
719 strbuf_insert(&msg, 0, "notes: ", 7);
Johan Herland6cfd6a92010-11-09 22:49:54 +0100720 update_ref(msg.buf, o->local_ref, sha1,
721 is_null_sha1(parent_sha1) ? NULL : parent_sha1,
Michael Haggertyf4124112014-04-07 15:47:56 +0200722 0, UPDATE_REFS_DIE_ON_ERR);
Johan Herland6abb3652010-11-09 22:49:52 +0100723
724 free_notes(t);
725 strbuf_release(&msg);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +0700726 ret = merge_abort(o);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700727 free(local_ref_to_free);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +0700728 return ret;
Johan Herland6abb3652010-11-09 22:49:52 +0100729}
730
Johan Herland75ef3f42010-11-09 22:49:46 +0100731static int merge(int argc, const char **argv, const char *prefix)
732{
733 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
734 unsigned char result_sha1[20];
Johan Herland2085b162010-11-15 00:54:11 +0100735 struct notes_tree *t;
Johan Herland75ef3f42010-11-09 22:49:46 +0100736 struct notes_merge_options o;
Johan Herland6abb3652010-11-09 22:49:52 +0100737 int do_merge = 0, do_commit = 0, do_abort = 0;
Johan Herland75ef3f42010-11-09 22:49:46 +0100738 int verbosity = 0, result;
Johan Herland3228e672010-11-15 00:55:12 +0100739 const char *strategy = NULL;
Johan Herland75ef3f42010-11-09 22:49:46 +0100740 struct option options[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700741 OPT_GROUP(N_("General options")),
Johan Herland75ef3f42010-11-09 22:49:46 +0100742 OPT__VERBOSITY(&verbosity),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700743 OPT_GROUP(N_("Merge options")),
744 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
745 N_("resolve notes conflicts using the given strategy "
746 "(manual/ours/theirs/union/cat_sort_uniq)")),
747 OPT_GROUP(N_("Committing unmerged notes")),
Stefan Beller4741edd2013-08-03 13:51:18 +0200748 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700749 N_("finalize notes merge by committing unmerged notes"),
Stefan Beller4741edd2013-08-03 13:51:18 +0200750 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700751 OPT_GROUP(N_("Aborting notes merge resolution")),
Stefan Beller4741edd2013-08-03 13:51:18 +0200752 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700753 N_("abort notes merge"),
Stefan Beller4741edd2013-08-03 13:51:18 +0200754 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
Johan Herland75ef3f42010-11-09 22:49:46 +0100755 OPT_END()
756 };
757
758 argc = parse_options(argc, argv, prefix, options,
759 git_notes_merge_usage, 0);
760
Johan Herland6abb3652010-11-09 22:49:52 +0100761 if (strategy || do_commit + do_abort == 0)
762 do_merge = 1;
763 if (do_merge + do_commit + do_abort != 1) {
764 error("cannot mix --commit, --abort or -s/--strategy");
765 usage_with_options(git_notes_merge_usage, options);
766 }
767
768 if (do_merge && argc != 1) {
Johan Herland75ef3f42010-11-09 22:49:46 +0100769 error("Must specify a notes ref to merge");
770 usage_with_options(git_notes_merge_usage, options);
Johan Herland6abb3652010-11-09 22:49:52 +0100771 } else if (!do_merge && argc) {
772 error("too many parameters");
773 usage_with_options(git_notes_merge_usage, options);
Johan Herland75ef3f42010-11-09 22:49:46 +0100774 }
775
776 init_notes_merge_options(&o);
777 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
778
Johan Herland6abb3652010-11-09 22:49:52 +0100779 if (do_abort)
780 return merge_abort(&o);
781 if (do_commit)
782 return merge_commit(&o);
783
Johan Herland75ef3f42010-11-09 22:49:46 +0100784 o.local_ref = default_notes_ref();
785 strbuf_addstr(&remote_ref, argv[0]);
786 expand_notes_ref(&remote_ref);
787 o.remote_ref = remote_ref.buf;
788
Johan Herland3228e672010-11-15 00:55:12 +0100789 if (strategy) {
790 if (!strcmp(strategy, "manual"))
791 o.strategy = NOTES_MERGE_RESOLVE_MANUAL;
792 else if (!strcmp(strategy, "ours"))
793 o.strategy = NOTES_MERGE_RESOLVE_OURS;
794 else if (!strcmp(strategy, "theirs"))
795 o.strategy = NOTES_MERGE_RESOLVE_THEIRS;
796 else if (!strcmp(strategy, "union"))
797 o.strategy = NOTES_MERGE_RESOLVE_UNION;
Johan Herlanda6a09092010-11-15 00:57:17 +0100798 else if (!strcmp(strategy, "cat_sort_uniq"))
799 o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
Johan Herland3228e672010-11-15 00:55:12 +0100800 else {
801 error("Unknown -s/--strategy: %s", strategy);
802 usage_with_options(git_notes_merge_usage, options);
803 }
804 }
805
Johan Herland2085b162010-11-15 00:54:11 +0100806 t = init_notes_check("merge");
Johan Herland75ef3f42010-11-09 22:49:46 +0100807
808 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
809 remote_ref.buf, default_notes_ref());
Johan Herland443259c2010-11-09 22:49:53 +0100810 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
Johan Herland2085b162010-11-15 00:54:11 +0100811
812 result = notes_merge(&o, t, result_sha1);
813
814 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
Johan Herland75ef3f42010-11-09 22:49:46 +0100815 /* Update default notes ref with new commit */
816 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
Michael Haggertyf4124112014-04-07 15:47:56 +0200817 0, UPDATE_REFS_DIE_ON_ERR);
Johan Herland6abb3652010-11-09 22:49:52 +0100818 else { /* Merge has unresolved conflicts */
819 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
820 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
Michael Haggertyf4124112014-04-07 15:47:56 +0200821 0, UPDATE_REFS_DIE_ON_ERR);
Johan Herland6abb3652010-11-09 22:49:52 +0100822 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
823 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
824 die("Failed to store link to current notes ref (%s)",
825 default_notes_ref());
826 printf("Automatic notes merge failed. Fix conflicts in %s and "
827 "commit the result with 'git notes merge --commit', or "
828 "abort the merge with 'git notes merge --abort'.\n",
Johan Herland809f38c2010-11-09 22:49:51 +0100829 git_path(NOTES_MERGE_WORKTREE));
Johan Herland6abb3652010-11-09 22:49:52 +0100830 }
Johan Herland75ef3f42010-11-09 22:49:46 +0100831
Johan Herland2085b162010-11-15 00:54:11 +0100832 free_notes(t);
Johan Herland75ef3f42010-11-09 22:49:46 +0100833 strbuf_release(&remote_ref);
834 strbuf_release(&msg);
Johan Herland809f38c2010-11-09 22:49:51 +0100835 return result < 0; /* return non-zero on conflicts */
Johan Herland75ef3f42010-11-09 22:49:46 +0100836}
837
Junio C Hamano46538012011-05-18 16:44:30 -0700838#define IGNORE_MISSING 1
Junio C Hamano2d370d22011-05-18 16:02:58 -0700839
840static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700841{
842 int status;
843 unsigned char sha1[20];
844 if (get_sha1(name, sha1))
845 return error(_("Failed to resolve '%s' as a valid ref."), name);
846 status = remove_note(t, sha1);
847 if (status)
848 fprintf(stderr, _("Object %s has no note\n"), name);
849 else
850 fprintf(stderr, _("Removing note for object %s\n"), name);
Junio C Hamano46538012011-05-18 16:44:30 -0700851 return (flag & IGNORE_MISSING) ? 0 : status;
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700852}
853
Stephen Boyd74884b52010-02-27 00:59:22 -0800854static int remove_cmd(int argc, const char **argv, const char *prefix)
855{
Junio C Hamano2d370d22011-05-18 16:02:58 -0700856 unsigned flag = 0;
Junio C Hamano46538012011-05-18 16:44:30 -0700857 int from_stdin = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800858 struct option options[] = {
Junio C Hamano2d370d22011-05-18 16:02:58 -0700859 OPT_BIT(0, "ignore-missing", &flag,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700860 N_("attempt to remove non-existent note is not an error"),
Junio C Hamano46538012011-05-18 16:44:30 -0700861 IGNORE_MISSING),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200862 OPT_BOOL(0, "stdin", &from_stdin,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700863 N_("read object names from the standard input")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800864 OPT_END()
865 };
Stephen Boyd74884b52010-02-27 00:59:22 -0800866 struct notes_tree *t;
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700867 int retval = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800868
869 argc = parse_options(argc, argv, prefix, options,
870 git_notes_remove_usage, 0);
871
Stephen Boyd74884b52010-02-27 00:59:22 -0800872 t = init_notes_check("remove");
873
Junio C Hamano46538012011-05-18 16:44:30 -0700874 if (!argc && !from_stdin) {
Junio C Hamano2d370d22011-05-18 16:02:58 -0700875 retval = remove_one_note(t, "HEAD", flag);
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700876 } else {
877 while (*argv) {
Junio C Hamano2d370d22011-05-18 16:02:58 -0700878 retval |= remove_one_note(t, *argv, flag);
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700879 argv++;
880 }
Johan Herland1ee1e432010-08-31 17:56:50 +0200881 }
Junio C Hamano46538012011-05-18 16:44:30 -0700882 if (from_stdin) {
883 struct strbuf sb = STRBUF_INIT;
884 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
885 strbuf_rtrim(&sb);
886 retval |= remove_one_note(t, sb.buf, flag);
887 }
888 strbuf_release(&sb);
889 }
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700890 if (!retval)
891 commit_notes(t, "Notes removed by 'git notes remove'");
Stephen Boyd74884b52010-02-27 00:59:22 -0800892 free_notes(t);
Johan Herland1ee1e432010-08-31 17:56:50 +0200893 return retval;
Stephen Boyd74884b52010-02-27 00:59:22 -0800894}
895
896static int prune(int argc, const char **argv, const char *prefix)
897{
898 struct notes_tree *t;
Michael J Grubera9f2adf2010-05-14 23:42:07 +0200899 int show_only = 0, verbose = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800900 struct option options[] = {
René Scharfee21adb82010-11-08 18:58:51 +0100901 OPT__DRY_RUN(&show_only, "do not remove, show only"),
René Scharfefd038812010-11-08 18:56:39 +0100902 OPT__VERBOSE(&verbose, "report pruned notes"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800903 OPT_END()
904 };
905
906 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
907 0);
908
909 if (argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000910 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800911 usage_with_options(git_notes_prune_usage, options);
912 }
913
914 t = init_notes_check("prune");
915
Michael J Grubera9f2adf2010-05-14 23:42:07 +0200916 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
917 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
918 if (!show_only)
919 commit_notes(t, "Notes removed by 'git notes prune'");
Stephen Boyd74884b52010-02-27 00:59:22 -0800920 free_notes(t);
921 return 0;
922}
923
Johan Herland618cd752010-11-09 22:49:57 +0100924static int get_ref(int argc, const char **argv, const char *prefix)
925{
926 struct option options[] = { OPT_END() };
927 argc = parse_options(argc, argv, prefix, options,
928 git_notes_get_ref_usage, 0);
929
930 if (argc) {
931 error("too many parameters");
932 usage_with_options(git_notes_get_ref_usage, options);
933 }
934
935 puts(default_notes_ref());
936 return 0;
937}
938
Stephen Boyd74884b52010-02-27 00:59:22 -0800939int cmd_notes(int argc, const char **argv, const char *prefix)
940{
941 int result;
942 const char *override_notes_ref = NULL;
943 struct option options[] = {
Junio C Hamanoe703d712014-03-23 15:58:12 -0700944 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700945 N_("use notes from <notes_ref>")),
Johan Herlandcd067d32010-02-13 22:28:20 +0100946 OPT_END()
947 };
948
949 git_config(git_default_config, NULL);
Stephen Boyd74884b52010-02-27 00:59:22 -0800950 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
951 PARSE_OPT_STOP_AT_NON_OPTION);
Johan Herlandcd067d32010-02-13 22:28:20 +0100952
Thomas Rastdcf783a2010-03-12 18:04:35 +0100953 if (override_notes_ref) {
954 struct strbuf sb = STRBUF_INIT;
Thomas Rastdcf783a2010-03-12 18:04:35 +0100955 strbuf_addstr(&sb, override_notes_ref);
Johan Herland8ef313e2010-11-09 22:49:45 +0100956 expand_notes_ref(&sb);
Thomas Rastdcf783a2010-03-12 18:04:35 +0100957 setenv("GIT_NOTES_REF", sb.buf, 1);
958 strbuf_release(&sb);
959 }
960
Stephen Boyd74884b52010-02-27 00:59:22 -0800961 if (argc < 1 || !strcmp(argv[0], "list"))
962 result = list(argc, argv, prefix);
963 else if (!strcmp(argv[0], "add"))
964 result = add(argc, argv, prefix);
965 else if (!strcmp(argv[0], "copy"))
966 result = copy(argc, argv, prefix);
967 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
968 result = append_edit(argc, argv, prefix);
969 else if (!strcmp(argv[0], "show"))
970 result = show(argc, argv, prefix);
Johan Herland75ef3f42010-11-09 22:49:46 +0100971 else if (!strcmp(argv[0], "merge"))
972 result = merge(argc, argv, prefix);
Stephen Boyd74884b52010-02-27 00:59:22 -0800973 else if (!strcmp(argv[0], "remove"))
974 result = remove_cmd(argc, argv, prefix);
975 else if (!strcmp(argv[0], "prune"))
976 result = prune(argc, argv, prefix);
Johan Herland618cd752010-11-09 22:49:57 +0100977 else if (!strcmp(argv[0], "get-ref"))
978 result = get_ref(argc, argv, prefix);
Stephen Boyd74884b52010-02-27 00:59:22 -0800979 else {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000980 result = error(_("Unknown subcommand: %s"), argv[0]);
Johan Herland92b33852010-02-13 22:28:25 +0100981 usage_with_options(git_notes_usage, options);
982 }
983
Stephen Boyd74884b52010-02-27 00:59:22 -0800984 return result ? 1 : 0;
Johan Herlandcd067d32010-02-13 22:28:20 +0100985}