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