citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

gh-clone (791B)


      1 #!/usr/bin/env bash
      2 
      3 set -e
      4 
      5 usage () {
      6   printf "usage: gh-clone <owner>[/]<repo>\n" >&2
      7   exit 1
      8 }
      9 
     10 already_exists () {
     11   printf "%s\n" "$2"
     12   exit 2
     13 }
     14 
     15 root=${GITHUB_ROOT:-"$HOME/dev/github"}
     16 owner="$1"
     17 
     18 shift
     19 
     20 [ -z "$owner" ] && usage
     21 
     22 IFS='/' read -ra parsed <<< "$owner"
     23 owner="${parsed[0]}"
     24 repo="${parsed[1]}"
     25 if [ -z "$owner" ] || [ -z "$repo" ]; then
     26     usage
     27 fi
     28 
     29 dest="$root/$owner"
     30 dir="$dest/$repo"
     31 
     32 [ -d "$dir" ] && already_exists "$owner/$repo" "$dir"
     33 
     34 mkdir -p "$dest"
     35 cd "$dest"
     36 git clone "$@" "gh:$owner/$repo"
     37 
     38 git config \
     39     --file="$dir/.git/config" \
     40     --add remote.origin.fetch '+refs/pull/*/head:refs/pull/origin/*'
     41 
     42 git config \
     43     --file="$dir/.git/config" \
     44     --add remote.origin.fetch '+refs/pull/*/merge:refs/merge/origin/*'
     45 
     46 printf "%s\n" "$dest/$repo"