Skip to content

Commit

Permalink
Delegate the creation and the moving of branches to libgit2 native me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
nulltoken committed May 11, 2012
1 parent 0a7de3b commit 6238840
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
21 changes: 14 additions & 7 deletions LibGit2Sharp/BranchCollection.cs
Expand Up @@ -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];
}

Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions 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,
}
}
21 changes: 21 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Expand Up @@ -80,6 +80,27 @@ public static bool RepositoryStateChecker(RepositorySafeHandle repositoryPtr, Fu
[DllImport(libgit2)]
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);

Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Expand Up @@ -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" />
Expand Down

0 comments on commit 6238840

Please sign in to comment.