summaryrefslogtreecommitdiff
path: root/.local/bin/checkout
blob: 96190858610736e491a0a182a359ac7319e96633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
error () {
    echo "$1"
    exit 1
}

. <(pass show work/env)
DEST_DIR=""
case "${@}" in
    az|"az "*)
        shift
        LIST_PROJECTS="/_apis/projects?api-version=7.1-preview.4"
        AUTH_HEADER="Authorization: Basic $(echo -n ":$GIT_PASS" | base64)"
        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|"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 ! [[ -z "$DEST_DIR" ]]; then
    cd $DEST_DIR
    git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
    $EDITOR "$DEST_DIR"
fi