blob: 7b891471c461ddc3cd2b9d3186cc8b005f68ca2b [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[] =
Vasco Almeida996ee6d2016-07-28 11:26:15 +000094 N_("Write/edit the notes for the following object:");
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');
Vasco Almeida996ee6d2016-07-28 11:26:15 +0000182 strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
183 strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100184 strbuf_addch(&buf, '\n');
185 write_or_die(fd, buf.buf, buf.len);
Johan Herlandcd067d32010-02-13 22:28:20 +0100186
187 write_commented_object(fd, object);
188
189 close(fd);
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100190 strbuf_release(&buf);
Johan Herlandbebf5c02014-11-09 13:30:49 +0100191 strbuf_reset(&d->buf);
Johan Herlandcd067d32010-02-13 22:28:20 +0100192
Johan Herland4282af02014-11-09 13:30:50 +0100193 if (launch_editor(d->edit_path, &d->buf, NULL)) {
Vasco Almeida8d795892016-09-15 14:59:04 +0000194 die(_("please supply the note contents using either -m or -F option"));
Johan Herlandcd067d32010-02-13 22:28:20 +0100195 }
Tobias Klauser63af4a82015-10-16 17:16:42 +0200196 strbuf_stripspace(&d->buf, 1);
Johan Herlandcd067d32010-02-13 22:28:20 +0100197 }
Johan Herland52694cd2014-11-12 01:40:13 +0100198}
Johan Herlandcd067d32010-02-13 22:28:20 +0100199
Johan Herland52694cd2014-11-12 01:40:13 +0100200static void write_note_data(struct note_data *d, unsigned char *sha1)
201{
202 if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) {
203 error(_("unable to write note object"));
204 if (d->edit_path)
Vasco Almeida8d795892016-09-15 14:59:04 +0000205 error(_("the note contents have been left in %s"),
Johan Herland52694cd2014-11-12 01:40:13 +0100206 d->edit_path);
207 exit(128);
Johan Herlandcd067d32010-02-13 22:28:20 +0100208 }
Johan Herlandcd067d32010-02-13 22:28:20 +0100209}
210
Johan Herlandcd067d32010-02-13 22:28:20 +0100211static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
212{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100213 struct note_data *d = opt->value;
Johan Herlandcd067d32010-02-13 22:28:20 +0100214
Johan Herlandbebf5c02014-11-09 13:30:49 +0100215 strbuf_grow(&d->buf, strlen(arg) + 2);
216 if (d->buf.len)
217 strbuf_addch(&d->buf, '\n');
218 strbuf_addstr(&d->buf, arg);
Tobias Klauser63af4a82015-10-16 17:16:42 +0200219 strbuf_stripspace(&d->buf, 0);
Johan Herland348f1992010-02-13 22:28:35 +0100220
Johan Herlandbebf5c02014-11-09 13:30:49 +0100221 d->given = 1;
Johan Herland348f1992010-02-13 22:28:35 +0100222 return 0;
223}
224
225static int parse_file_arg(const struct option *opt, const char *arg, int unset)
226{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100227 struct note_data *d = opt->value;
Johan Herland348f1992010-02-13 22:28:35 +0100228
Johan Herlandbebf5c02014-11-09 13:30:49 +0100229 if (d->buf.len)
230 strbuf_addch(&d->buf, '\n');
Johan Herland348f1992010-02-13 22:28:35 +0100231 if (!strcmp(arg, "-")) {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100232 if (strbuf_read(&d->buf, 0, 1024) < 0)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000233 die_errno(_("cannot read '%s'"), arg);
Johan Herlandbebf5c02014-11-09 13:30:49 +0100234 } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000235 die_errno(_("could not open or read '%s'"), arg);
Tobias Klauser63af4a82015-10-16 17:16:42 +0200236 strbuf_stripspace(&d->buf, 0);
Johan Herland348f1992010-02-13 22:28:35 +0100237
Johan Herlandbebf5c02014-11-09 13:30:49 +0100238 d->given = 1;
Johan Herlandcd067d32010-02-13 22:28:20 +0100239 return 0;
240}
241
Johan Herland0691cff2010-02-13 22:28:36 +0100242static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
243{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100244 struct note_data *d = opt->value;
Johan Herland0691cff2010-02-13 22:28:36 +0100245 char *buf;
246 unsigned char object[20];
247 enum object_type type;
248 unsigned long len;
249
Johan Herlandbebf5c02014-11-09 13:30:49 +0100250 if (d->buf.len)
251 strbuf_addch(&d->buf, '\n');
Johan Herland0691cff2010-02-13 22:28:36 +0100252
253 if (get_sha1(arg, object))
Vasco Almeida8d795892016-09-15 14:59:04 +0000254 die(_("failed to resolve '%s' as a valid ref."), arg);
Johan Herland511726e2014-11-09 13:30:47 +0100255 if (!(buf = read_sha1_file(object, &type, &len))) {
Johan Herland0691cff2010-02-13 22:28:36 +0100256 free(buf);
Vasco Almeida8d795892016-09-15 14:59:04 +0000257 die(_("failed to read object '%s'."), arg);
Johan Herlandce8daa12014-02-12 10:54:16 +0100258 }
259 if (type != OBJ_BLOB) {
260 free(buf);
Vasco Almeida8d795892016-09-15 14:59:04 +0000261 die(_("cannot read note data from non-blob object '%s'."), arg);
Johan Herland0691cff2010-02-13 22:28:36 +0100262 }
Johan Herlandbebf5c02014-11-09 13:30:49 +0100263 strbuf_add(&d->buf, buf, len);
Johan Herland0691cff2010-02-13 22:28:36 +0100264 free(buf);
265
Johan Herlandbebf5c02014-11-09 13:30:49 +0100266 d->given = 1;
Johan Herland0691cff2010-02-13 22:28:36 +0100267 return 0;
268}
269
270static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
271{
Johan Herlandbebf5c02014-11-09 13:30:49 +0100272 struct note_data *d = opt->value;
273 d->use_editor = 1;
Johan Herland0691cff2010-02-13 22:28:36 +0100274 return parse_reuse_arg(opt, arg, unset);
275}
276
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700277static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
Thomas Rast160baa02010-03-12 18:04:31 +0100278{
279 struct strbuf buf = STRBUF_INIT;
Thomas Rast6956f852010-03-12 18:04:32 +0100280 struct notes_rewrite_cfg *c = NULL;
Ævar Arnfjörð Bjarmasonef7a8e32010-06-14 23:40:05 +0000281 struct notes_tree *t = NULL;
Thomas Rast160baa02010-03-12 18:04:31 +0100282 int ret = 0;
Johan Herland80a14662013-06-12 02:12:59 +0200283 const char *msg = "Notes added by 'git notes copy'";
Thomas Rast160baa02010-03-12 18:04:31 +0100284
Thomas Rast6956f852010-03-12 18:04:32 +0100285 if (rewrite_cmd) {
286 c = init_copy_notes_for_rewrite(rewrite_cmd);
287 if (!c)
288 return 0;
289 } else {
Mike Hommeyee76f922015-10-08 11:54:43 +0900290 init_notes(NULL, NULL, NULL, NOTES_INIT_WRITABLE);
Thomas Rast6956f852010-03-12 18:04:32 +0100291 t = &default_notes_tree;
292 }
Thomas Rast160baa02010-03-12 18:04:31 +0100293
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800294 while (strbuf_getline_lf(&buf, stdin) != EOF) {
Thomas Rast160baa02010-03-12 18:04:31 +0100295 unsigned char from_obj[20], to_obj[20];
296 struct strbuf **split;
297 int err;
298
299 split = strbuf_split(&buf, ' ');
300 if (!split[0] || !split[1])
Vasco Almeida8d795892016-09-15 14:59:04 +0000301 die(_("malformed input line: '%s'."), buf.buf);
Thomas Rast160baa02010-03-12 18:04:31 +0100302 strbuf_rtrim(split[0]);
303 strbuf_rtrim(split[1]);
304 if (get_sha1(split[0]->buf, from_obj))
Vasco Almeida8d795892016-09-15 14:59:04 +0000305 die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
Thomas Rast160baa02010-03-12 18:04:31 +0100306 if (get_sha1(split[1]->buf, to_obj))
Vasco Almeida8d795892016-09-15 14:59:04 +0000307 die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
Thomas Rast160baa02010-03-12 18:04:31 +0100308
Thomas Rast6956f852010-03-12 18:04:32 +0100309 if (rewrite_cmd)
310 err = copy_note_for_rewrite(c, from_obj, to_obj);
311 else
312 err = copy_note(t, from_obj, to_obj, force,
313 combine_notes_overwrite);
Thomas Rast160baa02010-03-12 18:04:31 +0100314
315 if (err) {
Vasco Almeida8d795892016-09-15 14:59:04 +0000316 error(_("failed to copy notes from '%s' to '%s'"),
Thomas Rast160baa02010-03-12 18:04:31 +0100317 split[0]->buf, split[1]->buf);
318 ret = 1;
319 }
320
321 strbuf_list_free(split);
322 }
323
Thomas Rast6956f852010-03-12 18:04:32 +0100324 if (!rewrite_cmd) {
Johan Herland80a14662013-06-12 02:12:59 +0200325 commit_notes(t, msg);
Thomas Rast6956f852010-03-12 18:04:32 +0100326 free_notes(t);
327 } else {
Johan Herland80a14662013-06-12 02:12:59 +0200328 finish_copy_notes_for_rewrite(c, msg);
Thomas Rast6956f852010-03-12 18:04:32 +0100329 }
Thomas Rast160baa02010-03-12 18:04:31 +0100330 return ret;
331}
332
Mike Hommeyee76f922015-10-08 11:54:43 +0900333static struct notes_tree *init_notes_check(const char *subcommand,
334 int flags)
Johan Herlandcd067d32010-02-13 22:28:20 +0100335{
Johan Herlandcd067d32010-02-13 22:28:20 +0100336 struct notes_tree *t;
Mike Hommeyee76f922015-10-08 11:54:43 +0900337 const char *ref;
338 init_notes(NULL, NULL, NULL, flags);
Stephen Boyd74884b52010-02-27 00:59:22 -0800339 t = &default_notes_tree;
Johan Herlanda0b4dfa2010-02-13 22:28:24 +0100340
Mike Hommeyee76f922015-10-08 11:54:43 +0900341 ref = (flags & NOTES_INIT_WRITABLE) ? t->update_ref : t->ref;
342 if (!starts_with(ref, "refs/notes/"))
Vasco Almeida2d1252d2016-09-15 14:59:03 +0000343 /* TRANSLATORS: the first %s will be replaced by a
344 git notes command: 'add', 'merge', 'remove', etc.*/
Vasco Almeida8d795892016-09-15 14:59:04 +0000345 die(_("refusing to %s notes in %s (outside of refs/notes/)"),
Mike Hommeyee76f922015-10-08 11:54:43 +0900346 subcommand, ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800347 return t;
348}
349
350static int list(int argc, const char **argv, const char *prefix)
351{
352 struct notes_tree *t;
353 unsigned char object[20];
354 const unsigned char *note;
355 int retval = -1;
Johan Herlandcd067d32010-02-13 22:28:20 +0100356 struct option options[] = {
Stephen Boyd74884b52010-02-27 00:59:22 -0800357 OPT_END()
358 };
359
360 if (argc)
361 argc = parse_options(argc, argv, prefix, options,
362 git_notes_list_usage, 0);
363
364 if (1 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000365 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800366 usage_with_options(git_notes_list_usage, options);
367 }
368
Mike Hommeyee76f922015-10-08 11:54:43 +0900369 t = init_notes_check("list", 0);
Stephen Boyd74884b52010-02-27 00:59:22 -0800370 if (argc) {
371 if (get_sha1(argv[0], object))
Vasco Almeida8d795892016-09-15 14:59:04 +0000372 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
Stephen Boyd74884b52010-02-27 00:59:22 -0800373 note = get_note(t, object);
374 if (note) {
375 puts(sha1_to_hex(note));
376 retval = 0;
377 } else
Vasco Almeida8d795892016-09-15 14:59:04 +0000378 retval = error(_("no note found for object %s."),
Stephen Boyd74884b52010-02-27 00:59:22 -0800379 sha1_to_hex(object));
380 } else
381 retval = for_each_note(t, 0, list_each_note, NULL);
382
383 free_notes(t);
384 return retval;
385}
386
Johan Herland84a7e352011-03-30 02:02:55 +0200387static int append_edit(int argc, const char **argv, const char *prefix);
388
Stephen Boyd74884b52010-02-27 00:59:22 -0800389static int add(int argc, const char **argv, const char *prefix)
390{
Johan Herlandd73a5b92014-11-12 01:40:14 +0100391 int force = 0, allow_empty = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800392 const char *object_ref;
393 struct notes_tree *t;
394 unsigned char object[20], new_note[20];
Stephen Boyd74884b52010-02-27 00:59:22 -0800395 const unsigned char *note;
Johan Herland4282af02014-11-09 13:30:50 +0100396 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
Stephen Boyd74884b52010-02-27 00:59:22 -0800397 struct option options[] = {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100398 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700399 N_("note contents as a string"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100400 parse_msg_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100401 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700402 N_("note contents in a file"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100403 parse_file_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100404 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700405 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100406 parse_reedit_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100407 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700408 N_("reuse specified note object"), PARSE_OPT_NONEG,
Johan Herland43a61b82010-02-25 01:48:11 +0100409 parse_reuse_arg},
Johan Herlandd73a5b92014-11-12 01:40:14 +0100410 OPT_BOOL(0, "allow-empty", &allow_empty,
411 N_("allow storing empty note")),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700412 OPT__FORCE(&force, N_("replace existing notes")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800413 OPT_END()
414 };
415
416 argc = parse_options(argc, argv, prefix, options, git_notes_add_usage,
Johan Herland84a7e352011-03-30 02:02:55 +0200417 PARSE_OPT_KEEP_ARGV0);
Stephen Boyd74884b52010-02-27 00:59:22 -0800418
Johan Herland84a7e352011-03-30 02:02:55 +0200419 if (2 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000420 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800421 usage_with_options(git_notes_add_usage, options);
422 }
423
Johan Herland84a7e352011-03-30 02:02:55 +0200424 object_ref = argc > 1 ? argv[1] : "HEAD";
Stephen Boyd74884b52010-02-27 00:59:22 -0800425
426 if (get_sha1(object_ref, object))
Vasco Almeida8d795892016-09-15 14:59:04 +0000427 die(_("failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800428
Mike Hommeyee76f922015-10-08 11:54:43 +0900429 t = init_notes_check("add", NOTES_INIT_WRITABLE);
Stephen Boyd74884b52010-02-27 00:59:22 -0800430 note = get_note(t, object);
431
432 if (note) {
433 if (!force) {
Johan Herlandb0de56c2014-11-12 01:40:12 +0100434 free_notes(t);
435 if (d.given) {
Johan Herland4282af02014-11-09 13:30:50 +0100436 free_note_data(&d);
Johan Herlandb0de56c2014-11-12 01:40:12 +0100437 return error(_("Cannot add notes. "
438 "Found existing notes for object %s. "
439 "Use '-f' to overwrite existing notes"),
440 sha1_to_hex(object));
Johan Herland84a7e352011-03-30 02:02:55 +0200441 }
Johan Herlandb0de56c2014-11-12 01:40:12 +0100442 /*
443 * Redirect to "edit" subcommand.
444 *
445 * We only end up here if none of -m/-F/-c/-C or -f are
446 * given. The original args are therefore still in
447 * argv[0-1].
448 */
449 argv[0] = "edit";
450 return append_edit(argc, argv, prefix);
Stephen Boyd74884b52010-02-27 00:59:22 -0800451 }
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000452 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800453 sha1_to_hex(object));
454 }
455
Johan Herland52694cd2014-11-12 01:40:13 +0100456 prepare_note_data(object, &d, note);
Johan Herlandd73a5b92014-11-12 01:40:14 +0100457 if (d.buf.len || allow_empty) {
Johan Herland52694cd2014-11-12 01:40:13 +0100458 write_note_data(&d, new_note);
459 if (add_note(t, object, new_note, combine_notes_overwrite))
460 die("BUG: combine_notes_overwrite failed");
461 commit_notes(t, "Notes added by 'git notes add'");
462 } else {
463 fprintf(stderr, _("Removing note for object %s\n"),
464 sha1_to_hex(object));
Stephen Boyd74884b52010-02-27 00:59:22 -0800465 remove_note(t, object);
Johan Herland52694cd2014-11-12 01:40:13 +0100466 commit_notes(t, "Notes removed by 'git notes add'");
467 }
Stephen Boyd74884b52010-02-27 00:59:22 -0800468
Johan Herland52694cd2014-11-12 01:40:13 +0100469 free_note_data(&d);
Stephen Boyd74884b52010-02-27 00:59:22 -0800470 free_notes(t);
Johan Herlandb0de56c2014-11-12 01:40:12 +0100471 return 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800472}
473
474static int copy(int argc, const char **argv, const char *prefix)
475{
476 int retval = 0, force = 0, from_stdin = 0;
477 const unsigned char *from_note, *note;
478 const char *object_ref;
479 unsigned char object[20], from_obj[20];
480 struct notes_tree *t;
481 const char *rewrite_cmd = NULL;
482 struct option options[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700483 OPT__FORCE(&force, N_("replace existing notes")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200484 OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700485 OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"),
486 N_("load rewriting config for <command> (implies "
487 "--stdin)")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800488 OPT_END()
489 };
490
491 argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage,
492 0);
493
494 if (from_stdin || rewrite_cmd) {
495 if (argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000496 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800497 usage_with_options(git_notes_copy_usage, options);
498 } else {
499 return notes_copy_from_stdin(force, rewrite_cmd);
500 }
501 }
502
Jeff Kingbbb1b8a2010-06-28 04:59:07 -0400503 if (argc < 2) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000504 error(_("too few parameters"));
Jeff Kingbbb1b8a2010-06-28 04:59:07 -0400505 usage_with_options(git_notes_copy_usage, options);
506 }
Stephen Boyd74884b52010-02-27 00:59:22 -0800507 if (2 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000508 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800509 usage_with_options(git_notes_copy_usage, options);
510 }
511
512 if (get_sha1(argv[0], from_obj))
Vasco Almeida8d795892016-09-15 14:59:04 +0000513 die(_("failed to resolve '%s' as a valid ref."), argv[0]);
Stephen Boyd74884b52010-02-27 00:59:22 -0800514
515 object_ref = 1 < argc ? argv[1] : "HEAD";
516
517 if (get_sha1(object_ref, object))
Vasco Almeida8d795892016-09-15 14:59:04 +0000518 die(_("failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800519
Mike Hommeyee76f922015-10-08 11:54:43 +0900520 t = init_notes_check("copy", NOTES_INIT_WRITABLE);
Stephen Boyd74884b52010-02-27 00:59:22 -0800521 note = get_note(t, object);
522
523 if (note) {
524 if (!force) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000525 retval = error(_("Cannot copy notes. Found existing "
Stephen Boyd74884b52010-02-27 00:59:22 -0800526 "notes for object %s. Use '-f' to "
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000527 "overwrite existing notes"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800528 sha1_to_hex(object));
529 goto out;
530 }
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000531 fprintf(stderr, _("Overwriting existing notes for object %s\n"),
Stephen Boyd74884b52010-02-27 00:59:22 -0800532 sha1_to_hex(object));
533 }
534
535 from_note = get_note(t, from_obj);
536 if (!from_note) {
Vasco Almeida8d795892016-09-15 14:59:04 +0000537 retval = error(_("missing notes on source object %s. Cannot "
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000538 "copy."), sha1_to_hex(from_obj));
Stephen Boyd74884b52010-02-27 00:59:22 -0800539 goto out;
540 }
541
Johan Herland180619a2010-11-15 00:52:26 +0100542 if (add_note(t, object, from_note, combine_notes_overwrite))
543 die("BUG: combine_notes_overwrite failed");
Stephen Boyd74884b52010-02-27 00:59:22 -0800544 commit_notes(t, "Notes added by 'git notes copy'");
545out:
546 free_notes(t);
547 return retval;
548}
549
550static int append_edit(int argc, const char **argv, const char *prefix)
551{
Johan Herlandd73a5b92014-11-12 01:40:14 +0100552 int allow_empty = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800553 const char *object_ref;
554 struct notes_tree *t;
555 unsigned char object[20], new_note[20];
556 const unsigned char *note;
Jeff King5b1ef2c2017-03-28 15:46:50 -0400557 char *logmsg;
Stephen Boyd74884b52010-02-27 00:59:22 -0800558 const char * const *usage;
Johan Herland4282af02014-11-09 13:30:50 +0100559 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
Stephen Boyd74884b52010-02-27 00:59:22 -0800560 struct option options[] = {
Johan Herlandbebf5c02014-11-09 13:30:49 +0100561 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700562 N_("note contents as a string"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800563 parse_msg_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100564 { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700565 N_("note contents in a file"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800566 parse_file_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100567 { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700568 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800569 parse_reedit_arg},
Johan Herlandbebf5c02014-11-09 13:30:49 +0100570 { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700571 N_("reuse specified note object"), PARSE_OPT_NONEG,
Stephen Boyd74884b52010-02-27 00:59:22 -0800572 parse_reuse_arg},
Johan Herlandd73a5b92014-11-12 01:40:14 +0100573 OPT_BOOL(0, "allow-empty", &allow_empty,
574 N_("allow storing empty note")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800575 OPT_END()
576 };
577 int edit = !strcmp(argv[0], "edit");
578
579 usage = edit ? git_notes_edit_usage : git_notes_append_usage;
580 argc = parse_options(argc, argv, prefix, options, usage,
581 PARSE_OPT_KEEP_ARGV0);
582
583 if (2 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000584 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800585 usage_with_options(usage, options);
586 }
587
Johan Herlandbebf5c02014-11-09 13:30:49 +0100588 if (d.given && edit)
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000589 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
Stephen Boyd74884b52010-02-27 00:59:22 -0800590 "for the 'edit' subcommand.\n"
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000591 "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800592
593 object_ref = 1 < argc ? argv[1] : "HEAD";
594
595 if (get_sha1(object_ref, object))
Vasco Almeida8d795892016-09-15 14:59:04 +0000596 die(_("failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800597
Mike Hommeyee76f922015-10-08 11:54:43 +0900598 t = init_notes_check(argv[0], NOTES_INIT_WRITABLE);
Stephen Boyd74884b52010-02-27 00:59:22 -0800599 note = get_note(t, object);
600
Johan Herland52694cd2014-11-12 01:40:13 +0100601 prepare_note_data(object, &d, edit ? note : NULL);
Stephen Boyd74884b52010-02-27 00:59:22 -0800602
Johan Herland52694cd2014-11-12 01:40:13 +0100603 if (note && !edit) {
604 /* Append buf to previous note contents */
605 unsigned long size;
606 enum object_type type;
607 char *prev_buf = read_sha1_file(note, &type, &size);
608
609 strbuf_grow(&d.buf, size + 1);
610 if (d.buf.len && prev_buf && size)
611 strbuf_insert(&d.buf, 0, "\n", 1);
612 if (prev_buf && size)
613 strbuf_insert(&d.buf, 0, prev_buf, size);
614 free(prev_buf);
615 }
616
Johan Herlandd73a5b92014-11-12 01:40:14 +0100617 if (d.buf.len || allow_empty) {
Johan Herland52694cd2014-11-12 01:40:13 +0100618 write_note_data(&d, new_note);
619 if (add_note(t, object, new_note, combine_notes_overwrite))
620 die("BUG: combine_notes_overwrite failed");
Jeff King5b1ef2c2017-03-28 15:46:50 -0400621 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
Johan Herland52694cd2014-11-12 01:40:13 +0100622 } else {
623 fprintf(stderr, _("Removing note for object %s\n"),
624 sha1_to_hex(object));
Stephen Boyd74884b52010-02-27 00:59:22 -0800625 remove_note(t, object);
Jeff King5b1ef2c2017-03-28 15:46:50 -0400626 logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
Johan Herland52694cd2014-11-12 01:40:13 +0100627 }
Stephen Boyd74884b52010-02-27 00:59:22 -0800628 commit_notes(t, logmsg);
Johan Herland52694cd2014-11-12 01:40:13 +0100629
Jeff King5b1ef2c2017-03-28 15:46:50 -0400630 free(logmsg);
Johan Herland52694cd2014-11-12 01:40:13 +0100631 free_note_data(&d);
Stephen Boyd74884b52010-02-27 00:59:22 -0800632 free_notes(t);
Stephen Boyd74884b52010-02-27 00:59:22 -0800633 return 0;
634}
635
636static int show(int argc, const char **argv, const char *prefix)
637{
638 const char *object_ref;
639 struct notes_tree *t;
640 unsigned char object[20];
641 const unsigned char *note;
642 int retval;
643 struct option options[] = {
644 OPT_END()
645 };
646
647 argc = parse_options(argc, argv, prefix, options, git_notes_show_usage,
648 0);
649
650 if (1 < argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000651 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800652 usage_with_options(git_notes_show_usage, options);
653 }
654
655 object_ref = argc ? argv[0] : "HEAD";
656
657 if (get_sha1(object_ref, object))
Vasco Almeida8d795892016-09-15 14:59:04 +0000658 die(_("failed to resolve '%s' as a valid ref."), object_ref);
Stephen Boyd74884b52010-02-27 00:59:22 -0800659
Mike Hommeyee76f922015-10-08 11:54:43 +0900660 t = init_notes_check("show", 0);
Stephen Boyd74884b52010-02-27 00:59:22 -0800661 note = get_note(t, object);
662
663 if (!note)
Vasco Almeida8d795892016-09-15 14:59:04 +0000664 retval = error(_("no note found for object %s."),
Stephen Boyd74884b52010-02-27 00:59:22 -0800665 sha1_to_hex(object));
666 else {
667 const char *show_args[3] = {"show", sha1_to_hex(note), NULL};
668 retval = execv_git_cmd(show_args);
669 }
670 free_notes(t);
671 return retval;
672}
673
Johan Herland6abb3652010-11-09 22:49:52 +0100674static int merge_abort(struct notes_merge_options *o)
675{
676 int ret = 0;
677
678 /*
679 * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call
680 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
681 */
682
Kyle Meyer755b49a2017-02-20 20:10:32 -0500683 if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
Vasco Almeida8d795892016-09-15 14:59:04 +0000684 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
Kyle Meyer755b49a2017-02-20 20:10:32 -0500685 if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NODEREF))
Vasco Almeida8d795892016-09-15 14:59:04 +0000686 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
Johan Herland6abb3652010-11-09 22:49:52 +0100687 if (notes_merge_abort(o))
Vasco Almeida8d795892016-09-15 14:59:04 +0000688 ret += error(_("failed to remove 'git notes merge' worktree"));
Johan Herland6abb3652010-11-09 22:49:52 +0100689 return ret;
690}
691
692static int merge_commit(struct notes_merge_options *o)
693{
694 struct strbuf msg = STRBUF_INIT;
brian m. carlson29283252017-02-21 23:47:29 +0000695 struct object_id oid, parent_oid;
Johan Herland6abb3652010-11-09 22:49:52 +0100696 struct notes_tree *t;
697 struct commit *partial;
698 struct pretty_print_context pretty_ctx;
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700699 void *local_ref_to_free;
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +0700700 int ret;
Johan Herland6abb3652010-11-09 22:49:52 +0100701
702 /*
703 * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
704 * and target notes ref from .git/NOTES_MERGE_REF.
705 */
706
brian m. carlson29283252017-02-21 23:47:29 +0000707 if (get_oid("NOTES_MERGE_PARTIAL", &oid))
Vasco Almeida8d795892016-09-15 14:59:04 +0000708 die(_("failed to read ref NOTES_MERGE_PARTIAL"));
brian m. carlson29283252017-02-21 23:47:29 +0000709 else if (!(partial = lookup_commit_reference(oid.hash)))
Vasco Almeida8d795892016-09-15 14:59:04 +0000710 die(_("could not find commit from NOTES_MERGE_PARTIAL."));
Johan Herland6abb3652010-11-09 22:49:52 +0100711 else if (parse_commit(partial))
Vasco Almeida8d795892016-09-15 14:59:04 +0000712 die(_("could not parse commit from NOTES_MERGE_PARTIAL."));
Johan Herland6abb3652010-11-09 22:49:52 +0100713
Johan Herland6cfd6a92010-11-09 22:49:54 +0100714 if (partial->parents)
brian m. carlson29283252017-02-21 23:47:29 +0000715 oidcpy(&parent_oid, &partial->parents->item->object.oid);
Johan Herland6cfd6a92010-11-09 22:49:54 +0100716 else
brian m. carlson29283252017-02-21 23:47:29 +0000717 oidclr(&parent_oid);
Johan Herland6cfd6a92010-11-09 22:49:54 +0100718
Johan Herland6abb3652010-11-09 22:49:52 +0100719 t = xcalloc(1, sizeof(struct notes_tree));
720 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
721
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700722 o->local_ref = local_ref_to_free =
brian m. carlson29283252017-02-21 23:47:29 +0000723 resolve_refdup("NOTES_MERGE_REF", 0, oid.hash, NULL);
Johan Herland6abb3652010-11-09 22:49:52 +0100724 if (!o->local_ref)
Vasco Almeida8d795892016-09-15 14:59:04 +0000725 die(_("failed to resolve NOTES_MERGE_REF"));
Johan Herland6abb3652010-11-09 22:49:52 +0100726
brian m. carlson29283252017-02-21 23:47:29 +0000727 if (notes_merge_commit(o, t, partial, oid.hash))
Vasco Almeida8d795892016-09-15 14:59:04 +0000728 die(_("failed to finalize notes merge"));
Johan Herland6abb3652010-11-09 22:49:52 +0100729
730 /* Reuse existing commit message in reflog message */
731 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
732 format_commit_message(partial, "%s", &msg, &pretty_ctx);
733 strbuf_trim(&msg);
734 strbuf_insert(&msg, 0, "notes: ", 7);
brian m. carlson29283252017-02-21 23:47:29 +0000735 update_ref(msg.buf, o->local_ref, oid.hash,
736 is_null_oid(&parent_oid) ? NULL : parent_oid.hash,
Michael Haggertyf4124112014-04-07 15:47:56 +0200737 0, UPDATE_REFS_DIE_ON_ERR);
Johan Herland6abb3652010-11-09 22:49:52 +0100738
739 free_notes(t);
740 strbuf_release(&msg);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +0700741 ret = merge_abort(o);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700742 free(local_ref_to_free);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +0700743 return ret;
Johan Herland6abb3652010-11-09 22:49:52 +0100744}
745
Jacob Kellerd2d68d92015-08-17 14:33:33 -0700746static int git_config_get_notes_strategy(const char *key,
747 enum notes_merge_strategy *strategy)
748{
Stefan Beller344b5482016-03-31 17:35:43 -0700749 char *value;
Jacob Kellerd2d68d92015-08-17 14:33:33 -0700750
Stefan Beller344b5482016-03-31 17:35:43 -0700751 if (git_config_get_string(key, &value))
Jacob Kellerd2d68d92015-08-17 14:33:33 -0700752 return 1;
753 if (parse_notes_merge_strategy(value, strategy))
Vasco Almeida53138272016-06-17 20:21:14 +0000754 git_die_config(key, _("unknown notes merge strategy %s"), value);
Jacob Kellerd2d68d92015-08-17 14:33:33 -0700755
Stefan Beller344b5482016-03-31 17:35:43 -0700756 free(value);
Jacob Kellerd2d68d92015-08-17 14:33:33 -0700757 return 0;
758}
759
Johan Herland75ef3f42010-11-09 22:49:46 +0100760static int merge(int argc, const char **argv, const char *prefix)
761{
762 struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
763 unsigned char result_sha1[20];
Johan Herland2085b162010-11-15 00:54:11 +0100764 struct notes_tree *t;
Johan Herland75ef3f42010-11-09 22:49:46 +0100765 struct notes_merge_options o;
Johan Herland6abb3652010-11-09 22:49:52 +0100766 int do_merge = 0, do_commit = 0, do_abort = 0;
Johan Herland75ef3f42010-11-09 22:49:46 +0100767 int verbosity = 0, result;
Johan Herland3228e672010-11-15 00:55:12 +0100768 const char *strategy = NULL;
Johan Herland75ef3f42010-11-09 22:49:46 +0100769 struct option options[] = {
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700770 OPT_GROUP(N_("General options")),
Johan Herland75ef3f42010-11-09 22:49:46 +0100771 OPT__VERBOSITY(&verbosity),
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700772 OPT_GROUP(N_("Merge options")),
773 OPT_STRING('s', "strategy", &strategy, N_("strategy"),
774 N_("resolve notes conflicts using the given strategy "
775 "(manual/ours/theirs/union/cat_sort_uniq)")),
776 OPT_GROUP(N_("Committing unmerged notes")),
Stefan Beller4741edd2013-08-03 13:51:18 +0200777 { OPTION_SET_INT, 0, "commit", &do_commit, NULL,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700778 N_("finalize notes merge by committing unmerged notes"),
Stefan Beller4741edd2013-08-03 13:51:18 +0200779 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700780 OPT_GROUP(N_("Aborting notes merge resolution")),
Stefan Beller4741edd2013-08-03 13:51:18 +0200781 { OPTION_SET_INT, 0, "abort", &do_abort, NULL,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700782 N_("abort notes merge"),
Stefan Beller4741edd2013-08-03 13:51:18 +0200783 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
Johan Herland75ef3f42010-11-09 22:49:46 +0100784 OPT_END()
785 };
786
787 argc = parse_options(argc, argv, prefix, options,
788 git_notes_merge_usage, 0);
789
Johan Herland6abb3652010-11-09 22:49:52 +0100790 if (strategy || do_commit + do_abort == 0)
791 do_merge = 1;
792 if (do_merge + do_commit + do_abort != 1) {
Vasco Almeida53138272016-06-17 20:21:14 +0000793 error(_("cannot mix --commit, --abort or -s/--strategy"));
Johan Herland6abb3652010-11-09 22:49:52 +0100794 usage_with_options(git_notes_merge_usage, options);
795 }
796
797 if (do_merge && argc != 1) {
Vasco Almeida8d795892016-09-15 14:59:04 +0000798 error(_("must specify a notes ref to merge"));
Johan Herland75ef3f42010-11-09 22:49:46 +0100799 usage_with_options(git_notes_merge_usage, options);
Johan Herland6abb3652010-11-09 22:49:52 +0100800 } else if (!do_merge && argc) {
Vasco Almeida53138272016-06-17 20:21:14 +0000801 error(_("too many parameters"));
Johan Herland6abb3652010-11-09 22:49:52 +0100802 usage_with_options(git_notes_merge_usage, options);
Johan Herland75ef3f42010-11-09 22:49:46 +0100803 }
804
805 init_notes_merge_options(&o);
806 o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT;
807
Johan Herland6abb3652010-11-09 22:49:52 +0100808 if (do_abort)
809 return merge_abort(&o);
810 if (do_commit)
811 return merge_commit(&o);
812
Johan Herland75ef3f42010-11-09 22:49:46 +0100813 o.local_ref = default_notes_ref();
814 strbuf_addstr(&remote_ref, argv[0]);
Jacob Kellerb3715b72015-12-29 14:40:28 -0800815 expand_loose_notes_ref(&remote_ref);
Johan Herland75ef3f42010-11-09 22:49:46 +0100816 o.remote_ref = remote_ref.buf;
817
Mike Hommeyee76f922015-10-08 11:54:43 +0900818 t = init_notes_check("merge", NOTES_INIT_WRITABLE);
Jacob Kellerd2d68d92015-08-17 14:33:33 -0700819
Johan Herland3228e672010-11-15 00:55:12 +0100820 if (strategy) {
Jacob Keller93efcad2015-08-17 14:33:31 -0700821 if (parse_notes_merge_strategy(strategy, &o.strategy)) {
Vasco Almeida8d795892016-09-15 14:59:04 +0000822 error(_("unknown -s/--strategy: %s"), strategy);
Johan Herland3228e672010-11-15 00:55:12 +0100823 usage_with_options(git_notes_merge_usage, options);
824 }
Jacob Kellerd2d68d92015-08-17 14:33:33 -0700825 } else {
Jacob Keller4f655e22015-08-17 14:33:34 -0700826 struct strbuf merge_key = STRBUF_INIT;
827 const char *short_ref = NULL;
Johan Herland3228e672010-11-15 00:55:12 +0100828
Jacob Keller4f655e22015-08-17 14:33:34 -0700829 if (!skip_prefix(o.local_ref, "refs/notes/", &short_ref))
830 die("BUG: local ref %s is outside of refs/notes/",
831 o.local_ref);
832
833 strbuf_addf(&merge_key, "notes.%s.mergeStrategy", short_ref);
834
835 if (git_config_get_notes_strategy(merge_key.buf, &o.strategy))
836 git_config_get_notes_strategy("notes.mergeStrategy", &o.strategy);
837
838 strbuf_release(&merge_key);
Johan Herland75ef3f42010-11-09 22:49:46 +0100839 }
Johan Herland75ef3f42010-11-09 22:49:46 +0100840
841 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
842 remote_ref.buf, default_notes_ref());
Johan Herland443259c2010-11-09 22:49:53 +0100843 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
Johan Herland75ef3f42010-11-09 22:49:46 +0100844
845 result = notes_merge(&o, t, result_sha1);
846
847 if (result >= 0) /* Merge resulted (trivially) in result_sha1 */
848 /* Update default notes ref with new commit */
849 update_ref(msg.buf, default_notes_ref(), result_sha1, NULL,
Michael Haggertyf4124112014-04-07 15:47:56 +0200850 0, UPDATE_REFS_DIE_ON_ERR);
Johan Herland6abb3652010-11-09 22:49:52 +0100851 else { /* Merge has unresolved conflicts */
Nguyễn Thái Ngọc Duyd3b9ac02016-04-22 20:01:27 +0700852 const struct worktree *wt;
Johan Herland6abb3652010-11-09 22:49:52 +0100853 /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
854 update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL,
Michael Haggertyf4124112014-04-07 15:47:56 +0200855 0, UPDATE_REFS_DIE_ON_ERR);
Johan Herland6abb3652010-11-09 22:49:52 +0100856 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
Nguyễn Thái Ngọc Duyd3b9ac02016-04-22 20:01:27 +0700857 wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
858 if (wt)
Vasco Almeida8d795892016-09-15 14:59:04 +0000859 die(_("a notes merge into %s is already in-progress at %s"),
Nguyễn Thái Ngọc Duyd3b9ac02016-04-22 20:01:27 +0700860 default_notes_ref(), wt->path);
Johan Herland6abb3652010-11-09 22:49:52 +0100861 if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL))
Vasco Almeida8d795892016-09-15 14:59:04 +0000862 die(_("failed to store link to current notes ref (%s)"),
Johan Herland6abb3652010-11-09 22:49:52 +0100863 default_notes_ref());
Vasco Almeida53138272016-06-17 20:21:14 +0000864 printf(_("Automatic notes merge failed. Fix conflicts in %s and "
865 "commit the result with 'git notes merge --commit', or "
866 "abort the merge with 'git notes merge --abort'.\n"),
Johan Herland809f38c2010-11-09 22:49:51 +0100867 git_path(NOTES_MERGE_WORKTREE));
Johan Herland6abb3652010-11-09 22:49:52 +0100868 }
Johan Herland75ef3f42010-11-09 22:49:46 +0100869
870 free_notes(t);
Stephen Boyd74884b52010-02-27 00:59:22 -0800871 strbuf_release(&remote_ref);
872 strbuf_release(&msg);
Johan Herland809f38c2010-11-09 22:49:51 +0100873 return result < 0; /* return non-zero on conflicts */
Stephen Boyd74884b52010-02-27 00:59:22 -0800874}
875
Junio C Hamano46538012011-05-18 16:44:30 -0700876#define IGNORE_MISSING 1
Junio C Hamano2d370d22011-05-18 16:02:58 -0700877
878static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700879{
880 int status;
881 unsigned char sha1[20];
882 if (get_sha1(name, sha1))
883 return error(_("Failed to resolve '%s' as a valid ref."), name);
884 status = remove_note(t, sha1);
885 if (status)
886 fprintf(stderr, _("Object %s has no note\n"), name);
887 else
888 fprintf(stderr, _("Removing note for object %s\n"), name);
Junio C Hamano46538012011-05-18 16:44:30 -0700889 return (flag & IGNORE_MISSING) ? 0 : status;
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700890}
891
Stephen Boyd74884b52010-02-27 00:59:22 -0800892static int remove_cmd(int argc, const char **argv, const char *prefix)
893{
Junio C Hamano2d370d22011-05-18 16:02:58 -0700894 unsigned flag = 0;
Junio C Hamano46538012011-05-18 16:44:30 -0700895 int from_stdin = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800896 struct option options[] = {
Junio C Hamano2d370d22011-05-18 16:02:58 -0700897 OPT_BIT(0, "ignore-missing", &flag,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700898 N_("attempt to remove non-existent note is not an error"),
Junio C Hamano46538012011-05-18 16:44:30 -0700899 IGNORE_MISSING),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200900 OPT_BOOL(0, "stdin", &from_stdin,
Nguyễn Thái Ngọc Duye6b895e2012-08-20 19:32:28 +0700901 N_("read object names from the standard input")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800902 OPT_END()
903 };
Stephen Boyd74884b52010-02-27 00:59:22 -0800904 struct notes_tree *t;
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700905 int retval = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800906
907 argc = parse_options(argc, argv, prefix, options,
908 git_notes_remove_usage, 0);
909
Mike Hommeyee76f922015-10-08 11:54:43 +0900910 t = init_notes_check("remove", NOTES_INIT_WRITABLE);
Stephen Boyd74884b52010-02-27 00:59:22 -0800911
Junio C Hamano46538012011-05-18 16:44:30 -0700912 if (!argc && !from_stdin) {
Junio C Hamano2d370d22011-05-18 16:02:58 -0700913 retval = remove_one_note(t, "HEAD", flag);
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700914 } else {
915 while (*argv) {
Junio C Hamano2d370d22011-05-18 16:02:58 -0700916 retval |= remove_one_note(t, *argv, flag);
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700917 argv++;
918 }
Johan Herland1ee1e432010-08-31 17:56:50 +0200919 }
Junio C Hamano46538012011-05-18 16:44:30 -0700920 if (from_stdin) {
921 struct strbuf sb = STRBUF_INIT;
922 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
923 strbuf_rtrim(&sb);
924 retval |= remove_one_note(t, sb.buf, flag);
925 }
926 strbuf_release(&sb);
927 }
Junio C Hamanoc3ab1a82011-05-18 15:44:37 -0700928 if (!retval)
929 commit_notes(t, "Notes removed by 'git notes remove'");
Stephen Boyd74884b52010-02-27 00:59:22 -0800930 free_notes(t);
Johan Herland1ee1e432010-08-31 17:56:50 +0200931 return retval;
Stephen Boyd74884b52010-02-27 00:59:22 -0800932}
933
934static int prune(int argc, const char **argv, const char *prefix)
935{
936 struct notes_tree *t;
Michael J Grubera9f2adf2010-05-14 23:42:07 +0200937 int show_only = 0, verbose = 0;
Stephen Boyd74884b52010-02-27 00:59:22 -0800938 struct option options[] = {
Vasco Almeidab34c77e2016-06-17 20:21:15 +0000939 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
940 OPT__VERBOSE(&verbose, N_("report pruned notes")),
Stephen Boyd74884b52010-02-27 00:59:22 -0800941 OPT_END()
942 };
943
944 argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage,
945 0);
946
947 if (argc) {
Ævar Arnfjörð Bjarmasoncaeba0e2011-02-22 23:42:26 +0000948 error(_("too many parameters"));
Stephen Boyd74884b52010-02-27 00:59:22 -0800949 usage_with_options(git_notes_prune_usage, options);
950 }
951
Mike Hommeyee76f922015-10-08 11:54:43 +0900952 t = init_notes_check("prune", NOTES_INIT_WRITABLE);
Stephen Boyd74884b52010-02-27 00:59:22 -0800953
Michael J Grubera9f2adf2010-05-14 23:42:07 +0200954 prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
955 (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
956 if (!show_only)
957 commit_notes(t, "Notes removed by 'git notes prune'");
Stephen Boyd74884b52010-02-27 00:59:22 -0800958 free_notes(t);
959 return 0;
960}
961
Johan Herland618cd752010-11-09 22:49:57 +0100962static int get_ref(int argc, const char **argv, const char *prefix)
963{
964 struct option options[] = { OPT_END() };
965 argc = parse_options(argc, argv, prefix, options,
966 git_notes_get_ref_usage, 0);
967
968 if (argc) {
Vasco Almeida53138272016-06-17 20:21:14 +0000969 error(_("too many parameters"));
Johan Herland618cd752010-11-09 22:49:57 +0100970 usage_with_options(git_notes_get_ref_usage, options);
971 }
972
973 puts(default_notes_ref());
974 return 0;
975}
976
Stephen Boyd74884b52010-02-27 00:59:22 -0800977int cmd_notes(int argc, const char **argv, const char *prefix)
978{
979 int result;
980 const char *override_notes_ref = NULL;
981 struct option options[] = {
Junio C Hamanoe703d712014-03-23 15:58:12 -0700982 OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700983 N_("use notes from <notes-ref>")),
Johan Herlandcd067d32010-02-13 22:28:20 +0100984 OPT_END()
985 };
986
987 git_config(git_default_config, NULL);
Stephen Boyd74884b52010-02-27 00:59:22 -0800988 argc = parse_options(argc, argv, prefix, options, git_notes_usage,
989 PARSE_OPT_STOP_AT_NON_OPTION);
Johan Herlandcd067d32010-02-13 22:28:20 +0100990
Thomas Rastdcf783a2010-03-12 18:04:35 +0100991 if (override_notes_ref) {
992 struct strbuf sb = STRBUF_INIT;
Thomas Rastdcf783a2010-03-12 18:04:35 +0100993 strbuf_addstr(&sb, override_notes_ref);
Johan Herland8ef313e2010-11-09 22:49:45 +0100994 expand_notes_ref(&sb);
Thomas Rastdcf783a2010-03-12 18:04:35 +0100995 setenv("GIT_NOTES_REF", sb.buf, 1);
996 strbuf_release(&sb);
997 }
998
Stephen Boyd74884b52010-02-27 00:59:22 -0800999 if (argc < 1 || !strcmp(argv[0], "list"))
1000 result = list(argc, argv, prefix);
1001 else if (!strcmp(argv[0], "add"))
1002 result = add(argc, argv, prefix);
1003 else if (!strcmp(argv[0], "copy"))
1004 result = copy(argc, argv, prefix);
1005 else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit"))
1006 result = append_edit(argc, argv, prefix);
1007 else if (!strcmp(argv[0], "show"))
1008 result = show(argc, argv, prefix);
Johan Herland75ef3f42010-11-09 22:49:46 +01001009 else if (!strcmp(argv[0], "merge"))
1010 result = merge(argc, argv, prefix);
Stephen Boyd74884b52010-02-27 00:59:22 -08001011 else if (!strcmp(argv[0], "remove"))
1012 result = remove_cmd(argc, argv, prefix);
1013 else if (!strcmp(argv[0], "prune"))
1014 result = prune(argc, argv, prefix);
Johan Herland618cd752010-11-09 22:49:57 +01001015 else if (!strcmp(argv[0], "get-ref"))
1016 result = get_ref(argc, argv, prefix);
Stephen Boyd74884b52010-02-27 00:59:22 -08001017 else {
Vasco Almeida8d795892016-09-15 14:59:04 +00001018 result = error(_("unknown subcommand: %s"), argv[0]);
Johan Herland92b33852010-02-13 22:28:25 +01001019 usage_with_options(git_notes_usage, options);
1020 }
1021
Stephen Boyd74884b52010-02-27 00:59:22 -08001022 return result ? 1 : 0;
Johan Herlandcd067d32010-02-13 22:28:20 +01001023}