Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 572
Rafal Avatar asked Mar 17 '15 12:03

Rafal


1 Answers

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.

like image 79
nulltoken Avatar answered Nov 13 '22 08:11

nulltoken