Skip to content

Commit bfee58c

Browse files
committed
Merge branch 'dp/filter-branch-prune-empty' into maint
"git filter-branch --prune-empty" drops a single-parent commit that becomes a no-op, but did not drop a root commit whose tree is empty. * dp/filter-branch-prune-empty: p7000: add test for filter-branch with --prune-empty filter-branch: fix --prune-empty on parentless commits t7003: ensure --prune-empty removes entire branch when applicable t7003: ensure --prune-empty can prune root commit
2 parents 2b69d31 + 32da746 commit bfee58c

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

Documentation/git-filter-branch.txt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,12 @@ to other tags will be rewritten to point to the underlying commit.
167167
project root. Implies <<Remap_to_ancestor>>.
168168

169169
--prune-empty::
170-
Some kind of filters will generate empty commits, that left the tree
171-
untouched. This switch allow git-filter-branch to ignore such
172-
commits. Though, this switch only applies for commits that have one
173-
and only one parent, it will hence keep merges points. Also, this
174-
option is not compatible with the use of `--commit-filter`. Though you
175-
just need to use the function 'git_commit_non_empty_tree "$@"' instead
176-
of the `git commit-tree "$@"` idiom in your commit filter to make that
177-
happen.
170+
Some filters will generate empty commits that leave the tree untouched.
171+
This option instructs git-filter-branch to remove such commits if they
172+
have exactly one or zero non-pruned parents; merge commits will
173+
therefore remain intact. This option cannot be used together with
174+
`--commit-filter`, though the same effect can be achieved by using the
175+
provided `git_commit_non_empty_tree` function in a commit filter.
178176

179177
--original <namespace>::
180178
Use this option to set the namespace where the original commits

git-filter-branch.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ git_commit_non_empty_tree()
4646
{
4747
if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
4848
map "$3"
49+
elif test $# = 1 && test "$1" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904; then
50+
:
4951
else
5052
git commit-tree "$@"
5153
fi

t/perf/p7000-filter-branch.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ test_perf 'noop filter' '
1616
git filter-branch -f base..HEAD
1717
'
1818

19+
test_perf 'noop prune-empty' '
20+
git checkout --detach tip &&
21+
git filter-branch -f --prune-empty base..HEAD
22+
'
23+
1924
test_done

t/t7003-filter-branch.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,27 @@ test_expect_success 'Tag name filtering allows slashes in tag names' '
313313
git cat-file tag X/2 > actual &&
314314
test_cmp expect actual
315315
'
316+
test_expect_success 'setup --prune-empty comparisons' '
317+
git checkout --orphan master-no-a &&
318+
git rm -rf . &&
319+
unset test_tick &&
320+
test_tick &&
321+
GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
322+
test_commit --notick B B.t B Bx &&
323+
git checkout -b branch-no-a Bx &&
324+
test_commit D D.t D Dx &&
325+
mkdir dir &&
326+
test_commit dir/D dir/D.t dir/D dir/Dx &&
327+
test_commit E E.t E Ex &&
328+
git checkout master-no-a &&
329+
test_commit C C.t C Cx &&
330+
git checkout branch-no-a &&
331+
git merge Cx -m "Merge tag '\''C'\'' into branch" &&
332+
git tag Fx &&
333+
test_commit G G.t G Gx &&
334+
test_commit H H.t H Hx &&
335+
git checkout branch
336+
'
316337

317338
test_expect_success 'Prune empty commits' '
318339
git rev-list HEAD > expect &&
@@ -341,6 +362,22 @@ test_expect_success 'prune empty works even without index/tree filters' '
341362
test_cmp expect actual
342363
'
343364

365+
test_expect_success '--prune-empty is able to prune root commit' '
366+
git rev-list branch-no-a >expect &&
367+
git branch testing H &&
368+
git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t" testing &&
369+
git rev-list testing >actual &&
370+
git branch -D testing &&
371+
test_cmp expect actual
372+
'
373+
374+
test_expect_success '--prune-empty is able to prune entire branch' '
375+
git branch prune-entire B &&
376+
git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t B.t" prune-entire &&
377+
test_path_is_missing .git/refs/heads/prune-entire &&
378+
test_must_fail git reflog exists refs/heads/prune-entire
379+
'
380+
344381
test_expect_success '--remap-to-ancestor with filename filters' '
345382
git checkout master &&
346383
git reset --hard A &&

0 commit comments

Comments
 (0)