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
worktree.add corrupts repository index #665
Comments
Thank you for the report, @tiandrey ! I'll look into this as time allows. |
I was able to duplicate the problem outside of this library using the script below. The script will output Comment out the GIT_INDEX_FILE line and then Is this a defect in git itself? Regardless, I will search for a workaround such as not setting the GIT_INDEX_FILE environment variable if an index was not specified. When I tried this, some other tests failed. I am still looking into it. Examples of why you might want to set the index to something other than the default value are covered in this article by Linus Torvalds. Examples along those lines can be found in #!/bin/bash
BASEDIR=`mktemp -d`
cd "${BASEDIR}"
mkdir main_worktree
cd main_worktree
git init --initial-branch=main
echo '1.0' > VERSION
git add .
git commit -m 'init commit'
git checkout -b new_branch
echo '2.0' > VERSION
git add .
git commit -m 'new version'
# Comment out the GIT_INDEX_FILE line and then `git worktree add` will succeed
GIT_INDEX_FILE="${BASEDIR}/main_worktree/.git/index" \
GIT_DIR="${BASEDIR}/main_worktree/.git" \
GIT_WORK_TREE="${BASEDIR}/main_worktree" \
git --git-dir="${BASEDIR}/main_worktree/.git" --work-tree="${BASEDIR}/main_worktree" '-c' 'core.quotePath=true' '-c' 'color.ui=false' 'worktree' 'add' "${BASEDIR}/linked_worktree" 'main'
echo
if git status | grep -q 'nothing to commit, working tree clean'; then
echo 'SUCCESS: Index is clean'
else
echo 'FAILED: Index has been corrupted'
fi |
Subject of the issue
Working with git worktree via ruby-git corrupts repository index: previously clean original branch has files deleted/modified and reverse changes unstaged, new worktree has the same problems.
Your environment
git version 2.38.1
git (1.18.0)
ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5) [x86_64-linux]
Steps to reproduce
For demonstration I've created new repository with two branches, second 1 commit ahead of first, and from that second branch I've added worktree with first branch in a new directory - after that both worktrees became dirty.
Expected behaviour
Original and new worktrees are clean
Actual behaviour
Original and new worktrees are dirty, although files were not actually changed, only its status in index
Investigation
This case almost blew my brain out, I was debugging with strace to see which git commands ruby-git executes, and when I ran the same commands by hand - everything worked as expected, i.e. both worktrees were clean. But after thorough examination I found out that ruby-git adds three environment variables:
GIT_DIR
,GIT_WORK_TREE
andGIT_INDEX_FILE
. The first two were duplicated by command-line options--git-dir
and--work-tree
, but the last one was the root of the problem: it points to index file in the original worktree while new worktree should use its own index.I'm unsure whether that environment variable is needed at all, because
GIT_DIR
should be enough. But for sure it should be treated separately when working withgit worktree
.The text was updated successfully, but these errors were encountered: