From 260a2696e636ecf5db23e738d48c6d0c68bf95f2 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Thu, 1 Oct 2020 02:00:01 +0000 Subject: [PATCH] filter-repo --- other-test-repos/README.md | 1 + other-test-repos/filter-repo.sh | 61 +++++++++++++++++++++++++++++++ other-test-repos/partial-clone.sh | 4 +- 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100755 other-test-repos/filter-repo.sh diff --git a/other-test-repos/README.md b/other-test-repos/README.md index e4c6bee..b7797b8 100644 --- a/other-test-repos/README.md +++ b/other-test-repos/README.md @@ -17,6 +17,7 @@ There are also some tests that could not be included here conveniently: - - - : [partial-clone.sh](partial-clone.sh) | https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository/52269934#52269934 +- : [filter-repo.sh](filter-repo.sh) - - - diff --git a/other-test-repos/filter-repo.sh b/other-test-repos/filter-repo.sh new file mode 100755 index 00000000..cfdd895 --- /dev/null +++ b/other-test-repos/filter-repo.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +set -eu + +# Reproducibility. +export GIT_COMMITTER_NAME='a' +export GIT_COMMITTER_EMAIL='a' +export GIT_AUTHOR_NAME='a' +export GIT_AUTHOR_EMAIL='a' +export GIT_COMMITTER_DATE='2000-01-01T00:00:00+0000' +export GIT_AUTHOR_DATE='2000-01-01T00:00:00+0000' + +# https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository/52269934#52269934 +rm -rf filter-repo.tmp +mkdir -p filter-repo.tmp +cd filter-repo.tmp +git init + +# Create repo. +git init --quiet + +# First commit. +# Directories present in all branches. +mkdir d1 d2 +printf 'd1/a' > ./d1/a +printf 'd1/b' > ./d1/b +printf 'd2/a' > ./d2/a +printf 'd2/b' > ./d2/b +# Present only in root. +mkdir 'root' +printf 'root' > ./root/root +git add . +git commit -m 'root' --quiet +root_commmit_sha="$(git log -1 --format="%H")" + +# Second commit only on master. +git rm --quiet -r ./root +mkdir 'master' +printf 'master' > ./master/master +printf 'd1/a' > ./d1/a2 +git add . +git commit -m 'master commit' --quiet + +# Third commit only on master. +printf 'd1/b' > ./d1/b2 +git add . +git commit -m 'master commit' --quiet + +# Second commit only on mybranch. +git checkout -b mybranch --quiet "$root_commmit_sha" +git rm --quiet -r ./root +mkdir 'mybranch' +printf 'mybranch' > ./mybranch/mybranch +git add . +git commit -m 'mybranch commit' --quiet + +# Restore master. +git checkout --quiet master + +# To easily compare with the original repo. +git remote add origin https://github.com/cirosantilli/test-git-filter-repository +git fetch diff --git a/other-test-repos/partial-clone.sh b/other-test-repos/partial-clone.sh index 444a645..8686f9b 100755 --- a/other-test-repos/partial-clone.sh +++ b/other-test-repos/partial-clone.sh @@ -1,13 +1,11 @@ #!/usr/bin/env bash +set -eu # https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository/52269934#52269934 rm -rf partial-clone.tmp mkdir -p partial-clone.tmp cd partial-clone.tmp -#!/usr/bin/env bash -set -eu - list-objects() ( git rev-list --all --objects echo "master commit SHA: $(git log -1 --format="%H")"