-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from ap0llo/add-git-remote-aliases
Add GitRemotes() and GitRemote() aliases
- Loading branch information
Showing
2 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Cake.Core; | ||
using Cake.Core.Annotations; | ||
using Cake.Core.IO; | ||
using Cake.Git.Extensions; | ||
using LibGit2Sharp; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
// ReSharper disable UnusedMember.Global | ||
namespace Cake.Git | ||
{ | ||
public static partial class GitAliases | ||
{ | ||
/// <summary> | ||
/// Gets a Git repository's remotes. | ||
/// </summary> | ||
/// <example> | ||
/// <code> | ||
/// var remotes = GitRemotes("c:/myrepo"); | ||
/// Information("Found remotes: {0}", string.Join(", ", remotes.Select(x => x.Name + " -> " + x.Url))); | ||
/// </code> | ||
/// </example> | ||
/// <param name="context">The context.</param> | ||
/// <param name="repositoryDirectoryPath">Path to repository.</param> | ||
/// <returns>A list of <see cref="GitRemote"/> objects for the specified repository.</returns> | ||
/// <exception cref="ArgumentNullException">If any of the parameters are null.</exception> | ||
/// <exception cref="RepositoryNotFoundException">If path doesn't exist.</exception> | ||
[CakeMethodAlias] | ||
[CakeAliasCategory("Remotes")] | ||
public static IReadOnlyList<GitRemote> GitRemotes(this ICakeContext context, DirectoryPath repositoryDirectoryPath) | ||
{ | ||
ArgumentNullException.ThrowIfNull(context); | ||
|
||
ArgumentNullException.ThrowIfNull(repositoryDirectoryPath); | ||
|
||
if (!context.FileSystem.Exist(repositoryDirectoryPath)) | ||
{ | ||
throw new RepositoryNotFoundException($"Path '{repositoryDirectoryPath}' doesn't exists."); | ||
} | ||
|
||
return context.UseRepository( | ||
repositoryDirectoryPath, | ||
repository => repository.Network.Remotes.Select(remote => new GitRemote(remote.Name, remote.PushUrl, remote.Url)).ToList() | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the specified remote from a Git repository. | ||
/// </summary> | ||
/// <example> | ||
/// <code> | ||
/// GitRemote remote = GitRemote("c:/temp/cake", "origin"); | ||
/// Information(remote.Url); | ||
/// </code> | ||
/// </example> | ||
/// <param name="context">The context.</param> | ||
/// <param name="repositoryDirectoryPath">Path to repository.</param> | ||
/// <param name="remoteName">The name of the remote to get.</param> | ||
/// <returns>Information about the requested remote or null if no matching remote was found.</returns> | ||
/// <exception cref="ArgumentNullException">If any of the parameters are null.</exception> | ||
/// <exception cref="RepositoryNotFoundException">If path doesn't exist.</exception> | ||
[CakeMethodAlias] | ||
[CakeAliasCategory("Remotes")] | ||
public static GitRemote GitRemote(this ICakeContext context, DirectoryPath repositoryDirectoryPath, string remoteName) | ||
{ | ||
ArgumentNullException.ThrowIfNull(context); | ||
|
||
ArgumentNullException.ThrowIfNull(repositoryDirectoryPath); | ||
|
||
if (!context.FileSystem.Exist(repositoryDirectoryPath)) | ||
{ | ||
throw new RepositoryNotFoundException($"Path '{repositoryDirectoryPath}' doesn't exists."); | ||
} | ||
|
||
return context.UseRepository( | ||
repositoryDirectoryPath, | ||
repository => repository.Network.Remotes[remoteName] is { } remote ? new GitRemote(remote.Name, remote.PushUrl, remote.Url) : null | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters