pygit2 icon indicating copy to clipboard operation
pygit2 copied to clipboard

`Repository.diff(cached=True)` does not list diffs for indexed files

Open jbaiter opened this issue 8 years ago • 1 comments

This is with pygit2 version 0.24.0.

$ git init
Initialized empty Git repository in /tmp/minimal/.git/
$ touch foo
$ git add foo
$ git commit -am "First commit"
[master (root-commit) 3551222] First commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo
$ echo "foobar" >> foo                                                                                                                                                                                                                                                                                 
$ git add foo
$ git diff --cached
diff --git a/foo b/foo
index e69de29..323fae0 100644
--- a/foo
+++ b/foo
@@ -0,0 +1 @@
+foobar
$ python
>>> import pygit2
>>> repo = pygit2.Repository('.')
>>> list(repo.diff(cached=True))
[]

jbaiter avatar Jun 19 '17 12:06 jbaiter

Still the case, wow... :disappointed:

louis 🌟 ~/lab/pygit2/test_repo $ git add foo 
louis 🌟 ~/lab/pygit2/test_repo $ gcmmt "Initial commit"
[master (root-commit) 414c2d8] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo
louis 🌟 ~/lab/pygit2/test_repo $ echo "foobar" >>foo
louis 🌟 ~/lab/pygit2/test_repo $ git add foo
louis 🌟 ~/lab/pygit2/test_repo $ gdiff
diff --git a/foo b/foo
index e69de29..323fae0 100644
--- a/foo
+++ b/foo
@@ -0,0 +1 @@
+foobar
>>> import pygit2
>>> pygit2.Repository(".").diff(cached=True)
<_pygit2.Diff object at 0x7f455ae19330>
>>> pygit2.Repository(".").diff(cached=True).deltas
<_pygit2.DeltasIter object at 0x7f455bbf73f0>
>>> list(pygit2.Repository(".").diff(cached=True).deltas)
[]

Edit you need to pass "HEAD" and it will work, though the cached flag doesn't correspond to what I see from git diff vs git diff --cached

>>> import pygit2
>>> repo = pygit2.Repository(".")
>>> list(repo.diff("HEAD", cached=True))
[<_pygit2.Patch object at 0x7f00d08eb5d0>, <_pygit2.Patch object at 0x7f00d08eac40>]

That was correct: there are 2 files in the cached diff, 1 added (hello) 1 modified (foo)

louis 🌟 ~/lab/pygit2/test_repo $ git diff
louis 🌟 ~/lab/pygit2/test_repo $ git diff --cached
diff --git a/foo b/foo
index e69de29..323fae0 100644
--- a/foo
+++ b/foo
@@ -0,0 +1 @@
+foobar
diff --git a/hello b/hello
new file mode 100644
index 0000000..ce01362
--- /dev/null
+++ b/hello
@@ -0,0 +1 @@
+hello

The uncached version has 1 file in whereas git diff (no --cached flag) gives 0 files. However... if you're after the cached version then it should be OK (based on this initial test).

>>> import pygit2
>>> repo = pygit2.Repository(".")
>>> diff = repo.diff("HEAD", cached=False)
>>> diff.
diff.deltas         diff.from_c(        diff.parse_diff(    diff.patchid        
diff.find_similar(  diff.merge(         diff.patch          diff.stats          
>>> diff.patch
'diff --git a/foo b/foo\nindex e69de29..323fae0 100644\n--- a/foo\n+++ b/foo\n@@ -0,0 +1 @@\n+foobar\n'
>>> print(diff.patch)
diff --git a/foo b/foo
index e69de29..323fae0 100644
--- a/foo
+++ b/foo
@@ -0,0 +1 @@
+foobar

>>> diff.deltas
<_pygit2.DeltasIter object at 0x7fa088e58d50>
>>> list(diff.deltas)
[<_pygit2.DiffDelta object at 0x7fa05310b3f0>]

lmmx avatar Dec 19 '23 21:12 lmmx