libgit2 / libgit2/libgit2sharp

Index.Remove with directory/pattern does not always work

Open
#1,350 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
3.5k
Forks
925
PR merge metrics
No merged PRs in 30d

Description

The problem could be related to the commit operation but I'm not sure. If instead of repo.Index.Remove("*") you pass the filename, it is correctly removed from the Index.

Also if you commit the file using the console git client the * version starts working correctly.

For reproduction please see the following unit test:

using LibGit2Sharp;
using NUnit.Framework;
using System;
using System.IO;
using System.Linq;

namespace Tests
{
    public class LibGitTest
    {
        [Test]
        public void TestRemove()
        {
            var tempPath = Path.Combine(Path.GetTempPath(), "LibGitTest");

            if (Directory.Exists(tempPath))
            {
                DeleteDirectory(tempPath);
            }
            Directory.CreateDirectory(tempPath);

            using (var repo = new Repository(Repository.Init(tempPath)))
            {
                File.WriteAllText(Path.Combine(tempPath, "a.txt"), "some text");

                repo.Stage("*.*");
                var signature = repo.Config.BuildSignature(DateTimeOffset.Now);
                repo.Commit("Initial commit", signature, signature);

                repo.Index.Remove("*");
                // repo.Index.Remove("a.txt"); // uncomment this line to make the test pass

                var status = repo.RetrieveStatus();
                Assert.Contains("a.txt", status.Removed.Select(entry => entry.FilePath).ToArray());
            }
        }

        private static void DeleteDirectory(string tempPath)
        {
            // Some files in the .git folder are readonly, I'm not sure whether that's normal, but we need to reset the flag in order to successfully delete the directory
            Directory.EnumerateFiles(Path.Combine(tempPath), "*", SearchOption.AllDirectories)
                .ForEach(f => File.SetAttributes(f, File.GetAttributes(f) & ~FileAttributes.ReadOnly));

            Directory.Delete(tempPath, recursive: true);
        }
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the TestRemove unit test and inspect the behavior of repo.Index.Remove("*") compared with removing "a.txt" directly. Trace the index removal and subsequent RetrieveStatus() result, including whether the commit operation affects pattern matching. Done means the existing test passes with the wildcard removal and the direct filename behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, git
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.