Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .bare directory #15

Open
calebdw opened this issue Jun 9, 2024 · 4 comments
Open

Add support for .bare directory #15

calebdw opened this issue Jun 9, 2024 · 4 comments
Labels
documentation Improvements or additions to documentation

Comments

@calebdw
Copy link

calebdw commented Jun 9, 2024

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`
#!/usr/bin/env bash

set -e

usage() {
    echo "Usage: $0 [--upstream=<upstream-url>] <repo-url> [<directory-name>]"
    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
Copy link
Owner

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.

@polarmutex polarmutex added the documentation Improvements or additions to documentation label Jul 18, 2024
@calebdw
Copy link
Author

calebdw commented Jul 18, 2024

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
Copy link

rfguimaraes commented Aug 30, 2024

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
Copy link

Apologies, I did more testing: it seems that when passing ../ because this check gets triggered:

Git.has_worktree(path, function(found)
if found then
Log.error('worktree already exists')
return
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants