/*
* Builtin "git notes"
*
* Copyright (c) 2010 Johan Herland <johan@herland.net>
*
* Based on git-notes.sh by Johannes Schindelin,
* and builtin/tag.c by Kristian Høgsberg and Carlos Rica.
*/
#include "cache.h"
#include "config.h"
#include "builtin.h"
#include "notes.h"
#include "object-store.h"
#include "repository.h"
#include "blob.h"
#include "pretty.h"
#include "refs.h"
#include "exec-cmd.h"
#include "run-command.h"
#include "parse-options.h"
#include "string-list.h"
#include "notes-merge.h"
#include "notes-utils.h"
#include "worktree.h"
static const char * const git_notes_usage[] = {
N_("git notes [--ref <notes-ref>] [list [<object>]]"),
N_("git notes [--ref <notes-ref>] add [-f] [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
N_("git notes [--ref <notes-ref>] copy [-f] <from-object> <to-object>"),
N_("git notes [--ref <notes-ref>] append [--allow-empty] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]"),
N_("git notes [--ref <notes-ref>] edit [--allow-empty] [<object>]"),
N_("git notes [--ref <notes-ref>] show [<object>]"),
N_("git notes [--ref <notes-ref>] merge [-v | -q] [-s <strategy>] <notes-ref>"),
N_("git notes merge --commit [-v | -q]"),
N_("git notes merge --abort [-v | -q]"),
N_("git notes [--ref <notes-ref>] remove [<object>...]"),
N_("git notes [--ref <notes-ref>] prune [-n] [-v]"),
N_("git notes [--ref <notes-ref>] get-ref"),
NULL
};
static const char * const git_notes_list_usage[] = {
N_("git notes [list [<object>]]"),
NULL
};
static const char * const git_notes_add_usage[] = {
N_("git notes add [<options>] [<object>]"),
NULL
};
static const char * const git_notes_copy_usage[] = {
N_("git notes copy [<options>] <from-object> <to-object>"),
N_("git notes copy --stdin [<from-object> <to-object>]..."),
NULL
};
static const char * const git_notes_append_usage[] = {
N_("git notes append [<options>] [<object>]"),
NULL
};
static const char * const git_notes_edit_usage[] = {
N_("git notes edit [<object>]"),
NULL
};
static const char * const git_notes_show_usage[] = {
N_("git notes show [<object>]"),
NULL
};
static const char * const git_notes_merge_usage[] = {
N_("git notes merge [<options>] <notes-ref>"),
N_("git notes merge --commit [<options>]"),
N_("git notes merge --abort [<options>]"),
NULL
};
static const char * const git_notes_remove_usage[] = {
N_("git notes remove [<object>]"),
NULL
};
static const char * const git_notes_prune_usage[] = {
N_("git notes prune [<options>]"),
NULL
};
static const char * const git_notes_get_ref_usage[] = {
N_("git notes get-ref"),
NULL
};
static const char note_template[] =
N_("Write/edit the notes for the following object:");
struct note_data {
int given;
int use_editor;
char *edit_path;
struct strbuf buf;
};
static void free_note_data(struct note_data *d)
{
if (d->edit_path) {
unlink_or_warn(d->edit_path);
free(d->edit_path);
}
strbuf_release(&d->buf);
}
static int list_each_note(const struct object_id *object_oid,
const struct object_id *note_oid, char *note_path,
void *cb_data)
{
printf("%s %s\n", oid_to_hex(note_oid), oid_to_hex(object_oid));
return 0;
}
static void copy_obj_to_fd(int fd, const struct object_id *oid)
{
unsigned long size;
enum object_type type;
char *buf = read_object_file(oid, &type, &size);
if (buf) {
if (size)
write_or_die(fd, buf, size);
free(buf);
}
}
static void write_commented_object(int fd, const struct object_id *object)
{
const char *show_args[5] =
{"show", "--stat", "--no-notes", oid_to_hex(object), NULL};
struct child_process show = CHILD_PROCESS_INIT;
struct strbuf buf = STRBUF_INIT;
struct strbuf cbuf = STRBUF_INIT;
/* Invoke "git show --stat --no-notes $object" */
show.argv = show_args;
show.no_stdin = 1;
show.out = -1;
show.err = 0;
show.git_cmd = 1;
if (start_command(&show))
die(_("unable to start 'show' for object '%s'"),
oid_to_hex(object));
if (strbuf_read(&buf, show.out, 0) < 0)
die_errno(_("could not read 'show' output"));
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
write_or_die(fd, cbuf.buf, cbuf.len);
strbuf_release(&cbuf);
strbuf_release(&buf);
if (finish_command(&show))
die(_("failed to finish 'show' for object '%s'"),
oid_to_hex(object));
}
static void prepare_note_data(const struct object_id *object, struct note_data *d,
const struct object_id *old_note)
{
if (d->use_editor || !d->given) {
int fd;
struct strbuf buf = STRBUF_INIT;
/* write the template message before editing: */
d->edit_path = git_pathdup("NOTES_EDITMSG");
fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
|