diff --git a/.travis.yml b/.travis.yml index b2ebcf580..ea4a794b1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,10 +36,10 @@ deploy: branch: master skip_cleanup: true script: npm run --silent deploy -- -x -a -r https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git -- provider: script - on: - branch: greenkeeper/* - script: ./clean_greenkeeper +# - provider: script +# on: +# branch: greenkeeper/* +# script: ./clean_greenkeeper - provider: npm on: branch: diff --git a/clean_greenkeeper.sh b/clean_greenkeeper.sh index 063992597..1e9e55371 100755 --- a/clean_greenkeeper.sh +++ b/clean_greenkeeper.sh @@ -1,19 +1,51 @@ #!/bin/bash - # gh-pages cleanup script: Switches to gh-pages branch, and removes all # directories that aren't listed as remote branches +function deslash () { + # Recursively build a string of a directory's parents. E.g., + # deslashed "feature/test/branch" returns feature/test feature + deslashed=$(dirname $1) + if [[ $deslashed =~ .*/.* ]] + then + echo $deslashed $(deslash $deslashed) + else + echo $deslashed + fi +} +# Cache current branch current=$(git rev-parse --abbrev-ref HEAD) -git fetch origin -branches=$(git branch -r --list origin/*) -git checkout -f gh-pages -# Remove all directories that aren't remote branch names -find * -type d \( -path ./.git $(printf " -o -path ./%s" "${branches[@]//origin\//}") \) -prune -o -type d -exec rm -rfvi "$0" {} \; +# Checkout most recent gh-pages +git fetch upstream +git fetch origin +git checkout -f gh-pages-empty-history +git reset --hard origin/gh-pages-empty-history + +# Make an array of directories to not delete, from the list of remote branches +branches=$(git branch -r --list upstream/*) + +# Strip off the remote name prefix +branches="${branches[@]//upstream\//}" + +# Add parent directories of branches to the exclusion list (e.g. greenkeeper/) +for branch in $branches; do + if [[ $branch =~ .*/.* ]]; then + branches+=" $(deslash $branch)" + fi +done + +# Dedupe all the greenkeepers +branches=$(echo "${branches[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ') + +# Remove all directories that don't have corresponding branches +find . -type d \( -path ./.git $(printf " -o -path ./%s" $branches) \) -prune -o -type d -mindepth 1 -exec rm -rfv {} \; + +# Push git add -u git commit -m "Remove stale directories" -git push origin +# git push origin gh-pages # Return to where we were git checkout -f $current