Skip to content

Instantly share code, notes, and snippets.

@phemmer
Created January 5, 2016 17:01
Show Gist options
  • Select an option

  • Save phemmer/01c9e3fda97537dc7bfb to your computer and use it in GitHub Desktop.

Select an option

Save phemmer/01c9e3fda97537dc7bfb to your computer and use it in GitHub Desktop.
git clean stale archives
#!/bin/bash
eval "$(go env)"
function clean_gopath() {
gopath="$1"
pkgpath="$gopath/pkg/${GOHOSTOS}_$GOHOSTARCH"
if [[ ! -e "$pkgpath" ]]; then
return
fi
while IFS='' read a; do
pkg="${a%.a}"
if [[ ! -e "$gopath/src/$pkg" ]]; then
continue
fi
find "$gopath/src/$pkg" -maxdepth 1 \( -path "$gopath/src/$pkg" -o \( -name '*.go' \! -name '*_test.go' \) \) -newer "$pkgpath/$a" \( -exec rm "$pkgpath/$a" \; -o -true \) -quit &
done < <(find "$pkgpath" -name '*.a' -printf '%P\n')
wait
}
declare -A checked
IFS=: read -d '' -a gopaths < <(printf "%s" "$GOPATH")
for gopath in "${gopaths[@]}"; do
if [[ -n "${checked[$gopath]}" ]]; then
continue
fi
checked["$gopath"]=1
clean_gopath "$gopath" &
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment