t / t7003-filter-branch.shon commit get_short_sha1: make default disambiguation configurable (5b33cb1)
   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        test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
 191'
 192
 193test_expect_success "remove a certain author's commits" '
 194        echo i > i &&
 195        test_tick &&
 196        git commit -m i i &&
 197        git branch removed-author &&
 198        git filter-branch -f --commit-filter "\
 199                if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
 200                then\
 201                        skip_commit \"\$@\";
 202                else\
 203                        git commit-tree \"\$@\";\
 204                fi" removed-author &&
 205        cnt1=$(git rev-list master | wc -l) &&
 206        cnt2=$(git rev-list removed-author | wc -l) &&
 207        test $cnt1 -eq $(($cnt2 + 1)) &&
 208        test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)
 209'
 210
 211test_expect_success 'barf on invalid name' '
 212        test_must_fail git filter-branch -f master xy-problem &&
 213        test_must_fail git filter-branch -f HEAD^
 214'
 215
 216test_expect_success '"map" works in commit filter' '
 217        git filter-branch -f --commit-filter "\
 218                parent=\$(git rev-parse \$GIT_COMMIT^) &&
 219                mapped=\$(map \$parent) &&
 220                actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
 221                test \$mapped = \$actual &&
 222                git commit-tree \"\$@\";" master~2..master &&
 223        git rev-parse --verify master
 224'
 225
 226test_expect_success 'Name needing quotes' '
 227
 228        git checkout -b rerere A &&
 229        mkdir foo &&
 230        name="れれれ" &&
 231        >foo/$name &&
 232        git add foo &&
 233        git commit -m "Adding a file" &&
 234        git filter-branch --tree-filter "rm -fr foo" &&
 235        test_must_fail git ls-files --error-unmatch "foo/$name" &&
 236        test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
 237
 238'
 239
 240test_expect_success 'Subdirectory filter with disappearing trees' '
 241        git reset --hard &&
 242        git checkout master &&
 243
 244        mkdir foo &&
 245        touch foo/bar &&
 246        git add foo &&
 247        test_tick &&
 248        git commit -m "Adding foo" &&
 249
 250        git rm -r foo &&
 251        test_tick &&
 252        git commit -m "Removing foo" &&
 253
 254        mkdir foo &&
 255        touch foo/bar &&
 256        git add foo &&
 257        test_tick &&
 258        git commit -m "Re-adding foo" &&
 259
 260        git filter-branch -f --subdirectory-filter foo &&
 261        test $(git rev-list master | wc -l) = 3
 262'
 263
 264test_expect_success 'Tag name filtering retains tag message' '
 265        git tag -m atag T &&
 266        git cat-file tag T > expect &&
 267        git filter-branch -f --tag-name-filter cat &&
 268        git cat-file tag T > actual &&
 269        test_cmp expect actual
 270'
 271
 272faux_gpg_tag='object XXXXXX
 273type commit
 274tag S
 275tagger T A Gger <tagger@example.com> 1206026339 -0500
 276
 277This is a faux gpg signed tag.
 278-----BEGIN PGP SIGNATURE-----
 279Version: FauxGPG v0.0.0 (FAUX/Linux)
 280
 281gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
 282acmwXaWET20H0GeAGP+7vow=
 283=agpO
 284-----END PGP SIGNATURE-----
 285'
 286test_expect_success 'Tag name filtering strips gpg signature' '
 287        sha1=$(git rev-parse HEAD) &&
 288        sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
 289        git update-ref "refs/tags/S" "$sha1t" &&
 290        echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
 291        git filter-branch -f --tag-name-filter cat &&
 292        git cat-file tag S > actual &&
 293        test_cmp expect actual
 294'
 295
 296test_expect_success GPG 'Filtering retains message of gpg signed commit' '
 297        mkdir gpg &&
 298        touch gpg/foo &&
 299        git add gpg &&
 300        test_tick &&
 301        git commit -S -m "Adding gpg" &&
 302
 303        git log -1 --format="%s" > expect &&
 304        git filter-branch -f --msg-filter "cat" &&
 305        git log -1 --format="%s" > actual &&
 306        test_cmp expect actual
 307'
 308
 309test_expect_success 'Tag name filtering allows slashes in tag names' '
 310        git tag -m tag-with-slash X/1 &&
 311        git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&
 312        git filter-branch -f --tag-name-filter "echo X/2" &&
 313        git cat-file tag X/2 > actual &&
 314        test_cmp expect actual
 315'
 316
 317test_expect_success 'Prune empty commits' '
 318        git rev-list HEAD > expect &&
 319        test_commit to_remove &&
 320        git filter-branch -f --index-filter "git update-index --remove to_remove.t" --prune-empty HEAD &&
 321        git rev-list HEAD > actual &&
 322        test_cmp expect actual
 323'
 324
 325test_expect_success 'prune empty collapsed merges' '
 326        test_config merge.ff false &&
 327        git rev-list HEAD >expect &&
 328        test_commit to_remove_2 &&
 329        git reset --hard HEAD^ &&
 330        test_merge non-ff to_remove_2 &&
 331        git filter-branch -f --index-filter "git update-index --remove to_remove_2.t" --prune-empty HEAD &&
 332        git rev-list HEAD >actual &&
 333        test_cmp expect actual
 334'
 335
 336test_expect_success 'prune empty works even without index/tree filters' '
 337        git rev-list HEAD >expect &&
 338        git commit --allow-empty -m empty &&
 339        git filter-branch -f --prune-empty HEAD &&
 340        git rev-list HEAD >actual &&
 341        test_cmp expect actual
 342'
 343
 344test_expect_success '--remap-to-ancestor with filename filters' '
 345        git checkout master &&
 346        git reset --hard A &&
 347        test_commit add-foo foo 1 &&
 348        git branch moved-foo &&
 349        test_commit add-bar bar a &&
 350        git branch invariant &&
 351        orig_invariant=$(git rev-parse invariant) &&
 352        git branch moved-bar &&
 353        test_commit change-foo foo 2 &&
 354        git filter-branch -f --remap-to-ancestor \
 355                moved-foo moved-bar A..master \
 356                -- -- foo &&
 357        test $(git rev-parse moved-foo) = $(git rev-parse moved-bar) &&
 358        test $(git rev-parse moved-foo) = $(git rev-parse master^) &&
 359        test $orig_invariant = $(git rev-parse invariant)
 360'
 361
 362test_expect_success 'automatic remapping to ancestor with filename filters' '
 363        git checkout master &&
 364        git reset --hard A &&
 365        test_commit add-foo2 foo 1 &&
 366        git branch moved-foo2 &&
 367        test_commit add-bar2 bar a &&
 368        git branch invariant2 &&
 369        orig_invariant=$(git rev-parse invariant2) &&
 370        git branch moved-bar2 &&
 371        test_commit change-foo2 foo 2 &&
 372        git filter-branch -f \
 373                moved-foo2 moved-bar2 A..master \
 374                -- -- foo &&
 375        test $(git rev-parse moved-foo2) = $(git rev-parse moved-bar2) &&
 376        test $(git rev-parse moved-foo2) = $(git rev-parse master^) &&
 377        test $orig_invariant = $(git rev-parse invariant2)
 378'
 379
 380test_expect_success 'setup submodule' '
 381        rm -fr ?* .git &&
 382        git init &&
 383        test_commit file &&
 384        mkdir submod &&
 385        submodurl="$PWD/submod" &&
 386        ( cd submod &&
 387          git init &&
 388          test_commit file-in-submod ) &&
 389        git submodule add "$submodurl" &&
 390        git commit -m "added submodule" &&
 391        test_commit add-file &&
 392        ( cd submod && test_commit add-in-submodule ) &&
 393        git add submod &&
 394        git commit -m "changed submodule" &&
 395        git branch original HEAD
 396'
 397
 398orig_head=$(git show-ref --hash --head HEAD)
 399
 400test_expect_success 'rewrite submodule with another content' '
 401        git filter-branch --tree-filter "test -d submod && {
 402                                         rm -rf submod &&
 403                                         git rm -rf --quiet submod &&
 404                                         mkdir submod &&
 405                                         : > submod/file
 406                                         } || :" HEAD &&
 407        test $orig_head != $(git show-ref --hash --head HEAD)
 408'
 409
 410test_expect_success 'replace submodule revision' '
 411        git reset --hard original &&
 412        git filter-branch -f --tree-filter \
 413            "if git ls-files --error-unmatch -- submod > /dev/null 2>&1
 414             then git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 submod
 415             fi" HEAD &&
 416        test $orig_head != $(git show-ref --hash --head HEAD)
 417'
 418
 419test_expect_success 'filter commit message without trailing newline' '
 420        git reset --hard original &&
 421        commit=$(printf "no newline" | git commit-tree HEAD^{tree}) &&
 422        git update-ref refs/heads/no-newline $commit &&
 423        git filter-branch -f refs/heads/no-newline &&
 424        echo $commit >expect &&
 425        git rev-parse refs/heads/no-newline >actual &&
 426        test_cmp expect actual
 427'
 428
 429test_expect_success 'tree-filter deals with object name vs pathname ambiguity' '
 430        test_when_finished "git reset --hard original" &&
 431        ambiguous=$(git rev-list -1 HEAD) &&
 432        git filter-branch --tree-filter "mv file.t $ambiguous" HEAD^.. &&
 433        git show HEAD:$ambiguous
 434'
 435
 436test_done