summaryrefslogtreecommitdiff
path: root/shell-scripts/kakup
blob: f2f894b7d72ee3ffcb5ba8809c0bec186f373100 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!@bash@/bin/bash
session-or-client() {
        {
            project="$(cat -)"
            if [[ -z $project ]]; then
                return
            fi
            pushd $project
            export name=${PWD#$HOME/}
            export name=${name//\//-}
            export name=${name#-}
            export server=kaks@$name
            export client=kakc@$name

            tmux has-session -t $server || {
                tmux new -d -s $server -n $server bash -c '[[ -f .envrc ]] && eval "$(direnv export bash)"; { kak -s '$name' -d & }; tmux wait -S '$name'; wait'
                tmux wait "$name"
            }
            if [[ -z $TMUX ]]; then
		tmux has-session -t $client || tmux new -d -s $client -n $client kak -c $name
            else
            	tmux new-window -n $client kak -c $name
            fi
            popd
        } </dev/stdin >debug 2>&1
        echo $client
}

search() {
    if tty -s
    then
        fd -d1 "." -t d $HOME $HOME/projects | fzf -1
    else
        output=$(mktemp -d /tmp/kakup.XXXXXXXX)/fifo
        mkfifo ${output}
        st -e bash -c "fd -d1 '.' -t d $HOME $HOME/projects | fzf -1 | tee ${output} >/dev/null 2>&1" &
        project="$(cat ${output})"
        rm -r $(dirname ${output})
    fi

    pushd $project >/dev/null 2>&1
    if grep 'bare = true' config >/dev/null 2>&1 ; then
        if tty -s
        then
            branch="$(git branch -a --format '%(refname)' | fzf -1)"
        else
            output=$(mktemp -d /tmp/kakup.XXXXXXXX)/fifo
            mkfifo ${output}
            st -e bash -c "git branch -a --format '%(refname)' | fzf -1 >${output}" &
            branch="$(cat ${output})"
            rm -r $(dirname ${output})
        fi
        echo "$branch" >debug 2>&1
        if [[ "$branch" == "refs/remotes/"* ]]; then
            git branch ${branch#refs/remotes/*/} -t $branch >debug 2>&1
            git worktree add ${branch#refs/remotes/*/} ${branch#refs/remotes/*/} >debug 2>&1
            branch="${branch#refs/remotes/*/}"
        elif [[ "$branch" == "refs/heads/"* ]]; then
            branch="${branch#refs/heads/}"
            git worktree add $branch $branch >debug 2>&1
        fi
        echo "$project/$branch"
    else
        echo "$project"
    fi
    popd >/dev/null 2>&1
}

NEW_TERM=0
while getopts "tf" opt; do
  case "$opt" in
    h|\?) exit 0 ;;
    t)  NEW_TERM=1 ;;
    f)  client="$(search | session-or-client)"  ;;
  esac
done

shift $((OPTIND - 1))

[ -z "$client" ] && {
    case "${@}" in
        "")		client="$(pwd | session-or-client)" ;;
        *)		client="$(echo "${@}" | session-or-client)" ;;
    esac
}

[[ ! -z "$client" ]] && {
    if [ $NEW_TERM -eq 0 ]
    then
        tmux attach -t "$client"
    else
        st -e tmux attach -t "$client"
    fi
}