forked from adamralph/minver
-
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.
- Loading branch information
Showing
5 changed files
with
35 additions
and
121 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,52 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.IO; | ||
using LibGit2Sharp; | ||
|
||
namespace MinVer.Lib; | ||
|
||
internal static class Git | ||
{ | ||
public static bool IsWorkingDirectory(string directory, ILogger log) => GitCommand.TryRun("status --short", directory, log, out _); | ||
|
||
public static bool TryGetHead(string directory, [NotNullWhen(returnValue: true)] out Commit? head, ILogger log) | ||
public static bool TryGetRepository(string directory, [NotNullWhen(returnValue: true)] out Repository? repository) | ||
{ | ||
head = null; | ||
|
||
if (!GitCommand.TryRun("log --pretty=format:\"%H %P\"", directory, log, out var output)) | ||
{ | ||
return false; | ||
} | ||
|
||
var lines = output.Split(new[] { '\r', '\n', }, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
if (lines.Length == 0) | ||
{ | ||
return false; | ||
} | ||
|
||
var commits = new Dictionary<string, Commit>(); | ||
var valid = false; | ||
repository = null; | ||
|
||
foreach (var shas in lines | ||
.Select(line => line.Split(new[] { ' ', }, StringSplitOptions.RemoveEmptyEntries))) | ||
while (!valid && directory is not null) | ||
{ | ||
commits.GetOrAdd(shas[0], () => new Commit(shas[0])) | ||
.Parents.AddRange(shas.Skip(1).Select(parentSha => commits.GetOrAdd(parentSha, () => new Commit(parentSha)))); | ||
var repoPath = Path.Combine(directory, ".git"); | ||
valid = Repository.IsValid(repoPath); | ||
|
||
if (valid) | ||
{ | ||
repository = new Repository(repoPath); | ||
return true; | ||
} | ||
else | ||
{ | ||
directory = Path.GetDirectoryName(directory); | ||
} | ||
} | ||
|
||
head = commits.Values.First(); | ||
|
||
return true; | ||
return false; | ||
} | ||
|
||
public static IEnumerable<(string Name, string Sha)> GetTags(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([' ',], 2)) | ||
.Select(tokens => (tokens[1][10..].RemoveFromEnd("^{}"), tokens[0])) | ||
: Enumerable.Empty<(string, string)>(); | ||
|
||
private static string RemoveFromEnd(this string text, string value) => | ||
text.EndsWith(value, StringComparison.OrdinalIgnoreCase) ? text[..^value.Length] : text; | ||
} |
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