Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitRemotes() and GitRemote() aliases #192

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading