我对git存储库中的文件进行了一些更改,但尚未git add
。然后,我尝试使用jgit(通过rjgit)查看差异。
无需索取补丁,就可以正常工作:
> repo = RJGit::Repo.new('/path/to/repo', git_dir: '/path/to/repo/.git')
> RJGit::Porcelain.diff(repo, file_path: 'some_file')
[{:changetype=>"MODIFY",
:oldpath=>"some_file",
:newpath=>"some_file",
:oldmode=>33188,
:newmode=>33188,
:score=>0,
:oldid=>"9ace5cb286c3030f4ce67f30e62e1ae3da6b0152",
:newid=>"8131e7090f2ff0026149db01a6cc892df8c27793"}]
但是当我要求看补丁时,我得到了一个例外:
> repo = RJGit::Repo.new('/path/to/repo', git_dir: '/path/to/repo/.git')
> RJGit::Porcelain.diff(repo, patch: true, file_path: 'some_file')
Java::OrgEclipseJgitErrors::MissingObjectException: Missing blob 8131e7090f2ff0026149db01a6cc892df8c27793
from org.eclipse.jgit.internal.storage.file.WindowCursor.open(org/eclipse/jgit/internal/storage/file/WindowCursor.java:170)
org.eclipse.jgit.diff.ContentSource$ObjectReaderSource.open(org/eclipse/jgit/diff/ContentSource.java:145)
org.eclipse.jgit.diff.ContentSource$Pair.open(org/eclipse/jgit/diff/ContentSource.java:282)
org.eclipse.jgit.diff.DiffFormatter.open(org/eclipse/jgit/diff/DiffFormatter.java:1065)
org.eclipse.jgit.diff.DiffFormatter.createFormatResult(org/eclipse/jgit/diff/DiffFormatter.java:993)
org.eclipse.jgit.diff.DiffFormatter.format(org/eclipse/jgit/diff/DiffFormatter.java:698)
Jgit是正确的-该对象不存在:
$ ls .git/objects/81/31e7090f2ff0026149db01a6cc892df8c27793
ls: cannot access '.git/objects/81/31e7090f2ff0026149db01a6cc892df8c27793': No such file or directory
但是,git diff
可以向我展示补丁,而没有对象。如果我git add
创建了对象,那么即使我git reset
,jgit也不会再引发异常,因为该对象现在已经存在。
1)git diff
如何为尚不存在的对象显示补丁?
2)如何使jgit在不创建对象的情况下向我显示补丁,或者在不将文件添加到索引的情况下创建对象?
(注意:我在https://www.eclipse.org/forums/index.php/t/268050/和https://www.eclipse.org/forums/index.php/t/1086495/遇到了类似的问题,但没有一个解决方案。)
更新: 我找到了第二个问题的解决方案(“如何让jgit在不创建对象的情况下向我显示补丁,或者在不将文件添加到索引的情况下创建对象?”):
RJGit::Blob.new_from_string(repo, File.read('some_file'))
这将创建对象而不将其添加到索引中。这似乎不太理想,因为应该可以不接触文件系统。