libgit2 / pygit2

Python bindings for libgit2
https://www.pygit2.org/
Other
1.58k stars 382 forks source link

Error on attempt to realize equivalent of "git add file.txt", when file.txt was removed #1252

Closed sofakingworld closed 7 months ago

sofakingworld commented 7 months ago

Hello!

I'm a new user of Python and pygit2, I think I found some bug.

I'm trying to code the scripts from the code below using pygit2 and I'm having problems.

Bash code sample

➜  git init new
➜  cd new
➜  new git:(master) echo Hello > file.txt
➜  new git:(master) ✗ git add file.txt 
➜  new git:(master) ✗ git status
...
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   file.txt

➜  new git:(master) ✗ rm file.txt 
➜  new git:(master) ✗ git status
...
Changes to be committed:
    new file:   file.txt

Changes not staged for commit:
    deleted:    file.txt

➜  new git:(master) ✗ git add file.txt
➜  new git:(master) git status
...
nothing to commit (create/copy files and use "git add" to track)

pygit2 code sample (could be added into test/test_index.py)

def test_remove_tracked_file_and_stage_changes(testrepo):
    index = testrepo.index
    real_path_to_file = (testrepo.path.replace("/.git", "")) + '/hello.txt'

    os.remove(real_path_to_file)
    assert {'bye.txt': 128, 'hello.txt': 512} == testrepo.status()
    index.add('hello.txt')

Error

image

At the stage when I try to add a deleted file to the index (stage), the error is returned: file.txt' in stat: No such file or directory

sofakingworld commented 7 months ago

@jdavid Good day!

Could you help with the problem above? May be there are any workarounds?

jdavid commented 7 months ago

Good day. Use index.remove() to remove an entry from the index.