@@ -199,6 +199,43 @@ clone(){
202+# Remove all remote branches, remove tags not reachable from HEAD, do a full
203+# repack and then garbage collect unreferenced objects.
204+make_deterministic_repo(){
207+ # run in sub-shell to not touch current working directory
210+ # Remove files that contain timestamps or otherwise have non-deterministic
212+ rm -rf .git/logs/ .git/hooks/ .git/index .git/FETCH_HEAD .git/ORIG_HEAD \
213+ .git/refs/remotes/origin/HEAD .git/config
215+ # Remove all remote branches.
216+ git branch -r | while read branch; do
217+ git branch -rD "$branch" >&2
220+ # Remove tags not reachable from HEAD. If we're exactly on a tag, don't
222+ maybe_tag=$(git tag --points-at HEAD)
223+ git tag --contains HEAD | while read tag; do
224+ if [ "$tag" != "$maybe_tag" ]; then
225+ git tag -d "$tag" >&2
229+ # Do a full repack, for determinism.
230+ # Repack does not add unreferenced objects to a pack file.
233+ # Garbage collect unreferenced objects.
202clone_user_rev() {
239clone_user_rev() {
203 local dir="$1"
240 local dir="$1"
204 local url="$2"
241 local url="$2"
@@ -227,9 +264,9 @@ clone_user_rev() {
227 echo "removing \`.git'..." >&2
264 echo "removing \`.git'..." >&2
228 find $dir -name .git\* | xargs rm -rf
265 find $dir -name .git\* | xargs rm -rf
230− # The logs and index contain timestamps, and the hooks contain
231− # the nix path of git's bash
232− find $dir -name .git | xargs -I {} rm -rf {}/logs {}/index {}/hooks
267+ find $dir -name .git | while read gitdir; do
268+ make_deterministic_repo "$(readlink -f "$gitdir/..")"