提交 ce8922a2 编写于 作者: N nulltoken

Added Repository.Init() implementation.

上级 f9607841
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
......@@ -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
......@@ -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
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
......@@ -47,12 +47,14 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="GitDateFixture.cs" />
<Compile Include="InitializingARepository.cs" />
<Compile Include="ObjectIdFixture.cs" />
<Compile Include="ReadOnlyRepositoryFixtureBase.cs" />
<Compile Include="ReadWriteRepositoryFixtureBase.cs" />
<Compile Include="RepositoryFixtures.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="InstantiatingARepository.cs" />
<Compile Include="RepositoryToBeCreatedFixtureBase.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\libgit2sharp\libgit2sharp.csproj">
......
......@@ -86,7 +86,14 @@ private TType ReadInternal<TType>(string objectId, DatabaseReader reader, Func<g
public static string Init(string path, bool isBare)
{
throw new NotImplementedException();
string repositoryDirectory;
using (var lifecycleManager = new RepositoryLifecycleManager(path, isBare))
{
repositoryDirectory = lifecycleManager.Details.RepositoryDirectory;
}
return repositoryDirectory;
}
void IDisposable.Dispose()
......
......@@ -20,13 +20,28 @@ public RepositoryDetails Details
get { return _details; }
}
public RepositoryLifecycleManager(string initializationDirectory, bool isBare)
{
#region Parameters Validation
if (string.IsNullOrEmpty("initializationDirectory"))
{
throw new ArgumentNullException("initializationDirectory");
}
#endregion Parameters Validation
OpenRepository(() => 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
......
......@@ -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);
......
......@@ -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
......
#include "libgit2wrap.h"
#include <assert.h>
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);
......
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册