filter-branch: fix behaviour of '-k'
The option '-k' says that the given commit and _all_ of its ancestors are kept as-is. However, if a to-be-rewritten commit branched from an ancestor of an ancestor of a commit given with '-k', filter-branch would fail. Example: A - B \ C If filter-branch was called with '-k B -s C', it would actually keep B (and A as its parent), but would rewrite C, and its parent. Noticed by Johannes Sixt. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jun 5, 2007 at 16:58 UTC
9840906026be807d0882f96396de3a3cdb9fb43e
2 files changed
+26
-12
git-filter-branch.sh
+17
-12
@@ -327,11 +327,6 @@ ret=0
327
328
mkdir ../map # map old->new commit ids for rewriting parents
329
330
-# seed with identity mappings for the parents where we start off
331
-for commit in $unchanged; do
332
- echo $commit > ../map/$commit
333
-done
334
-
330
git-rev-list --reverse --topo-order $srcbranch --not $unchanged >../revs
331
commits=$(cat ../revs | wc -l | tr -d " ")
332
@@ -372,7 +367,8 @@ while read commit; do
367
parentstr="$parentstr -p $reparent"
368
done
369
else
375
- die "assertion failed: parent $parent for commit $commit not found in rewritten ones"
370
+ # if it was not rewritten, take the original
371
+ parentstr="$parentstr -p $parent"
372
fi
373
done
374
if [ "$filter_parent" ]; then
@@ -385,12 +381,21 @@ while read commit; do
381
tee ../map/$commit
382
done <../revs
383
388
-git-update-ref refs/heads/"$dstbranch" $(head -n 1 ../map/$(tail -n 1 ../revs))
389
-if [ "$(cat ../map/$(tail -n 1 ../revs) | wc -l)" -gt 1 ]; then
390
- echo "WARNING: Your commit filter caused the head commit to expand to several rewritten commits. Only the first such commit was recorded as the current $dstbranch head but you will need to resolve the situation now (probably by manually merging the other commits). These are all the commits:" >&2
391
- sed 's/^/ /' ../map/$(tail -n 1 ../revs) >&2
392
- ret=1
393
-fi
384
+src_head=$(tail -n 1 ../revs)
385
+target_head=$(head -n 1 ../map/$src_head)
386
+case "$target_head" in
387
+'')
388
+ echo Nothing rewritten
389
+ ;;
390
+*)
391
+ git-update-ref refs/heads/"$dstbranch" $target_head
392
+ if [ $(cat ../map/$src_head | wc -l) -gt 1 ]; then
393
+ echo "WARNING: Your commit filter caused the head commit to expand to several rewritten commits. Only the first such commit was recorded as the current $dstbranch head but you will need to resolve the situation now (probably by manually merging the other commits). These are all the commits:" >&2
394
+ sed 's/^/ /' ../map/$src_head >&2
395
+ ret=1
396
+ fi
397
+ ;;
398
+esac
399
400
if [ "$filter_tag_name" ]; then
401
git-for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
t/t7003-filter-branch.sh
+9
@@ -45,4 +45,13 @@ test_expect_success 'test that the file was renamed' '
45
test d = $(git show H3:doh)
46
'
47
48
+git tag oldD H3~4
49
+test_expect_success 'rewrite one branch, keeping a side branch' '
50
+ git-filter-branch --tree-filter "mv b boh || :" -k D -s oldD modD
51
+'
52
+
53
+test_expect_success 'common ancestor is still common (unchanged)' '
54
+ test "$(git-merge-base modD D)" = "$(git-rev-parse B)"
55
+'
56
+
57
test_done