Skip to content

Add support for .bare directory #15

@calebdw

Description

@calebdw

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions