8000 Delegate the creation and the moving of branches to libgit2 native me… · gitextensions/libgit2sharp@5c5b9c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c5b9c7

Browse files
committed
Delegate the creation and the moving of branches to libgit2 native methods
1 parent aaa8c7e commit 5c5b9c7

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

LibGit2Sharp/BranchCollection.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,20 @@ public Branch Checkout(string shaOrReferenceName)
7878
/// </summary>
7979
/// <param name = "name">The name of the branch.</param>
8080
/// <param name = "shaOrReferenceName">The target which can be sha or a canonical reference name.</param>
81+
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
8182
/// <returns></returns>
82-
public Branch Create(string name, string shaOrReferenceName)
83+
public Branch Create(string name, string shaOrReferenceName, bool allowOverwrite = false)
8384
{
85+
Ensure.ArgumentNotNullOrEmptyString(name, "name");
86+
8487
ObjectId commitId = repo.LookupCommit(shaOrReferenceName).Id;
8588

86-
repo.Refs.Create(NormalizeToCanonicalName(name), commitId.Sha);
89+
using (var osw = new ObjectSafeWrapper(commitId, repo))
90+
{
91+
GitOid oid;
92+
Ensure.Success(NativeMethods.git_branch_create(out oid, repo.Handle, name, osw.ObjectPtr, allowOverwrite));
93+
}
94+
8795
return this[name];
8896
}
8997

@@ -107,21 +115,20 @@ public void Delete(string name)
107115
}
108116

109117
///<summary>
110-
/// Rename an existing branch with a new name.
118+
/// Rename an existing local branch with a new name.
111119
///</summary>
112120
///<param name = "currentName">The current branch name.</param>
113121
///<param name = "newName">The new name of the existing branch should bear.</param>
114122
///<param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
115123
///<returns></returns>
116124
public Branch Move(string currentName, string newName, bool allowOverwrite = false)
117125
{
118-
Ensure.ArgumentNotNullOrEmptyString(currentName, "name");
126+
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
119127
Ensure.ArgumentNotNullOrEmptyString(newName, "name");
120128

121-
Reference reference = repo.Refs.Move(NormalizeToCanonicalName(currentName), NormalizeToCanonicalName(newName),
122-
allowOverwrite);
129+
Ensure.Success(NativeMethods.git_branch_move(repo.Handle, currentName, newName, allowOverwrite));
123130

124-
return this[reference.CanonicalName];
131+
return this[newName];
125132
}
126133

127134
private static bool LooksLikeABranchName(string referenceName)

LibGit2Sharp/Core/GitBranchType.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace LibGit2Sharp.Core
4+
{
5+
[Flags]
6+
internal enum GitBranchType
7+
{
8+
GIT_BRANCH_LOCAL = 1,
9+
GIT_BRANCH_REMOTE = 2,
10+
}
11+
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ public static extern int git_blob_create_fromfile(
8080
[DllImport(libgit2)]
8181
public static extern int git_blob_rawsize(GitObjectSafeHandle blob);
8282

83+
[DllImport(libgit2)]
84+
public static extern int git_branch_create(
85+
out GitOid oid_out,
86+
RepositorySafeHandle repo,
87+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string branch_name,
88+
GitObjectSafeHandle target,
89+
[MarshalAs(UnmanagedType.Bool)] bool force);
90+
91+
[DllImport(libgit2)]
92+
public static extern int git_branch_move(
93+
RepositorySafeHandle repo,
94+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string old_branch_name,
95+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string new_branch_name,
96+
[MarshalAs(UnmanagedType.Bool)] bool force);
97+
8398
[DllImport(libgit2)]
8499
public static extern IntPtr git_commit_author(GitObjectSafeHandle commit);
85100

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="Core\DisposableEnumerable.cs" />
6161
<Compile Include="Core\EnumExtensions.cs" />
6262
<Compile Include="ChangeKind.cs" />
63+
<Compile Include="Core\GitBranchType.cs" />
6364
<Compile Include="Core\GitDiff.cs" />
6465
<Compile Include="Core\GitError.cs" />
6566
<Compile Include="Core\GitObjectExtensions.cs" />

0 commit comments

Comments
 (0)
0