RichiH/vcsh

pre-merge-unclobber hook with branches

Opened this issue · 2 comments

I'm trying to use the pre-merge-unclobber hook with branches, but hitting some issues. I've modified it a bit for my preference:

# debugging
echo '### pre-merge-unclobber called ###'

# ensure the clobbered directory exists
mkdir -p ~/.cache/vcsh/clobbered

for object in $(git ls-tree -r origin/master | awk '{print $4}'); do
  echo "$object"
  # [ -e "$object" ] && mv "$object" "~/.cache/vcsh/clobbered/$object"
done

When I try to clone a repository using vcsh clone -b bash git@github.com:nfarrar/dotfiles.git bash, I get the following output:

Initialized empty shared Git repository in /home/nfarrar/.config/vcsh/repo.d/bash.git/
Switched to a new branch 'bash'
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:nfarrar/dotfiles
 * branch            bash       -> FETCH_HEAD
 * [new branch]      bash       -> origin/bash
### pre-merge-unclobber called ###
fatal: Not a valid object name origin/master
vcsh: error: '.bashrc' exists.
vcsh: error: '.profile' exists.
vcsh: fatal: will stop after fetching and not try to merge!
  Once this situation has been resolved, run 'vcsh bash pull' to finish cloning.

It looks to me like the issue is that ls-tree is being called against the hardcoded origin/master - which doesn't work with branches - and this needs to be determined at runtime. I did some digging around in vcsh's docs to see if anything is exposed to the hooks at runtime, but didn't find what I was looking for - specifically the name of the branch we're currently running against. I also looked around to see if git exposes something that's being passed through and found this, which provides some environment variables - but not what I'm looking for. Any suggestions?

With a standard git hook, you can do something like branch=$(git rev-parse --symbolic --abbrev-ref $1) to parse the branch name - however not working with vcsh hook.

I think I've got it figured out:

#!/bin/sh

VCSH_CLOBBER_MSG=0
VCSH_CLOBBER_DIR="$HOME/.cache/vcsh/clobbered/$VCSH_REPO_NAME"

# move files out of the way
for object in $(git ls-tree -r "origin/$VCSH_REPO_NAME" | awk '{print $4}'); do

  if test -e $object; then

    # create the clobber directory
    [ ! -d "$VCSH_CLOBBER_DIR" ] && mkdir -p "$VCSH_CLOBBER_DIR"

    # display message
    if $VCSH_CLOBBER_MSG == 0; then
      echo "File conflicts detected. Existing files will be moved to $VCSH_CLOBBER_DIR."
      VCSH_CLOBBER_MSG=1
    fi

    echo "mv $HOME/$object to $VCSH_CLOBBER_DIR/$object"
    mv "$HOME/$object" "$VCSH_CLOBBER_DIR/$object"

  fi

done