Skip to content

Commit 2d4fef9

Browse files
committed
Merge branch 'mg/notes-dry-run'
* mg/notes-dry-run: notes: dry-run and verbose options for prune
2 parents cde3ead + a9f2adf commit 2d4fef9

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

Documentation/git-notes.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ SYNOPSIS
1515
'git notes' edit [<object>]
1616
'git notes' show [<object>]
1717
'git notes' remove [<object>]
18-
'git notes' prune
18+
'git notes' prune [-n | -v]
1919

2020

2121
DESCRIPTION
@@ -128,6 +128,13 @@ OPTIONS
128128
'GIT_NOTES_REF' and the "core.notesRef" configuration. The ref
129129
is taken to be in `refs/notes/` if it is not qualified.
130130

131+
-n::
132+
Do not remove anything; just report the object names whose notes
133+
would be removed.
134+
135+
-v::
136+
Report all object names whose notes are removed.
137+
131138

132139
DISCUSSION
133140
----------

builtin/notes.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static const char * const git_notes_usage[] = {
2626
"git notes [--ref <notes_ref>] edit [<object>]",
2727
"git notes [--ref <notes_ref>] show [<object>]",
2828
"git notes [--ref <notes_ref>] remove [<object>]",
29-
"git notes [--ref <notes_ref>] prune",
29+
"git notes [--ref <notes_ref>] prune [-n | -v]",
3030
NULL
3131
};
3232

@@ -67,7 +67,7 @@ static const char * const git_notes_remove_usage[] = {
6767
};
6868

6969
static const char * const git_notes_prune_usage[] = {
70-
"git notes prune",
70+
"git notes prune [<options>]",
7171
NULL
7272
};
7373

@@ -792,7 +792,10 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
792792
static int prune(int argc, const char **argv, const char *prefix)
793793
{
794794
struct notes_tree *t;
795+
int show_only = 0, verbose = 0;
795796
struct option options[] = {
797+
OPT_BOOLEAN('n', NULL, &show_only, "do not remove, show only"),
798+
OPT_BOOLEAN('v', NULL, &verbose, "report pruned notes"),
796799
OPT_END()
797800
};
798801

@@ -806,8 +809,10 @@ static int prune(int argc, const char **argv, const char *prefix)
806809

807810
t = init_notes_check("prune");
808811

809-
prune_notes(t);
810-
commit_notes(t, "Notes removed by 'git notes prune'");
812+
prune_notes(t, (verbose ? NOTES_PRUNE_VERBOSE : 0) |
813+
(show_only ? NOTES_PRUNE_VERBOSE|NOTES_PRUNE_DRYRUN : 0) );
814+
if (!show_only)
815+
commit_notes(t, "Notes removed by 'git notes prune'");
811816
free_notes(t);
812817
return 0;
813818
}

notes.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result)
10831083
return ret;
10841084
}
10851085

1086-
void prune_notes(struct notes_tree *t)
1086+
void prune_notes(struct notes_tree *t, int flags)
10871087
{
10881088
struct note_delete_list *l = NULL;
10891089

@@ -1094,7 +1094,10 @@ void prune_notes(struct notes_tree *t)
10941094
for_each_note(t, 0, prune_notes_helper, &l);
10951095

10961096
while (l) {
1097-
remove_note(t, l->sha1);
1097+
if (flags & NOTES_PRUNE_VERBOSE)
1098+
printf("%s\n", sha1_to_hex(l->sha1));
1099+
if (!(flags & NOTES_PRUNE_DRYRUN))
1100+
remove_note(t, l->sha1);
10981101
l = l->next;
10991102
}
11001103
}

notes.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
171171
*/
172172
int write_notes_tree(struct notes_tree *t, unsigned char *result);
173173

174+
/* Flags controlling the operation of prune */
175+
#define NOTES_PRUNE_VERBOSE 1
176+
#define NOTES_PRUNE_DRYRUN 2
174177
/*
175178
* Remove all notes annotating non-existing objects from the given notes tree
176179
*
@@ -181,7 +184,7 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result);
181184
* structure are not persistent until a subsequent call to write_notes_tree()
182185
* returns zero.
183186
*/
184-
void prune_notes(struct notes_tree *t);
187+
void prune_notes(struct notes_tree *t, int flags);
185188

186189
/*
187190
* Free (and de-initialize) the given notes_tree structure

t/t3306-notes-prune.sh

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ test_expect_success 'verify commits and notes' '
6060

6161
test_expect_success 'remove some commits' '
6262
63-
git reset --hard HEAD~2 &&
63+
git reset --hard HEAD~1 &&
6464
git reflog expire --expire=now HEAD &&
6565
git gc --prune=now
6666
'
6767

6868
test_expect_success 'verify that commits are gone' '
6969
7070
! git cat-file -p 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&
71-
! git cat-file -p 08341ad9e94faa089d60fd3f523affb25c6da189 &&
71+
git cat-file -p 08341ad9e94faa089d60fd3f523affb25c6da189 &&
7272
git cat-file -p ab5f302035f2e7aaf04265f08b42034c23256e1f
7373
'
7474

@@ -79,11 +79,55 @@ test_expect_success 'verify that notes are still present' '
7979
git notes show ab5f302035f2e7aaf04265f08b42034c23256e1f
8080
'
8181

82+
test_expect_success 'prune -n does not remove notes' '
83+
84+
git notes list > expect &&
85+
git notes prune -n &&
86+
git notes list > actual &&
87+
test_cmp expect actual
88+
'
89+
90+
cat > expect <<EOF
91+
5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29
92+
EOF
93+
94+
test_expect_success 'prune -n lists prunable notes' '
95+
96+
97+
git notes prune -n > actual &&
98+
test_cmp expect actual
99+
'
100+
101+
82102
test_expect_success 'prune notes' '
83103
84104
git notes prune
85105
'
86106

107+
test_expect_success 'verify that notes are gone' '
108+
109+
! git notes show 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&
110+
git notes show 08341ad9e94faa089d60fd3f523affb25c6da189 &&
111+
git notes show ab5f302035f2e7aaf04265f08b42034c23256e1f
112+
'
113+
114+
test_expect_success 'remove some commits' '
115+
116+
git reset --hard HEAD~1 &&
117+
git reflog expire --expire=now HEAD &&
118+
git gc --prune=now
119+
'
120+
121+
cat > expect <<EOF
122+
08341ad9e94faa089d60fd3f523affb25c6da189
123+
EOF
124+
125+
test_expect_success 'prune -v notes' '
126+
127+
git notes prune -v > actual &&
128+
test_cmp expect actual
129+
'
130+
87131
test_expect_success 'verify that notes are gone' '
88132
89133
! git notes show 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&

0 commit comments

Comments
 (0)