diff options
Diffstat (limited to 'shell-scripts/checkout')
| -rw-r--r-- | shell-scripts/checkout | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/shell-scripts/checkout b/shell-scripts/checkout new file mode 100644 index 0000000..a1cc9ce --- /dev/null +++ b/shell-scripts/checkout @@ -0,0 +1,69 @@ +#!/bin/bash +error () { + echo "$1" + exit 1 +} + +eval "$(pass show personal/shell-scripts/secrets)" +DEST_DIR="" +case "${@}" in + "az "*) + shift + LIST_PROJECTS="/_apis/projects?api-version=7.1-preview.4" + AUTH_HEADER="Authorization: Basic $WORK_AZDO_GIT_AUTH" + LIST_REPOSITORIES="/_apis/git/repositories?api-version=7.1-preview.1" + GIT_DIR="$HOME/projects/" + if [ ! -d $GIT_DIR ]; then + mkdir -p $GIT_DIR + fi + MAX_REPOS=20 + + echo "curl -s -H \"$AUTH_HEADER\" $WORK_AZDO_GIT_ORG_URL$LIST_PROJECTS" + PROJECT=$(curl -s -H "$AUTH_HEADER" $WORK_AZDO_GIT_ORG_URL$LIST_PROJECTS \ + | jq ' + .value[].name + ' \ + | xargs -I{} bash -c " + curl -s -H '$AUTH_HEADER' $WORK_AZDO_GIT_ORG_URL/{}$LIST_REPOSITORIES \ + | jq ' + .value[].name + ' \ + | awk '{ gsub(/\"/, \"\", \$1); printf \"{}/_git/%s\\n\", \$1 }' + " \ + | fzf) + + DEST_DIR="$GIT_DIR/$(echo $PROJECT | cut -d '/' -f3)" + if [ ! -d $DEST_DIR ] + then + git clone --bare $WORK_AZDO_GIT_ORG_URL/$PROJECT $DEST_DIR + fi + ;; + "gh "*) + shift + repo=$(gh repo list --json owner,name -q '.[] | "\(.owner.login)/\(.name)"' | fzf --print-query -1) + GIT_DIR="$HOME/projects" + if [ ! -d $GIT_DIR ]; then + mkdir -p $GIT_DIR + fi + + if [[ "$(echo "$repo" | wc -l)" -ne 1 ]]; then + echo "Fetching my repo" + repo="$(echo "$repo" | tail -n1)" + fi + + DEST_DIR="$GIT_DIR/$(echo $repo | cut -d '/' -f2)" + if [ ! -d $DEST_DIR ] + then + gh repo clone $repo $DEST_DIR -- --bare + fi + ;; + *) + error "Don't know how to fetch this" + ;; +esac + +if [[ $DEST_DIR -ne "" ]]; then + cd $DEST_DIR + git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" + $EDITOR +fi |
