Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » JGit DiffCommand result is different from command line Git(JGit diff shows changes where "git diff" does not)
JGit DiffCommand result is different from command line Git [message #1782493] Sat, 24 February 2018 16:46 Go to next message
Wernke zur Borg is currently offline Wernke zur BorgFriend
Messages: 23
Registered: December 2011
Junior Member
In the following scenario the result of the JGit DiffCommand is different from the git diff command line. We would like JGit to behave the same as the git command line.

We are running an RCP application to generate a set of files in a folder named "gen" with these steps:

1. Rename the exiting folder gen to gen_bak.
2. Create a new folder gen and generate the files.
3. Delete folder gen_bak when everything was ok.

If we commit the result and run the generation a second time with the identical result, we have a strange effect.

Calling "git diff" on the command line shows no changes, the workspace is clean. This is the desired result. Running git.diff() from within the RCP application however lists all generated files as modified. It has noticed the renaming and deletion of the original folder and shows the backup files as missing, whereas the newly generated files are untracked. Using a DiffFormatter as decribed in RĂ¼diger Herrmann's great article yields the same result. I am setting the old tree to a CanonicalTreeParser on HEAD and the new tree to a FileTreeIterator of the workspace.

Question: How can we use the JGit DiffCommand in a way that yields the same result as the diff command line, i.e. shows no differences in the above scenario?

Thanks for any hints.
Wernke
Re: JGit DiffCommand result is different from command line Git [message #1782534 is a reply to message #1782493] Mon, 26 February 2018 09:02 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 256
Registered: July 2009
Senior Member
I don't think that JGit can detect that you have first moved the original folder, recreated the files newly and then deleted the backup folder. Especially because you say that JGit complains that the backup files are 'missing' this is a strong hint that backup files have been added before to the index.

But let's try it. I downloaded the latest 'jgit.sh' from https://www.eclipse.org/jgit/download/ and tried what you explain:

> curl -o jgit.sh https://repo.eclipse.org/content/groups/releases//org/eclipse/jgit/org.eclipse.jgit.pgm/4.10.0.201712302008-r/org.eclipse.jgit.pgm-4.10.0.201712302008-r.sh
...
> chmod +x jgit.sh
> ./jgit.sh init test
Initialized empty Git repository in /private/tmp/x/test/.git
> cd test
> mkdir gen
> echo -e 'abcd\nefgh' >gen/a.txt
> echo -e '1234\n5678' >gen/b.txt
> ../jgit.sh add gen
> ../jgit.sh status
...
	new file:   gen/a.txt
	new file:   gen/b.txt
> git status
...
	new file:   gen/a.txt
	new file:   gen/b.txt
> ../jgit.sh commit -m initial
[master 6ee4f0889631406b29c35c2b1966902edad7f2ee] initial
> ../jgit.sh status
On branch master
> git status
On branch master
nothing to commit, working tree clean
> mv gen/ gen.bak
> git status
...
	deleted:    gen/a.txt
	deleted:    gen/b.txt

no changes added to commit (use "git add" and/or "git commit -a")
> ../jgit.sh status
...
	deleted:    gen/a.txt
	deleted:    gen/b.txt
> mkdir gen
> echo -e 'abcd\nefgh' >gen/a.txt
> echo -e '1234\n5678' >gen/b.txt
> git status    # Isn't that strange. Why is git not telling about new files in gen.bak. But at least JGit and git do the same here.
On branch master
nothing to commit, working tree clean
> ../jgit.sh status
On branch master
> rm -fr gen.bak/
> git status
On branch master
nothing to commit, working tree clean
> ../jgit.sh status
On branch master
> git diff
> ../jgit.sh diff
>


Do you have the chance to call 'jgit.sh debug-show-dir-cache' in the root folder of your repo right before you do the git?
Can you reproduce your error with such a script?
On which platform are you running, windows?


Ciao
Chris
Re: JGit DiffCommand result is different from command line Git [message #1782536 is a reply to message #1782534] Mon, 26 February 2018 09:18 Go to previous messageGo to next message
Wernke zur Borg is currently offline Wernke zur BorgFriend
Messages: 23
Registered: December 2011
Junior Member
Hi Chris,

Thanks for your reply and for your example. I am trying hard to reproduce the problem but currently I am getting inconsistent results. No clear picture really.

Meanwhile I am pretty sure that in my case it is EGit tracking moves and deletions in the Eclipse workspace using an IResourceChangeListener and staging the changes behind my back. Of course this is not reproducible with your test setup.

I am not sure if I can prevent EGit from automatically staging moves and deletions. In the end I could always do a git reset --mixed after the generation to unstage all changes.

I am running on Windows, will try on Linux once I have a clear reproducible result.

Cheers, Wernke

[Updated on: Mon, 26 February 2018 09:20]

Report message to a moderator

Re: JGit DiffCommand result is different from command line Git [message #1782539 is a reply to message #1782536] Mon, 26 February 2018 09:36 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 256
Registered: July 2009
Senior Member
ok, great. One tip regarding inconsistent test results. Especially when testing with scripts which create repos, stage content und produce commits very fast you often run into the 'racy-git' situation [1]. That's a internal problem of git clients which you should as git or JGit user not care about. But if we are searching for bugs in JGit then it is easier if we can avoid the racy git situation. To avoid this in your scripts sleep for a second before adding files to the index. Like in
echo a>f; sleep 1; jgit add f


[1] https://www.kernel.org/pub/software/scm/git/docs/technical/racy-git.txt


Ciao
Chris
Re: JGit DiffCommand result is different from command line Git [message #1782583 is a reply to message #1782539] Mon, 26 February 2018 16:58 Go to previous message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1032
Registered: July 2009
Senior Member
You can switch off automatic staging of deletions and moves in EGit preferences:
Preferences > Team > Git > Committing > Automatically stage files being deleted
Preferences > Team > Git > Committing > Automatically stage files being moved
Previous Topic:JGit Find Merge Base
Next Topic:Git user.name set to one thing, but staging view says something else
Goto Forum:
  


Current Time: Fri Sep 21 13:21:51 GMT 2018

Powered by FUDForum. Page generated in 0.02059 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top