Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Question: How to Index.Remove() already deleted files #95

Closed
Flonix opened this Issue Dec 6, 2011 · 8 comments

Comments

Projects
None yet
2 participants
Contributor

Flonix commented Dec 6, 2011

Hi @ALL,

lets say we have the following workflow:

  1. Adding a file to the repository
  2. Commit
  3. The file will be deleted in the working copy (not with Index.Remove())
  4. How to get the repository in sync with the current working copy?

If you call Index.Remove() on an already deleted file you will get an exception.

I made some changes but my changes will result in some failing tests afterwards... So... How to remove an already deleted file?

[Update]
The above mentioned changes can be seen here

Member

nulltoken commented Dec 6, 2011

How to get the repository in sync with the current working copy?

You'd have to Stage() the removal of the file. This test demonstrates this feature.

If you call Index.Remove() on an already deleted file you will get an exception.

Hmm.. You're right.

$ mkdir test
$ cd test
$ git init .
$ echo "a" > a.txt
$ echo "b" > b.txt
$ git add .
$ git commit -m "Initial commit"

$ git status
# On branch master
nothing to commit (working directory clean)

# One can GitRemove an existing file
$ git rm b.txt

# One can GitRemove a file which has been deleted from the workdir
$ rm a.txt
$ git rm a.txt

# This produces similar results
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    a.txt
#       deleted:    b.txt
#

# However, one can not remove a file which has already been GitRemoved (supressed from both the workdir and the index)
$ git rm a.txt
fatal: pathspec 'a.txt' did not match any files

Damn! We're not "git compliant". Looks like you've found a bug ;-)

I made some changes

They look OK. Two nitpicks though:

  • Could you please fix the braces of the If Exists Then Delete statement?
  • Could you update the Xml documentation of the Remove methods as well?

but my changes will result in some failing tests afterwards...

Fix them as well, send a PR. I'll gladly merge it.

Very nice spotting 👏

Contributor

Flonix commented Dec 6, 2011

Hey @nulltoken,

thanks for your answer.

Should we allow Remove() of files with FileStatus.Modified assigned?
I'm not sure if libgit2 will allow this, but libgit2sharp currently allows it.
This will result in loosing some uncommited changes.

I will think of recursive Index methods...

Thanks so far...

Contributor

Flonix commented Dec 6, 2011

Hi again:

One more question:

  1. How to check a file path (string) for a directory path?

    Directory.Exists() is not a good solution, and in case of Remove() wrong.
    (Allow GitRemoving of a previously removed directory)

    I thought about Path.GetExtension() == "" ... but I hope there is a more intelligent way.

    Any idea/solution ?

Member

nulltoken commented Dec 7, 2011

Should we allow Remove() of files with FileStatus.Modified assigned?

$ mkdir test
$ cd test
$ git init .
$ echo "a" > a.txt
$ git add .
$ git commit -m "Initial commit"

$ echo "b" >> a.txt

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

$ git rm a.txt
error: 'a.txt' has local modifications
(use --cached to keep the file, or -f to force removal)

WOW! You're on a bug killing spree ;-) 👊

Could you please take care of fixing this (along with the tests) in a separate commit?

Member

nulltoken commented Dec 7, 2011

I will think of recursive Index methods... How to check a file path (string) for a directory path?

I'd prefer dealing with Directories in a dedicated Pull Request/Issue.

Contributor

Flonix commented Dec 7, 2011

I updated my topic/index branch.
Current commit can be seen here

I have problems with one test:

RemovingAInvalidFileThrows is failing with parameter set to: deleted_unstaged_file.txt

What exactly is a deleted_unstaged_file and why should this test fail?

Member

nulltoken commented Dec 8, 2011

$ mkdir test
$ cd test
$ git init .
$ echo "a" > a.txt
$ echo "a" > b.txt
$ git add .
$ git commit -m "Initial commit"

$ rm a.txt
$ rm b.txt

$ git add -u b.txt

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    b.txt
#
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    a.txt
#

a.txt would be FileStatus.Missing, similarly to to file deleted_unstaged_file.txt
b.txt would be FileStatus.Removed, similarly to to file deleted_staged_file.txt

$ git rm b.txt
fatal: pathspec 'b.txt' did not match any files

$ git rm a.txt

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    a.txt
#       deleted:    b.txt
#

This would mean that:

  • One can not IndexRemove a Removed file
  • One can IndexRemove a Missing file

What exactly is a deleted_unstaged_file and why should this test fail?

According to previous git test, it shouldn't. This is one of the tests that should be fixed as part of your commit.

nulltoken added a commit to nulltoken/libgit2sharp that referenced this issue Dec 15, 2011

Make Index.Remove() cope with files which have been deleted from the …
…working directory

Fixes issue #95 - 1/2.

One can GitRemove a file which has been deleted from the workdir

$ mkdir test
$ cd test
$ git init .
$ echo "a" > a.txt

$ git add .
$ git commit -m "Initial commit"

$ git status
nothing to commit (working directory clean)

$ rm a.txt

$ git status
On branch master
 Changes not staged for commit:
   (use "git add/rm <file>..." to update what will be committed)
   (use "git checkout -- <file>..." to discard changes in working directory)

       deleted:    a.txt

$ git rm a.txt

$ git status
On branch master
Changes to be committed:
   (use "git reset HEAD <file>..." to unstage)
       deleted:    a.txt

nulltoken added a commit to nulltoken/libgit2sharp that referenced this issue Dec 15, 2011

Make Index.Remove() cope with files which have been modified in the w…
…orking directory

Fixes issue #95 - 2/2.

One can't GitRemove a modified file.

$ mkdir test
$ cd test
$ git init .
$ echo "a" > a.txt
$ git add .
$ git commit -m "Initial commit"

$ echo "b" >> a.txt

$ git status
no changes added to commit (use "git add" and/or "git commit -a")

$ git rm a.txt
error: 'a.txt' has local modifications
(use --cached to keep the file, or -f to force removal)
Member

nulltoken commented Dec 15, 2011

Closing this issue as fixes have been applied on vNext.

Cheers!

@nulltoken nulltoken closed this Dec 15, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment