~ben/bin

b218f9b9c4bd2bf1a5655a50e853a1df087214d2 — Ben Sima 9 months ago d0ddaec master
add copy of jb55's gh-clone script
1 files changed, 47 insertions(+), 0 deletions(-)

A ghclone
A ghclone => ghclone +47 -0
@@ 0,0 1,47 @@
#!/usr/bin/env bash

set -e

usage () {
  printf "usage: $(basename $0) <owner>[/]<repo>\n" >&2
  exit 1
}

already_exists () {
  printf "%s\n" "$2"
  exit 2
}

root=${GITHUB_ROOT:-"$HOME/src"}
owner="$1"
repo="$2"

[ -z "$owner" ] && usage

if [ -z "$repo" ]; then
  IFS='/' read -ra parsed <<< "$owner"
  owner="${parsed[0]}"
  repo="${parsed[1]}"
  if [ -z "$owner" ] || [ -z "$repo" ]; then
    usage
  fi
fi

dest="$root/$owner"
dir="$dest/$repo"

[ -d "$dir" ] && already_exists "$owner/$repo" "$dir"

mkdir -p "$dest"
cd "$dest"
git clone "gh:$owner/$repo"

git config \
    --file="$dir/.git/config" \
    --add remote.origin.fetch '+refs/pull/*/head:refs/pull/origin/*'

git config \
    --file="$dir/.git/config" \
    --add remote.origin.fetch '+refs/pull/*/merge:refs/merge/origin/*'

printf "%s\n" "$dest/$repo"