8000 Remove some code duplication · rlazev/libgit2sharp@35d1425 · GitHub
[go: up one dir, main page]

Skip to content

Commit 35d1425

Browse files
committed
Remove some code duplication
1 parent e27e18a commit 35d1425

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ private static bool IsRunningOnLinux()
4242
return (p == 4) || (p == 6) || (p == 128);
4343
}
4444

45+
public static bool RepositoryStateChecker(RepositorySafeHandle repositoryPtr, Func<RepositorySafeHandle, int> checker)
46+
{
47+
int res = checker(repositoryPtr);
48+
Ensure.Success(res, true);
49+
50+
return (res == 1);
51+
}
52+
4553
[DllImport(libgit2)]
4654
public static extern IntPtr git_blob_rawcontent(IntPtr blob);
4755

@@ -319,8 +327,7 @@ public static extern int git_repository_init(
319327
[MarshalAs(UnmanagedType.Bool)] bool isBare);
320328

321329
[DllImport(libgit2)]
322-
[return: MarshalAs(UnmanagedType.Bool)]
323-
public static extern bool git_repository_is_bare(RepositorySafeHandle handle);
330+
public static extern int git_repository_is_bare(RepositorySafeHandle handle);
324331

325332
[DllImport(libgit2)]
326333
public static extern int git_repository_is_empty(RepositorySafeHandle repo);

LibGit2Sharp/Repository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Repository(string path)
3535
int res = NativeMethods.git_repository_open(out handle, PosixPathHelper.ToPosix(path));
3636
Ensure.Success(res);
3737

38-
isBare = NativeMethods.git_repository_is_bare(handle);
38+
isBare = NativeMethods.RepositoryStateChecker(handle, NativeMethods.git_repository_is_bare);
3939

4040
if (!isBare)
4141
{

LibGit2Sharp/RepositoryInformation.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using LibGit2Sharp.Core;
1+
using System;
2+
using LibGit2Sharp.Core;
23

34
namespace LibGit2Sharp
45
{
@@ -47,27 +48,15 @@ internal RepositoryInformation(Repository repo, bool isBare)
4748
/// </value>
4849
public bool IsEmpty
4950
{
50-
get
51-
{
52-
int res = NativeMethods.git_repository_is_empty(repo.Handle);
53-
Ensure.Success(res, true);
54-
55-
return (res == 1);
56-
}
51+
get { return NativeMethods.RepositoryStateChecker(repo.Handle, NativeMethods.git_repository_is_empty); }
5752
}
5853

5954
/// <summary>
6055
/// Indicates whether the Head points to an arbitrary commit instead of the tip of a local banch.
6156
/// </summary>
6257
public bool IsHeadDetached
6358
{
64-
get
65-
{
66-
int res = NativeMethods.git_repository_head_detached(repo.Handle);
67-
Ensure.Success(res, true);
68-
69-
return (res == 1);
70-
}
59+
get { return NativeMethods.RepositoryStateChecker(repo.Handle, NativeMethods.git_repository_head_detached); }
7160
}
7261
}
7362
}

0 commit comments

Comments
 (0)
0