aboutsummaryrefslogtreecommitdiff
path: root/bin/project-open
blob: adb27a4dc6309510cdd42836bfe147dea012e5a9 (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
#!/bin/sh

# shellcheck disable=SC2088
# (tile does not expand in single quote)
dest=$(
    find ~/git -mindepth 1 -maxdepth 1 -printf '%A@\t%f\n' |
    sort -r |
    cut -f 2 |
    dmenu -l 10 -p '~/git/'
)

[ -z "$dest" ] && exit 1

dest_path="$HOME/git/$dest"

if cd "$dest_path" 2> /dev/null
then
    touch "$dest_path"
    exec st
else
    while [ -z "$choice" ]
    do
        choice=$(printf 'create\nclone\ncancel' | dmenu -p "New repository at ~/git/$dest?")
    done

    case "$choice" in
        "create")
            mkdir -p "$dest_path"
            cd "$dest_path" || exit 1
            git init
            exec st
            ;;

        "clone")
            while [ -z "$remote_choice" ]
            do
                remote_choice=$(printf 'github.com\ncacharle.xyz\nclipboard\nother' | dmenu -p "Remote location")
            done
            case "$remote_choice" in
                github.com)   prefix='git@github.com:cacharle/'       ;;
                cacharle.xyz) prefix='git@cacharle.xyz:/srv/git/'     ;;
                clipboard)    dest="$(xclip -selection clipboard -o)" ;;
                other) ;;
            esac
            url="$prefix$(echo "$dest" | dmenu -p "Enter repository url: $prefix")"
            st -e /bin/sh -c "git clone --recursive '$url' '$dest_path' && cd '$dest_path' && exec $SHELL" ||
                notify-send -u critical "Could not clone $url in $dest_path" && exit 1
            ;;

        "cancel") exit ;;
        *) exit 1 ;;
    esac
fi