int cmd_notes(int argc, const char **argv, const char *prefix) { int result; const char *override_notes_ref = NULL; struct option options[] = { OPT_STRING(0, "ref", &override_notes_ref, "notes_ref", "use notes from <notes_ref>"), OPT_END() }; git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, git_notes_usage, PARSE_OPT_STOP_AT_NON_OPTION); if (override_notes_ref) { struct strbuf sb = STRBUF_INIT; if (!prefixcmp(override_notes_ref, "refs/notes/")) /* we're happy */; else if (!prefixcmp(override_notes_ref, "notes/")) strbuf_addstr(&sb, "refs/"); else strbuf_addstr(&sb, "refs/notes/"); strbuf_addstr(&sb, override_notes_ref); setenv("GIT_NOTES_REF", sb.buf, 1); strbuf_release(&sb); } if (argc < 1 || !strcmp(argv[0], "list")) result = list(argc, argv, prefix); else if (!strcmp(argv[0], "add")) result = add(argc, argv, prefix); else if (!strcmp(argv[0], "copy")) result = copy(argc, argv, prefix); else if (!strcmp(argv[0], "append") || !strcmp(argv[0], "edit")) result = append_edit(argc, argv, prefix); else if (!strcmp(argv[0], "show")) result = show(argc, argv, prefix); else if (!strcmp(argv[0], "remove")) result = remove_cmd(argc, argv, prefix); else if (!strcmp(argv[0], "prune")) result = prune(argc, argv, prefix); else { result = error("Unknown subcommand: %s", argv[0]); usage_with_options(git_notes_usage, options); } return result ? 1 : 0; }
static int add(int argc, const char **argv, const char *prefix) { int force = 0, allow_empty = 0; const char *object_ref; struct notes_tree *t; unsigned char object[20], new_note[20]; const unsigned char *note; struct note_data d = { 0, 0, NULL, STRBUF_INIT }; struct option options[] = { { OPTION_CALLBACK, 'm', "message", &d, N_("message"), N_("note contents as a string"), PARSE_OPT_NONEG, parse_msg_arg}, { OPTION_CALLBACK, 'F', "file", &d, N_("file"), N_("note contents in a file"), PARSE_OPT_NONEG, parse_file_arg}, { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"), N_("reuse and edit specified note object"), PARSE_OPT_NONEG, parse_reedit_arg}, { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"), N_("reuse specified note object"), PARSE_OPT_NONEG, parse_reuse_arg}, OPT_BOOL(0, "allow-empty", &allow_empty, N_("allow storing empty note")), OPT__FORCE(&force, N_("replace existing notes")), OPT_END() }; argc = parse_options(argc, argv, prefix, options, git_notes_add_usage, PARSE_OPT_KEEP_ARGV0); if (2 < argc) { error(_("too many parameters")); usage_with_options(git_notes_add_usage, options); } object_ref = argc > 1 ? argv[1] : "HEAD"; if (get_sha1(object_ref, object)) die(_("Failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check("add"); note = get_note(t, object); if (note) { if (!force) { free_notes(t); if (d.given) { free_note_data(&d); return error(_("Cannot add notes. " "Found existing notes for object %s. " "Use '-f' to overwrite existing notes"), sha1_to_hex(object)); } /* * Redirect to "edit" subcommand. * * We only end up here if none of -m/-F/-c/-C or -f are * given. The original args are therefore still in * argv[0-1]. */ argv[0] = "edit"; return append_edit(argc, argv, prefix); } fprintf(stderr, _("Overwriting existing notes for object %s\n"), sha1_to_hex(object)); } prepare_note_data(object, &d, note); if (d.buf.len || allow_empty) { write_note_data(&d, new_note); if (add_note(t, object, new_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); commit_notes(t, "Notes added by 'git notes add'"); } else { fprintf(stderr, _("Removing note for object %s\n"), sha1_to_hex(object)); remove_note(t, object); commit_notes(t, "Notes removed by 'git notes add'"); } free_note_data(&d); free_notes(t); return 0; }
static int add(int argc, const char **argv, const char *prefix) { int retval = 0, force = 0; const char *object_ref; struct notes_tree *t; unsigned char object[20], new_note[20]; char logmsg[100]; const unsigned char *note; struct msg_arg msg = { 0, 0, STRBUF_INIT }; struct option options[] = { { OPTION_CALLBACK, 'm', "message", &msg, N_("message"), N_("note contents as a string"), PARSE_OPT_NONEG, parse_msg_arg}, { OPTION_CALLBACK, 'F', "file", &msg, N_("file"), N_("note contents in a file"), PARSE_OPT_NONEG, parse_file_arg}, { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"), N_("reuse and edit specified note object"), PARSE_OPT_NONEG, parse_reedit_arg}, { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"), N_("reuse specified note object"), PARSE_OPT_NONEG, parse_reuse_arg}, OPT__FORCE(&force, N_("replace existing notes")), OPT_END() }; argc = parse_options(argc, argv, prefix, options, git_notes_add_usage, PARSE_OPT_KEEP_ARGV0); if (2 < argc) { error(_("too many parameters")); usage_with_options(git_notes_add_usage, options); } object_ref = argc > 1 ? argv[1] : "HEAD"; if (get_sha1(object_ref, object)) die(_("Failed to resolve '%s' as a valid ref."), object_ref); t = init_notes_check("add"); note = get_note(t, object); if (note) { if (!force) { if (!msg.given) { /* * Redirect to "edit" subcommand. * * We only end up here if none of -m/-F/-c/-C * or -f are given. The original args are * therefore still in argv[0-1]. */ argv[0] = "edit"; free_notes(t); return append_edit(argc, argv, prefix); } retval = error(_("Cannot add notes. Found existing notes " "for object %s. Use '-f' to overwrite " "existing notes"), sha1_to_hex(object)); goto out; } fprintf(stderr, _("Overwriting existing notes for object %s\n"), sha1_to_hex(object)); } create_note(object, &msg, 0, note, new_note); if (is_null_sha1(new_note)) remove_note(t, object); else if (add_note(t, object, new_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'", is_null_sha1(new_note) ? "removed" : "added", "add"); commit_notes(t, logmsg); out: free_notes(t); strbuf_release(&(msg.buf)); return retval; }