Skip to content

Commit

Permalink
Added push & pull
Browse files Browse the repository at this point in the history
  • Loading branch information
devlead committed Dec 29, 2015
1 parent 26c901b commit 457510f
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Cake.Git/Cake.Git.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@
<Compile Include="GitAliases.Commit.cs" />
<Compile Include="GitAliases.cs" />
<Compile Include="GitAliases.Init.cs" />
<Compile Include="GitAliases.Pull.cs" />
<Compile Include="GitAliases.Push.cs" />
<Compile Include="GitCommit.cs" />
<Compile Include="GitMergeResult.cs" />
<Compile Include="GitMergeStatus.cs" />
<Compile Include="GitSignature.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Namespaces.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/Cake.Git/GitAliases.Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// ReSharper disable UnusedMember.Global
namespace Cake.Git
{
// ReSharper disable once PublicMembersMustHaveComments
public static partial class GitAliases
{
/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Cake.Git/GitAliases.Clone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Cake.Git
public static partial class GitAliases
{
/// <summary>
/// Clone using default options.
/// Clone unauthenticated using default options.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="sourceUrl">URI for the remote repository.</param>
Expand Down Expand Up @@ -54,7 +54,7 @@ DirectoryPath workDirectoryPath
}

/// <summary>
/// Clone using specified options.
/// Clone authenticated using default options.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="sourceUrl">URI for the remote repository.</param>
Expand Down
147 changes: 147 additions & 0 deletions src/Cake.Git/GitAliases.Pull.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Core.IO;
using Cake.Git.Extensions;
using LibGit2Sharp;

// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
namespace Cake.Git
{
// ReSharper disable once PublicMembersMustHaveComments
public static partial class GitAliases
{
/// <summary>
/// Pull unauthenticated using default options.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="repositoryDirectoryPath">Repository path.</param>
/// <param name="mergerName">The name of the merger.</param>
/// <param name="mergerEmail">The email of the merger.</param>
/// <returns>The path to the created repository.</returns>
/// <exception cref="ArgumentNullException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Pull")]
public static GitMergeResult GitPull(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath,
string mergerName,
string mergerEmail
)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

var workFullDirectoryPath = repositoryDirectoryPath.MakeAbsolute(context.Environment);

if (!context.FileSystem.Exist(workFullDirectoryPath))
{
throw new DirectoryNotFoundException($"Failed to find workDirectoryPath: {workFullDirectoryPath}");
}

return new GitMergeResult(
context.UseRepository(
repositoryDirectoryPath,
repository =>
repository.Network.Pull(
new Signature(
mergerName,
mergerEmail,
DateTimeOffset.Now
),
new PullOptions()
)
)
);
}

/// <summary>
/// Pull authenticating using default options.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="repositoryDirectoryPath">Repository path.</param>
/// <param name="mergerName">The name of the merger.</param>
/// <param name="mergerEmail">The email of the merger.</param>
/// <param name="username">Username used for authentication.</param>
/// <param name="password">Password used for authentication.</param>
/// <param name="remoteName">Name of remote to pull from.</param>
/// <returns>The path to the created repository.</returns>
/// <exception cref="ArgumentNullException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Pull")]
public static GitMergeResult GitPull(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath,
string mergerName,
string mergerEmail,
string username,
string password,
string remoteName
)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

var repositoryFullDirectoryPath = repositoryDirectoryPath.MakeAbsolute(context.Environment);

if (!context.FileSystem.Exist(repositoryFullDirectoryPath))
{
throw new DirectoryNotFoundException($"Failed to find repositoryDirectoryPath: {repositoryFullDirectoryPath}");
}

return new GitMergeResult(
context.UseRepository(
repositoryDirectoryPath,
repository =>
{
var remote = repository.Network.Remotes[remoteName];
if (remote == null)
{
throw new NotFoundException("Remote named {0} not found", remoteName);
}

repository.Network.Fetch(
remote,
new FetchOptions
{
CredentialsProvider =
(url, usernameFromUrl, types) =>
new UsernamePasswordCredentials
{
Username = username,
Password = password
}
}
);

return repository.MergeFetchedRefs(
new Signature(
mergerName,
mergerEmail,
DateTimeOffset.Now
),
new MergeOptions()
);
}
)
);
}
}
}
174 changes: 174 additions & 0 deletions src/Cake.Git/GitAliases.Push.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System;
using System.IO;
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Core.IO;
using Cake.Git.Extensions;
using LibGit2Sharp;

// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
namespace Cake.Git
{
// ReSharper disable once PublicMembersMustHaveComments
public static partial class GitAliases
{
/// <summary>
/// Push all branches unauthenticated.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="repositoryDirectoryPath">Repository path.</param>
/// <returns>The path to the created repository.</returns>
/// <exception cref="ArgumentNullException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Push")]
public static void GitPush(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath
)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

var workFullDirectoryPath = repositoryDirectoryPath.MakeAbsolute(context.Environment);

if (!context.FileSystem.Exist(workFullDirectoryPath))
{
throw new DirectoryNotFoundException($"Failed to find workDirectoryPath: {workFullDirectoryPath}");
}

context.UseRepository(
repositoryDirectoryPath,
repository =>
repository.Network.Push(
repository.Branches
)
);
}

/// <summary>
/// Push all branches authenticated.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="repositoryDirectoryPath">Repository path.</param>
/// <param name="username">Username used for authentication.</param>
/// <param name="password">Password used for authentication.</param>
/// <returns>The path to the created repository.</returns>
/// <exception cref="ArgumentNullException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Push")]
public static void GitPush(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath,
string username,
string password
)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

var repositoryFullDirectoryPath = repositoryDirectoryPath.MakeAbsolute(context.Environment);

if (!context.FileSystem.Exist(repositoryFullDirectoryPath))
{
throw new DirectoryNotFoundException($"Failed to find repositoryDirectoryPath: {repositoryFullDirectoryPath}");
}

context.UseRepository(
repositoryDirectoryPath,
repository =>
repository.Network.Push(
repository.Branches,
new PushOptions
{
CredentialsProvider =
(url, usernameFromUrl, types) =>
new UsernamePasswordCredentials
{
Username = username,
Password = password
}
}
)
);
}

/// <summary>
/// Push specific branch authenticated.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="repositoryDirectoryPath">Repository path.</param>
/// <param name="username">Username used for authentication.</param>
/// <param name="password">Password used for authentication.</param>
/// <param name="branchName">Name of branch to push.</param>
/// <returns>The path to the created repository.</returns>
/// <exception cref="ArgumentNullException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Push")]
public static void GitPush(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath,
string username,
string password,
string branchName
)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (repositoryDirectoryPath == null)
{
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
}

var repositoryFullDirectoryPath = repositoryDirectoryPath.MakeAbsolute(context.Environment);

if (!context.FileSystem.Exist(repositoryFullDirectoryPath))
{
throw new DirectoryNotFoundException($"Failed to find repositoryDirectoryPath: {repositoryFullDirectoryPath}");
}

context.UseRepository(
repositoryDirectoryPath,
repository =>
{
var branch = repository.Branches[branchName];
if (branch == null)
{
throw new NotFoundException("Failed to find branch {0}", branchName);
}

repository.Network.Push(
branch,
new PushOptions
{
CredentialsProvider =
(url, usernameFromUrl, types) =>
new UsernamePasswordCredentials
{
Username = username,
Password = password
}
}
);
}
);
}
}
}
33 changes: 33 additions & 0 deletions src/Cake.Git/GitMergeResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using LibGit2Sharp;
// ReSharper disable MemberCanBePrivate.Global

namespace Cake.Git
{
/// <summary>
/// Git repository merge operation result
/// </summary>
public sealed class GitMergeResult
{
/// <summary>
/// Merge commit.
/// </summary>
public GitCommit Commit { get; }

/// <summary>
/// Merge status
/// </summary>
public GitMergeStatus Status { get; }

internal GitMergeResult(MergeResult mergeResult)
{
if (mergeResult == null)
{
throw new ArgumentNullException(nameof(mergeResult));
}

Commit = new GitCommit(mergeResult.Commit);
Status = (GitMergeStatus) mergeResult.Status;
}
}
}
Loading

0 comments on commit 457510f

Please sign in to comment.