summaryrefslogtreecommitdiff
path: root/builtin-notes.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtin-notes.c')
-rw-r--r--builtin-notes.c56
1 files changed, 55 insertions, 1 deletions
diff --git a/builtin-notes.c b/builtin-notes.c
index 123ecad..daeb14e 100644
--- a/builtin-notes.c
+++ b/builtin-notes.c
@@ -278,6 +278,46 @@ int commit_notes(struct notes_tree *t, const char *msg)
return 0;
}
+int notes_copy_from_stdin(int force)
+{
+ struct strbuf buf = STRBUF_INIT;
+ struct notes_tree *t;
+ int ret = 0;
+
+ init_notes(NULL, NULL, NULL, 0);
+ t = &default_notes_tree;
+
+ while (strbuf_getline(&buf, stdin, '\n') != EOF) {
+ unsigned char from_obj[20], to_obj[20];
+ struct strbuf **split;
+ int err;
+
+ split = strbuf_split(&buf, ' ');
+ if (!split[0] || !split[1])
+ die("Malformed input line: '%s'.", buf.buf);
+ strbuf_rtrim(split[0]);
+ strbuf_rtrim(split[1]);
+ if (get_sha1(split[0]->buf, from_obj))
+ die("Failed to resolve '%s' as a valid ref.", split[0]->buf);
+ if (get_sha1(split[1]->buf, to_obj))
+ die("Failed to resolve '%s' as a valid ref.", split[1]->buf);
+
+ err = copy_note(t, from_obj, to_obj, force, combine_notes_overwrite);
+
+ if (err) {
+ error("Failed to copy notes from '%s' to '%s'",
+ split[0]->buf, split[1]->buf);
+ ret = 1;
+ }
+
+ strbuf_list_free(split);
+ }
+
+ commit_notes(t, "Notes added by 'git notes copy'");
+ free_notes(t);
+ return ret;
+}
+
int cmd_notes(int argc, const char **argv, const char *prefix)
{
struct notes_tree *t;
@@ -287,7 +327,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
char logmsg[100];
int list = 0, add = 0, copy = 0, append = 0, edit = 0, show = 0,
- remove = 0, prune = 0, force = 0;
+ remove = 0, prune = 0, force = 0, from_stdin = 0;
int given_object = 0, i = 1, retval = 0;
struct msg_arg msg = { 0, 0, STRBUF_INIT };
struct option options[] = {
@@ -301,6 +341,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
OPT_CALLBACK('C', "reuse-message", &msg, "OBJECT",
"reuse specified note object", parse_reuse_arg),
OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
+ OPT_BOOLEAN(0, "stdin", &from_stdin, "read objects from stdin"),
OPT_END()
};
@@ -349,8 +390,21 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
usage_with_options(git_notes_usage, options);
}
+ if (!copy && from_stdin) {
+ error("cannot use --stdin with %s subcommand.", argv[0]);
+ usage_with_options(git_notes_usage, options);
+ }
+
if (copy) {
const char *from_ref;
+ if (from_stdin) {
+ if (argc > 1) {
+ error("too many parameters");
+ usage_with_options(git_notes_usage, options);
+ } else {
+ return notes_copy_from_stdin(force);
+ }
+ }
if (argc < 3) {
error("too few parameters");
usage_with_options(git_notes_usage, options);