Skip to content

Commit

Permalink
Merge pull request #192 from ap0llo/add-git-remote-aliases
Browse files Browse the repository at this point in the history
Add GitRemotes() and GitRemote() aliases
  • Loading branch information
pascalberger authored Dec 14, 2024
2 parents 16ad111 + 302c594 commit 746f1a6
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 2 deletions.
82 changes: 82 additions & 0 deletions src/Cake.Git/GitAliases.Remote.cs
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
);
}
}
}
70 changes: 68 additions & 2 deletions test.cake
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,68 @@ Task("Git-Fetch-Remote-Tags")
}
});

Task("Git-Remotes")
.IsDependentOn("Git-Modify-Commit")
.Does(() =>
{
var originDir = testRepo.Combine(Guid.NewGuid().ToString("d"));
var testDir = testRepo.Combine(Guid.NewGuid().ToString("d"));

try
{
// Arrange: create a repo and a clone
GitClone((IsRunningOnWindows() ? "" : "file://")+testInitialRepo.FullPath, originDir);
GitClone((IsRunningOnWindows() ? "" : "file://")+originDir.FullPath, testDir);

// Act
var remotes = GitRemotes(testDir);

// Assert
Information("Found remotes: {0}", string.Join(", ", remotes.Select(x => x.Name + " -> " + x.Url)));
}
finally
{
// cleanup
var settings = new DeleteDirectorySettings {
Recursive = true,
Force = true
};
DeleteDirectory(originDir, settings);
DeleteDirectory(testDir, settings);
}
});

Task("Git-Remote")
.IsDependentOn("Git-Modify-Commit")
.Does(() =>
{
var originDir = testRepo.Combine(Guid.NewGuid().ToString("d"));
var testDir = testRepo.Combine(Guid.NewGuid().ToString("d"));

try
{
// Arrange: create a repo and a clone
GitClone((IsRunningOnWindows() ? "" : "file://")+testInitialRepo.FullPath, originDir);
GitClone((IsRunningOnWindows() ? "" : "file://")+originDir.FullPath, testDir);

// Act
var remote = GitRemote(testDir, "origin");

// Assert
Information("Found remote: {0}", remote.Url);
}
finally
{
// cleanup
var settings = new DeleteDirectorySettings {
Recursive = true,
Force = true
};
DeleteDirectory(originDir, settings);
DeleteDirectory(testDir, settings);
}
});

Task("Git-Tag")
.IsDependentOn("Git-Tag-Apply")
.IsDependentOn("Git-Tag-Apply-Objectish");
Expand Down Expand Up @@ -1120,7 +1182,9 @@ Task("Default-Tests")
.IsDependentOn("Git-Clean")
.IsDependentOn("Git-Config")
.IsDependentOn("Git-ShortenSha")
.IsDependentOn("Git-Fetch-Remote");
.IsDependentOn("Git-Fetch-Remote")
.IsDependentOn("Git-Remotes")
.IsDependentOn("Git-Remote");

Task("Local-Tests")
.IsDependentOn("Git-Init")
Expand Down Expand Up @@ -1160,7 +1224,9 @@ Task("Local-Tests")
.IsDependentOn("Git-Clean")
.IsDependentOn("Git-Config")
.IsDependentOn("Git-ShortenSha")
.IsDependentOn("Git-Fetch-Remote");
.IsDependentOn("Git-Fetch-Remote")
.IsDependentOn("Git-Remotes")
.IsDependentOn("Git-Remote");

///////////////////////////////////////////////////////////////////////////////
// EXECUTION
Expand Down

0 comments on commit 746f1a6

Please sign in to comment.