forked from cake-contrib/Cake.Issues.Recipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(cake-contribGH-183) Add option to use Git CLI
- Loading branch information
1 parent
92f208b
commit 29ea7d6
Showing
17 changed files
with
406 additions
and
13 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
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
20 changes: 20 additions & 0 deletions
20
....Frosting.Issues.Recipe/Cake.Frosting.Issues.Recipe/Context/RepositoryInfoProviderType.cs
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,20 @@ | ||
namespace Cake.Frosting.Issues.Recipe | ||
{ | ||
/// <summary> | ||
/// Supported ways to read repository information. | ||
/// </summary> | ||
public enum RepositoryInfoProviderType | ||
{ | ||
/// <summary> | ||
/// Read repository information using Cake.Git addin. | ||
/// Requires system to be compatible with Cake.Git addin. | ||
/// </summary> | ||
CakeGit, | ||
|
||
/// <summary> | ||
/// Read repository information using Git CLI. | ||
/// Requires Git CLI to be available in path. | ||
/// </summary> | ||
Cli | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
...Issues.Recipe/Cake.Frosting.Issues.Recipe/RepositoryInfo/CakeGitRepositoryInfoProvider.cs
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,43 @@ | ||
using Cake.Core; | ||
using Cake.Core.IO; | ||
using Cake.Git; | ||
using Cake.Issues; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Cake.Frosting.Issues.Recipe | ||
{ | ||
/// <summary> | ||
/// Provider to retrieve repository information using <see href="https://cakebuild.net/extensions/cake-git/">Cake.Git addin</see>. | ||
/// </summary> | ||
public class CakeGitRepositoryInfoProvider : IRepositoryInfoProvider | ||
{ | ||
/// <inheritdoc /> | ||
public DirectoryPath GetRepositoryRootDirectory(ICakeContext context, DirectoryPath buildRootDirectory) | ||
{ | ||
context.NotNull(nameof(context)); | ||
buildRootDirectory.NotNull(nameof(buildRootDirectory)); | ||
|
||
return context.GitFindRootFromPath(buildRootDirectory); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Uri GetRepositoryRemoteUrl(ICakeContext context, DirectoryPath repositoryRootDirectory) | ||
{ | ||
context.NotNull(nameof(context)); | ||
repositoryRootDirectory.NotNull(nameof(repositoryRootDirectory)); | ||
|
||
var currentBranch = context.GitBranchCurrent(repositoryRootDirectory); | ||
return new Uri(currentBranch.Remotes.Single(x => x.Name == "origin").Url); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string GetCommitId(ICakeContext context, DirectoryPath repositoryRootDirectory) | ||
{ | ||
context.NotNull(nameof(context)); | ||
repositoryRootDirectory.NotNull(nameof(repositoryRootDirectory)); | ||
|
||
return context.GitLogTip(repositoryRootDirectory).Sha; | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...ing.Issues.Recipe/Cake.Frosting.Issues.Recipe/RepositoryInfo/CliRepositoryInfoProvider.cs
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,75 @@ | ||
using Cake.Common; | ||
using Cake.Core; | ||
using Cake.Core.IO; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Cake.Frosting.Issues.Recipe | ||
{ | ||
/// <summary> | ||
/// Provider to retrieve repository information using Git CLI. | ||
/// </summary> | ||
public class CliRepositoryInfoProvider : IRepositoryInfoProvider | ||
{ | ||
/// <inheritdoc /> | ||
public DirectoryPath GetRepositoryRootDirectory(ICakeContext context, DirectoryPath buildRootDirectory) | ||
{ | ||
var result = | ||
this.GitCommand(context, buildRootDirectory, "rev-parse", "--show-toplevel"); | ||
return new DirectoryPath(result.Single()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Uri GetRepositoryRemoteUrl(ICakeContext context, DirectoryPath repositoryRootDirectory) | ||
{ | ||
var result = | ||
this.GitCommand(context, repositoryRootDirectory, "config", "--get", "remote.origin.url"); | ||
return new Uri(result.Single()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string GetCommitId(ICakeContext context, DirectoryPath repositoryRootDirectory) | ||
{ | ||
return | ||
this.GitCommand(context, repositoryRootDirectory, "rev-parse", "HEAD") | ||
.Single(); | ||
} | ||
|
||
private IEnumerable<string> GitCommand( | ||
ICakeContext context, | ||
DirectoryPath repositoryRootFolder, | ||
params string[] arguments) | ||
{ | ||
if (!arguments.Any()) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(arguments)); | ||
} | ||
|
||
var gitArguments = string.Join(" ", arguments); | ||
|
||
var exitCode = context.StartProcess( | ||
"git", | ||
new ProcessSettings | ||
{ | ||
Arguments = gitArguments, | ||
WorkingDirectory = repositoryRootFolder.FullPath, | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true | ||
}, | ||
out var redirectedStandardOutput, | ||
out var redirectedErrorOutput | ||
); | ||
|
||
if (exitCode != 0) | ||
{ | ||
throw new Exception( | ||
$"Git command failed with arguments {gitArguments}. Exit code: {exitCode}. Error output: {string.Join(Environment.NewLine, redirectedErrorOutput)}" | ||
); | ||
} | ||
|
||
return redirectedStandardOutput; | ||
|
||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...sting.Issues.Recipe/Cake.Frosting.Issues.Recipe/RepositoryInfo/IRepositoryInfoProvider.cs
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,36 @@ | ||
using Cake.Core; | ||
using Cake.Core.IO; | ||
using System; | ||
|
||
namespace Cake.Frosting.Issues.Recipe | ||
{ | ||
/// <summary> | ||
/// Description of a provider to retrieve repository information. | ||
/// </summary> | ||
public interface IRepositoryInfoProvider | ||
{ | ||
/// <summary> | ||
/// Returns the root directory of the current repository. | ||
/// </summary> | ||
/// <param name="context">The Cake context.</param> | ||
/// <param name="buildRootDirectory">Root directory of the build script.</param> | ||
/// <returns>The root directory of the current repository.</returns> | ||
DirectoryPath GetRepositoryRootDirectory(ICakeContext context, DirectoryPath buildRootDirectory); | ||
|
||
/// <summary> | ||
/// Returns the URL of the remote repository. | ||
/// </summary> | ||
/// <param name="context">The Cake context.</param> | ||
/// <param name="repositoryRootDirectory">Root directory of the repository.</param> | ||
/// <returns>The URL of the remote repository.</returns> | ||
Uri GetRepositoryRemoteUrl(ICakeContext context, DirectoryPath repositoryRootDirectory); | ||
|
||
/// <summary> | ||
/// Returns the SHA hash of the current commit. | ||
/// </summary> | ||
/// <param name="context">The Cake context.</param> | ||
/// <param name="repositoryRootDirectory">Root directory of the repository.</param> | ||
/// <returns>The SHA hash of the current commit</returns> | ||
string GetCommitId(ICakeContext context, DirectoryPath repositoryRootDirectory); | ||
} | ||
} |
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
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
Oops, something went wrong.