Skip to content

Commit 4653801

Browse files
committed
notes remove: --stdin reads from the standard input
Teach the command to read object names to remove from the standard input, in addition to the object names given from the command line. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2d370d2 commit 4653801

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

Documentation/git-notes.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ SYNOPSIS
1717
'git notes' merge [-v | -q] [-s <strategy> ] <notes_ref>
1818
'git notes' merge --commit [-v | -q]
1919
'git notes' merge --abort [-v | -q]
20-
'git notes' remove [--ignore-missing] [<object>...]
20+
'git notes' remove [--ignore-missing] [--stdin] [<object>...]
2121
'git notes' prune [-n | -v]
2222
'git notes' get-ref
2323

@@ -159,6 +159,11 @@ OPTIONS
159159
Do not consider it an error to request removing notes from an
160160
object that does not have notes attached to it.
161161

162+
--stdin::
163+
Also read the object names to remove notes from from the standard
164+
input (there is no reason you cannot combine this with object
165+
names from the command line).
166+
162167
-n::
163168
--dry-run::
164169
Do not remove anything; just report the object names whose notes

builtin/notes.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ static int merge(int argc, const char **argv, const char *prefix)
953953
return result < 0; /* return non-zero on conflicts */
954954
}
955955

956-
#define MISSING_OK 1
956+
#define IGNORE_MISSING 1
957957

958958
static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
959959
{
@@ -966,16 +966,19 @@ static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag
966966
fprintf(stderr, _("Object %s has no note\n"), name);
967967
else
968968
fprintf(stderr, _("Removing note for object %s\n"), name);
969-
return (flag & MISSING_OK) ? 0 : status;
969+
return (flag & IGNORE_MISSING) ? 0 : status;
970970
}
971971

972972
static int remove_cmd(int argc, const char **argv, const char *prefix)
973973
{
974974
unsigned flag = 0;
975+
int from_stdin = 0;
975976
struct option options[] = {
976977
OPT_BIT(0, "ignore-missing", &flag,
977978
"attempt to remove non-existent note is not an error",
978-
MISSING_OK),
979+
IGNORE_MISSING),
980+
OPT_BOOLEAN(0, "stdin", &from_stdin,
981+
"read object names from the standard input"),
979982
OPT_END()
980983
};
981984
struct notes_tree *t;
@@ -986,14 +989,22 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
986989

987990
t = init_notes_check("remove");
988991

989-
if (!argc) {
992+
if (!argc && !from_stdin) {
990993
retval = remove_one_note(t, "HEAD", flag);
991994
} else {
992995
while (*argv) {
993996
retval |= remove_one_note(t, *argv, flag);
994997
argv++;
995998
}
996999
}
1000+
if (from_stdin) {
1001+
struct strbuf sb = STRBUF_INIT;
1002+
while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1003+
strbuf_rtrim(&sb);
1004+
retval |= remove_one_note(t, sb.buf, flag);
1005+
}
1006+
strbuf_release(&sb);
1007+
}
9971008
if (!retval)
9981009
commit_notes(t, "Notes removed by 'git notes remove'");
9991010
free_notes(t);

t/t3301-notes.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,41 @@ test_expect_success 'removing with --ignore-missing but bogus ref' '
475475
test "$before" = "$after"
476476
'
477477

478+
test_expect_success 'remove reads from --stdin' '
479+
before=$(git rev-parse --verify refs/notes/commits) &&
480+
test_when_finished "git update-ref refs/notes/commits $before" &&
481+
482+
# We have only two -- add another and make sure it stays
483+
git notes add -m "extra" &&
484+
git notes list HEAD >after-removal-expect &&
485+
git rev-parse HEAD^^ HEAD^^^ >input &&
486+
git notes remove --stdin <input &&
487+
git notes list | sed -e "s/ .*//" >actual &&
488+
test_cmp after-removal-expect actual
489+
'
490+
491+
test_expect_success 'remove --stdin is also atomic' '
492+
before=$(git rev-parse --verify refs/notes/commits) &&
493+
test_when_finished "git update-ref refs/notes/commits $before" &&
494+
git rev-parse HEAD^^ HEAD^^^ HEAD^ >input &&
495+
test_must_fail git notes remove --stdin <input &&
496+
after=$(git rev-parse --verify refs/notes/commits) &&
497+
test "$before" = "$after"
498+
'
499+
500+
test_expect_success 'removing with --stdin --ignore-missing' '
501+
before=$(git rev-parse --verify refs/notes/commits) &&
502+
test_when_finished "git update-ref refs/notes/commits $before" &&
503+
504+
# We have only two -- add another and make sure it stays
505+
git notes add -m "extra" &&
506+
git notes list HEAD >after-removal-expect &&
507+
git rev-parse HEAD^^ HEAD^^^ HEAD^ >input &&
508+
git notes remove --ignore-missing --stdin <input &&
509+
git notes list | sed -e "s/ .*//" >actual &&
510+
test_cmp after-removal-expect actual
511+
'
512+
478513
test_expect_success 'list notes with "git notes list"' '
479514
git notes list > output &&
480515
test_cmp expect output

0 commit comments

Comments
 (0)