coder: 2.16.1 -> 2.17.3 (#363393)
[NixPkgs.git] / pkgs / build-support / fetchgit / deterministic-git
blob67f5855591167d49452edfe906d5499797e54e1c
1 #!/bin/sh
3 # some git commands print to stdout, which would contaminate our JSON output
4 clean_git(){
5 git "$@" >&2
8 # Remove all remote branches, remove tags not reachable from HEAD, do a full
9 # repack and then garbage collect unreferenced objects.
10 make_deterministic_repo(){
11 local repo="$1"
13 # run in sub-shell to not touch current working directory
15 cd "$repo"
16 # Remove files that contain timestamps or otherwise have non-deterministic
17 # properties.
18 rm -rf .git/logs/ .git/hooks/ .git/index .git/FETCH_HEAD .git/ORIG_HEAD \
19 .git/refs/remotes/origin/HEAD .git/config
21 # Remove all remote branches.
22 git branch -r | while read -r branch; do
23 clean_git branch -rD "$branch"
24 done
26 # Remove tags not reachable from HEAD. If we're exactly on a tag, don't
27 # delete it.
28 maybe_tag=$(git tag --points-at HEAD)
29 git tag --contains HEAD | while read -r tag; do
30 if [ "$tag" != "$maybe_tag" ]; then
31 clean_git tag -d "$tag"
33 done
35 # Do a full repack. Must run single-threaded, or else we lose determinism.
36 clean_git config pack.threads 1
37 clean_git repack -A -d -f
38 rm -f .git/config
40 # Garbage collect unreferenced objects.
41 # Note: --keep-largest-pack prevents non-deterministic ordering of packs
42 # listed in .git/objects/info/packs by only using a single pack
43 clean_git gc --prune=all --keep-largest-pack