diff --git a/squash-my-commits b/squash-my-commits index 3e27a435b..79f91d865 100755 --- a/squash-my-commits +++ b/squash-my-commits @@ -8,25 +8,117 @@ # use './squash-my-commits -S' to sign the result with your pgp key # ########################################################################## -do_sign="$1" + +usage() +{ + echo "Usage: $0 [options]" + echo 'Options:' + echo ' -S, sign the result with your pgp key' + echo ' --push, force push result' + echo ' --ssh, use ssh to fetch from the registry' + echo ' --https, use https to fetch from the registry' + echo 'Environment variables:' + echo ' DN42_REG_URL, set the registry URL to use' +} + +########################################################################## +# parse arguments + +for arg +do + case "$arg" in + + -S) + do_sign='-S' + ;; + --push) + do_push=1 + ;; + --ssh) + echo 'Forcing use of SSH to fetch from registry' + reg_proto="ssh" + ;; + --https) + echo 'Forcing use of HTTPS to fetch from registry' + reg_proto="https" + ;; + --help) + usage + exit 0 + ;; + *) + echo "Unknown option: $arg" + usage + exit 1 + ;; + + esac +done + +########################################################################## # check for dn42registry remote, and add if missing git remote -v | grep dn42registry > /dev/null 2>&1 if [ "$?" -ne 0 ] then - reg='git@git.dn42.dev:dn42/registry.git' + + # was the URL specified directly ? + if [ -n "$DN42_REG_URL" ] + then + reg="$DN42_REG_URL" + else + + if [ -z "$reg_proto" ] + then + # if the proto wasn't forced, try to guess it + git remote -v | grep 'https' > /dev/null 2>&1 + if [ $? -eq 0 ] + then + reg_proto='https' + else + reg_proto='ssh' + fi + fi + + case "$reg_proto" in + ssh) + reg='git@git.dn42.dev:dn42/registry.git' + ;; + https) + reg='https://git.dn42.dev/dn42/registry.git' + ;; + *) + echo 'ERROR: Unknown registry protocol' + exit 1 + ;; + esac + fi + echo "Adding dn42registry remote: $reg" git remote add dn42registry "$reg" fi +########################################################################## + +# ensure the local branch is up to date +echo 'Rebasing local changes against the registry master' +git fetch dn42registry master +if [ $? -ne 0 ] +then + echo 'ERROR: Failed to fetch registry master branch' + echo 'Hint: you can use --ssh/--https to force use of ssh or https' + echo 'If all else fails, you can also set the DN42_REG_URL' + echo 'environment variable to directly specify the URL to fetch' + exit 1 +fi + # fail on errors from here onwards set -e -# ensure the local branch is up to date -echo "Rebasing local changes against the registry master" -git fetch dn42registry master git rebase dn42registry/master +########################################################################## + # find number of local commits count=$(git rev-list --count HEAD ^dn42registry/master) @@ -37,7 +129,7 @@ then exit 0 fi -echo "Squashing $count commits ..." +echo 'Squashing $count commits ...' # construct a new comment based on previous commits comment="squashed commit: @@ -48,10 +140,21 @@ $(git log --oneline HEAD ^dn42registry/master)" git reset --soft dn42registry/master git commit $do_sign -m "$comment" -echo "---" +# show what happened +echo '---' git log -n 1 --show-signature -echo "---" +echo '---' + +########################################################################## + +# push changes if requested +if [ "$do_push" -eq 1 ] +then + echo 'Force pushing changes' + git push --force +else + echo 'Remember to push your changes using: git push --force' +fi -echo "Remember to push your changes using: git push --force" ########################################################################## # end of file