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