|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -eu |
| 3 | + |
| 4 | +# Reproducibility. |
| 5 | +export GIT_COMMITTER_NAME='a' |
| 6 | +export GIT_COMMITTER_EMAIL='a' |
| 7 | +export GIT_AUTHOR_NAME='a' |
| 8 | +export GIT_AUTHOR_EMAIL='a' |
| 9 | +export GIT_COMMITTER_DATE='2000-01-01T00:00:00+0000' |
| 10 | +export GIT_AUTHOR_DATE='2000-01-01T00:00:00+0000' |
| 11 | + |
| 12 | +# https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository/52269934#52269934 |
| 13 | +rm -rf filter-repo.tmp |
| 14 | +mkdir -p filter-repo.tmp |
| 15 | +cd filter-repo.tmp |
| 16 | +git init |
| 17 | + |
| 18 | +# Create repo. |
| 19 | +git init --quiet |
| 20 | + |
| 21 | +# First commit. |
| 22 | +# Directories present in all branches. |
| 23 | +mkdir d1 d2 |
| 24 | +printf 'd1/a' > ./d1/a |
| 25 | +printf 'd1/b' > ./d1/b |
| 26 | +printf 'd2/a' > ./d2/a |
| 27 | +printf 'd2/b' > ./d2/b |
| 28 | +# Present only in root. |
| 29 | +mkdir 'root' |
| 30 | +printf 'root' > ./root/root |
| 31 | +git add . |
| 32 | +git commit -m 'root' --quiet |
| 33 | +root_commmit_sha="$(git log -1 --format="%H")" |
| 34 | + |
| 35 | +# Second commit only on master. |
| 36 | +git rm --quiet -r ./root |
| 37 | +mkdir 'master' |
| 38 | +printf 'master' > ./master/master |
| 39 | +printf 'd1/a' > ./d1/a2 |
| 40 | +git add . |
| 41 | +git commit -m 'master commit' --quiet |
| 42 | + |
| 43 | +# Third commit only on master. |
| 44 | +printf 'd1/b' > ./d1/b2 |
| 45 | +git add . |
| 46 | +git commit -m 'master commit' --quiet |
| 47 | + |
| 48 | +# Second commit only on mybranch. |
| 49 | +git checkout -b mybranch --quiet "$root_commmit_sha" |
| 50 | +git rm --quiet -r ./root |
| 51 | +mkdir 'mybranch' |
| 52 | +printf 'mybranch' > ./mybranch/mybranch |
| 53 | +git add . |
| 54 | +git commit -m 'mybranch commit' --quiet |
| 55 | + |
| 56 | +# Restore master. |
| 57 | +git checkout --quiet master |
| 58 | + |
| 59 | +# To easily compare with the original repo. |
| 60 | +git remote add origin https://github.com/cirosantilli/test-git-filter-repository |
| 61 | +git fetch |
0 commit comments