dotfiles/.config/fish/functions/worktree.fish

55 lines
1.5 KiB
Fish
Raw Permalink Normal View History

2024-03-06 10:42:56 -05:00
# inspired heavily by https://github.com/llimllib/personal_code/blob/daab9eb1da9f777df57c742e5629247a94b54947/homedir/.local/bin/worktree
function worktree
set worktree_commands add list lock move prune remove repair unlock
2024-03-06 10:42:56 -05:00
set branchname $argv[1]
if contains $branchname $worktree_commands
echo "did you mean `git worktree $branchname`?"
return 1
end
2024-03-06 10:42:56 -05:00
set dirname (string replace -a "/" "_" $branchname)
if not git pull
echo "Unable to run git pull, there may not be an upstream"
end
if git for-each-ref --format='%(refname:lstrip=2)' refs/heads | grep -E "^$branchname\$" > /dev/null 2>&1
git worktree add "../$dirname" "$branchname"
else if git for-each-ref --format='%(refname:lstrip=3)' refs/remotes/origin | grep -E "^$branchname\$" > /dev/null 2>&1
git worktree add "../$dirname" "$branchname"
else
git worktree add -b "$branchname" "../$dirname"
end
# Node projects
# if test -e "package.json"
# if test -d "node_modules"
# cp -Rc node_modules ../$dirname/node_modules
# end
# end
# Elixir projects
2024-03-15 08:07:05 -04:00
if test -e "mix.exs"
2024-04-24 10:11:46 -04:00
echo "Elixir project detected. Copying over _build and deps"
if test -d "_build"
cp -cR _build ../$dirname/
end
if test -d "deps"
cp -cR deps ../$dirname/
end
end
# typically untracked files
if test -e ".envrc"
2024-04-24 10:11:46 -04:00
echo "Copying over .envrc"
cp .envrc ../$dirname/
2024-03-06 10:42:56 -05:00
end
if test -e ".npmrc"
cp .npmrc ../$dirname/
end
cd ../$dirname
2024-03-06 10:42:56 -05:00
end