Commit | Line | Data |
---|---|---|
cd067d3b JH |
1 | /* |
2 | * Builtin "git notes" | |
3 | * | |
4 | * Copyright (c) 2010 Johan Herland <johan@herland.net> | |
5 | * | |
6 | * Based on git-notes.sh by Johannes Schindelin, | |
09b7e220 | 7 | * and builtin/tag.c by Kristian Høgsberg and Carlos Rica. |
cd067d3b JH |
8 | */ |
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" | |
6956f858 | 19 | #include "string-list.h" |
75ef3f4a | 20 | #include "notes-merge.h" |
49c24704 | 21 | #include "notes-utils.h" |
f50fee4a | 22 | |
cd067d3b | 23 | static const char * const git_notes_usage[] = { |
9c9b4f2f AH |
24 | N_("git notes [--ref <notes-ref>] [list [<object>]]"), |
25 | N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-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 [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"), | |
28 | N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"), | |
29 | N_("git notes [--ref <notes-ref>] show [<object>]"), | |
30 | N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"), | |
e6b895ef NTND |
31 | N_("git notes merge --commit [-v | -q]"), |
32 | N_("git notes merge --abort [-v | -q]"), | |
9c9b4f2f AH |
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"), | |
74884b52 SB |
36 | NULL |
37 | }; | |
38 | ||
39 | static const char * const git_notes_list_usage[] = { | |
e6b895ef | 40 | N_("git notes [list [<object>]]"), |
74884b52 SB |
41 | NULL |
42 | }; | |
43 | ||
44 | static const char * const git_notes_add_usage[] = { | |
e6b895ef | 45 | N_("git notes add [<options>] [<object>]"), |
74884b52 SB |
46 | NULL |
47 | }; | |
48 | ||
49 | static const char * const git_notes_copy_usage[] = { | |
e6b895ef NTND |
50 | N_("git notes copy [<options>] <from-object> <to-object>"), |
51 | N_("git notes copy --stdin [<from-object> <to-object>]..."), | |
74884b52 SB |
52 | NULL |
53 | }; | |
54 | ||
55 | static const char * const git_notes_append_usage[] = { | |
e6b895ef | 56 | N_("git notes append [<options>] [<object>]"), |
74884b52 SB |
57 | NULL |
58 | }; | |
59 | ||
60 | static const char * const git_notes_edit_usage[] = { | |
e6b895ef | 61 | N_("git notes edit [<object>]"), |
74884b52 SB |
62 | NULL |
63 | }; | |
64 | ||
65 | static const char * const git_notes_show_usage[] = { | |
e6b895ef | 66 | N_("git notes show [<object>]"), |
74884b52 SB |
67 | NULL |
68 | }; | |
69 | ||
75ef3f4a | 70 | static const char * const git_notes_merge_usage[] = { |
9c9b4f2f | 71 | N_("git notes merge [<options>] <notes-ref>"), |
e6b895ef NTND |
72 | N_("git notes merge --commit [<options>]"), |
73 | N_("git notes merge --abort [<options>]"), | |
75ef3f4a JH |
74 | NULL |
75 | }; | |
76 | ||
74884b52 | 77 | static const char * const git_notes_remove_usage[] = { |
e6b895ef | 78 | N_("git notes remove [<object>]"), |
74884b52 SB |
79 | NULL |
80 | }; | |
81 | ||
82 | static const char * const git_notes_prune_usage[] = { | |
e6b895ef | 83 | N_("git notes prune [<options>]"), |
cd067d3b JH |
84 | NULL |
85 | }; | |
86 | ||
618cd757 | 87 | static const char * const git_notes_get_ref_usage[] = { |
e6b895ef | 88 | N_("git notes get-ref"), |
618cd757 JH |
89 | NULL |
90 | }; | |
91 | ||
cd067d3b | 92 | static const char note_template[] = |
eff80a9f | 93 | "\nWrite/edit the notes for the following object:\n"; |
cd067d3b | 94 | |
bebf5c04 | 95 | struct note_data { |
348f199b | 96 | int given; |
0691cff7 | 97 | int use_editor; |
4282af0f | 98 | char *edit_path; |
348f199b JH |
99 | struct strbuf buf; |
100 | }; | |
101 | ||
4282af0f JH |
102 | static 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 | ||
e397421a JH |
111 | static 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 | ||
bebf5c04 | 119 | static void copy_obj_to_fd(int fd, const unsigned char *sha1) |
cd067d3b JH |
120 | { |
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 | ||
131 | static 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}; | |
d3180279 | 135 | struct child_process show = CHILD_PROCESS_INIT; |
cd067d3b | 136 | struct strbuf buf = STRBUF_INIT; |
eff80a9f | 137 | struct strbuf cbuf = STRBUF_INIT; |
cd067d3b JH |
138 | |
139 | /* Invoke "git show --stat --no-notes $object" */ | |
cd067d3b JH |
140 | 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)) | |
caeba0ef | 146 | die(_("unable to start 'show' for object '%s'"), |
cd067d3b JH |
147 | sha1_to_hex(object)); |
148 | ||
eff80a9f JH |
149 | 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); | |
cd067d3b | 153 | |
eff80a9f | 154 | strbuf_release(&cbuf); |
cd067d3b | 155 | strbuf_release(&buf); |
eff80a9f | 156 | |
cd067d3b | 157 | if (finish_command(&show)) |
caeba0ef | 158 | die(_("failed to finish 'show' for object '%s'"), |
cd067d3b JH |
159 | sha1_to_hex(object)); |
160 | } | |
161 | ||
52694cda JH |
162 | static void prepare_note_data(const unsigned char *object, struct note_data *d, |
163 | const unsigned char *old_note) | |
cd067d3b | 164 | { |
bebf5c04 | 165 | if (d->use_editor || !d->given) { |
cd067d3b | 166 | int fd; |
eff80a9f | 167 | struct strbuf buf = STRBUF_INIT; |
cd067d3b JH |
168 | |
169 | /* write the template message before editing: */ | |
4282af0f JH |
170 | d->edit_path = git_pathdup("NOTES_EDITMSG"); |
171 | fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600); | |
cd067d3b | 172 | if (fd < 0) |
4282af0f | 173 | die_errno(_("could not create file '%s'"), d->edit_path); |
cd067d3b | 174 | |
bebf5c04 JH |
175 | if (d->given) |
176 | write_or_die(fd, d->buf.buf, d->buf.len); | |
52694cda JH |
177 | else if (old_note) |
178 | copy_obj_to_fd(fd, old_note); | |
eff80a9f JH |
179 | |
180 | strbuf_addch(&buf, '\n'); | |
181 | strbuf_add_commented_lines(&buf, note_template, strlen(note_template)); | |
182 | strbuf_addch(&buf, '\n'); | |
183 | write_or_die(fd, buf.buf, buf.len); | |
cd067d3b JH |
184 | |
185 | write_commented_object(fd, object); | |
186 | ||
187 | close(fd); | |
eff80a9f | 188 | strbuf_release(&buf); |
bebf5c04 | 189 | strbuf_reset(&d->buf); |
cd067d3b | 190 | |
4282af0f | 191 | if (launch_editor(d->edit_path, &d->buf, NULL)) { |
bebf5c04 | 192 | die(_("Please supply the note contents using either -m or -F option")); |
cd067d3b | 193 | } |
bebf5c04 | 194 | stripspace(&d->buf, 1); |
cd067d3b | 195 | } |
52694cda | 196 | } |
cd067d3b | 197 | |
52694cda JH |
198 | static void write_note_data(struct note_data *d, unsigned char *sha1) |
199 | { | |
200 | if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) { | |
201 | error(_("unable to write note object")); | |
202 | if (d->edit_path) | |
203 | error(_("The note contents have been left in %s"), | |
204 | d->edit_path); | |
205 | exit(128); | |
cd067d3b | 206 | } |
cd067d3b JH |
207 | } |
208 | ||
cd067d3b JH |
209 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) |
210 | { | |
bebf5c04 | 211 | struct note_data *d = opt->value; |
cd067d3b | 212 | |
bebf5c04 JH |
213 | strbuf_grow(&d->buf, strlen(arg) + 2); |
214 | if (d->buf.len) | |
215 | strbuf_addch(&d->buf, '\n'); | |
216 | strbuf_addstr(&d->buf, arg); | |
217 | stripspace(&d->buf, 0); | |
348f199b | 218 | |
bebf5c04 | 219 | d->given = 1; |
348f199b JH |
220 | return 0; |
221 | } | |
222 | ||
223 | static int parse_file_arg(const struct option *opt, const char *arg, int unset) | |
224 | { | |
bebf5c04 | 225 | struct note_data *d = opt->value; |
348f199b | 226 | |
bebf5c04 JH |
227 | if (d->buf.len) |
228 | strbuf_addch(&d->buf, '\n'); | |
348f199b | 229 | if (!strcmp(arg, "-")) { |
bebf5c04 | 230 | if (strbuf_read(&d->buf, 0, 1024) < 0) |
caeba0ef | 231 | die_errno(_("cannot read '%s'"), arg); |
bebf5c04 | 232 | } else if (strbuf_read_file(&d->buf, arg, 1024) < 0) |
caeba0ef | 233 | die_errno(_("could not open or read '%s'"), arg); |
bebf5c04 | 234 | stripspace(&d->buf, 0); |
348f199b | 235 | |
bebf5c04 | 236 | d->given = 1; |
cd067d3b JH |
237 | return 0; |
238 | } | |
239 | ||
0691cff7 JH |
240 | static int parse_reuse_arg(const struct option *opt, const char *arg, int unset) |
241 | { | |
bebf5c04 | 242 | struct note_data *d = opt->value; |
0691cff7 JH |
243 | char *buf; |
244 | unsigned char object[20]; | |
245 | enum object_type type; | |
246 | unsigned long len; | |
247 | ||
bebf5c04 JH |
248 | if (d->buf.len) |
249 | strbuf_addch(&d->buf, '\n'); | |
0691cff7 JH |
250 | |
251 | if (get_sha1(arg, object)) | |
caeba0ef | 252 | die(_("Failed to resolve '%s' as a valid ref."), arg); |
511726e4 | 253 | if (!(buf = read_sha1_file(object, &type, &len))) { |
0691cff7 | 254 | free(buf); |
ce8daa1e JH |
255 | die(_("Failed to read object '%s'."), arg); |
256 | } | |
257 | if (type != OBJ_BLOB) { | |
258 | free(buf); | |
259 | die(_("Cannot read note data from non-blob object '%s'."), arg); | |
0691cff7 | 260 | } |
bebf5c04 | 261 | strbuf_add(&d->buf, buf, len); |
0691cff7 JH |
262 | free(buf); |
263 | ||
bebf5c04 | 264 | d->given = 1; |
0691cff7 JH |
265 | return 0; |
266 | } | |
267 | ||
268 | static int parse_reedit_arg(const struct option *opt, const char *arg, int unset) | |
269 | { | |
bebf5c04 JH |
270 | struct note_data *d = opt->value; |
271 | d->use_editor = 1; | |
0691cff7 JH |
272 | return parse_reuse_arg(opt, arg, unset); |
273 | } | |
274 | ||
c2e86add | 275 | static int notes_copy_from_stdin(int force, const char *rewrite_cmd) |
160baa0d TR |
276 | { |
277 | struct strbuf buf = STRBUF_INIT; | |
6956f858 | 278 | struct notes_rewrite_cfg *c = NULL; |
ef7a8e3b | 279 | struct notes_tree *t = NULL; |
160baa0d | 280 | int ret = 0; |
80a14665 | 281 | const char *msg = "Notes added by 'git notes copy'"; |
160baa0d | 282 | |
6956f858 TR |
283 | if (rewrite_cmd) { |
284 | c = init_copy_notes_for_rewrite(rewrite_cmd); | |
285 | if (!c) | |
286 | return 0; | |
287 | } else { | |
288 | init_notes(NULL, NULL, NULL, 0); | |
289 | t = &default_notes_tree; | |
290 | } | |
160baa0d TR |
291 | |
292 | while (strbuf_getline(&buf, stdin, '\n') != EOF) { | |
293 | unsigned char from_obj[20], to_obj[20]; | |
294 | struct strbuf **split; | |
295 | int err; | |
296 | ||
297 | split = strbuf_split(&buf, ' '); | |
298 | if (!split[0] || !split[1]) | |
caeba0ef | 299 | die(_("Malformed input line: '%s'."), buf.buf); |
160baa0d TR |
300 | strbuf_rtrim(split[0]); |
301 | strbuf_rtrim(split[1]); | |
302 | if (get_sha1(split[0]->buf, from_obj)) | |
caeba0ef | 303 | die(_("Failed to resolve '%s' as a valid ref."), split[0]->buf); |
160baa0d | 304 | if (get_sha1(split[1]->buf, to_obj)) |
caeba0ef | 305 | die(_("Failed to resolve '%s' as a valid ref."), split[1]->buf); |
160baa0d | 306 | |
6956f858 TR |
307 | if (rewrite_cmd) |
308 | err = copy_note_for_rewrite(c, from_obj, to_obj); | |
309 | else | |
310 | err = copy_note(t, from_obj, to_obj, force, | |
311 | combine_notes_overwrite); | |
160baa0d TR |
312 | |
313 | if (err) { | |
caeba0ef | 314 | error(_("Failed to copy notes from '%s' to '%s'"), |
160baa0d TR |
315 | split[0]->buf, split[1]->buf); |
316 | ret = 1; | |
317 | } | |
318 | ||
319 | strbuf_list_free(split); | |
320 | } | |
321 | ||
6956f858 | 322 | if (!rewrite_cmd) { |
80a14665 | 323 | commit_notes(t, msg); |
6956f858 TR |
324 | free_notes(t); |
325 | } else { | |
80a14665 | 326 | finish_copy_notes_for_rewrite(c, msg); |
6956f858 | 327 | } |
160baa0d TR |
328 | return ret; |
329 | } | |
330 | ||
74884b52 | 331 | static struct notes_tree *init_notes_check(const char *subcommand) |
cd067d3b | 332 | { |
cd067d3b | 333 | struct notes_tree *t; |
74884b52 SB |
334 | init_notes(NULL, NULL, NULL, 0); |
335 | t = &default_notes_tree; | |
a0b4dfa9 | 336 | |
59556548 | 337 | if (!starts_with(t->ref, "refs/notes/")) |
74884b52 SB |
338 | die("Refusing to %s notes in %s (outside of refs/notes/)", |
339 | subcommand, t->ref); | |
340 | return t; | |
341 | } | |
342 | ||
343 | static int list(int argc, const char **argv, const char *prefix) | |
cd067d3b | 344 | { |
cd067d3b | 345 | struct notes_tree *t; |
74884b52 | 346 | unsigned char object[20]; |
cd067d3b | 347 | const unsigned char *note; |
74884b52 | 348 | int retval = -1; |
cd067d3b | 349 | struct option options[] = { |
cd067d3b JH |
350 | OPT_END() |
351 | }; | |
352 | ||
74884b52 SB |
353 | if (argc) |
354 | argc = parse_options(argc, argv, prefix, options, | |
355 | git_notes_list_usage, 0); | |
cd067d3b | 356 | |
74884b52 | 357 | if (1 < argc) { |
caeba0ef | 358 | error(_("too many parameters")); |
74884b52 | 359 | usage_with_options(git_notes_list_usage, options); |
dcf783a2 TR |
360 | } |
361 | ||
74884b52 SB |
362 | t = init_notes_check("list"); |
363 | if (argc) { | |
364 | if (get_sha1(argv[0], object)) | |
caeba0ef | 365 | die(_("Failed to resolve '%s' as a valid ref."), argv[0]); |
74884b52 SB |
366 | note = get_note(t, object); |
367 | if (note) { | |
368 | puts(sha1_to_hex(note)); | |
369 | retval = 0; | |
370 | } else | |
caeba0ef | 371 | retval = error(_("No note found for object %s."), |
74884b52 SB |
372 | sha1_to_hex(object)); |
373 | } else | |
374 | retval = for_each_note(t, 0, list_each_note, NULL); | |
cd067d3b | 375 | |
74884b52 SB |
376 | free_notes(t); |
377 | return retval; | |
378 | } | |
cd067d3b | 379 | |
84a7e35e JH |
380 | static int append_edit(int argc, const char **argv, const char *prefix); |
381 | ||
74884b52 SB |
382 | static int add(int argc, const char **argv, const char *prefix) |
383 | { | |
d73a5b93 | 384 | int force = 0, allow_empty = 0; |
92b3385f | 385 | const char *object_ref; |
74884b52 SB |
386 | struct notes_tree *t; |
387 | unsigned char object[20], new_note[20]; | |
74884b52 | 388 | const unsigned char *note; |
4282af0f | 389 | struct note_data d = { 0, 0, NULL, STRBUF_INIT }; |
cd067d3b | 390 | struct option options[] = { |
bebf5c04 | 391 | { OPTION_CALLBACK, 'm', "message", &d, N_("message"), |
e6b895ef | 392 | N_("note contents as a string"), PARSE_OPT_NONEG, |
43a61b84 | 393 | parse_msg_arg}, |
bebf5c04 | 394 | { OPTION_CALLBACK, 'F', "file", &d, N_("file"), |
e6b895ef | 395 | N_("note contents in a file"), PARSE_OPT_NONEG, |
43a61b84 | 396 | parse_file_arg}, |
bebf5c04 | 397 | { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"), |
e6b895ef | 398 | N_("reuse and edit specified note object"), PARSE_OPT_NONEG, |
43a61b84 | 399 | parse_reedit_arg}, |
bebf5c04 | 400 | { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"), |
e6b895ef | 401 | N_("reuse specified note object"), PARSE_OPT_NONEG, |
43a61b84 | 402 | parse_reuse_arg}, |
d73a5b93 JH |
403 | OPT_BOOL(0, "allow-empty", &allow_empty, |
404 | N_("allow storing empty note")), | |
e6b895ef | 405 | OPT__FORCE(&force, N_("replace existing notes")), |
cd067d3b JH |
406 | OPT_END() |
407 | }; | |
408 | ||
74884b52 | 409 | argc = parse_options(argc, argv, prefix, options, git_notes_add_usage, |
84a7e35e | 410 | PARSE_OPT_KEEP_ARGV0); |
cd067d3b | 411 | |
84a7e35e | 412 | if (2 < argc) { |
caeba0ef | 413 | error(_("too many parameters")); |
74884b52 | 414 | usage_with_options(git_notes_add_usage, options); |
dcf783a2 TR |
415 | } |
416 | ||
84a7e35e | 417 | object_ref = argc > 1 ? argv[1] : "HEAD"; |
cd067d3b | 418 | |
74884b52 | 419 | if (get_sha1(object_ref, object)) |
caeba0ef | 420 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
cd067d3b | 421 | |
74884b52 SB |
422 | t = init_notes_check("add"); |
423 | note = get_note(t, object); | |
92b3385f | 424 | |
74884b52 SB |
425 | if (note) { |
426 | if (!force) { | |
b0de56c6 JH |
427 | free_notes(t); |
428 | if (d.given) { | |
4282af0f | 429 | free_note_data(&d); |
b0de56c6 JH |
430 | return error(_("Cannot add notes. " |
431 | "Found existing notes for object %s. " | |
432 | "Use '-f' to overwrite existing notes"), | |
433 | sha1_to_hex(object)); | |
84a7e35e | 434 | } |
b0de56c6 JH |
435 | /* |
436 | * Redirect to "edit" subcommand. | |
437 | * | |
438 | * We only end up here if none of -m/-F/-c/-C or -f are | |
439 | * given. The original args are therefore still in | |
440 | * argv[0-1]. | |
441 | */ | |
442 | argv[0] = "edit"; | |
443 | return append_edit(argc, argv, prefix); | |
74884b52 | 444 | } |
caeba0ef | 445 | fprintf(stderr, _("Overwriting existing notes for object %s\n"), |
74884b52 | 446 | sha1_to_hex(object)); |
aaec9bcf JH |
447 | } |
448 | ||
52694cda | 449 | prepare_note_data(object, &d, note); |
d73a5b93 | 450 | if (d.buf.len || allow_empty) { |
52694cda JH |
451 | write_note_data(&d, new_note); |
452 | if (add_note(t, object, new_note, combine_notes_overwrite)) | |
453 | die("BUG: combine_notes_overwrite failed"); | |
454 | commit_notes(t, "Notes added by 'git notes add'"); | |
455 | } else { | |
456 | fprintf(stderr, _("Removing note for object %s\n"), | |
457 | sha1_to_hex(object)); | |
74884b52 | 458 | remove_note(t, object); |
52694cda JH |
459 | commit_notes(t, "Notes removed by 'git notes add'"); |
460 | } | |
160baa0d | 461 | |
52694cda | 462 | free_note_data(&d); |
74884b52 | 463 | free_notes(t); |
b0de56c6 | 464 | return 0; |
74884b52 | 465 | } |
cd067d3b | 466 | |
74884b52 SB |
467 | static int copy(int argc, const char **argv, const char *prefix) |
468 | { | |
469 | int retval = 0, force = 0, from_stdin = 0; | |
470 | const unsigned char *from_note, *note; | |
471 | const char *object_ref; | |
472 | unsigned char object[20], from_obj[20]; | |
473 | struct notes_tree *t; | |
474 | const char *rewrite_cmd = NULL; | |
475 | struct option options[] = { | |
e6b895ef | 476 | OPT__FORCE(&force, N_("replace existing notes")), |
d5d09d47 | 477 | OPT_BOOL(0, "stdin", &from_stdin, N_("read objects from stdin")), |
e6b895ef NTND |
478 | OPT_STRING(0, "for-rewrite", &rewrite_cmd, N_("command"), |
479 | N_("load rewriting config for <command> (implies " | |
480 | "--stdin)")), | |
74884b52 SB |
481 | OPT_END() |
482 | }; | |
cd067d3b | 483 | |
74884b52 SB |
484 | argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage, |
485 | 0); | |
e397421a | 486 | |
74884b52 SB |
487 | if (from_stdin || rewrite_cmd) { |
488 | if (argc) { | |
caeba0ef | 489 | error(_("too many parameters")); |
74884b52 | 490 | usage_with_options(git_notes_copy_usage, options); |
5848769f | 491 | } else { |
74884b52 | 492 | return notes_copy_from_stdin(force, rewrite_cmd); |
e73bbd96 | 493 | } |
e73bbd96 JH |
494 | } |
495 | ||
bbb1b8a3 | 496 | if (argc < 2) { |
caeba0ef | 497 | error(_("too few parameters")); |
bbb1b8a3 JK |
498 | usage_with_options(git_notes_copy_usage, options); |
499 | } | |
74884b52 | 500 | if (2 < argc) { |
caeba0ef | 501 | error(_("too many parameters")); |
74884b52 | 502 | usage_with_options(git_notes_copy_usage, options); |
cd067d3b JH |
503 | } |
504 | ||
74884b52 | 505 | if (get_sha1(argv[0], from_obj)) |
caeba0ef | 506 | die(_("Failed to resolve '%s' as a valid ref."), argv[0]); |
cd067d3b | 507 | |
74884b52 | 508 | object_ref = 1 < argc ? argv[1] : "HEAD"; |
cd067d3b | 509 | |
74884b52 | 510 | if (get_sha1(object_ref, object)) |
caeba0ef | 511 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
cd067d3b | 512 | |
74884b52 | 513 | t = init_notes_check("copy"); |
cd067d3b JH |
514 | note = get_note(t, object); |
515 | ||
74884b52 | 516 | if (note) { |
5848769f | 517 | if (!force) { |
caeba0ef | 518 | retval = error(_("Cannot copy notes. Found existing " |
74884b52 | 519 | "notes for object %s. Use '-f' to " |
caeba0ef | 520 | "overwrite existing notes"), |
74884b52 SB |
521 | sha1_to_hex(object)); |
522 | goto out; | |
5848769f | 523 | } |
caeba0ef | 524 | fprintf(stderr, _("Overwriting existing notes for object %s\n"), |
5848769f | 525 | sha1_to_hex(object)); |
e397421a JH |
526 | } |
527 | ||
74884b52 SB |
528 | from_note = get_note(t, from_obj); |
529 | if (!from_note) { | |
caeba0ef ÆAB |
530 | retval = error(_("Missing notes on source object %s. Cannot " |
531 | "copy."), sha1_to_hex(from_obj)); | |
74884b52 | 532 | goto out; |
cd067d3b JH |
533 | } |
534 | ||
180619a5 JH |
535 | if (add_note(t, object, from_note, combine_notes_overwrite)) |
536 | die("BUG: combine_notes_overwrite failed"); | |
74884b52 SB |
537 | commit_notes(t, "Notes added by 'git notes copy'"); |
538 | out: | |
539 | free_notes(t); | |
540 | return retval; | |
541 | } | |
7aa4754e | 542 | |
74884b52 SB |
543 | static int append_edit(int argc, const char **argv, const char *prefix) |
544 | { | |
d73a5b93 | 545 | int allow_empty = 0; |
74884b52 SB |
546 | const char *object_ref; |
547 | struct notes_tree *t; | |
548 | unsigned char object[20], new_note[20]; | |
549 | const unsigned char *note; | |
550 | char logmsg[100]; | |
551 | const char * const *usage; | |
4282af0f | 552 | struct note_data d = { 0, 0, NULL, STRBUF_INIT }; |
74884b52 | 553 | struct option options[] = { |
bebf5c04 | 554 | { OPTION_CALLBACK, 'm', "message", &d, N_("message"), |
e6b895ef | 555 | N_("note contents as a string"), PARSE_OPT_NONEG, |
74884b52 | 556 | parse_msg_arg}, |
bebf5c04 | 557 | { OPTION_CALLBACK, 'F', "file", &d, N_("file"), |
e6b895ef | 558 | N_("note contents in a file"), PARSE_OPT_NONEG, |
74884b52 | 559 | parse_file_arg}, |
bebf5c04 | 560 | { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"), |
e6b895ef | 561 | N_("reuse and edit specified note object"), PARSE_OPT_NONEG, |
74884b52 | 562 | parse_reedit_arg}, |
bebf5c04 | 563 | { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"), |
e6b895ef | 564 | N_("reuse specified note object"), PARSE_OPT_NONEG, |
74884b52 | 565 | parse_reuse_arg}, |
d73a5b93 JH |
566 | OPT_BOOL(0, "allow-empty", &allow_empty, |
567 | N_("allow storing empty note")), | |
74884b52 SB |
568 | OPT_END() |
569 | }; | |
570 | int edit = !strcmp(argv[0], "edit"); | |
571 | ||
572 | usage = edit ? git_notes_edit_usage : git_notes_append_usage; | |
573 | argc = parse_options(argc, argv, prefix, options, usage, | |
574 | PARSE_OPT_KEEP_ARGV0); | |
92b3385f | 575 | |
74884b52 | 576 | if (2 < argc) { |
caeba0ef | 577 | error(_("too many parameters")); |
74884b52 | 578 | usage_with_options(usage, options); |
cd067d3b JH |
579 | } |
580 | ||
bebf5c04 | 581 | if (d.given && edit) |
caeba0ef | 582 | fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated " |
74884b52 | 583 | "for the 'edit' subcommand.\n" |
caeba0ef | 584 | "Please use 'git notes add -f -m/-F/-c/-C' instead.\n")); |
74884b52 SB |
585 | |
586 | object_ref = 1 < argc ? argv[1] : "HEAD"; | |
587 | ||
588 | if (get_sha1(object_ref, object)) | |
caeba0ef | 589 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
74884b52 SB |
590 | |
591 | t = init_notes_check(argv[0]); | |
592 | note = get_note(t, object); | |
593 | ||
52694cda | 594 | prepare_note_data(object, &d, edit ? note : NULL); |
5848769f | 595 | |
52694cda JH |
596 | if (note && !edit) { |
597 | /* Append buf to previous note contents */ | |
598 | unsigned long size; | |
599 | enum object_type type; | |
600 | char *prev_buf = read_sha1_file(note, &type, &size); | |
601 | ||
602 | strbuf_grow(&d.buf, size + 1); | |
603 | if (d.buf.len && prev_buf && size) | |
604 | strbuf_insert(&d.buf, 0, "\n", 1); | |
605 | if (prev_buf && size) | |
606 | strbuf_insert(&d.buf, 0, prev_buf, size); | |
607 | free(prev_buf); | |
608 | } | |
5848769f | 609 | |
d73a5b93 | 610 | if (d.buf.len || allow_empty) { |
52694cda JH |
611 | write_note_data(&d, new_note); |
612 | if (add_note(t, object, new_note, combine_notes_overwrite)) | |
613 | die("BUG: combine_notes_overwrite failed"); | |
614 | snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'", | |
615 | argv[0]); | |
616 | } else { | |
617 | fprintf(stderr, _("Removing note for object %s\n"), | |
618 | sha1_to_hex(object)); | |
619 | remove_note(t, object); | |
620 | snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'", | |
621 | argv[0]); | |
622 | } | |
a0b4dfa9 | 623 | commit_notes(t, logmsg); |
52694cda JH |
624 | |
625 | free_note_data(&d); | |
cd067d3b | 626 | free_notes(t); |
74884b52 SB |
627 | return 0; |
628 | } | |
629 | ||
630 | static int show(int argc, const char **argv, const char *prefix) | |
631 | { | |
632 | const char *object_ref; | |
633 | struct notes_tree *t; | |
634 | unsigned char object[20]; | |
635 | const unsigned char *note; | |
636 | int retval; | |
637 | struct option options[] = { | |
638 | OPT_END() | |
639 | }; | |
640 | ||
641 | argc = parse_options(argc, argv, prefix, options, git_notes_show_usage, | |
642 | 0); | |
643 | ||
644 | if (1 < argc) { | |
caeba0ef | 645 | error(_("too many parameters")); |
74884b52 SB |
646 | usage_with_options(git_notes_show_usage, options); |
647 | } | |
648 | ||
649 | object_ref = argc ? argv[0] : "HEAD"; | |
650 | ||
651 | if (get_sha1(object_ref, object)) | |
caeba0ef | 652 | die(_("Failed to resolve '%s' as a valid ref."), object_ref); |
74884b52 SB |
653 | |
654 | t = init_notes_check("show"); | |
655 | note = get_note(t, object); | |
656 | ||
657 | if (!note) | |
caeba0ef | 658 | retval = error(_("No note found for object %s."), |
74884b52 SB |
659 | sha1_to_hex(object)); |
660 | else { | |
661 | const char *show_args[3] = {"show", sha1_to_hex(note), NULL}; | |
662 | retval = execv_git_cmd(show_args); | |
663 | } | |
664 | free_notes(t); | |
5848769f | 665 | return retval; |
cd067d3b | 666 | } |
74884b52 | 667 | |
6abb3655 JH |
668 | static int merge_abort(struct notes_merge_options *o) |
669 | { | |
670 | int ret = 0; | |
671 | ||
672 | /* | |
673 | * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call | |
674 | * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE. | |
675 | */ | |
676 | ||
677 | if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0)) | |
678 | ret += error("Failed to delete ref NOTES_MERGE_PARTIAL"); | |
679 | if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF)) | |
680 | ret += error("Failed to delete ref NOTES_MERGE_REF"); | |
681 | if (notes_merge_abort(o)) | |
682 | ret += error("Failed to remove 'git notes merge' worktree"); | |
683 | return ret; | |
684 | } | |
685 | ||
686 | static int merge_commit(struct notes_merge_options *o) | |
687 | { | |
688 | struct strbuf msg = STRBUF_INIT; | |
6cfd6a9d | 689 | unsigned char sha1[20], parent_sha1[20]; |
6abb3655 JH |
690 | struct notes_tree *t; |
691 | struct commit *partial; | |
692 | struct pretty_print_context pretty_ctx; | |
96ec7b1e | 693 | void *local_ref_to_free; |
d5a35c11 | 694 | int ret; |
6abb3655 JH |
695 | |
696 | /* | |
697 | * Read partial merge result from .git/NOTES_MERGE_PARTIAL, | |
698 | * and target notes ref from .git/NOTES_MERGE_REF. | |
699 | */ | |
700 | ||
701 | if (get_sha1("NOTES_MERGE_PARTIAL", sha1)) | |
702 | die("Failed to read ref NOTES_MERGE_PARTIAL"); | |
703 | else if (!(partial = lookup_commit_reference(sha1))) | |
704 | die("Could not find commit from NOTES_MERGE_PARTIAL."); | |
705 | else if (parse_commit(partial)) | |
706 | die("Could not parse commit from NOTES_MERGE_PARTIAL."); | |
707 | ||
6cfd6a9d JH |
708 | if (partial->parents) |
709 | hashcpy(parent_sha1, partial->parents->item->object.sha1); | |
710 | else | |
711 | hashclr(parent_sha1); | |
712 | ||
6abb3655 JH |
713 | t = xcalloc(1, sizeof(struct notes_tree)); |
714 | init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0); | |
715 | ||
96ec7b1e | 716 | o->local_ref = local_ref_to_free = |
7695d118 | 717 | resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL); |
6abb3655 JH |
718 | if (!o->local_ref) |
719 | die("Failed to resolve NOTES_MERGE_REF"); | |
720 | ||
721 | if (notes_merge_commit(o, t, partial, sha1)) | |
722 | die("Failed to finalize notes merge"); | |
723 | ||
724 | /* Reuse existing commit message in reflog message */ | |
725 | memset(&pretty_ctx, 0, sizeof(pretty_ctx)); | |
726 | format_commit_message(partial, "%s", &msg, &pretty_ctx); | |
727 | strbuf_trim(&msg); | |
728 | strbuf_insert(&msg, 0, "notes: ", 7); | |
6cfd6a9d JH |
729 | update_ref(msg.buf, o->local_ref, sha1, |
730 | is_null_sha1(parent_sha1) ? NULL : parent_sha1, | |
f4124112 | 731 | 0, UPDATE_REFS_DIE_ON_ERR); |
6abb3655 JH |
732 | |
733 | free_notes(t); | |
734 | strbuf_release(&msg); | |
d5a35c11 | 735 | ret = merge_abort(o); |
96ec7b1e | 736 | free(local_ref_to_free); |
d5a35c11 | 737 | return ret; |
6abb3655 JH |
738 | } |
739 | ||
75ef3f4a JH |
740 | static int merge(int argc, const char **argv, const char *prefix) |
741 | { | |
742 | struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT; | |
743 | unsigned char result_sha1[20]; | |
2085b16a | 744 | struct notes_tree *t; |
75ef3f4a | 745 | struct notes_merge_options o; |
6abb3655 | 746 | int do_merge = 0, do_commit = 0, do_abort = 0; |
75ef3f4a | 747 | int verbosity = 0, result; |
3228e671 | 748 | const char *strategy = NULL; |
75ef3f4a | 749 | struct option options[] = { |
e6b895ef | 750 | OPT_GROUP(N_("General options")), |
75ef3f4a | 751 | OPT__VERBOSITY(&verbosity), |
e6b895ef NTND |
752 | OPT_GROUP(N_("Merge options")), |
753 | OPT_STRING('s', "strategy", &strategy, N_("strategy"), | |
754 | N_("resolve notes conflicts using the given strategy " | |
755 | "(manual/ours/theirs/union/cat_sort_uniq)")), | |
756 | OPT_GROUP(N_("Committing unmerged notes")), | |
4741edd5 | 757 | { OPTION_SET_INT, 0, "commit", &do_commit, NULL, |
e6b895ef | 758 | N_("finalize notes merge by committing unmerged notes"), |
4741edd5 | 759 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1}, |
e6b895ef | 760 | OPT_GROUP(N_("Aborting notes merge resolution")), |
4741edd5 | 761 | { OPTION_SET_INT, 0, "abort", &do_abort, NULL, |
e6b895ef | 762 | N_("abort notes merge"), |
4741edd5 | 763 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1}, |
75ef3f4a JH |
764 | OPT_END() |
765 | }; | |
766 | ||
767 | argc = parse_options(argc, argv, prefix, options, | |
768 | git_notes_merge_usage, 0); | |
769 | ||
6abb3655 JH |
770 | if (strategy || do_commit + do_abort == 0) |
771 | do_merge = 1; | |
772 | if (do_merge + do_commit + do_abort != 1) { | |
773 | error("cannot mix --commit, --abort or -s/--strategy"); | |
774 | usage_with_options(git_notes_merge_usage, options); | |
775 | } | |
776 | ||
777 | if (do_merge && argc != 1) { | |
75ef3f4a JH |
778 | error("Must specify a notes ref to merge"); |
779 | usage_with_options(git_notes_merge_usage, options); | |
6abb3655 JH |
780 | } else if (!do_merge && argc) { |
781 | error("too many parameters"); | |
782 | usage_with_options(git_notes_merge_usage, options); | |
75ef3f4a JH |
783 | } |
784 | ||
785 | init_notes_merge_options(&o); | |
786 | o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT; | |
787 | ||
6abb3655 JH |
788 | if (do_abort) |
789 | return merge_abort(&o); | |
790 | if (do_commit) | |
791 | return merge_commit(&o); | |
792 | ||
75ef3f4a JH |
793 | o.local_ref = default_notes_ref(); |
794 | strbuf_addstr(&remote_ref, argv[0]); | |
795 | expand_notes_ref(&remote_ref); | |
796 | o.remote_ref = remote_ref.buf; | |
797 | ||
3228e671 JH |
798 | if (strategy) { |
799 | if (!strcmp(strategy, "manual")) | |
800 | o.strategy = NOTES_MERGE_RESOLVE_MANUAL; | |
801 | else if (!strcmp(strategy, "ours")) | |
802 | o.strategy = NOTES_MERGE_RESOLVE_OURS; | |
803 | else if (!strcmp(strategy, "theirs")) | |
804 | o.strategy = NOTES_MERGE_RESOLVE_THEIRS; | |
805 | else if (!strcmp(strategy, "union")) | |
806 | o.strategy = NOTES_MERGE_RESOLVE_UNION; | |
a6a09095 JH |
807 | else if (!strcmp(strategy, "cat_sort_uniq")) |
808 | o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ; | |
3228e671 JH |
809 | else { |
810 | error("Unknown -s/--strategy: %s", strategy); | |
811 | usage_with_options(git_notes_merge_usage, options); | |
812 | } | |
813 | } | |
814 | ||
2085b16a | 815 | t = init_notes_check("merge"); |
75ef3f4a JH |
816 | |
817 | strbuf_addf(&msg, "notes: Merged notes from %s into %s", | |
818 | remote_ref.buf, default_notes_ref()); | |
443259cf | 819 | strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */ |
2085b16a JH |
820 | |
821 | result = notes_merge(&o, t, result_sha1); | |
822 | ||
823 | if (result >= 0) /* Merge resulted (trivially) in result_sha1 */ | |
75ef3f4a JH |
824 | /* Update default notes ref with new commit */ |
825 | update_ref(msg.buf, default_notes_ref(), result_sha1, NULL, | |
f4124112 | 826 | 0, UPDATE_REFS_DIE_ON_ERR); |
6abb3655 JH |
827 | else { /* Merge has unresolved conflicts */ |
828 | /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */ | |
829 | update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL, | |
f4124112 | 830 | 0, UPDATE_REFS_DIE_ON_ERR); |
6abb3655 JH |
831 | /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */ |
832 | if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL)) | |
833 | die("Failed to store link to current notes ref (%s)", | |
834 | default_notes_ref()); | |
835 | printf("Automatic notes merge failed. Fix conflicts in %s and " | |
836 | "commit the result with 'git notes merge --commit', or " | |
837 | "abort the merge with 'git notes merge --abort'.\n", | |
809f38c8 | 838 | git_path(NOTES_MERGE_WORKTREE)); |
6abb3655 | 839 | } |
75ef3f4a | 840 | |
2085b16a | 841 | free_notes(t); |
75ef3f4a JH |
842 | strbuf_release(&remote_ref); |
843 | strbuf_release(&msg); | |
809f38c8 | 844 | return result < 0; /* return non-zero on conflicts */ |
75ef3f4a JH |
845 | } |
846 | ||
46538012 | 847 | #define IGNORE_MISSING 1 |
2d370d2f JH |
848 | |
849 | static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag) | |
c3ab1a8e JH |
850 | { |
851 | int status; | |
852 | unsigned char sha1[20]; | |
853 | if (get_sha1(name, sha1)) | |
854 | return error(_("Failed to resolve '%s' as a valid ref."), name); | |
855 | status = remove_note(t, sha1); | |
856 | if (status) | |
857 | fprintf(stderr, _("Object %s has no note\n"), name); | |
858 | else | |
859 | fprintf(stderr, _("Removing note for object %s\n"), name); | |
46538012 | 860 | return (flag & IGNORE_MISSING) ? 0 : status; |
c3ab1a8e JH |
861 | } |
862 | ||
74884b52 SB |
863 | static int remove_cmd(int argc, const char **argv, const char *prefix) |
864 | { | |
2d370d2f | 865 | unsigned flag = 0; |
46538012 | 866 | int from_stdin = 0; |
74884b52 | 867 | struct option options[] = { |
2d370d2f | 868 | OPT_BIT(0, "ignore-missing", &flag, |
e6b895ef | 869 | N_("attempt to remove non-existent note is not an error"), |
46538012 | 870 | IGNORE_MISSING), |
d5d09d47 | 871 | OPT_BOOL(0, "stdin", &from_stdin, |
e6b895ef | 872 | N_("read object names from the standard input")), |
74884b52 SB |
873 | OPT_END() |
874 | }; | |
74884b52 | 875 | struct notes_tree *t; |
c3ab1a8e | 876 | int retval = 0; |
74884b52 SB |
877 | |
878 | argc = parse_options(argc, argv, prefix, options, | |
879 | git_notes_remove_usage, 0); | |
880 | ||
74884b52 SB |
881 | t = init_notes_check("remove"); |
882 | ||
46538012 | 883 | if (!argc && !from_stdin) { |
2d370d2f | 884 | retval = remove_one_note(t, "HEAD", flag); |
c3ab1a8e JH |
885 | } else { |
886 | while (*argv) { | |
2d370d2f | 887 | retval |= remove_one_note(t, *argv, flag); |
c3ab1a8e JH |
888 | argv++; |
889 | } | |
1ee1e43d | 890 | } |
46538012 JH |
891 | if (from_stdin) { |
892 | struct strbuf sb = STRBUF_INIT; | |
893 | while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) { | |
894 | strbuf_rtrim(&sb); | |
895 | retval |= remove_one_note(t, sb.buf, flag); | |
896 | } | |
897 | strbuf_release(&sb); | |
898 | } | |
c3ab1a8e JH |
899 | if (!retval) |
900 | commit_notes(t, "Notes removed by 'git notes remove'"); | |
74884b52 | 901 | free_notes(t); |
1ee1e43d | 902 | return retval; |
74884b52 SB |
903 | } |
904 | ||
905 | static int prune(int argc, const char **argv, const char *prefix) | |
906 | { | |
907 | struct notes_tree *t; | |
a9f2adff | 908 | int show_only = 0, verbose = 0; |
74884b52 | 909 | struct option options[] = { |
e21adb8c | 910 | OPT__DRY_RUN(&show_only, "do not remove, show only"), |
fd03881a | 911 | OPT__VERBOSE(&verbose, "report pruned notes"), |
74884b52 SB |
912 | OPT_END() |
913 | }; | |
914 | ||
915 | argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage, | |
916 | 0); | |
917 | ||
918 | if (argc) { | |
caeba0ef | 919 | error(_("too many parameters")); |
74884b52 SB |
920 | usage_with_options(git_notes_prune_usage, options); |
921 | } | |
922 | ||
923 | t = init_notes_check("prune"); | |
924 | ||
a9f2adff MG |
925 | prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) | |
926 | (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) ); | |
927 | if (!show_only) | |
928 | commit_notes(t, "Notes removed by 'git notes prune'"); | |
74884b52 SB |
929 | free_notes(t); |
930 | return 0; | |
931 | } | |
932 | ||
618cd757 JH |
933 | static int get_ref(int argc, const char **argv, const char *prefix) |
934 | { | |
935 | struct option options[] = { OPT_END() }; | |
936 | argc = parse_options(argc, argv, prefix, options, | |
937 | git_notes_get_ref_usage, 0); | |
938 | ||
939 | if (argc) { | |
940 | error("too many parameters"); | |
941 | usage_with_options(git_notes_get_ref_usage, options); | |
942 | } | |
943 | ||
944 | puts(default_notes_ref()); | |
945 | return 0; | |
946 | } | |
947 | ||
74884b52 SB |
948 | int cmd_notes(int argc, const char **argv, const char *prefix) |
949 | { | |
950 | int result; | |
951 | const char *override_notes_ref = NULL; | |
952 | struct option options[] = { | |
e703d711 | 953 | OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"), |
9c9b4f2f | 954 | N_("use notes from <notes-ref>")), |
74884b52 SB |
955 | OPT_END() |
956 | }; | |
957 | ||
958 | git_config(git_default_config, NULL); | |
959 | argc = parse_options(argc, argv, prefix, options, git_notes_usage, | |
960 | PARSE_OPT_STOP_AT_NON_OPTION); | |
961 | ||
962 | if (override_notes_ref) { | |
963 | struct strbuf sb = STRBUF_INIT; | |
74884b52 | 964 | strbuf_addstr(&sb, override_notes_ref); |
8ef313e1 | 965 | expand_notes_ref(&sb); |
74884b52 SB |
966 | setenv("GIT_NOTES_REF", sb.buf, 1); |
967 | strbuf_release(&sb); | |
968 | } | |
969 | ||
970 | if (argc < 1 || !strcmp(argv[0], "list")) | |
971 | result = list(argc, argv, prefix); | |
972 | else if (!strcmp(argv[0], "add")) | |
973 | result = add(argc, argv, prefix); | |
974 | else if (!strcmp(argv[0], "copy")) | |
975 | result = copy(argc, argv, prefix); | |
976 | else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit")) | |
977 | result = append_edit(argc, argv, prefix); | |
978 | else if (!strcmp(argv[0], "show")) | |
979 | result = show(argc, argv, prefix); | |
75ef3f4a JH |
980 | else if (!strcmp(argv[0], "merge")) |
981 | result = merge(argc, argv, prefix); | |
74884b52 SB |
982 | else if (!strcmp(argv[0], "remove")) |
983 | result = remove_cmd(argc, argv, prefix); | |
984 | else if (!strcmp(argv[0], "prune")) | |
985 | result = prune(argc, argv, prefix); | |
618cd757 JH |
986 | else if (!strcmp(argv[0], "get-ref")) |
987 | result = get_ref(argc, argv, prefix); | |
74884b52 | 988 | else { |
caeba0ef | 989 | result = error(_("Unknown subcommand: %s"), argv[0]); |
74884b52 SB |
990 | usage_with_options(git_notes_usage, options); |
991 | } | |
992 | ||
993 | return result ? 1 : 0; | |
994 | } |