Rafal
Rafal

Reputation: 12629

Deletion of git repository

Consider following program:

var path = Path.Combine(
    Path.GetTempPath(),
    Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
Directory.CreateDirectory(path);

var testFile = Path.Combine(path, "test.txt");
File.WriteAllText(testFile, "Test file");

var source = Repository.Init(path);

using (var repository = new Repository(source))
{
    repository.Index.Add("test.txt");
}

Directory.Delete(path, true); 

On deletion of repository folder I get an UnauthorizedAccessException - access to one of internal git files is denied. Is there anything else I should dispose of in order to delete the folder?

Upvotes: 7

Views: 1349

Answers (1)

nulltoken
nulltoken

Reputation: 67669

Is there anything else I should dispose of in order to delete the folder?

Your disposing pattern is just fine. The mentioned issue has a different origin.

As stated in the documentation, UnauthorizedAccessException is raised when there's a permission related issue.

Indeed, Libgit2Sharp behaves similarly to git regarding this and marks files under the .git/objects hierarchy as read-only, thus the thrown exception when attempting to delete them.

In order to work around this, and ease the clean up phase when our tests are run, we've developed a helper method (ie. DirectoryHelper.DeleteDirectory()), which recursively unsets those read-only attributes and delete files and directories.

See the source code would you be willing to reuse it.

Upvotes: 13

Related Questions