Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 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, |
| 7 | * and builtin-tag.c by Kristian Høgsberg and Carlos Rica. |
| 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" |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 19 | #include "string-list.h" |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 20 | #include "notes-merge.h" |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 21 | |
| 22 | static const char * const git_notes_usage[] = { |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 23 | "git notes [--ref <notes_ref>] [list [<object>]]", |
| 24 | "git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]", |
| 25 | "git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>", |
| 26 | "git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]", |
| 27 | "git notes [--ref <notes_ref>] edit [<object>]", |
| 28 | "git notes [--ref <notes_ref>] show [<object>]", |
Johan Herland | 3228e67 | 2010-11-15 00:55:12 +0100 | [diff] [blame] | 29 | "git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>", |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 30 | "git notes merge --commit [-v | -q]", |
| 31 | "git notes merge --abort [-v | -q]", |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 32 | "git notes [--ref <notes_ref>] remove [<object>]", |
Michael J Gruber | a9f2adf | 2010-05-14 23:42:07 +0200 | [diff] [blame] | 33 | "git notes [--ref <notes_ref>] prune [-n | -v]", |
Johan Herland | 618cd75 | 2010-11-09 22:49:57 +0100 | [diff] [blame] | 34 | "git notes [--ref <notes_ref>] get-ref", |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 35 | NULL |
| 36 | }; |
| 37 | |
| 38 | static const char * const git_notes_list_usage[] = { |
Johan Herland | e397421 | 2010-02-13 22:28:30 +0100 | [diff] [blame] | 39 | "git notes [list [<object>]]", |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 40 | NULL |
| 41 | }; |
| 42 | |
| 43 | static const char * const git_notes_add_usage[] = { |
| 44 | "git notes add [<options>] [<object>]", |
| 45 | NULL |
| 46 | }; |
| 47 | |
| 48 | static const char * const git_notes_copy_usage[] = { |
| 49 | "git notes copy [<options>] <from-object> <to-object>", |
| 50 | "git notes copy --stdin [<from-object> <to-object>]...", |
| 51 | NULL |
| 52 | }; |
| 53 | |
| 54 | static const char * const git_notes_append_usage[] = { |
| 55 | "git notes append [<options>] [<object>]", |
| 56 | NULL |
| 57 | }; |
| 58 | |
| 59 | static const char * const git_notes_edit_usage[] = { |
Johan Herland | aaec9bc | 2010-02-13 22:28:34 +0100 | [diff] [blame] | 60 | "git notes edit [<object>]", |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 61 | NULL |
| 62 | }; |
| 63 | |
| 64 | static const char * const git_notes_show_usage[] = { |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 65 | "git notes show [<object>]", |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 66 | NULL |
| 67 | }; |
| 68 | |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 69 | static const char * const git_notes_merge_usage[] = { |
| 70 | "git notes merge [<options>] <notes_ref>", |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 71 | "git notes merge --commit [<options>]", |
| 72 | "git notes merge --abort [<options>]", |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 73 | NULL |
| 74 | }; |
| 75 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 76 | static const char * const git_notes_remove_usage[] = { |
Johan Herland | 92b3385 | 2010-02-13 22:28:25 +0100 | [diff] [blame] | 77 | "git notes remove [<object>]", |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 78 | NULL |
| 79 | }; |
| 80 | |
| 81 | static const char * const git_notes_prune_usage[] = { |
Michael J Gruber | a9f2adf | 2010-05-14 23:42:07 +0200 | [diff] [blame] | 82 | "git notes prune [<options>]", |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 83 | NULL |
| 84 | }; |
| 85 | |
Johan Herland | 618cd75 | 2010-11-09 22:49:57 +0100 | [diff] [blame] | 86 | static const char * const git_notes_get_ref_usage[] = { |
| 87 | "git notes get-ref", |
| 88 | NULL |
| 89 | }; |
| 90 | |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 91 | static const char note_template[] = |
| 92 | "\n" |
| 93 | "#\n" |
| 94 | "# Write/edit the notes for the following object:\n" |
| 95 | "#\n"; |
| 96 | |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 97 | struct msg_arg { |
| 98 | int given; |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 99 | int use_editor; |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 100 | struct strbuf buf; |
| 101 | }; |
| 102 | |
Johan Herland | 8ef313e | 2010-11-09 22:49:45 +0100 | [diff] [blame] | 103 | static void expand_notes_ref(struct strbuf *sb) |
| 104 | { |
| 105 | if (!prefixcmp(sb->buf, "refs/notes/")) |
| 106 | return; /* we're happy */ |
| 107 | else if (!prefixcmp(sb->buf, "notes/")) |
| 108 | strbuf_insert(sb, 0, "refs/", 5); |
| 109 | else |
| 110 | strbuf_insert(sb, 0, "refs/notes/", 11); |
| 111 | } |
| 112 | |
Johan Herland | e397421 | 2010-02-13 22:28:30 +0100 | [diff] [blame] | 113 | static int list_each_note(const unsigned char *object_sha1, |
| 114 | const unsigned char *note_sha1, char *note_path, |
| 115 | void *cb_data) |
| 116 | { |
| 117 | printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1)); |
| 118 | return 0; |
| 119 | } |
| 120 | |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 121 | static void write_note_data(int fd, const unsigned char *sha1) |
| 122 | { |
| 123 | unsigned long size; |
| 124 | enum object_type type; |
| 125 | char *buf = read_sha1_file(sha1, &type, &size); |
| 126 | if (buf) { |
| 127 | if (size) |
| 128 | write_or_die(fd, buf, size); |
| 129 | free(buf); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static void write_commented_object(int fd, const unsigned char *object) |
| 134 | { |
| 135 | const char *show_args[5] = |
| 136 | {"show", "--stat", "--no-notes", sha1_to_hex(object), NULL}; |
| 137 | struct child_process show; |
| 138 | struct strbuf buf = STRBUF_INIT; |
| 139 | FILE *show_out; |
| 140 | |
| 141 | /* Invoke "git show --stat --no-notes $object" */ |
| 142 | memset(&show, 0, sizeof(show)); |
| 143 | show.argv = show_args; |
| 144 | show.no_stdin = 1; |
| 145 | show.out = -1; |
| 146 | show.err = 0; |
| 147 | show.git_cmd = 1; |
| 148 | if (start_command(&show)) |
| 149 | die("unable to start 'show' for object '%s'", |
| 150 | sha1_to_hex(object)); |
| 151 | |
| 152 | /* Open the output as FILE* so strbuf_getline() can be used. */ |
| 153 | show_out = xfdopen(show.out, "r"); |
| 154 | if (show_out == NULL) |
| 155 | die_errno("can't fdopen 'show' output fd"); |
| 156 | |
| 157 | /* Prepend "# " to each output line and write result to 'fd' */ |
| 158 | while (strbuf_getline(&buf, show_out, '\n') != EOF) { |
| 159 | write_or_die(fd, "# ", 2); |
| 160 | write_or_die(fd, buf.buf, buf.len); |
| 161 | write_or_die(fd, "\n", 1); |
| 162 | } |
| 163 | strbuf_release(&buf); |
| 164 | if (fclose(show_out)) |
| 165 | die_errno("failed to close pipe to 'show' for object '%s'", |
| 166 | sha1_to_hex(object)); |
| 167 | if (finish_command(&show)) |
| 168 | die("failed to finish 'show' for object '%s'", |
| 169 | sha1_to_hex(object)); |
| 170 | } |
| 171 | |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 172 | static void create_note(const unsigned char *object, struct msg_arg *msg, |
| 173 | int append_only, const unsigned char *prev, |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 174 | unsigned char *result) |
| 175 | { |
| 176 | char *path = NULL; |
| 177 | |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 178 | if (msg->use_editor || !msg->given) { |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 179 | int fd; |
| 180 | |
| 181 | /* write the template message before editing: */ |
| 182 | path = git_pathdup("NOTES_EDITMSG"); |
| 183 | fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 184 | if (fd < 0) |
| 185 | die_errno("could not create file '%s'", path); |
| 186 | |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 187 | if (msg->given) |
| 188 | write_or_die(fd, msg->buf.buf, msg->buf.len); |
| 189 | else if (prev && !append_only) |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 190 | write_note_data(fd, prev); |
| 191 | write_or_die(fd, note_template, strlen(note_template)); |
| 192 | |
| 193 | write_commented_object(fd, object); |
| 194 | |
| 195 | close(fd); |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 196 | strbuf_reset(&(msg->buf)); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 197 | |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 198 | if (launch_editor(path, &(msg->buf), NULL)) { |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 199 | die("Please supply the note contents using either -m" \ |
| 200 | " or -F option"); |
| 201 | } |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 202 | stripspace(&(msg->buf), 1); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Johan Herland | 2347fae | 2010-02-13 22:28:33 +0100 | [diff] [blame] | 205 | if (prev && append_only) { |
| 206 | /* Append buf to previous note contents */ |
| 207 | unsigned long size; |
| 208 | enum object_type type; |
| 209 | char *prev_buf = read_sha1_file(prev, &type, &size); |
| 210 | |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 211 | strbuf_grow(&(msg->buf), size + 1); |
| 212 | if (msg->buf.len && prev_buf && size) |
| 213 | strbuf_insert(&(msg->buf), 0, "\n", 1); |
Johan Herland | 2347fae | 2010-02-13 22:28:33 +0100 | [diff] [blame] | 214 | if (prev_buf && size) |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 215 | strbuf_insert(&(msg->buf), 0, prev_buf, size); |
Johan Herland | 2347fae | 2010-02-13 22:28:33 +0100 | [diff] [blame] | 216 | free(prev_buf); |
| 217 | } |
| 218 | |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 219 | if (!msg->buf.len) { |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 220 | fprintf(stderr, "Removing note for object %s\n", |
| 221 | sha1_to_hex(object)); |
| 222 | hashclr(result); |
| 223 | } else { |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 224 | if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) { |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 225 | error("unable to write note object"); |
| 226 | if (path) |
| 227 | error("The note contents has been left in %s", |
| 228 | path); |
| 229 | exit(128); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (path) { |
| 234 | unlink_or_warn(path); |
| 235 | free(path); |
| 236 | } |
| 237 | } |
| 238 | |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 239 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) |
| 240 | { |
| 241 | struct msg_arg *msg = opt->value; |
| 242 | |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 243 | strbuf_grow(&(msg->buf), strlen(arg) + 2); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 244 | if (msg->buf.len) |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 245 | strbuf_addch(&(msg->buf), '\n'); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 246 | strbuf_addstr(&(msg->buf), arg); |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 247 | stripspace(&(msg->buf), 0); |
| 248 | |
| 249 | msg->given = 1; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int parse_file_arg(const struct option *opt, const char *arg, int unset) |
| 254 | { |
| 255 | struct msg_arg *msg = opt->value; |
| 256 | |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 257 | if (msg->buf.len) |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 258 | strbuf_addch(&(msg->buf), '\n'); |
Johan Herland | 348f199 | 2010-02-13 22:28:35 +0100 | [diff] [blame] | 259 | if (!strcmp(arg, "-")) { |
| 260 | if (strbuf_read(&(msg->buf), 0, 1024) < 0) |
| 261 | die_errno("cannot read '%s'", arg); |
| 262 | } else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0) |
| 263 | die_errno("could not open or read '%s'", arg); |
| 264 | stripspace(&(msg->buf), 0); |
| 265 | |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 266 | msg->given = 1; |
| 267 | return 0; |
| 268 | } |
| 269 | |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 270 | static int parse_reuse_arg(const struct option *opt, const char *arg, int unset) |
| 271 | { |
| 272 | struct msg_arg *msg = opt->value; |
| 273 | char *buf; |
| 274 | unsigned char object[20]; |
| 275 | enum object_type type; |
| 276 | unsigned long len; |
| 277 | |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 278 | if (msg->buf.len) |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 279 | strbuf_addch(&(msg->buf), '\n'); |
Johan Herland | 0691cff | 2010-02-13 22:28:36 +0100 | [diff] [blame] | 280 | |
| 281 | if (get_sha1(arg, object)) |
| 282 | die("Failed to resolve '%s' as a valid ref.", arg); |
| 283 | if (!(buf = read_sha1_file(object, &type, &len)) || !len) { |
| 284 | free(buf); |
| 285 | die("Failed to read object '%s'.", arg);; |
| 286 | } |
| 287 | strbuf_add(&(msg->buf), buf, len); |
| 288 | free(buf); |
| 289 | |
| 290 | msg->given = 1; |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int parse_reedit_arg(const struct option *opt, const char *arg, int unset) |
| 295 | { |
| 296 | struct msg_arg *msg = opt->value; |
| 297 | msg->use_editor = 1; |
| 298 | return parse_reuse_arg(opt, arg, unset); |
| 299 | } |
| 300 | |
Johan Herland | 5688184 | 2010-11-09 22:49:47 +0100 | [diff] [blame] | 301 | void commit_notes(struct notes_tree *t, const char *msg) |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 302 | { |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 303 | struct strbuf buf = STRBUF_INIT; |
Johan Herland | 5688184 | 2010-11-09 22:49:47 +0100 | [diff] [blame] | 304 | unsigned char commit_sha1[20]; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 305 | |
| 306 | if (!t) |
| 307 | t = &default_notes_tree; |
| 308 | if (!t->initialized || !t->ref || !*t->ref) |
| 309 | die("Cannot commit uninitialized/unreferenced notes tree"); |
Thomas Rast | 7f710ea | 2010-03-12 18:04:36 +0100 | [diff] [blame] | 310 | if (!t->dirty) |
Johan Herland | 5688184 | 2010-11-09 22:49:47 +0100 | [diff] [blame] | 311 | return; /* don't have to commit an unchanged tree */ |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 312 | |
| 313 | /* Prepare commit message and reflog message */ |
| 314 | strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */ |
| 315 | strbuf_addstr(&buf, msg); |
| 316 | if (buf.buf[buf.len - 1] != '\n') |
| 317 | strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */ |
| 318 | |
Johan Herland | 5688184 | 2010-11-09 22:49:47 +0100 | [diff] [blame] | 319 | create_notes_commit(t, NULL, buf.buf + 7, commit_sha1); |
| 320 | update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 321 | |
| 322 | strbuf_release(&buf); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Ramsay Jones | 4e0d7a8 | 2010-06-23 20:40:19 +0100 | [diff] [blame] | 325 | combine_notes_fn parse_combine_notes_fn(const char *v) |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 326 | { |
| 327 | if (!strcasecmp(v, "overwrite")) |
| 328 | return combine_notes_overwrite; |
| 329 | else if (!strcasecmp(v, "ignore")) |
| 330 | return combine_notes_ignore; |
| 331 | else if (!strcasecmp(v, "concatenate")) |
| 332 | return combine_notes_concatenate; |
Johan Herland | a6a0909 | 2010-11-15 00:57:17 +0100 | [diff] [blame] | 333 | else if (!strcasecmp(v, "cat_sort_uniq")) |
| 334 | return combine_notes_cat_sort_uniq; |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 335 | else |
| 336 | return NULL; |
| 337 | } |
| 338 | |
| 339 | static int notes_rewrite_config(const char *k, const char *v, void *cb) |
| 340 | { |
| 341 | struct notes_rewrite_cfg *c = cb; |
| 342 | if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) { |
| 343 | c->enabled = git_config_bool(k, v); |
| 344 | return 0; |
| 345 | } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) { |
| 346 | if (!v) |
| 347 | config_error_nonbool(k); |
| 348 | c->combine = parse_combine_notes_fn(v); |
| 349 | if (!c->combine) { |
| 350 | error("Bad notes.rewriteMode value: '%s'", v); |
| 351 | return 1; |
| 352 | } |
| 353 | return 0; |
| 354 | } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) { |
| 355 | /* note that a refs/ prefix is implied in the |
| 356 | * underlying for_each_glob_ref */ |
| 357 | if (!prefixcmp(v, "refs/notes/")) |
| 358 | string_list_add_refs_by_glob(c->refs, v); |
| 359 | else |
| 360 | warning("Refusing to rewrite notes in %s" |
| 361 | " (outside of refs/notes/)", v); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | |
| 369 | struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd) |
| 370 | { |
| 371 | struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg)); |
| 372 | const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT); |
| 373 | const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT); |
| 374 | c->cmd = cmd; |
| 375 | c->enabled = 1; |
| 376 | c->combine = combine_notes_concatenate; |
| 377 | c->refs = xcalloc(1, sizeof(struct string_list)); |
| 378 | c->refs->strdup_strings = 1; |
| 379 | c->refs_from_env = 0; |
| 380 | c->mode_from_env = 0; |
| 381 | if (rewrite_mode_env) { |
| 382 | c->mode_from_env = 1; |
| 383 | c->combine = parse_combine_notes_fn(rewrite_mode_env); |
| 384 | if (!c->combine) |
| 385 | error("Bad " GIT_NOTES_REWRITE_MODE_ENVIRONMENT |
| 386 | " value: '%s'", rewrite_mode_env); |
| 387 | } |
| 388 | if (rewrite_refs_env) { |
| 389 | c->refs_from_env = 1; |
| 390 | string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env); |
| 391 | } |
| 392 | git_config(notes_rewrite_config, c); |
| 393 | if (!c->enabled || !c->refs->nr) { |
| 394 | string_list_clear(c->refs, 0); |
| 395 | free(c->refs); |
| 396 | free(c); |
| 397 | return NULL; |
| 398 | } |
| 399 | c->trees = load_notes_trees(c->refs); |
| 400 | string_list_clear(c->refs, 0); |
| 401 | free(c->refs); |
| 402 | return c; |
| 403 | } |
| 404 | |
| 405 | int copy_note_for_rewrite(struct notes_rewrite_cfg *c, |
| 406 | const unsigned char *from_obj, const unsigned char *to_obj) |
| 407 | { |
| 408 | int ret = 0; |
| 409 | int i; |
| 410 | for (i = 0; c->trees[i]; i++) |
| 411 | ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret; |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c) |
| 416 | { |
| 417 | int i; |
| 418 | for (i = 0; c->trees[i]; i++) { |
| 419 | commit_notes(c->trees[i], "Notes added by 'git notes copy'"); |
| 420 | free_notes(c->trees[i]); |
| 421 | } |
| 422 | free(c->trees); |
| 423 | free(c); |
| 424 | } |
| 425 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 426 | static int notes_copy_from_stdin(int force, const char *rewrite_cmd) |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 427 | { |
| 428 | struct strbuf buf = STRBUF_INIT; |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 429 | struct notes_rewrite_cfg *c = NULL; |
Ævar Arnfjörð Bjarmason | ef7a8e3 | 2010-06-14 23:40:05 +0000 | [diff] [blame] | 430 | struct notes_tree *t = NULL; |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 431 | int ret = 0; |
| 432 | |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 433 | if (rewrite_cmd) { |
| 434 | c = init_copy_notes_for_rewrite(rewrite_cmd); |
| 435 | if (!c) |
| 436 | return 0; |
| 437 | } else { |
| 438 | init_notes(NULL, NULL, NULL, 0); |
| 439 | t = &default_notes_tree; |
| 440 | } |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 441 | |
| 442 | while (strbuf_getline(&buf, stdin, '\n') != EOF) { |
| 443 | unsigned char from_obj[20], to_obj[20]; |
| 444 | struct strbuf **split; |
| 445 | int err; |
| 446 | |
| 447 | split = strbuf_split(&buf, ' '); |
| 448 | if (!split[0] || !split[1]) |
| 449 | die("Malformed input line: '%s'.", buf.buf); |
| 450 | strbuf_rtrim(split[0]); |
| 451 | strbuf_rtrim(split[1]); |
| 452 | if (get_sha1(split[0]->buf, from_obj)) |
| 453 | die("Failed to resolve '%s' as a valid ref.", split[0]->buf); |
| 454 | if (get_sha1(split[1]->buf, to_obj)) |
| 455 | die("Failed to resolve '%s' as a valid ref.", split[1]->buf); |
| 456 | |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 457 | if (rewrite_cmd) |
| 458 | err = copy_note_for_rewrite(c, from_obj, to_obj); |
| 459 | else |
| 460 | err = copy_note(t, from_obj, to_obj, force, |
| 461 | combine_notes_overwrite); |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 462 | |
| 463 | if (err) { |
| 464 | error("Failed to copy notes from '%s' to '%s'", |
| 465 | split[0]->buf, split[1]->buf); |
| 466 | ret = 1; |
| 467 | } |
| 468 | |
| 469 | strbuf_list_free(split); |
| 470 | } |
| 471 | |
Thomas Rast | 6956f85 | 2010-03-12 18:04:32 +0100 | [diff] [blame] | 472 | if (!rewrite_cmd) { |
| 473 | commit_notes(t, "Notes added by 'git notes copy'"); |
| 474 | free_notes(t); |
| 475 | } else { |
| 476 | finish_copy_notes_for_rewrite(c); |
| 477 | } |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 478 | return ret; |
| 479 | } |
| 480 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 481 | static struct notes_tree *init_notes_check(const char *subcommand) |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 482 | { |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 483 | struct notes_tree *t; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 484 | init_notes(NULL, NULL, NULL, 0); |
| 485 | t = &default_notes_tree; |
Johan Herland | a0b4dfa | 2010-02-13 22:28:24 +0100 | [diff] [blame] | 486 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 487 | if (prefixcmp(t->ref, "refs/notes/")) |
| 488 | die("Refusing to %s notes in %s (outside of refs/notes/)", |
| 489 | subcommand, t->ref); |
| 490 | return t; |
| 491 | } |
| 492 | |
| 493 | static int list(int argc, const char **argv, const char *prefix) |
| 494 | { |
| 495 | struct notes_tree *t; |
| 496 | unsigned char object[20]; |
| 497 | const unsigned char *note; |
| 498 | int retval = -1; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 499 | struct option options[] = { |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 500 | OPT_END() |
| 501 | }; |
| 502 | |
| 503 | if (argc) |
| 504 | argc = parse_options(argc, argv, prefix, options, |
| 505 | git_notes_list_usage, 0); |
| 506 | |
| 507 | if (1 < argc) { |
| 508 | error("too many parameters"); |
| 509 | usage_with_options(git_notes_list_usage, options); |
| 510 | } |
| 511 | |
| 512 | t = init_notes_check("list"); |
| 513 | if (argc) { |
| 514 | if (get_sha1(argv[0], object)) |
| 515 | die("Failed to resolve '%s' as a valid ref.", argv[0]); |
| 516 | note = get_note(t, object); |
| 517 | if (note) { |
| 518 | puts(sha1_to_hex(note)); |
| 519 | retval = 0; |
| 520 | } else |
| 521 | retval = error("No note found for object %s.", |
| 522 | sha1_to_hex(object)); |
| 523 | } else |
| 524 | retval = for_each_note(t, 0, list_each_note, NULL); |
| 525 | |
| 526 | free_notes(t); |
| 527 | return retval; |
| 528 | } |
| 529 | |
Johan Herland | 84a7e35 | 2011-03-30 02:02:55 +0200 | [diff] [blame^] | 530 | static int append_edit(int argc, const char **argv, const char *prefix); |
| 531 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 532 | static int add(int argc, const char **argv, const char *prefix) |
| 533 | { |
| 534 | int retval = 0, force = 0; |
| 535 | const char *object_ref; |
| 536 | struct notes_tree *t; |
| 537 | unsigned char object[20], new_note[20]; |
| 538 | char logmsg[100]; |
| 539 | const unsigned char *note; |
| 540 | struct msg_arg msg = { 0, 0, STRBUF_INIT }; |
| 541 | struct option options[] = { |
Michael J Gruber | 23c6a80 | 2011-02-15 14:09:12 +0100 | [diff] [blame] | 542 | { OPTION_CALLBACK, 'm', "message", &msg, "msg", |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 543 | "note contents as a string", PARSE_OPT_NONEG, |
| 544 | parse_msg_arg}, |
Michael J Gruber | 23c6a80 | 2011-02-15 14:09:12 +0100 | [diff] [blame] | 545 | { OPTION_CALLBACK, 'F', "file", &msg, "file", |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 546 | "note contents in a file", PARSE_OPT_NONEG, |
| 547 | parse_file_arg}, |
Michael J Gruber | 23c6a80 | 2011-02-15 14:09:12 +0100 | [diff] [blame] | 548 | { OPTION_CALLBACK, 'c', "reedit-message", &msg, "object", |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 549 | "reuse and edit specified note object", PARSE_OPT_NONEG, |
| 550 | parse_reedit_arg}, |
Michael J Gruber | 23c6a80 | 2011-02-15 14:09:12 +0100 | [diff] [blame] | 551 | { OPTION_CALLBACK, 'C', "reuse-message", &msg, "object", |
Johan Herland | 43a61b8 | 2010-02-25 01:48:11 +0100 | [diff] [blame] | 552 | "reuse specified note object", PARSE_OPT_NONEG, |
| 553 | parse_reuse_arg}, |
René Scharfe | 76946b7 | 2010-11-08 19:01:54 +0100 | [diff] [blame] | 554 | OPT__FORCE(&force, "replace existing notes"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 555 | OPT_END() |
| 556 | }; |
| 557 | |
| 558 | argc = parse_options(argc, argv, prefix, options, git_notes_add_usage, |
Johan Herland | 84a7e35 | 2011-03-30 02:02:55 +0200 | [diff] [blame^] | 559 | PARSE_OPT_KEEP_ARGV0); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 560 | |
Johan Herland | 84a7e35 | 2011-03-30 02:02:55 +0200 | [diff] [blame^] | 561 | if (2 < argc) { |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 562 | error("too many parameters"); |
| 563 | usage_with_options(git_notes_add_usage, options); |
| 564 | } |
| 565 | |
Johan Herland | 84a7e35 | 2011-03-30 02:02:55 +0200 | [diff] [blame^] | 566 | object_ref = argc > 1 ? argv[1] : "HEAD"; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 567 | |
| 568 | if (get_sha1(object_ref, object)) |
| 569 | die("Failed to resolve '%s' as a valid ref.", object_ref); |
| 570 | |
| 571 | t = init_notes_check("add"); |
| 572 | note = get_note(t, object); |
| 573 | |
| 574 | if (note) { |
| 575 | if (!force) { |
Johan Herland | 84a7e35 | 2011-03-30 02:02:55 +0200 | [diff] [blame^] | 576 | if (!msg.given) { |
| 577 | /* |
| 578 | * Redirect to "edit" subcommand. |
| 579 | * |
| 580 | * We only end up here if none of -m/-F/-c/-C |
| 581 | * or -f are given. The original args are |
| 582 | * therefore still in argv[0-1]. |
| 583 | */ |
| 584 | argv[0] = "edit"; |
| 585 | free_notes(t); |
| 586 | return append_edit(argc, argv, prefix); |
| 587 | } |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 588 | retval = error("Cannot add notes. Found existing notes " |
| 589 | "for object %s. Use '-f' to overwrite " |
| 590 | "existing notes", sha1_to_hex(object)); |
| 591 | goto out; |
| 592 | } |
| 593 | fprintf(stderr, "Overwriting existing notes for object %s\n", |
| 594 | sha1_to_hex(object)); |
| 595 | } |
| 596 | |
| 597 | create_note(object, &msg, 0, note, new_note); |
| 598 | |
| 599 | if (is_null_sha1(new_note)) |
| 600 | remove_note(t, object); |
Johan Herland | 180619a | 2010-11-15 00:52:26 +0100 | [diff] [blame] | 601 | else if (add_note(t, object, new_note, combine_notes_overwrite)) |
| 602 | die("BUG: combine_notes_overwrite failed"); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 603 | |
| 604 | snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'", |
| 605 | is_null_sha1(new_note) ? "removed" : "added", "add"); |
| 606 | commit_notes(t, logmsg); |
| 607 | out: |
| 608 | free_notes(t); |
| 609 | strbuf_release(&(msg.buf)); |
| 610 | return retval; |
| 611 | } |
| 612 | |
| 613 | static int copy(int argc, const char **argv, const char *prefix) |
| 614 | { |
| 615 | int retval = 0, force = 0, from_stdin = 0; |
| 616 | const unsigned char *from_note, *note; |
| 617 | const char *object_ref; |
| 618 | unsigned char object[20], from_obj[20]; |
| 619 | struct notes_tree *t; |
| 620 | const char *rewrite_cmd = NULL; |
| 621 | struct option options[] = { |
René Scharfe | 76946b7 | 2010-11-08 19:01:54 +0100 | [diff] [blame] | 622 | OPT__FORCE(&force, "replace existing notes"), |
Thomas Rast | 160baa0 | 2010-03-12 18:04:31 +0100 | [diff] [blame] | 623 | OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 624 | OPT_STRING(0, "for-rewrite", &rewrite_cmd, "command", |
| 625 | "load rewriting config for <command> (implies " |
| 626 | "--stdin)"), |
| 627 | OPT_END() |
| 628 | }; |
| 629 | |
| 630 | argc = parse_options(argc, argv, prefix, options, git_notes_copy_usage, |
| 631 | 0); |
| 632 | |
| 633 | if (from_stdin || rewrite_cmd) { |
| 634 | if (argc) { |
| 635 | error("too many parameters"); |
| 636 | usage_with_options(git_notes_copy_usage, options); |
| 637 | } else { |
| 638 | return notes_copy_from_stdin(force, rewrite_cmd); |
| 639 | } |
| 640 | } |
| 641 | |
Jeff King | bbb1b8a | 2010-06-28 04:59:07 -0400 | [diff] [blame] | 642 | if (argc < 2) { |
| 643 | error("too few parameters"); |
| 644 | usage_with_options(git_notes_copy_usage, options); |
| 645 | } |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 646 | if (2 < argc) { |
| 647 | error("too many parameters"); |
| 648 | usage_with_options(git_notes_copy_usage, options); |
| 649 | } |
| 650 | |
| 651 | if (get_sha1(argv[0], from_obj)) |
| 652 | die("Failed to resolve '%s' as a valid ref.", argv[0]); |
| 653 | |
| 654 | object_ref = 1 < argc ? argv[1] : "HEAD"; |
| 655 | |
| 656 | if (get_sha1(object_ref, object)) |
| 657 | die("Failed to resolve '%s' as a valid ref.", object_ref); |
| 658 | |
| 659 | t = init_notes_check("copy"); |
| 660 | note = get_note(t, object); |
| 661 | |
| 662 | if (note) { |
| 663 | if (!force) { |
| 664 | retval = error("Cannot copy notes. Found existing " |
| 665 | "notes for object %s. Use '-f' to " |
| 666 | "overwrite existing notes", |
| 667 | sha1_to_hex(object)); |
| 668 | goto out; |
| 669 | } |
| 670 | fprintf(stderr, "Overwriting existing notes for object %s\n", |
| 671 | sha1_to_hex(object)); |
| 672 | } |
| 673 | |
| 674 | from_note = get_note(t, from_obj); |
| 675 | if (!from_note) { |
| 676 | retval = error("Missing notes on source object %s. Cannot " |
| 677 | "copy.", sha1_to_hex(from_obj)); |
| 678 | goto out; |
| 679 | } |
| 680 | |
Johan Herland | 180619a | 2010-11-15 00:52:26 +0100 | [diff] [blame] | 681 | if (add_note(t, object, from_note, combine_notes_overwrite)) |
| 682 | die("BUG: combine_notes_overwrite failed"); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 683 | commit_notes(t, "Notes added by 'git notes copy'"); |
| 684 | out: |
| 685 | free_notes(t); |
| 686 | return retval; |
| 687 | } |
| 688 | |
| 689 | static int append_edit(int argc, const char **argv, const char *prefix) |
| 690 | { |
| 691 | const char *object_ref; |
| 692 | struct notes_tree *t; |
| 693 | unsigned char object[20], new_note[20]; |
| 694 | const unsigned char *note; |
| 695 | char logmsg[100]; |
| 696 | const char * const *usage; |
| 697 | struct msg_arg msg = { 0, 0, STRBUF_INIT }; |
| 698 | struct option options[] = { |
Michael J Gruber | 23c6a80 | 2011-02-15 14:09:12 +0100 | [diff] [blame] | 699 | { OPTION_CALLBACK, 'm', "message", &msg, "msg", |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 700 | "note contents as a string", PARSE_OPT_NONEG, |
| 701 | parse_msg_arg}, |
Michael J Gruber | 23c6a80 | 2011-02-15 14:09:12 +0100 | [diff] [blame] | 702 | { OPTION_CALLBACK, 'F', "file", &msg, "file", |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 703 | "note contents in a file", PARSE_OPT_NONEG, |
| 704 | parse_file_arg}, |
Michael J Gruber | 23c6a80 | 2011-02-15 14:09:12 +0100 | [diff] [blame] | 705 | { OPTION_CALLBACK, 'c', "reedit-message", &msg, "object", |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 706 | "reuse and edit specified note object", PARSE_OPT_NONEG, |
| 707 | parse_reedit_arg}, |
Michael J Gruber | 23c6a80 | 2011-02-15 14:09:12 +0100 | [diff] [blame] | 708 | { OPTION_CALLBACK, 'C', "reuse-message", &msg, "object", |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 709 | "reuse specified note object", PARSE_OPT_NONEG, |
| 710 | parse_reuse_arg}, |
| 711 | OPT_END() |
| 712 | }; |
| 713 | int edit = !strcmp(argv[0], "edit"); |
| 714 | |
| 715 | usage = edit ? git_notes_edit_usage : git_notes_append_usage; |
| 716 | argc = parse_options(argc, argv, prefix, options, usage, |
| 717 | PARSE_OPT_KEEP_ARGV0); |
| 718 | |
| 719 | if (2 < argc) { |
| 720 | error("too many parameters"); |
| 721 | usage_with_options(usage, options); |
| 722 | } |
| 723 | |
| 724 | if (msg.given && edit) |
| 725 | fprintf(stderr, "The -m/-F/-c/-C options have been deprecated " |
| 726 | "for the 'edit' subcommand.\n" |
| 727 | "Please use 'git notes add -f -m/-F/-c/-C' instead.\n"); |
| 728 | |
| 729 | object_ref = 1 < argc ? argv[1] : "HEAD"; |
| 730 | |
| 731 | if (get_sha1(object_ref, object)) |
| 732 | die("Failed to resolve '%s' as a valid ref.", object_ref); |
| 733 | |
| 734 | t = init_notes_check(argv[0]); |
| 735 | note = get_note(t, object); |
| 736 | |
| 737 | create_note(object, &msg, !edit, note, new_note); |
| 738 | |
| 739 | if (is_null_sha1(new_note)) |
| 740 | remove_note(t, object); |
Johan Herland | 180619a | 2010-11-15 00:52:26 +0100 | [diff] [blame] | 741 | else if (add_note(t, object, new_note, combine_notes_overwrite)) |
| 742 | die("BUG: combine_notes_overwrite failed"); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 743 | |
| 744 | snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'", |
| 745 | is_null_sha1(new_note) ? "removed" : "added", argv[0]); |
| 746 | commit_notes(t, logmsg); |
| 747 | free_notes(t); |
| 748 | strbuf_release(&(msg.buf)); |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int show(int argc, const char **argv, const char *prefix) |
| 753 | { |
| 754 | const char *object_ref; |
| 755 | struct notes_tree *t; |
| 756 | unsigned char object[20]; |
| 757 | const unsigned char *note; |
| 758 | int retval; |
| 759 | struct option options[] = { |
| 760 | OPT_END() |
| 761 | }; |
| 762 | |
| 763 | argc = parse_options(argc, argv, prefix, options, git_notes_show_usage, |
| 764 | 0); |
| 765 | |
| 766 | if (1 < argc) { |
| 767 | error("too many parameters"); |
| 768 | usage_with_options(git_notes_show_usage, options); |
| 769 | } |
| 770 | |
| 771 | object_ref = argc ? argv[0] : "HEAD"; |
| 772 | |
| 773 | if (get_sha1(object_ref, object)) |
| 774 | die("Failed to resolve '%s' as a valid ref.", object_ref); |
| 775 | |
| 776 | t = init_notes_check("show"); |
| 777 | note = get_note(t, object); |
| 778 | |
| 779 | if (!note) |
| 780 | retval = error("No note found for object %s.", |
| 781 | sha1_to_hex(object)); |
| 782 | else { |
| 783 | const char *show_args[3] = {"show", sha1_to_hex(note), NULL}; |
| 784 | retval = execv_git_cmd(show_args); |
| 785 | } |
| 786 | free_notes(t); |
| 787 | return retval; |
| 788 | } |
| 789 | |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 790 | static int merge_abort(struct notes_merge_options *o) |
| 791 | { |
| 792 | int ret = 0; |
| 793 | |
| 794 | /* |
| 795 | * Remove .git/NOTES_MERGE_PARTIAL and .git/NOTES_MERGE_REF, and call |
| 796 | * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE. |
| 797 | */ |
| 798 | |
| 799 | if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0)) |
| 800 | ret += error("Failed to delete ref NOTES_MERGE_PARTIAL"); |
| 801 | if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF)) |
| 802 | ret += error("Failed to delete ref NOTES_MERGE_REF"); |
| 803 | if (notes_merge_abort(o)) |
| 804 | ret += error("Failed to remove 'git notes merge' worktree"); |
| 805 | return ret; |
| 806 | } |
| 807 | |
| 808 | static int merge_commit(struct notes_merge_options *o) |
| 809 | { |
| 810 | struct strbuf msg = STRBUF_INIT; |
Johan Herland | 6cfd6a9 | 2010-11-09 22:49:54 +0100 | [diff] [blame] | 811 | unsigned char sha1[20], parent_sha1[20]; |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 812 | struct notes_tree *t; |
| 813 | struct commit *partial; |
| 814 | struct pretty_print_context pretty_ctx; |
| 815 | |
| 816 | /* |
| 817 | * Read partial merge result from .git/NOTES_MERGE_PARTIAL, |
| 818 | * and target notes ref from .git/NOTES_MERGE_REF. |
| 819 | */ |
| 820 | |
| 821 | if (get_sha1("NOTES_MERGE_PARTIAL", sha1)) |
| 822 | die("Failed to read ref NOTES_MERGE_PARTIAL"); |
| 823 | else if (!(partial = lookup_commit_reference(sha1))) |
| 824 | die("Could not find commit from NOTES_MERGE_PARTIAL."); |
| 825 | else if (parse_commit(partial)) |
| 826 | die("Could not parse commit from NOTES_MERGE_PARTIAL."); |
| 827 | |
Johan Herland | 6cfd6a9 | 2010-11-09 22:49:54 +0100 | [diff] [blame] | 828 | if (partial->parents) |
| 829 | hashcpy(parent_sha1, partial->parents->item->object.sha1); |
| 830 | else |
| 831 | hashclr(parent_sha1); |
| 832 | |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 833 | t = xcalloc(1, sizeof(struct notes_tree)); |
| 834 | init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0); |
| 835 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 836 | o->local_ref = resolve_ref("NOTES_MERGE_REF", sha1, 0, NULL); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 837 | if (!o->local_ref) |
| 838 | die("Failed to resolve NOTES_MERGE_REF"); |
| 839 | |
| 840 | if (notes_merge_commit(o, t, partial, sha1)) |
| 841 | die("Failed to finalize notes merge"); |
| 842 | |
| 843 | /* Reuse existing commit message in reflog message */ |
| 844 | memset(&pretty_ctx, 0, sizeof(pretty_ctx)); |
| 845 | format_commit_message(partial, "%s", &msg, &pretty_ctx); |
| 846 | strbuf_trim(&msg); |
| 847 | strbuf_insert(&msg, 0, "notes: ", 7); |
Johan Herland | 6cfd6a9 | 2010-11-09 22:49:54 +0100 | [diff] [blame] | 848 | update_ref(msg.buf, o->local_ref, sha1, |
| 849 | is_null_sha1(parent_sha1) ? NULL : parent_sha1, |
| 850 | 0, DIE_ON_ERR); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 851 | |
| 852 | free_notes(t); |
| 853 | strbuf_release(&msg); |
| 854 | return merge_abort(o); |
| 855 | } |
| 856 | |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 857 | static int merge(int argc, const char **argv, const char *prefix) |
| 858 | { |
| 859 | struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT; |
| 860 | unsigned char result_sha1[20]; |
Johan Herland | 2085b16 | 2010-11-15 00:54:11 +0100 | [diff] [blame] | 861 | struct notes_tree *t; |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 862 | struct notes_merge_options o; |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 863 | int do_merge = 0, do_commit = 0, do_abort = 0; |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 864 | int verbosity = 0, result; |
Johan Herland | 3228e67 | 2010-11-15 00:55:12 +0100 | [diff] [blame] | 865 | const char *strategy = NULL; |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 866 | struct option options[] = { |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 867 | OPT_GROUP("General options"), |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 868 | OPT__VERBOSITY(&verbosity), |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 869 | OPT_GROUP("Merge options"), |
Johan Herland | 3228e67 | 2010-11-15 00:55:12 +0100 | [diff] [blame] | 870 | OPT_STRING('s', "strategy", &strategy, "strategy", |
Johan Herland | a6a0909 | 2010-11-15 00:57:17 +0100 | [diff] [blame] | 871 | "resolve notes conflicts using the given strategy " |
| 872 | "(manual/ours/theirs/union/cat_sort_uniq)"), |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 873 | OPT_GROUP("Committing unmerged notes"), |
| 874 | { OPTION_BOOLEAN, 0, "commit", &do_commit, NULL, |
| 875 | "finalize notes merge by committing unmerged notes", |
| 876 | PARSE_OPT_NOARG | PARSE_OPT_NONEG }, |
| 877 | OPT_GROUP("Aborting notes merge resolution"), |
| 878 | { OPTION_BOOLEAN, 0, "abort", &do_abort, NULL, |
| 879 | "abort notes merge", |
| 880 | PARSE_OPT_NOARG | PARSE_OPT_NONEG }, |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 881 | OPT_END() |
| 882 | }; |
| 883 | |
| 884 | argc = parse_options(argc, argv, prefix, options, |
| 885 | git_notes_merge_usage, 0); |
| 886 | |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 887 | if (strategy || do_commit + do_abort == 0) |
| 888 | do_merge = 1; |
| 889 | if (do_merge + do_commit + do_abort != 1) { |
| 890 | error("cannot mix --commit, --abort or -s/--strategy"); |
| 891 | usage_with_options(git_notes_merge_usage, options); |
| 892 | } |
| 893 | |
| 894 | if (do_merge && argc != 1) { |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 895 | error("Must specify a notes ref to merge"); |
| 896 | usage_with_options(git_notes_merge_usage, options); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 897 | } else if (!do_merge && argc) { |
| 898 | error("too many parameters"); |
| 899 | usage_with_options(git_notes_merge_usage, options); |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | init_notes_merge_options(&o); |
| 903 | o.verbosity = verbosity + NOTES_MERGE_VERBOSITY_DEFAULT; |
| 904 | |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 905 | if (do_abort) |
| 906 | return merge_abort(&o); |
| 907 | if (do_commit) |
| 908 | return merge_commit(&o); |
| 909 | |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 910 | o.local_ref = default_notes_ref(); |
| 911 | strbuf_addstr(&remote_ref, argv[0]); |
| 912 | expand_notes_ref(&remote_ref); |
| 913 | o.remote_ref = remote_ref.buf; |
| 914 | |
Johan Herland | 3228e67 | 2010-11-15 00:55:12 +0100 | [diff] [blame] | 915 | if (strategy) { |
| 916 | if (!strcmp(strategy, "manual")) |
| 917 | o.strategy = NOTES_MERGE_RESOLVE_MANUAL; |
| 918 | else if (!strcmp(strategy, "ours")) |
| 919 | o.strategy = NOTES_MERGE_RESOLVE_OURS; |
| 920 | else if (!strcmp(strategy, "theirs")) |
| 921 | o.strategy = NOTES_MERGE_RESOLVE_THEIRS; |
| 922 | else if (!strcmp(strategy, "union")) |
| 923 | o.strategy = NOTES_MERGE_RESOLVE_UNION; |
Johan Herland | a6a0909 | 2010-11-15 00:57:17 +0100 | [diff] [blame] | 924 | else if (!strcmp(strategy, "cat_sort_uniq")) |
| 925 | o.strategy = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ; |
Johan Herland | 3228e67 | 2010-11-15 00:55:12 +0100 | [diff] [blame] | 926 | else { |
| 927 | error("Unknown -s/--strategy: %s", strategy); |
| 928 | usage_with_options(git_notes_merge_usage, options); |
| 929 | } |
| 930 | } |
| 931 | |
Johan Herland | 2085b16 | 2010-11-15 00:54:11 +0100 | [diff] [blame] | 932 | t = init_notes_check("merge"); |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 933 | |
| 934 | strbuf_addf(&msg, "notes: Merged notes from %s into %s", |
| 935 | remote_ref.buf, default_notes_ref()); |
Johan Herland | 443259c | 2010-11-09 22:49:53 +0100 | [diff] [blame] | 936 | strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */ |
Johan Herland | 2085b16 | 2010-11-15 00:54:11 +0100 | [diff] [blame] | 937 | |
| 938 | result = notes_merge(&o, t, result_sha1); |
| 939 | |
| 940 | if (result >= 0) /* Merge resulted (trivially) in result_sha1 */ |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 941 | /* Update default notes ref with new commit */ |
| 942 | update_ref(msg.buf, default_notes_ref(), result_sha1, NULL, |
| 943 | 0, DIE_ON_ERR); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 944 | else { /* Merge has unresolved conflicts */ |
| 945 | /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */ |
| 946 | update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_sha1, NULL, |
| 947 | 0, DIE_ON_ERR); |
| 948 | /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */ |
| 949 | if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL)) |
| 950 | die("Failed to store link to current notes ref (%s)", |
| 951 | default_notes_ref()); |
| 952 | printf("Automatic notes merge failed. Fix conflicts in %s and " |
| 953 | "commit the result with 'git notes merge --commit', or " |
| 954 | "abort the merge with 'git notes merge --abort'.\n", |
Johan Herland | 809f38c | 2010-11-09 22:49:51 +0100 | [diff] [blame] | 955 | git_path(NOTES_MERGE_WORKTREE)); |
Johan Herland | 6abb365 | 2010-11-09 22:49:52 +0100 | [diff] [blame] | 956 | } |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 957 | |
Johan Herland | 2085b16 | 2010-11-15 00:54:11 +0100 | [diff] [blame] | 958 | free_notes(t); |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 959 | strbuf_release(&remote_ref); |
| 960 | strbuf_release(&msg); |
Johan Herland | 809f38c | 2010-11-09 22:49:51 +0100 | [diff] [blame] | 961 | return result < 0; /* return non-zero on conflicts */ |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 962 | } |
| 963 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 964 | static int remove_cmd(int argc, const char **argv, const char *prefix) |
| 965 | { |
| 966 | struct option options[] = { |
| 967 | OPT_END() |
| 968 | }; |
| 969 | const char *object_ref; |
| 970 | struct notes_tree *t; |
| 971 | unsigned char object[20]; |
Johan Herland | 1ee1e43 | 2010-08-31 17:56:50 +0200 | [diff] [blame] | 972 | int retval; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 973 | |
| 974 | argc = parse_options(argc, argv, prefix, options, |
| 975 | git_notes_remove_usage, 0); |
| 976 | |
| 977 | if (1 < argc) { |
| 978 | error("too many parameters"); |
| 979 | usage_with_options(git_notes_remove_usage, options); |
| 980 | } |
| 981 | |
| 982 | object_ref = argc ? argv[0] : "HEAD"; |
| 983 | |
| 984 | if (get_sha1(object_ref, object)) |
| 985 | die("Failed to resolve '%s' as a valid ref.", object_ref); |
| 986 | |
| 987 | t = init_notes_check("remove"); |
| 988 | |
Johan Herland | 1ee1e43 | 2010-08-31 17:56:50 +0200 | [diff] [blame] | 989 | retval = remove_note(t, object); |
| 990 | if (retval) |
| 991 | fprintf(stderr, "Object %s has no note\n", sha1_to_hex(object)); |
| 992 | else { |
| 993 | fprintf(stderr, "Removing note for object %s\n", |
| 994 | sha1_to_hex(object)); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 995 | |
Johan Herland | 1ee1e43 | 2010-08-31 17:56:50 +0200 | [diff] [blame] | 996 | commit_notes(t, "Notes removed by 'git notes remove'"); |
| 997 | } |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 998 | free_notes(t); |
Johan Herland | 1ee1e43 | 2010-08-31 17:56:50 +0200 | [diff] [blame] | 999 | return retval; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static int prune(int argc, const char **argv, const char *prefix) |
| 1003 | { |
| 1004 | struct notes_tree *t; |
Michael J Gruber | a9f2adf | 2010-05-14 23:42:07 +0200 | [diff] [blame] | 1005 | int show_only = 0, verbose = 0; |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1006 | struct option options[] = { |
René Scharfe | e21adb8 | 2010-11-08 18:58:51 +0100 | [diff] [blame] | 1007 | OPT__DRY_RUN(&show_only, "do not remove, show only"), |
René Scharfe | fd03881 | 2010-11-08 18:56:39 +0100 | [diff] [blame] | 1008 | OPT__VERBOSE(&verbose, "report pruned notes"), |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1009 | OPT_END() |
| 1010 | }; |
| 1011 | |
| 1012 | argc = parse_options(argc, argv, prefix, options, git_notes_prune_usage, |
| 1013 | 0); |
| 1014 | |
| 1015 | if (argc) { |
| 1016 | error("too many parameters"); |
| 1017 | usage_with_options(git_notes_prune_usage, options); |
| 1018 | } |
| 1019 | |
| 1020 | t = init_notes_check("prune"); |
| 1021 | |
Michael J Gruber | a9f2adf | 2010-05-14 23:42:07 +0200 | [diff] [blame] | 1022 | prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) | |
| 1023 | (show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) ); |
| 1024 | if (!show_only) |
| 1025 | commit_notes(t, "Notes removed by 'git notes prune'"); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1026 | free_notes(t); |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
Johan Herland | 618cd75 | 2010-11-09 22:49:57 +0100 | [diff] [blame] | 1030 | static int get_ref(int argc, const char **argv, const char *prefix) |
| 1031 | { |
| 1032 | struct option options[] = { OPT_END() }; |
| 1033 | argc = parse_options(argc, argv, prefix, options, |
| 1034 | git_notes_get_ref_usage, 0); |
| 1035 | |
| 1036 | if (argc) { |
| 1037 | error("too many parameters"); |
| 1038 | usage_with_options(git_notes_get_ref_usage, options); |
| 1039 | } |
| 1040 | |
| 1041 | puts(default_notes_ref()); |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1045 | int cmd_notes(int argc, const char **argv, const char *prefix) |
| 1046 | { |
| 1047 | int result; |
| 1048 | const char *override_notes_ref = NULL; |
| 1049 | struct option options[] = { |
Thomas Rast | dcf783a | 2010-03-12 18:04:35 +0100 | [diff] [blame] | 1050 | OPT_STRING(0, "ref", &override_notes_ref, "notes_ref", |
| 1051 | "use notes from <notes_ref>"), |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 1052 | OPT_END() |
| 1053 | }; |
| 1054 | |
| 1055 | git_config(git_default_config, NULL); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1056 | argc = parse_options(argc, argv, prefix, options, git_notes_usage, |
| 1057 | PARSE_OPT_STOP_AT_NON_OPTION); |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 1058 | |
Thomas Rast | dcf783a | 2010-03-12 18:04:35 +0100 | [diff] [blame] | 1059 | if (override_notes_ref) { |
| 1060 | struct strbuf sb = STRBUF_INIT; |
Thomas Rast | dcf783a | 2010-03-12 18:04:35 +0100 | [diff] [blame] | 1061 | strbuf_addstr(&sb, override_notes_ref); |
Johan Herland | 8ef313e | 2010-11-09 22:49:45 +0100 | [diff] [blame] | 1062 | expand_notes_ref(&sb); |
Thomas Rast | dcf783a | 2010-03-12 18:04:35 +0100 | [diff] [blame] | 1063 | setenv("GIT_NOTES_REF", sb.buf, 1); |
| 1064 | strbuf_release(&sb); |
| 1065 | } |
| 1066 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1067 | if (argc < 1 || !strcmp(argv[0], "list")) |
| 1068 | result = list(argc, argv, prefix); |
| 1069 | else if (!strcmp(argv[0], "add")) |
| 1070 | result = add(argc, argv, prefix); |
| 1071 | else if (!strcmp(argv[0], "copy")) |
| 1072 | result = copy(argc, argv, prefix); |
| 1073 | else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit")) |
| 1074 | result = append_edit(argc, argv, prefix); |
| 1075 | else if (!strcmp(argv[0], "show")) |
| 1076 | result = show(argc, argv, prefix); |
Johan Herland | 75ef3f4 | 2010-11-09 22:49:46 +0100 | [diff] [blame] | 1077 | else if (!strcmp(argv[0], "merge")) |
| 1078 | result = merge(argc, argv, prefix); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1079 | else if (!strcmp(argv[0], "remove")) |
| 1080 | result = remove_cmd(argc, argv, prefix); |
| 1081 | else if (!strcmp(argv[0], "prune")) |
| 1082 | result = prune(argc, argv, prefix); |
Johan Herland | 618cd75 | 2010-11-09 22:49:57 +0100 | [diff] [blame] | 1083 | else if (!strcmp(argv[0], "get-ref")) |
| 1084 | result = get_ref(argc, argv, prefix); |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1085 | else { |
| 1086 | result = error("Unknown subcommand: %s", argv[0]); |
Johan Herland | 92b3385 | 2010-02-13 22:28:25 +0100 | [diff] [blame] | 1087 | usage_with_options(git_notes_usage, options); |
| 1088 | } |
| 1089 | |
Stephen Boyd | 74884b5 | 2010-02-27 00:59:22 -0800 | [diff] [blame] | 1090 | return result ? 1 : 0; |
Johan Herland | cd067d3 | 2010-02-13 22:28:20 +0100 | [diff] [blame] | 1091 | } |