-
-
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
11 changed files
with
196 additions
and
57 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,41 @@ | ||
namespace MinVer.Lib | ||
{ | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
|
||
internal static class Command | ||
{ | ||
public static string Read(string name, string args, string workingDirectory) | ||
{ | ||
using (var process = new Process()) | ||
{ | ||
process.StartInfo = new System.Diagnostics.ProcessStartInfo | ||
{ | ||
FileName = name, | ||
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; | ||
process.Start(); | ||
var runProcess = tcs.Task; | ||
|
||
var readOutput = process.StandardOutput.ReadToEndAsync(); | ||
|
||
Task.WaitAll(runProcess, readOutput); | ||
|
||
if (process.ExitCode != 0) | ||
{ | ||
throw new NonZeroExitCodeException(); | ||
} | ||
|
||
return readOutput.Result; | ||
} | ||
} | ||
} | ||
} |
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,13 @@ | ||
namespace MinVer.Lib | ||
{ | ||
using System.Collections.Generic; | ||
|
||
public class Commit | ||
{ | ||
public Commit(string sha) => this.Sha = sha; | ||
|
||
public string Sha { get; } | ||
|
||
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
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; | ||
|
||
#pragma warning disable CA1032 // Implement standard exception constructors | ||
#pragma warning disable CA1064 // Exceptions should be public | ||
internal class NonZeroExitCodeException : Exception | ||
#pragma warning restore CA1064 // Exceptions should be public | ||
#pragma warning restore CA1032 // Implement standard exception constructors | ||
{ | ||
public NonZeroExitCodeException() : base() | ||
{ | ||
} | ||
} | ||
} |
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,84 @@ | ||
namespace MinVer.Lib | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using static Command; | ||
|
||
public class Repository | ||
{ | ||
private Repository(Commit head, IEnumerable<Tag> tags) | ||
{ | ||
this.Head = head; | ||
this.Tags = tags; | ||
} | ||
|
||
public Commit Head { get; } | ||
|
||
public IEnumerable<Tag> Tags { get; } | ||
|
||
public static bool TryCreateRepo(string repoOrWorkDir, out Repository repository) | ||
{ | ||
repository = default; | ||
|
||
var commitCount = 0; | ||
|
||
while (true) | ||
{ | ||
try | ||
{ | ||
commitCount = int.Parse(Read("git", "rev-list --all --count", repoOrWorkDir).Trim()); | ||
break; | ||
} | ||
catch (NonZeroExitCodeException) | ||
{ | ||
repoOrWorkDir = Directory.GetParent(repoOrWorkDir)?.FullName; | ||
if (repoOrWorkDir == default) | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
var commitLines = commitCount == 0 ? "" : Read("git", "log --pretty=format:\"%H %P\"", repoOrWorkDir); | ||
|
||
var commits = new Dictionary<string, Commit>(); | ||
|
||
foreach (var shas in commitLines | ||
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) | ||
.Select(line => line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))) | ||
{ | ||
var commit = commits.GetOrAdd(shas[0], () => new Commit(shas[0])); | ||
var parents = shas.Skip(1).Select(parentSha => commits.GetOrAdd(parentSha, () => new Commit(parentSha))).ToList(); | ||
commit.Parents.AddRange(parents); | ||
} | ||
|
||
var tags = GetTags(repoOrWorkDir); | ||
|
||
repository = new Repository(commits.Values.FirstOrDefault(), tags); | ||
|
||
return true; | ||
} | ||
|
||
private static List<Tag> GetTags(string repoOrWorkDir) | ||
{ | ||
if (Read("git", "log --oneline --tags", repoOrWorkDir).Trim() == "") | ||
{ | ||
return new List<Tag>(); | ||
} | ||
|
||
var tagLines = Read("git", "show-ref --tags", repoOrWorkDir); | ||
|
||
var tags = new List<Tag>(); | ||
foreach (var tokens in tagLines | ||
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) | ||
.Select(line => line.Split(new[] { ' ' }, 2))) | ||
{ | ||
tags.Add(new Tag(tokens[1].Substring(10), tokens[0])); | ||
} | ||
|
||
return tags; | ||
} | ||
} | ||
} |
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
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 | ||
{ | ||
public 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
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