t / t7003-filter-branch.shon commit t: use common $SQ variable (bd482d6)
   1#!/bin/sh
   2
   3test_description='git filter-branch'
   4. ./test-lib.sh
   5. "$TEST_DIRECTORY/lib-gpg.sh"
   6
   7test_expect_success 'setup' '
   8        test_commit A &&
   9        GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
  10        test_commit --notick B &&
  11        git checkout -b branch B &&
  12        test_commit D &&
  13        mkdir dir &&
  14        test_commit dir/D &&
  15        test_commit E &&
  16        git checkout master &&
  17        test_commit C &&
  18        git checkout branch &&
  19        git merge C &&
  20        git tag F &&
  21        test_commit G &&
  22        test_commit H
  23'
  24# * (HEAD, branch) H
  25# * G
  26# *   Merge commit 'C' into branch
  27# |\
  28# | * (master) C
  29# * | E
  30# * | dir/D
  31# * | D
  32# |/
  33# * B
  34# * A
  35
  36
  37H=$(git rev-parse H)
  38
  39test_expect_success 'rewrite identically' '
  40        git filter-branch branch
  41'
  42test_expect_success 'result is really identical' '
  43        test $H = $(git rev-parse HEAD)
  44'
  45
  46test_expect_success 'rewrite bare repository identically' '
  47        (git config core.bare true && cd .git &&
  48         git filter-branch branch > filter-output 2>&1 &&
  49        ! fgrep fatal filter-output)
  50'
  51git config core.bare false
  52test_expect_success 'result is really identical' '
  53        test $H = $(git rev-parse HEAD)
  54'
  55
  56TRASHDIR=$(pwd)
  57test_expect_success 'correct GIT_DIR while using -d' '
  58        mkdir drepo &&
  59        ( cd drepo &&
  60        git init &&
  61        test_commit drepo &&
  62        git filter-branch -d "$TRASHDIR/dfoo" \
  63                --index-filter "cp \"$TRASHDIR\"/dfoo/backup-refs \"$TRASHDIR\"" \
  64        ) &&
  65        grep drepo "$TRASHDIR/backup-refs"
  66'
  67
  68test_expect_success 'tree-filter works with -d' '
  69        git init drepo-tree &&
  70        (
  71                cd drepo-tree &&
  72                test_commit one &&
  73                git filter-branch -d "$TRASHDIR/dfoo" \
  74                        --tree-filter "echo changed >one.t" &&
  75                echo changed >expect &&
  76                git cat-file blob HEAD:one.t >actual &&
  77                test_cmp expect actual &&
  78                test_cmp one.t actual
  79        )
  80'
  81
  82test_expect_success 'Fail if commit filter fails' '
  83        test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
  84'
  85
  86test_expect_success 'rewrite, renaming a specific file' '
  87        git filter-branch -f --tree-filter "mv D.t doh || :" HEAD
  88'
  89
  90test_expect_success 'test that the file was renamed' '
  91        test D = "$(git show HEAD:doh --)" &&
  92        ! test -f D.t &&
  93        test -f doh &&
  94        test D = "$(cat doh)"
  95'
  96
  97test_expect_success 'rewrite, renaming a specific directory' '
  98        git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
  99'
 100
 101test_expect_success 'test that the directory was renamed' '
 102        test dir/D = "$(git show HEAD:diroh/D.t --)" &&
 103        ! test -d dir &&
 104        test -d diroh &&
 105        ! test -d diroh/dir &&
 106        test -f diroh/D.t &&
 107        test dir/D = "$(cat diroh/D.t)"
 108'
 109
 110V=$(git rev-parse HEAD)
 111
 112test_expect_success 'populate --state-branch' '
 113        git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD
 114'
 115
 116W=$(git rev-parse HEAD)
 117
 118test_expect_success 'using --state-branch to skip already rewritten commits' '
 119        test_when_finished git reset --hard $V &&
 120        git reset --hard $V &&
 121        git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
 122        test_cmp_rev $W HEAD
 123'
 124
 125git tag oldD HEAD~4
 126test_expect_success 'rewrite one branch, keeping a side branch' '
 127        git branch modD oldD &&
 128        git filter-branch -f --tree-filter "mv B.t boh || :" D..modD
 129'
 130
 131test_expect_success 'common ancestor is still common (unchanged)' '
 132        test "$(git merge-base modD D)" = "$(git rev-parse B)"
 133'
 134
 135test_expect_success 'filter subdirectory only' '
 136        mkdir subdir &&
 137        touch subdir/new &&
 138        git add subdir/new &&
 139        test_tick &&
 140        git commit -m "subdir" &&
 141        echo H > A.t &&
 142        test_tick &&
 143        git commit -m "not subdir" A.t &&
 144        echo A > subdir/new &&
 145        test_tick &&
 146        git commit -m "again subdir" subdir/new &&
 147        git rm A.t &&
 148        test_tick &&
 149        git commit -m "again not subdir" &&
 150        git branch sub &&
 151        git branch sub-earlier HEAD~2 &&
 152        git filter-branch -f --subdirectory-filter subdir \
 153                refs/heads/sub refs/heads/sub-earlier
 154'
 155
 156test_expect_success 'subdirectory filter result looks okay' '
 157        test 2 = $(git rev-list sub | wc -l) &&
 158        git show sub:new &&
 159        test_must_fail git show sub:subdir &&
 160        git show sub-earlier:new &&
 161        test_must_fail git show sub-earlier:subdir
 162'
 163
 164test_expect_success 'more setup' '
 165        git checkout master &&
 166        mkdir subdir &&
 167        echo A > subdir/new &&
 168        git add subdir/new &&
 169        test_tick &&
 170        git commit -m "subdir on master" subdir/new &&
 171        git rm A.t &&
 172        test_tick &&
 173        git commit -m "again subdir on master" &&
 174        git merge branch
 175'
 176
 177test_expect_success 'use index-filter to move into a subdirectory' '
 178        git branch directorymoved &&
 179        git filter-branch -f --index-filter \
 180                 "git ls-files -s | sed \"s-    -&newsubdir/-\" |
 181                  GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
 182                        git update-index --index-info &&
 183                  mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
 184        git diff --exit-code HEAD directorymoved:newsubdir
 185'
 186
 187test_expect_success 'stops when msg filter fails' '
 188        old=$(git rev-parse HEAD) &&
 189        test_must_fail git filter-branch -f --msg-filter false HEAD &&
 190        test $old = $(git rev-parse HEAD) &&
 191        rm -rf .git-rewrite
 192'
 193
 194test_expect_success 'author information is preserved' '
 195        : > i &&
 196        git add i &&
 197        test_tick &&
 198        GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
 199        git branch preserved-author &&
 200        (sane_unset GIT_AUTHOR_NAME &&
 201         git filter-branch -f --msg-filter "cat; \
 202                        test \$GIT_COMMIT != $(git rev-parse master) || \
 203                        echo Hallo" \
 204                preserved-author) &&
 205        git rev-list --author="B V Uips" preserved-author >actual &&
 206        test_line_count = 1 actual
 207'
 208
 209test_expect_success "remove a certain author's commits" '
 210        echo i > i &&
 211        test_tick &&
 212        git commit -m i i &&
 213        git branch removed-author &&
 214        git filter-branch -f --commit-filter "\
 215                if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
 216                then\
 217                        skip_commit \"\$@\";
 218                else\
 219                        git commit-tree \"\$@\";\
 220                fi" removed-author &&
 221        cnt1=$(git rev-list master | wc -l) &&
 222        cnt2=$(git rev-list removed-author | wc -l) &&
 223        test $cnt1 -eq $(($cnt2 + 1)) &&
 224        git rev-list --author="B V Uips" removed-author >actual &&
 225        test_line_count = 0 actual
 226'
 227
 228test_expect_success 'barf on invalid name' '
 229        test_must_fail git filter-branch -f master xy-problem &&
 230        test_must_fail git filter-branch -f HEAD^
 231'
 232
 233test_expect_success '"map" works in commit filter' '
 234        git filter-branch -f --commit-filter "\
 235                parent=\$(git rev-parse \$GIT_COMMIT^) &&
 236                mapped=\$(map \$parent) &&
 237                actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
 238                test \$mapped = \$actual &&
 239                git commit-tree \"\$@\";" master~2..master &&
 240        git rev-parse --verify master
 241'
 242
 243test_expect_success 'Name needing quotes' '
 244
 245        git checkout -b rerere A &&
 246        mkdir foo &&
 247        name="れれれ" &&
 248        >foo/$name &&
 249        git add foo &&
 250        git commit -m "Adding a file" &&
 251        git filter-branch --tree-filter "rm -fr foo" &&
 252        test_must_fail git ls-files --error-unmatch "foo/$name" &&
 253        test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
 254
 255'
 256
 257test_expect_success 'Subdirectory filter with disappearing trees' '
 258        git reset --hard &&
 259        git checkout master &&
 260
 261        mkdir foo &&
 262        touch foo/bar &&
 263        git add foo &&
 264        test_tick &&
 265        git commit -m "Adding foo" &&
 266
 267        git rm -r foo &&
 268        test_tick &&
 269        git commit -m "Removing foo" &&
 270
 271        mkdir foo &&
 272        touch foo/bar &&
 273        git add foo &&
 274        test_tick &&
 275        git commit -m "Re-adding foo" &&
 276
 277        git filter-branch -f --subdirectory-filter foo &&
 278        git rev-list master >actual &&
 279        test_line_count = 3 actual
 280'
 281
 282test_expect_success 'Tag name filtering retains tag message' '
 283        git tag -m atag T &&
 284        git cat-file tag T > expect &&
 285        git filter-branch -f --tag-name-filter cat &&
 286        git cat-file tag T > actual &&
 287        test_cmp expect actual
 288'
 289
 290faux_gpg_tag='object XXXXXX
 291type commit
 292tag S
 293tagger T A Gger <tagger@example.com> 1206026339 -0500
 294
 295This is a faux gpg signed tag.
 296-----BEGIN PGP SIGNATURE-----
 297Version: FauxGPG v0.0.0 (FAUX/Linux)
 298
 299gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
 300acmwXaWET20H0GeAGP+7vow=
 301=agpO
 302-----END PGP SIGNATURE-----
 303'
 304test_expect_success 'Tag name filtering strips gpg signature' '
 305        sha1=$(git rev-parse HEAD) &&
 306        sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
 307        git update-ref "refs/tags/S" "$sha1t" &&
 308        echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
 309        git filter-branch -f --tag-name-filter cat &&
 310        git cat-file tag S > actual &&
 311        test_cmp expect actual
 312'
 313
 314test_expect_success GPG 'Filtering retains message of gpg signed commit' '
 315        mkdir gpg &&
 316        touch gpg/foo &&
 317        git add gpg &&
 318        test_tick &&
 319        git commit -S -m "Adding gpg" &&
 320
 321        git log -1 --format="%s" > expect &&
 322        git filter-branch -f --msg-filter "cat" &&
 323        git log -1 --format="%s" > actual &&
 324        test_cmp expect actual
 325'
 326
 327test_expect_success 'Tag name filtering allows slashes in tag names' '
 328        git tag -m tag-with-slash X/1 &&
 329        git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&
 330        git filter-branch -f --tag-name-filter "echo X/2" &&
 331        git cat-file tag X/2 > actual &&
 332        test_cmp expect actual
 333'
 334test_expect_success 'setup --prune-empty comparisons' '
 335        git checkout --orphan master-no-a &&
 336        git rm -rf . &&
 337        unset test_tick &&
 338        test_tick &&
 339        GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
 340        test_commit --notick B B.t B Bx &&
 341        git checkout -b branch-no-a Bx &&
 342        test_commit D D.t D Dx &&
 343        mkdir dir &&
 344        test_commit dir/D dir/D.t dir/D dir/Dx &&
 345        test_commit E E.t E Ex &&
 346        git checkout master-no-a &&
 347        test_commit C C.t C Cx &&
 348        git checkout branch-no-a &&
 349        git merge Cx -m "Merge tag '\''C'\'' into branch" &&
 350        git tag Fx &&
 351        test_commit G G.t G Gx &&
 352        test_commit H H.t H Hx &&
 353        git checkout branch
 354'
 355
 356test_expect_success 'Prune empty commits' '
 357        git rev-list HEAD > expect &&
 358        test_commit to_remove &&
 359        git filter-branch -f --index-filter "git update-index --remove to_remove.t" --prune-empty HEAD &&
 360        git rev-list HEAD > actual &&
 361        test_cmp expect actual
 362'
 363
 364test_expect_success 'prune empty collapsed merges' '
 365        test_config merge.ff false &&
 366        git rev-list HEAD >expect &&
 367        test_commit to_remove_2 &&
 368        git reset --hard HEAD^ &&
 369        test_merge non-ff to_remove_2 &&
 370        git filter-branch -f --index-filter "git update-index --remove to_remove_2.t" --prune-empty HEAD &&
 371        git rev-list HEAD >actual &&
 372        test_cmp expect actual
 373'
 374
 375test_expect_success 'prune empty works even without index/tree filters' '
 376        git rev-list HEAD >expect &&
 377        git commit --allow-empty -m empty &&
 378        git filter-branch -f --prune-empty HEAD &&
 379        git rev-list HEAD >actual &&
 380        test_cmp expect actual
 381'
 382
 383test_expect_success '--prune-empty is able to prune root commit' '
 384        git rev-list branch-no-a >expect &&
 385        git branch testing H &&
 386        git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t" testing &&
 387        git rev-list testing >actual &&
 388        git branch -D testing &&
 389        test_cmp expect actual
 390'
 391
 392test_expect_success '--prune-empty is able to prune entire branch' '
 393        git branch prune-entire B &&
 394        git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t B.t" prune-entire &&
 395        test_path_is_missing .git/refs/heads/prune-entire &&
 396        test_must_fail git reflog exists refs/heads/prune-entire
 397'
 398
 399test_expect_success '--remap-to-ancestor with filename filters' '
 400        git checkout master &&
 401        git reset --hard A &&
 402        test_commit add-foo foo 1 &&
 403        git branch moved-foo &&
 404        test_commit add-bar bar a &&
 405        git branch invariant &&
 406        orig_invariant=$(git rev-parse invariant) &&
 407        git branch moved-bar &&
 408        test_commit change-foo foo 2 &&
 409        git filter-branch -f --remap-to-ancestor \
 410                moved-foo moved-bar A..master \
 411                -- -- foo &&
 412        test $(git rev-parse moved-foo) = $(git rev-parse moved-bar) &&
 413        test $(git rev-parse moved-foo) = $(git rev-parse master^) &&
 414        test $orig_invariant = $(git rev-parse invariant)
 415'
 416
 417test_expect_success 'automatic remapping to ancestor with filename filters' '
 418        git checkout master &&
 419        git reset --hard A &&
 420        test_commit add-foo2 foo 1 &&
 421        git branch moved-foo2 &&
 422        test_commit add-bar2 bar a &&
 423        git branch invariant2 &&
 424        orig_invariant=$(git rev-parse invariant2) &&
 425        git branch moved-bar2 &&
 426        test_commit change-foo2 foo 2 &&
 427        git filter-branch -f \
 428                moved-foo2 moved-bar2 A..master \
 429                -- -- foo &&
 430        test $(git rev-parse moved-foo2) = $(git rev-parse moved-bar2) &&
 431        test $(git rev-parse moved-foo2) = $(git rev-parse master^) &&
 432        test $orig_invariant = $(git rev-parse invariant2)
 433'
 434
 435test_expect_success 'setup submodule' '
 436        rm -fr ?* .git &&
 437        git init &&
 438        test_commit file &&
 439        mkdir submod &&
 440        submodurl="$PWD/submod" &&
 441        ( cd submod &&
 442          git init &&
 443          test_commit file-in-submod ) &&
 444        git submodule add "$submodurl" &&
 445        git commit -m "added submodule" &&
 446        test_commit add-file &&
 447        ( cd submod && test_commit add-in-submodule ) &&
 448        git add submod &&
 449        git commit -m "changed submodule" &&
 450        git branch original HEAD
 451'
 452
 453orig_head=$(git show-ref --hash --head HEAD)
 454
 455test_expect_success 'rewrite submodule with another content' '
 456        git filter-branch --tree-filter "test -d submod && {
 457                                         rm -rf submod &&
 458                                         git rm -rf --quiet submod &&
 459                                         mkdir submod &&
 460                                         : > submod/file
 461                                         } || :" HEAD &&
 462        test $orig_head != $(git show-ref --hash --head HEAD)
 463'
 464
 465test_expect_success 'replace submodule revision' '
 466        git reset --hard original &&
 467        git filter-branch -f --tree-filter \
 468            "if git ls-files --error-unmatch -- submod > /dev/null 2>&1
 469             then git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 submod
 470             fi" HEAD &&
 471        test $orig_head != $(git show-ref --hash --head HEAD)
 472'
 473
 474test_expect_success 'filter commit message without trailing newline' '
 475        git reset --hard original &&
 476        commit=$(printf "no newline" | git commit-tree HEAD^{tree}) &&
 477        git update-ref refs/heads/no-newline $commit &&
 478        git filter-branch -f refs/heads/no-newline &&
 479        echo $commit >expect &&
 480        git rev-parse refs/heads/no-newline >actual &&
 481        test_cmp expect actual
 482'
 483
 484test_expect_success 'tree-filter deals with object name vs pathname ambiguity' '
 485        test_when_finished "git reset --hard original" &&
 486        ambiguous=$(git rev-list -1 HEAD) &&
 487        git filter-branch --tree-filter "mv file.t $ambiguous" HEAD^.. &&
 488        git show HEAD:$ambiguous
 489'
 490
 491test_expect_success 'rewrite repository including refs that point at non-commit object' '
 492        test_when_finished "git reset --hard original" &&
 493        tree=$(git rev-parse HEAD^{tree}) &&
 494        test_when_finished "git replace -d $tree" &&
 495        echo A >new &&
 496        git add new &&
 497        new_tree=$(git write-tree) &&
 498        git replace $tree $new_tree &&
 499        git tag -a -m "tag to a tree" treetag $new_tree &&
 500        git reset --hard HEAD &&
 501        git filter-branch -f -- --all >filter-output 2>&1 &&
 502        ! fgrep fatal filter-output
 503'
 504
 505test_done