Skip to content

Commit 2d370d2

Browse files
committed
notes remove: --ignore-missing
Depending on the application, it is not necessarily an error for an object to lack a note, especially if the only thing the caller wants to make sure is that notes are cleared for an object. By passing this option from the command line, the "git notes remove" command considers it a success if the object did not have any note to begin with. Signed-off-by: Junio C Hamano <[email protected]>
1 parent c3ab1a8 commit 2d370d2

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

Documentation/git-notes.txt

Lines changed: 5 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 [<object>...]
20+
'git notes' remove [--ignore-missing] [<object>...]
2121
'git notes' prune [-n | -v]
2222
'git notes' get-ref
2323

@@ -155,6 +155,10 @@ OPTIONS
155155
'GIT_NOTES_REF' and the "core.notesRef" configuration. The ref
156156
is taken to be in `refs/notes/` if it is not qualified.
157157

158+
--ignore-missing::
159+
Do not consider it an error to request removing notes from an
160+
object that does not have notes attached to it.
161+
158162
-n::
159163
--dry-run::
160164
Do not remove anything; just report the object names whose notes

builtin/notes.c

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

956-
static int remove_one_note(struct notes_tree *t, const char *name)
956+
#define MISSING_OK 1
957+
958+
static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag)
957959
{
958960
int status;
959961
unsigned char sha1[20];
@@ -964,12 +966,16 @@ static int remove_one_note(struct notes_tree *t, const char *name)
964966
fprintf(stderr, _("Object %s has no note\n"), name);
965967
else
966968
fprintf(stderr, _("Removing note for object %s\n"), name);
967-
return status;
969+
return (flag & MISSING_OK) ? 0 : status;
968970
}
969971

970972
static int remove_cmd(int argc, const char **argv, const char *prefix)
971973
{
974+
unsigned flag = 0;
972975
struct option options[] = {
976+
OPT_BIT(0, "ignore-missing", &flag,
977+
"attempt to remove non-existent note is not an error",
978+
MISSING_OK),
973979
OPT_END()
974980
};
975981
struct notes_tree *t;
@@ -981,10 +987,10 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
981987
t = init_notes_check("remove");
982988

983989
if (!argc) {
984-
retval = remove_one_note(t, "HEAD");
990+
retval = remove_one_note(t, "HEAD", flag);
985991
} else {
986992
while (*argv) {
987-
retval |= remove_one_note(t, *argv);
993+
retval |= remove_one_note(t, *argv, flag);
988994
argv++;
989995
}
990996
}

t/t3301-notes.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,26 @@ test_expect_success 'removing is atomic' '
455455
test "$before" = "$after"
456456
'
457457

458+
test_expect_success 'removing with --ignore-missing' '
459+
before=$(git rev-parse --verify refs/notes/commits) &&
460+
test_when_finished "git update-ref refs/notes/commits $before" &&
461+
462+
# We have only two -- add another and make sure it stays
463+
git notes add -m "extra" &&
464+
git notes list HEAD >after-removal-expect &&
465+
git notes remove --ignore-missing HEAD^^ HEAD^^^ HEAD^ &&
466+
git notes list | sed -e "s/ .*//" >actual &&
467+
test_cmp after-removal-expect actual
468+
'
469+
470+
test_expect_success 'removing with --ignore-missing but bogus ref' '
471+
before=$(git rev-parse --verify refs/notes/commits) &&
472+
test_when_finished "git update-ref refs/notes/commits $before" &&
473+
test_must_fail git notes remove --ignore-missing HEAD^^ HEAD^^^ NO-SUCH-COMMIT &&
474+
after=$(git rev-parse --verify refs/notes/commits) &&
475+
test "$before" = "$after"
476+
'
477+
458478
test_expect_success 'list notes with "git notes list"' '
459479
git notes list > output &&
460480
test_cmp expect output

0 commit comments

Comments
 (0)