Skip to content

Commit b2e9e6a

Browse files
authored
Add support for git worktrees (#353)
Adds numbering & aliasing for branches currently checked out by git worktrees. Previously, if you were to create a new git worktree, the branch does not get picked up with scm_breeze's numbering: ``` $ gco -b tmp && gco - && gco -b tmp2 && gco - $ git worktree add /tmp/scm_breeze_tmp tmp $ gb * [1] main + tmp [3] tmp2 $ gco 2 error: pathspec '+ tmp' did not match any file(s) known to git ``` With this change, worktrees now appear to the user as "just another branch." Using scm_breeze aliases, you can swap branches seamlessly, using numeric aliases and without needing to manually remember & change directories. ``` $ gb * [1] main + [2] tmp [3] tmp2 $ gco 2 Switching to worktree: /tmp/scm_breeze_tmp ``` The `+` distinguishes between branches currently checked out in separate worktrees
2 parents 62ff4cd + 48b6272 commit b2e9e6a

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

lib/git/aliases.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ function git(){
3030
case $1 in
3131
commit|blame|add|log|rebase|merge|difftool|switch)
3232
exec_scmb_expand_args "$_git_cmd" "$@";;
33-
checkout|diff|rm|reset|restore)
33+
checkout)
34+
_scmb_git_checkout_shortcuts "${@:2}";;
35+
diff|rm|reset|restore)
3436
exec_scmb_expand_args --relative "$_git_cmd" "$@";;
3537
branch)
3638
_scmb_git_branch_shortcuts "${@:2}";;

lib/git/branch_shortcuts.sh

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,54 @@ function _scmb_git_branch_shortcuts {
2727
line_count = output.lines.to_a.size
2828
output.lines.each_with_index do |line, i|
2929
spaces = (line_count > 9 && i < 9 ? " " : " ")
30-
puts line.sub(/^([ *]{2})/, "\\\1\033[2;37m[\033[0m#{i+1}\033[2;37m]\033[0m" << spaces)
30+
puts line.sub(/^([ *+]{2})/, "\\\1\033[2;37m[\033[0m#{i+1}\033[2;37m]\033[0m" << spaces)
3131
end
3232
EOF
33-
)"
33+
)"
3434

3535
# Set numbered file shortcut in variable
3636
local e=1 IFS=$'\n'
37-
for branch in $($_git_cmd branch "$@" | sed "s/^[* ]\{2\}//"); do
37+
for branch in $($_git_cmd branch "$@" | sed "s/^[*+ ]\{2\}//"); do
3838
export $GIT_ENV_CHAR$e="$branch"
3939
if [ "${scmbDebug:-}" = "true" ]; then echo "Set \$$GIT_ENV_CHAR$e => $file"; fi
4040
let e++
4141
done
4242
}
4343

44+
function _scmb_git_checkout_shortcuts {
45+
fail_if_not_git_repo || return 1
46+
47+
if [ -z "$1" ]; then
48+
exec_scmb_expand_args $_git_cmd checkout
49+
return $?
50+
fi
51+
52+
if [[ "$1" =~ ^[0-9]+$ ]]; then
53+
local branch_var="${git_env_char}$1"
54+
local branch=$(eval echo "\$$branch_var")
55+
56+
if [ -n "$branch" ]; then
57+
if $_git_cmd worktree list --porcelain 2>/dev/null | grep -q "^branch refs/heads/$branch$"; then
58+
local worktree_path=$($_git_cmd worktree list --porcelain | awk "
59+
/^worktree / {
60+
path = substr(\$0, 10); next
61+
}
62+
/^branch refs\/heads\/$branch$/ {
63+
print path; exit
64+
}
65+
")
66+
if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then
67+
echo "Switching to worktree: $worktree_path"
68+
cd "$worktree_path"
69+
return $?
70+
fi
71+
fi
72+
fi
73+
fi
74+
75+
exec_scmb_expand_args $_git_cmd checkout "$@"
76+
}
77+
4478
__git_alias "$git_branch_alias" "_scmb_git_branch_shortcuts" ""
4579
__git_alias "$git_branch_all_alias" "_scmb_git_branch_shortcuts" "-a"
4680
__git_alias "$git_branch_move_alias" "_scmb_git_branch_shortcuts" "-m"

0 commit comments

Comments
 (0)