threads / discuss / 50033

git-apply working on an index with smudged entries

Subject: git-apply working on an index with smudged entries

## tl;dr

2 messages between Dec 14, 2018 and Dec 15, 2018.

replies: 1people: 2as markdown or json

Christian Halstrick· Dec 14, 2018, 10:15 UTC · lore

I see that when I call "git apply --3way ..." on an index which was previously created by JGit and which contains smudged entries the command fails with message "error: foo.txt: does not match index". If I do a "git status" afterwards and then the same "git apply --3way ..." it succeeds. Looks like "git status" corrected the index in a way so "git apply" can accept it.

Question:
- is this because the index which jgit created is so corrupt that only
special commands
like "git status" can repair it?
- or is "git apply" not trying hard enough to consume a index with
smudged entries
Here is the trace of a script from Thomas which shows the effect:
> git --version
git version 2.19.1
> jgit --version
jgit version 5.2.0-SNAPSHOT
> git init
Initialized empty Git repository in /Users/d032780/tmp/a/.git/
> echo "foo" > foo.txt
> git add foo.txt
> git commit -m "Initial commit"
[master (root-commit) ff6c56c] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 foo.txt
> echo "bar" >> foo.txt
> git add foo.txt
> git commit -m "Second commit"
[master 2191919] Second commit
 1 file changed, 1 insertion(+)
> echo "baz" >> foo.txt
> git add foo.txt
> git commit -m "Third commit"
[master d863c4a] Third commit
 1 file changed, 1 insertion(+)
> git diff HEAD^1..HEAD foo.txt > ../foo.patch
> jgit reset --hard HEAD~
> git apply --3way ../foo.patch
error: foo.txt: does not match index
> git ls-files -sv --debug
H 100644 3bd1f0e29744a1f32b08d5650e62e2e62afb177c 0 foo.txt
  ctime: 0:0
  mtime: 1544782273:0
  dev: 0 ino: 0
  uid: 0 gid: 0
  size: 0 flags: 0
> git status

On branch master Untracked files: ...

> git ls-files -sv --debug
H 100644 3bd1f0e29744a1f32b08d5650e62e2e62afb177c 0 foo.txt
  ctime: 1544782273:797529188
  mtime: 1544782273:796936707
  dev: 16777220 ino: 64184423
  uid: 503 gid: 20
  size: 8 flags: 0
> git apply --3way ../foo.patch
> cat foo.txt

foo bar baz

>
Junio C Hamano· Dec 15, 2018, 00:40 UTC · re: Christian Halstrick · lore

Re: git-apply working on an index with smudged entries

Christian Halstrick <christian.halstrick@gmail.com> writes:
> Question:
> - is this because the index which jgit created is so corrupt that only
> special commands
> like "git status" can repair it?
No.
> - or is "git apply" not trying hard enough to consume a index with
> smudged entries
No.

Lower level "plumbing" commands like "git apply" want to see the cached stat information in the index refreshed before they are called, instead of having to refresh the index themselves before starting to work, for performance reasons. Higher level "Porcelain" commands like "git status" on the other hand refreshes the index before they start to work.

What you are seeing is a combination of
 * JGit not running its equivalent of "git update-index --refresh"
   before finishing and leaving the index file for you to look at.
 * "git status" running an equivalent of "git update-index --refresh"
   internally.
 * "git apply" not running "update-index --refresh" itself and
   instead expecting somebody else to have done so in the sequence
   of commands that it is a part of.
The latter two is working perfectly as designed.

I do not know what JGit wanted to do to leave the index in that state, so I cannot judge if that is a reasonable thing to do or JGit needs tobe fixed.

← back to recent threads