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 e8fd65a
Showing
5 changed files
with
169 additions
and
7 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
...ssues.Recipe/Cake.Frosting.Issues.Recipe/Context/Parameters/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
107 changes: 107 additions & 0 deletions
107
...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,107 @@ | ||
using Cake.Common; | ||
using Cake.Core; | ||
using Cake.Core.IO; | ||
using Cake.Git; | ||
using Cake.Issues; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Cake.Frosting.Issues.Recipe.RepositoryInfo | ||
{ | ||
public interface IRepositoryInfoProvider | ||
{ | ||
DirectoryPath GetRepositoryRootDirectory(IssuesContext context, DirectoryPath buildRootDirectory); | ||
Uri GetRepositoryRemoteUrl(IssuesContext context, DirectoryPath repositoryRootDirectory); | ||
string GetCommitId(IssuesContext context, DirectoryPath repositoryRootDirectory); | ||
} | ||
|
||
public class CakeGitRepositoryInfoProvider : IRepositoryInfoProvider | ||
{ | ||
/// <inheritdoc /> | ||
public DirectoryPath GetRepositoryRootDirectory(IssuesContext context, DirectoryPath buildRootDirectory) | ||
{ | ||
context.NotNull(nameof(context)); | ||
buildRootDirectory.NotNull(nameof(buildRootDirectory)); | ||
|
||
return context.GitFindRootFromPath(buildRootDirectory); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Uri GetRepositoryRemoteUrl(IssuesContext 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(IssuesContext context, DirectoryPath repositoryRootDirectory) | ||
{ | ||
context.NotNull(nameof(context)); | ||
repositoryRootDirectory.NotNull(nameof(repositoryRootDirectory)); | ||
|
||
return context.GitLogTip(repositoryRootDirectory).Sha; | ||
} | ||
} | ||
|
||
public class CliRepositoryInfoProvider : IRepositoryInfoProvider | ||
{ | ||
/// <inheritdoc /> | ||
public DirectoryPath GetRepositoryRootDirectory(IssuesContext context, DirectoryPath buildRootDirectory) | ||
{ | ||
var result = this.GitCommand(context, buildRootDirectory, "rev-parse", "--show-toplevel"); | ||
return new DirectoryPath(result.Single()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Uri GetRepositoryRemoteUrl(IssuesContext context, DirectoryPath repositoryRootDirectory) | ||
{ | ||
return null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string GetCommitId(IssuesContext context, DirectoryPath repositoryRootDirectory) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
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; | ||
|
||
} | ||
} | ||
} |