Skip to content

Index.Remove with directory/pattern does not always work #1350

@mbektchiev

Description

@mbektchiev

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);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions