-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4120574
commit d1448f9
Showing
3 changed files
with
161 additions
and
3 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,144 @@ | ||
/// <summary> | ||
/// Supported ways to interact with Git Repository. | ||
/// </summary> | ||
public enum GitProviderType | ||
{ | ||
/// <summary> | ||
/// Interact with Git though Cake.Git addin. | ||
/// Requires system to be compatible with Cake.Git addin. | ||
/// </summary> | ||
CakeGit, | ||
/// <summary> | ||
/// Interact with Git though Cake.Git using Git CLI. | ||
/// Requires Git CLI to be available in path. | ||
/// </summary> | ||
Cli | ||
} | ||
|
||
/// <summary> | ||
/// Description of a provider to work with a Git repository. | ||
/// </summary> | ||
public interface IGitProvider | ||
{ | ||
/// <summary> | ||
/// Commits changes in a repository. | ||
/// </summary> | ||
/// <param name="context">The Cake context.</param> | ||
/// <param name="repositoryDirectoryPath">Root directory of the repository.</param> | ||
/// <param name="name">Name of the committer.</param> | ||
/// <param name="email">Email address of the committer.</param> | ||
/// <param name="message">Commit message.</param> | ||
void Commit(ICakeContext context, DirectoryPath repositoryDirectoryPath, string name, string email, string message); | ||
} | ||
|
||
/// <summary> | ||
/// Provider to interact with a repository using Git CLI. | ||
/// </summary> | ||
public class CliGitProvider : IGitProvider | ||
{ | ||
/// <inheritdoc /> | ||
public void Commit(ICakeContext context, DirectoryPath repositoryDirectoryPath, string name, string email, string message) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (repositoryDirectoryPath == null) | ||
{ | ||
throw new ArgumentNullException(nameof(repositoryDirectoryPath)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(name)) | ||
{ | ||
throw new ArgumentNullException(nameof(name)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(email)) | ||
{ | ||
throw new ArgumentNullException(nameof(email)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(message)) | ||
{ | ||
throw new ArgumentNullException(nameof(message)); | ||
} | ||
|
||
this.GitCommand( | ||
context, | ||
repositoryDirectoryPath, | ||
"commit", | ||
"--author", | ||
$"{name} <{email}>", | ||
"--message", | ||
$"\"{message}\""); | ||
} | ||
|
||
private static IEnumerable<string> GitCommand( | ||
ICakeContext context, | ||
DirectoryPath repositoryDirectoryPath, | ||
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 = repositoryDirectoryPath.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(System.Environment.NewLine, redirectedErrorOutput)}" | ||
); | ||
} | ||
return redirectedStandardOutput; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Provider to interact with a repository using Cake.Git addin. | ||
/// </summary> | ||
public class CakeGitProvider : IGitProvider | ||
{ | ||
/// <inheritdoc /> | ||
public void Commit(ICakeContext context, DirectoryPath repositoryDirectoryPath, string name, string email, string message) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (repositoryDirectoryPath == null) | ||
{ | ||
throw new ArgumentNullException(nameof(repositoryDirectoryPath)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(name)) | ||
{ | ||
throw new ArgumentNullException(nameof(name)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(email)) | ||
{ | ||
throw new ArgumentNullException(nameof(email)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(message)) | ||
{ | ||
throw new ArgumentNullException(nameof(message)); | ||
} | ||
|
||
context.GitCommit(repositoryDirectoryPath, name, email, message); | ||
} | ||
} |
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