Skip to content

Instantly share code, notes, and snippets.

@mephir
Created September 18, 2017 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mephir/3288540b5bc681aa4f864af4c50c4d8b to your computer and use it in GitHub Desktop.
Save mephir/3288540b5bc681aa4f864af4c50c4d8b to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
function clone {
if [ ! -z "$3" ]; then
cmd="git clone --depth=50 --branch=$3 git@github.com:$1.git $2"
else
cmd="git clone --depth=50 git@github.com:$1.git $2"
fi;
echo "-> $cmd"
$cmd
}
function fetch {
cmd="git fetch origin +$1:"
echo "-> $cmd"
$cmd
}
function checkout {
cmd="git checkout -qf $1"
echo "-> $cmd"
$cmd
}
command -v git >/dev/null 2>&1 || { echo >&2 "I require git but it's not installed ;("; exit 1; }
usage="Usage `basename $0` [--help] [-o -h -f] repository target -- Clone and checkout repository
where:
-o output directory (default: "repository")
-h hash of commit
-f force
--help show this help text
repository github repository
target target branch or pull request
"
repo=""
target=$2
output=""
hash="HEAD"
force=false
if [ $# == 0 ] || [ $1 == "--help" ]; then
echo "$usage"
exit 0
fi
while getopts :r:t:o:f option
do
case "${option}"
in
h) hash=${OPTARG};;
o) output=${OPTARG};;
f) force=true;;
*) repo=${OPTARG};;
esac
done
shift $(($OPTIND - 1))
repo=$1
target=$2
if [ -z "$output" ]; then
output="$repo"
fi
if [ $force == true ] && [ -d $output ]; then
find -P $output -mindepth 1 -maxdepth 1 -exec rm -rf {} \;
fi
if [[ $target == "pull/"* ]]; then
hash="FETCH_HEAD"
fi
if [[ $target == "pull/"* ]]; then
clone $repo $output
(cd $output && fetch "refs/$target/merge")
else
clone $repo $output $target
fi
(cd $output && checkout $hash && echo "$target" > fetch.target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment