polarmutex / git-worktree.nvim

MIT License
48 stars 12 forks source link

Add support for `.bare` directory #15

Open calebdw opened 3 months ago

calebdw commented 3 months ago

Hello!

I just switched over to using bare repos with git worktrees and I think it's awesome!

I was reading a lot of articles, and I found that it's popular to place all the git related stuff into a .bare folder to make a really clean looking directory structure---all the worktrees are front and center without a bunch of noise:

~/sources/neovim/git-worktree.nvim$ ls
  .bare/
  .git
  main/
  # ... other worktrees

where the .git folder just contains:

gitdir: ./.bare

However, when I create worktrees with this plugin it places the new worktree into the .bare dir instead of with the rest of the worktrees up a level from .bare. It would be awesome if this plugin could detect the existence of the .bare dir and move the new worktree up a level!

Just for grins, here is a script I found online and adapted to easily clone repos that follow this bare/worktree setup.

`git-clone-bare-for-worktrees` ```bash #!/usr/bin/env bash set -e usage() { echo "Usage: $0 [--upstream=] []" exit 1 } upstream="" while [[ "$1" =~ ^-- ]]; do case "$1" in --upstream=*) upstream="${1#*=}" shift ;; *) usage ;; esac done if [ -z "$1" ]; then usage fi url=$1 basename=${url##*/} name=${2:-${basename%.*}} echo "Creating directory $name..." mkdir -p "$name" cd "$name" git clone --bare "$url" .bare echo "gitdir: ./.bare" > .git echo "Configuring origin remote..." git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" git config core.logallrefupdates true git fetch origin if [ -n "$upstream" ]; then echo "Adding upstream remote..." git remote add upstream "$upstream" git fetch upstream fi # Need to update all local branches to track the remote branches # See: https://stackoverflow.com/questions/54367011/git-bare-repositories-worktrees-and-tracking-branches git for-each-ref --format='%(refname:short)' refs/heads | while read branch; do git branch --set-upstream-to=origin/"$branch" "$branch" done default_branch=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p') if [ -n "$default_branch" ]; then remote=$(if [ -n "$upstream" ]; then echo "upstream"; else echo "origin"; fi) echo "Creating initial worktree for the default branch ($remote/$default_branch)..." git worktree add -B "$default_branch" "$default_branch" "${remote}/$default_branch" fi ```

Thanks!

polarmutex commented 2 months ago

you pass in a relative path in the worktree create function. So you should be able to create a worktree with '../' when you make it. the worktree create function creates the folder from the gitdir which in your case is the bare repo.

I like this feature but there are so many ways people use git and worktrees that I am trying to keep the API simple and let the user interact with the plugin based on their git workflow

I do need to keep improving the documentation to showcase use cases or maybe have a wiki with common ones.

calebdw commented 2 months ago

Sounds good, after some more experience, I realized there was no need for the .bare/ dir and .git file pointing to .bare/---you can just rename .bare to .git and everything still works! The repo is a bare repo and the git files aren't littering the root directory, I think this would at least be a good usecase to support?

~/sources/neovim/git-worktree.nvim$ ls
  .git/
  main/
  # ... other worktrees
rfguimaraes commented 2 weeks ago

you pass in a relative path in the worktree create function. So you should be able to create a worktree with '../' when you make it. the worktree create function creates the folder from the gitdir which in your case is the bare repo.

I have a similar setup, that is, a repo cloned bare, but when I pass ../, nothing happens, but passing the absolute path indeed triggers the creation of the worktree. Could it be the case that the resolution of relative paths does not accept paths outside the current working directory?

Being able to pass ../ would completely solve the issue for my case at least

rfguimaraes commented 2 weeks ago

Apologies, I did more testing: it seems that when passing ../ because this check gets triggered: https://github.com/polarmutex/git-worktree.nvim/blob/500629d0ad916ec362f53ecf21f84f3ba445f73e/lua/git-worktree/worktree.lua#L94-L98