/*
* 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 "builtin.h"
#include "notes.h"
#include "blob.h"
#include "commit.h"
#include "refs.h"
#include "exec_cmd.h"
#include "run-command.h"
#include "parse-options.h"
#include "string-list.h"
#include "notes-merge.h"
static const char * const git_notes_usage[] = {
"git notes [--ref <notes_ref>] [list [<object>]]",
"git notes [--ref <notes_ref>] add [-f] [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
"git notes [--ref <notes_ref>] copy [-f] <from-object> <to-object>",
"git notes [--ref <notes_ref>] append [-m <msg> | -F <file> | (-c | -C) <object>] [<object>]",
"git notes [--ref <notes_ref>] edit [<object>]",
"git notes [--ref <notes_ref>] show [<object>]",
"git notes [--ref <notes_ref>] merge [-v | -q] [-s <strategy> ] <notes_ref>",
"git notes merge --commit [-v | -q]",
"git notes merge --abort [-v | -q]",
"git notes [--ref <notes_ref>] remove [<object>...]",
"git notes [--ref <notes_ref>] prune [-n | -v]",
"git notes [--ref <notes_ref>] get-ref",
NULL
};
static const char * const git_notes_list_usage[] = {
"git notes [list [<object>]]",
NULL
};
static const char * const git_notes_add_usage[] = {
"git notes add [<options>] [<object>]",
NULL
};
static const char * const git_notes_copy_usage[] = {
"git notes copy [<options>] <from-object> <to-object>",
"git notes copy --stdin [<from-object> <to-object>]...",
NULL
};
static const char * const git_notes_append_usage[] = {
"git notes append [<options>] [<object>]",
NULL
};
static const char * const git_notes_edit_usage[] = {
"git notes edit [<object>]",
NULL
};
static const char * const git_notes_show_usage[] = {
"git notes show [<object>]",
NULL
};
static const char * const git_notes_merge_usage[] = {
"git notes merge [<options>] <notes_ref>",
"git notes merge --commit [<options>]",
"git notes merge --abort [<options>]",
NULL
};
static const char * const git_notes_remove_usage[] = {
"git notes remove [<object>]",
NULL
};
static const char * const git_notes_prune_usage[] = {
"git notes prune [<options>]",
NULL
};
static const char * const git_notes_get_ref_usage[] = {
"git notes get-ref",
NULL
};
static const char note_template[] =
"\n"
"#\n"
"# Write/edit the notes for the following object:\n"
"#\n";
struct msg_arg {
int given;
int use_editor;
struct strbuf buf;
};
static int list_each_note(const unsigned char *object_sha1,
const unsigned char *note_sha1, char *note_path,
void *cb_data)
{
printf("%s %s\n", sha1_to_hex(note_sha1), sha1_to_hex(object_sha1));
return 0;
}
static void write_note_data(int fd, const unsigned char *sha1)
{
unsigned long size;
enum object_type type;
char *buf = read_sha1_file(sha1, &type, &size);
if (buf) {
if (size)
write_or_die(fd, buf, size);
free(buf);
}
}
static void write_commented_object(int fd, const unsigned char *object)
{
const char *show_args[5] =
{"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
struct child_process show;
struct strbuf buf = STRBUF_INIT;
FILE *show_out;
/* Invoke "git show --stat --no-notes $object" */
memset(&show, 0, sizeof(show));
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'"),
sha1_to_hex(object));
/* Open the output as FILE* so strbuf_getline() can be used. */
show_out = xfdopen(show.out, "r");
if (show_out == NULL)
die_errno(_("can't fdopen 'show' output fd"));
/* Prepend "# " to each output line and write result to 'fd' */
while (strbuf_getline(&buf, show_out, '\n') != EOF) {
write_or_die(fd, "# ", 2);
write_or_die(fd, buf.buf, buf.len);
write_or_die(fd, "\n", 1);
}
strbuf_release(&buf);
if (fclose(show_out))
die_errno(_("failed to close pipe to 'show' for object '%s'"),
sha1_to_hex(object));
if (finish_command(&show))
die(_("failed to finish 'show' for object '%s'"),
sha1_to_hex(object));
}
static void create_note(const
|