-
-
Notifications
You must be signed in to change notification settings - Fork 59
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
Showing
16 changed files
with
240 additions
and
108 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 |
---|---|---|
|
@@ -13,4 +13,6 @@ matrix: | |
include: | ||
- os: linux | ||
dist: bionic | ||
- os: linux | ||
dist: xenial | ||
- os: osx |
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,15 @@ | ||
namespace MinVer.Lib | ||
{ | ||
using System.Collections.Generic; | ||
|
||
internal class Commit | ||
{ | ||
public Commit(string sha) => this.Sha = sha; | ||
|
||
public string Sha { get; } | ||
|
||
public string ShortSha => this.Sha.Substring(0, 7); | ||
|
||
public List<Commit> Parents { get; } = new List<Commit>(); | ||
} | ||
} |
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,18 @@ | ||
namespace MinVer.Lib | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
internal static class DictionaryExtensions | ||
{ | ||
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TValue> valueFactory) | ||
{ | ||
if (!dictionary.TryGetValue(key, out var value)) | ||
{ | ||
dictionary.Add(key, value = valueFactory()); | ||
} | ||
|
||
return value; | ||
} | ||
} | ||
} |
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,42 @@ | ||
namespace MinVer.Lib | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
internal static class Git | ||
{ | ||
public static bool IsWorkingDirectory(string directory, ILogger log) => GitCommand.TryRun("status --short", directory, log, out _); | ||
|
||
public static Commit GetHeadOrDefault(string directory, ILogger log) | ||
{ | ||
if (!GitCommand.TryRun("log --pretty=format:\"%H %P\"", directory, log, out var output)) | ||
{ | ||
return default; | ||
} | ||
|
||
var commits = new Dictionary<string, Commit>(); | ||
|
||
foreach (var shas in output | ||
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) | ||
.Select(line => line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))) | ||
{ | ||
commits.GetOrAdd(shas[0], () => new Commit(shas[0])) | ||
.Parents.AddRange(shas.Skip(1).Select(parentSha => commits.GetOrAdd(parentSha, () => new Commit(parentSha)))); | ||
} | ||
|
||
return commits.Values.FirstOrDefault(); | ||
} | ||
|
||
public static IEnumerable<Tag> GetTagsOrEmpty(string directory, ILogger log) => | ||
GitCommand.TryRun("show-ref --tags --dereference", directory, log, out var output) | ||
? output | ||
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) | ||
.Select(line => line.Split(new[] { ' ' }, 2)) | ||
.Select(tokens => new Tag(tokens[1].Substring(10).RemoveFromEnd("^{}"), tokens[0])) | ||
: Enumerable.Empty<Tag>(); | ||
|
||
private static string RemoveFromEnd(this string text, string value) => | ||
text.EndsWith(value) ? text.Substring(0, text.Length - value.Length) : text; | ||
} | ||
} |
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,57 @@ | ||
namespace MinVer.Lib | ||
{ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
|
||
internal static class GitCommand | ||
{ | ||
public static bool TryRun(string args, string workingDirectory, ILogger log, out string output) | ||
{ | ||
using (var process = new Process()) | ||
{ | ||
process.StartInfo = new ProcessStartInfo | ||
{ | ||
FileName = "git", | ||
Arguments = args, | ||
WorkingDirectory = workingDirectory, | ||
UseShellExecute = false, | ||
RedirectStandardError = true, | ||
RedirectStandardOutput = true, | ||
}; | ||
|
||
var tcs = new TaskCompletionSource<object>(); | ||
process.Exited += (s, e) => tcs.SetResult(null); | ||
process.EnableRaisingEvents = true; | ||
|
||
log.Trace($"Running Git: {process.StartInfo.FileName} {process.StartInfo.Arguments}"); | ||
|
||
try | ||
{ | ||
process.Start(); | ||
} | ||
catch (Win32Exception ex) | ||
{ | ||
throw new Exception("Failed to run Git. Git may not be installed on the system.", ex); | ||
} | ||
|
||
var runProcess = tcs.Task; | ||
var readOutput = process.StandardOutput.ReadToEndAsync(); | ||
var readError = process.StandardError.ReadToEndAsync(); | ||
|
||
Task.WaitAll(runProcess, readOutput, readError); | ||
|
||
var exitCode = process.ExitCode; | ||
output = readOutput.Result; | ||
var error = readError.Result; | ||
|
||
log.Trace($"Git exit code: {exitCode}"); | ||
log.Trace($"Git stdout:{Environment.NewLine}{output}"); | ||
log.Trace($"Git stderr:{Environment.NewLine}{error}"); | ||
|
||
return exitCode == 0; | ||
} | ||
} | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,15 @@ | ||
namespace MinVer.Lib | ||
{ | ||
internal class Tag | ||
{ | ||
public Tag(string name, string sha) | ||
{ | ||
this.Name = name; | ||
this.Sha = sha; | ||
} | ||
|
||
public string Name { get; } | ||
|
||
public string Sha { get; } | ||
} | ||
} |
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.