From ce8922a2de241b9eab838c4392bc3ec47f5a8c74 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Tue, 4 Jan 2011 20:13:08 +0100 Subject: [PATCH] Added Repository.Init() implementation. --- libgit2sharp.Tests/InitializingARepository.cs | 33 +++++++++ .../ReadOnlyRepositoryFixtureBase.cs | 4 +- .../ReadWriteRepositoryFixtureBase.cs | 60 +++------------- .../RepositoryToBeCreatedFixtureBase.cs | 69 +++++++++++++++++++ libgit2sharp.Tests/libgit2sharp.Tests.csproj | 2 + libgit2sharp/Repository.cs | 9 ++- libgit2sharp/RepositoryLifecycleManager.cs | 17 ++++- libgit2sharp/Wrapper/LibGit2Api.cs | 3 + libgit2sharp/backlog.md | 1 - libgit2wrap/src/libgit2wrap.c | 13 +++- libgit2wrap/src/libgit2wrap.h | 1 + 11 files changed, 155 insertions(+), 57 deletions(-) create mode 100644 libgit2sharp.Tests/InitializingARepository.cs create mode 100644 libgit2sharp.Tests/RepositoryToBeCreatedFixtureBase.cs diff --git a/libgit2sharp.Tests/InitializingARepository.cs b/libgit2sharp.Tests/InitializingARepository.cs new file mode 100644 index 00000000..cb123559 --- /dev/null +++ b/libgit2sharp.Tests/InitializingARepository.cs @@ -0,0 +1,33 @@ +using System.IO; +using NUnit.Framework; + +namespace libgit2sharp.Tests +{ + [TestFixture] + public class InitializingARepository : RepositoryToBeCreatedFixtureBase + { + [TestCase(true)] + [TestCase(false)] + public void ShouldReturnAValidGitPath(bool isBare) + { + var expectedGitDirName = new DirectoryInfo(PathToTempDirectory).Name; + + expectedGitDirName += isBare ? "/" : "/.git/"; + + var gitDirPath = Repository.Init(PathToTempDirectory, isBare); + StringAssert.EndsWith(expectedGitDirName, gitDirPath); + } + + [TestCase(true)] + [TestCase(false)] + public void ShouldGenerateAValidRepository(bool isBare) + { + var gitDirPath = Repository.Init(PathToTempDirectory, isBare); + + using (var repo = new Repository(gitDirPath)) + { + Assert.AreEqual(gitDirPath, repo.Details.RepositoryDirectory); + } + } + } +} \ No newline at end of file diff --git a/libgit2sharp.Tests/ReadOnlyRepositoryFixtureBase.cs b/libgit2sharp.Tests/ReadOnlyRepositoryFixtureBase.cs index 1b1c7123..c62af3e7 100644 --- a/libgit2sharp.Tests/ReadOnlyRepositoryFixtureBase.cs +++ b/libgit2sharp.Tests/ReadOnlyRepositoryFixtureBase.cs @@ -2,6 +2,8 @@ namespace libgit2sharp.Tests { public class ReadOnlyRepositoryFixtureBase { - protected virtual string PathToRepository { get { return "../../Resources/testrepo.git"; } } + private const string readOnlyGitRepository = "../../Resources/testrepo.git"; + + protected virtual string PathToRepository { get { return readOnlyGitRepository; } } } } \ No newline at end of file diff --git a/libgit2sharp.Tests/ReadWriteRepositoryFixtureBase.cs b/libgit2sharp.Tests/ReadWriteRepositoryFixtureBase.cs index 0fdc215f..5ddbff9c 100644 --- a/libgit2sharp.Tests/ReadWriteRepositoryFixtureBase.cs +++ b/libgit2sharp.Tests/ReadWriteRepositoryFixtureBase.cs @@ -5,34 +5,27 @@ namespace libgit2sharp.Tests { - public class ReadWriteRepositoryFixtureBase : ReadOnlyRepositoryFixtureBase + public class ReadWriteRepositoryFixtureBase : RepositoryToBeCreatedFixtureBase { - private const string TestRepositoriesDirectoryName = "TestRepos"; - private static readonly string _testRepositoriesDirectoryPath = RetrieveTestRepositoriesDirectory(); + private const string readOnlyGitRepository = "../../Resources/testrepo.git"; + private string _pathToRepository; - protected override string PathToRepository { get { return _pathToRepository; } } + protected string PathToRepository { get { return _pathToRepository; } } [SetUp] - public void Setup() + public override void Setup() { - // Create temporary working directory - string workDirpath = Path.Combine(_testRepositoriesDirectoryPath, this.GetType().Name, Guid.NewGuid().ToString().Substring(0, 8)); + base.Setup(); - var source = new DirectoryInfo(base.PathToRepository); - var tempRepository = new DirectoryInfo(Path.Combine(workDirpath, source.Name)); + var source = new DirectoryInfo(readOnlyGitRepository); + var tempRepository = new DirectoryInfo(Path.Combine(PathToTempDirectory, source.Name)); CopyFilesRecursively(source, tempRepository); _pathToRepository = tempRepository.FullName; } - [TestFixtureTearDown] - public void TestFixtureTearDown() - { - DeleteDirectory(_testRepositoriesDirectoryPath); - } - public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target) { // From http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c/58779#58779 @@ -42,42 +35,5 @@ public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo targ foreach (FileInfo file in source.GetFiles()) file.CopyTo(Path.Combine(target.FullName, file.Name)); } - - static private string RetrieveTestRepositoriesDirectory() - { - return Path.Combine(RetrieveAssemblyDirectory(), TestRepositoriesDirectoryName); - } - - static private string RetrieveAssemblyDirectory() - { - // From http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in/283917#283917 - - string codeBase = Assembly.GetExecutingAssembly().CodeBase; - var uri = new UriBuilder(codeBase); - string path = Uri.UnescapeDataString(uri.Path); - return Path.GetDirectoryName(path); - } - - private static void DeleteDirectory(string directoryPath) - { - // From http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502 - - string[] files = Directory.GetFiles(directoryPath); - string[] dirs = Directory.GetDirectories(directoryPath); - - foreach (string file in files) - { - File.SetAttributes(file, FileAttributes.Normal); - File.Delete(file); - } - - foreach (string dir in dirs) - { - DeleteDirectory(dir); - } - - File.SetAttributes(directoryPath, FileAttributes.Normal); - Directory.Delete(directoryPath, false); - } } } \ No newline at end of file diff --git a/libgit2sharp.Tests/RepositoryToBeCreatedFixtureBase.cs b/libgit2sharp.Tests/RepositoryToBeCreatedFixtureBase.cs new file mode 100644 index 00000000..0d1bfe2d --- /dev/null +++ b/libgit2sharp.Tests/RepositoryToBeCreatedFixtureBase.cs @@ -0,0 +1,69 @@ +using System; +using System.IO; +using System.Reflection; +using NUnit.Framework; + +namespace libgit2sharp.Tests +{ + public class RepositoryToBeCreatedFixtureBase + { + private const string TestRepositoriesDirectoryName = "TestRepos"; + private static readonly string _testRepositoriesDirectoryPath = RetrieveTestRepositoriesDirectory(); + private string _pathToTempDirectory; + + protected string PathToTempDirectory { get { return _pathToTempDirectory; } } + + [SetUp] + public virtual void Setup() + { + string workDirpath = Path.Combine(_testRepositoriesDirectoryPath, this.GetType().Name, Guid.NewGuid().ToString().Substring(0, 8)); + + Directory.CreateDirectory(workDirpath); + + _pathToTempDirectory = workDirpath; + } + + [TestFixtureTearDown] + public virtual void TestFixtureTearDown() + { + DeleteDirectory(_testRepositoriesDirectoryPath); + } + + private static void DeleteDirectory(string directoryPath) + { + // From http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502 + + string[] files = Directory.GetFiles(directoryPath); + string[] dirs = Directory.GetDirectories(directoryPath); + + foreach (string file in files) + { + File.SetAttributes(file, FileAttributes.Normal); + File.Delete(file); + } + + foreach (string dir in dirs) + { + DeleteDirectory(dir); + } + + File.SetAttributes(directoryPath, FileAttributes.Normal); + Directory.Delete(directoryPath, false); + } + + static private string RetrieveAssemblyDirectory() + { + // From http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in/283917#283917 + + string codeBase = Assembly.GetExecutingAssembly().CodeBase; + var uri = new UriBuilder(codeBase); + string path = Uri.UnescapeDataString(uri.Path); + return Path.GetDirectoryName(path); + } + + static private string RetrieveTestRepositoriesDirectory() + { + return Path.Combine(RetrieveAssemblyDirectory(), TestRepositoriesDirectoryName); + } + } +} \ No newline at end of file diff --git a/libgit2sharp.Tests/libgit2sharp.Tests.csproj b/libgit2sharp.Tests/libgit2sharp.Tests.csproj index 7962aa71..42b90bf6 100644 --- a/libgit2sharp.Tests/libgit2sharp.Tests.csproj +++ b/libgit2sharp.Tests/libgit2sharp.Tests.csproj @@ -47,12 +47,14 @@ Code + + diff --git a/libgit2sharp/Repository.cs b/libgit2sharp/Repository.cs index 9d6c43d1..5e01b714 100644 --- a/libgit2sharp/Repository.cs +++ b/libgit2sharp/Repository.cs @@ -86,7 +86,14 @@ private TType ReadInternal(string objectId, DatabaseReader reader, Func LibGit2Api.wrapped_git_repository_init(out _repositoryPtr, Posixify(initializationDirectory), isBare)); + } + + public RepositoryLifecycleManager(string repositoryDirectory) { #region Parameters Validation if (string.IsNullOrEmpty(repositoryDirectory)) { - throw new ArgumentNullException(repositoryDirectory); + throw new ArgumentNullException("repositoryDirectory"); } #endregion Parameters Validation diff --git a/libgit2sharp/Wrapper/LibGit2Api.cs b/libgit2sharp/Wrapper/LibGit2Api.cs index 19c121d9..64f7fba6 100644 --- a/libgit2sharp/Wrapper/LibGit2Api.cs +++ b/libgit2sharp/Wrapper/LibGit2Api.cs @@ -7,6 +7,9 @@ internal static class LibGit2Api { private const string Libgit2 = "libgit2wrap.dll"; + [DllImport(Libgit2)] + public static extern OperationResult wrapped_git_repository_init(out IntPtr repoPtr, [In] string path, [In] bool isBare); + [DllImport(Libgit2)] public static extern OperationResult wrapped_git_repository_open(out IntPtr repoPtr, [In] string path); diff --git a/libgit2sharp/backlog.md b/libgit2sharp/backlog.md index cfa25ca7..1f45e6e8 100644 --- a/libgit2sharp/backlog.md +++ b/libgit2sharp/backlog.md @@ -7,7 +7,6 @@ - Resolve: Get rid of read_header call() when type is not specified. Within the wrapper method, send the object type as an out param to help with the C -> C# marshaling - Refactor the error handling (OutputResult -> Exceptions) - Launch Code Analysis - - Implement Repository.Init() once it's available in libgit2 ### Wrapper diff --git a/libgit2wrap/src/libgit2wrap.c b/libgit2wrap/src/libgit2wrap.c index df12ea61..dc203ade 100644 --- a/libgit2wrap/src/libgit2wrap.c +++ b/libgit2wrap/src/libgit2wrap.c @@ -1,6 +1,17 @@ #include "libgit2wrap.h" #include +int wrapped_git_repository_init(git_repository** repo_out, const char* path, unsigned int is_bare) +{ + git_repository *repo; + int error = git_repository_init(&repo, path, is_bare); + if (error < GIT_SUCCESS) + return error; + + *repo_out = repo; + return error; +} + int wrapped_git_repository_open(git_repository** repo_out, const char* path) { git_repository *repo; @@ -145,7 +156,7 @@ int wrapped_git_apply_tag(git_tag** tag_out, git_repository* repo, const char *r if (error != GIT_SUCCESS) return error; - tagger = git_signature_new(tagger_name, tagger_email, tagger_time, tagger_timezone_offset); + tagger = git_signature_new(tagger_name, tagger_email, tagger_time, tagger_timezone_offset); git_tag_set_tagger(tag, tagger); git_tag_set_name(tag, tag_name); diff --git a/libgit2wrap/src/libgit2wrap.h b/libgit2wrap/src/libgit2wrap.h index bc9f583a..0f561e91 100644 --- a/libgit2wrap/src/libgit2wrap.h +++ b/libgit2wrap/src/libgit2wrap.h @@ -5,6 +5,7 @@ GIT_BEGIN_DECL +GIT_EXTERN(int) wrapped_git_repository_init(git_repository** repo_out, const char* path, unsigned int is_bare); GIT_EXTERN(int) wrapped_git_repository_open(git_repository** repo_out, const char* path); GIT_EXTERN(int) wrapped_git_repository_open2(git_repository** repo_out, const char *git_dir, const char *git_object_directory, const char *git_index_file, const char *git_work_tree); GIT_EXTERN(void) wrapped_git_repository_free(git_repository* repo); -- GitLab