Browse files

Delegate the creation and the moving of branches to libgit2 native me…

…thods
  • Loading branch information...
1 parent 0a7de3b commit 62388405a2dee8e4ca9cb6b9c3e069be6d9c3b00 @nulltoken nulltoken committed May 11, 2012
View
21 LibGit2Sharp/BranchCollection.cs
@@ -78,12 +78,20 @@ public Branch Checkout(string shaOrReferenceName)
/// </summary>
/// <param name = "name">The name of the branch.</param>
/// <param name = "shaOrReferenceName">The target which can be sha or a canonical reference name.</param>
+ /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
/// <returns></returns>
- public Branch Create(string name, string shaOrReferenceName)
+ public Branch Create(string name, string shaOrReferenceName, bool allowOverwrite = false)
{
+ Ensure.ArgumentNotNullOrEmptyString(name, "name");
+
ObjectId commitId = repo.LookupCommit(shaOrReferenceName).Id;
- repo.Refs.Create(NormalizeToCanonicalName(name), commitId.Sha);
+ using (var osw = new ObjectSafeWrapper(commitId, repo))
+ {
+ GitOid oid;
+ Ensure.Success(NativeMethods.git_branch_create(out oid, repo.Handle, name, osw.ObjectPtr, allowOverwrite));
+ }
+
return this[name];
}
@@ -107,21 +115,20 @@ public void Delete(string name)
}
///<summary>
- /// Rename an existing branch with a new name.
+ /// Rename an existing local branch with a new name.
///</summary>
///<param name = "currentName">The current branch name.</param>
///<param name = "newName">The new name of the existing branch should bear.</param>
///<param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
///<returns></returns>
public Branch Move(string currentName, string newName, bool allowOverwrite = false)
{
- Ensure.ArgumentNotNullOrEmptyString(currentName, "name");
+ Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
Ensure.ArgumentNotNullOrEmptyString(newName, "name");
- Reference reference = repo.Refs.Move(NormalizeToCanonicalName(currentName), NormalizeToCanonicalName(newName),
- allowOverwrite);
+ Ensure.Success(NativeMethods.git_branch_move(repo.Handle, currentName, newName, allowOverwrite));
- return this[reference.CanonicalName];
+ return this[newName];
}
private static bool LooksLikeABranchName(string referenceName)
View
11 LibGit2Sharp/Core/GitBranchType.cs
@@ -0,0 +1,11 @@
+using System;
+
+namespace LibGit2Sharp.Core
+{
+ [Flags]
+ internal enum GitBranchType
+ {
+ GIT_BRANCH_LOCAL = 1,
+ GIT_BRANCH_REMOTE = 2,
+ }
+}
View
21 LibGit2Sharp/Core/NativeMethods.cs
@@ -81,6 +81,27 @@ public static bool RepositoryStateChecker(RepositorySafeHandle repositoryPtr, Fu
public static extern int git_blob_rawsize(GitObjectSafeHandle blob);
[DllImport(libgit2)]
+ public static extern int git_branch_create(
+ out GitOid oid_out,
+ RepositorySafeHandle repo,
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string branch_name,
+ GitObjectSafeHandle target,
+ [MarshalAs(UnmanagedType.Bool)] bool force);
+
+ [DllImport(libgit2)]
+ public static extern int git_branch_delete(
+ RepositorySafeHandle repo,
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string branch_name,
+ GitBranchType branch_type);
+
+ [DllImport(libgit2)]
+ public static extern int git_branch_move(
+ RepositorySafeHandle repo,
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string old_branch_name,
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string new_branch_name,
+ [MarshalAs(UnmanagedType.Bool)] bool force);
+
+ [DllImport(libgit2)]
public static extern IntPtr git_commit_author(GitObjectSafeHandle commit);
[DllImport(libgit2)]
View
1 LibGit2Sharp/LibGit2Sharp.csproj
@@ -60,6 +60,7 @@
<Compile Include="Core\DisposableEnumerable.cs" />
<Compile Include="Core\EnumExtensions.cs" />
<Compile Include="ChangeKind.cs" />
+ <Compile Include="Core\GitBranchType.cs" />
<Compile Include="Core\GitDiff.cs" />
<Compile Include="Core\GitError.cs" />
<Compile Include="Core\GitObjectExtensions.cs" />

0 comments on commit 6238840

Please sign in to comment.