summaryrefslogtreecommitdiff
path: root/shell-scripts/kakup
blob: 3e45d291f123030e4a817cb842d86979f32fb313 (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
session-or-client() {
        {
            project="$(cat -)"
            if [[ -z $project ]]; then
                return
            fi
            pushd $project
            export name=${PWD#$HOME/}
            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
}

is-bare() {
    project="$(cat -)"
    pushd $project >/dev/null 2>&1
    if grep 'bare = true' config >/dev/null 2>&1 ; then
        branch="$(git branch -a --format '%(refname)' | fzf -1)"
        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
}

case "${@}" in
    "")		client="$(pwd | session-or-client)" ;;
    .)  	client="$(fd -d1 "." -t d $HOME $HOME/projects | fzf -1 | is-bare | session-or-client)" ;;
    *)		client="$(echo "${@}" | session-or-client)" ;;
esac

echo "client: $client"
[[ $client ]] && tmux attach -t "$client"