Skip to content

Commit 059afdb

Browse files
committed
feat: added git root detector
1 parent 6372765 commit 059afdb

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

Xenial.RTool.Tests/GitRepositoryDetectorTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,53 @@ public void DetectNotAGitRepository()
5656

5757
isRepo.ShouldBeFalse("Should not be a git repository");
5858
}
59+
60+
[Fact]
61+
public void DetectGitRoot()
62+
{
63+
var root = OperatingSystem.IsWindows()
64+
? @"c:\demo\"
65+
: "/var/demo/";
66+
67+
var s = Path.DirectorySeparatorChar;
68+
69+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
70+
{
71+
{ @$"{root}myfile.txt", new MockFileData("Testing is meh.") },
72+
{ @$"{root}.git{s}index.js", new MockFileData("some js") },
73+
{ @$"{root}sub{s}image.gif", new MockFileData(new byte[] { 0x12, 0x34, 0x56, 0xd2 }) },
74+
{ @$"{root}sub{s}sub{s}image.gif", new MockFileData(new byte[] { 0x12, 0x34, 0x56, 0xd2 }) },
75+
{ @$"{root}sub{s}sub{s}sub{s}image.gif", new MockFileData(new byte[] { 0x12, 0x34, 0x56, 0xd2 }) },
76+
});
77+
78+
var detector = new GitRepositoryDetector(fileSystem);
79+
var cd = @$"{root}sub{s}sub{s}sub{s}";
80+
var gitRoot = detector.DetectGitRootDirectory(cd);
81+
82+
gitRoot.ShouldBe(gitRoot);
83+
}
84+
85+
[Fact]
86+
public void DetectNotAGitRoot()
87+
{
88+
var root = OperatingSystem.IsWindows()
89+
? @"c:\demo\"
90+
: "/var/demo/";
91+
92+
var s = Path.DirectorySeparatorChar;
93+
94+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
95+
{
96+
{ @$"{root}myfile.txt", new MockFileData("Testing is meh.") },
97+
{ @$"{root}sub{s}image.gif", new MockFileData(new byte[] { 0x12, 0x34, 0x56, 0xd2 }) },
98+
{ @$"{root}sub{s}sub{s}image.gif", new MockFileData(new byte[] { 0x12, 0x34, 0x56, 0xd2 }) },
99+
{ @$"{root}sub{s}sub{s}sub{s}image.gif", new MockFileData(new byte[] { 0x12, 0x34, 0x56, 0xd2 }) },
100+
});
101+
102+
var detector = new GitRepositoryDetector(fileSystem);
103+
var cd = @$"{root}sub{s}sub{s}sub{s}";
104+
var gitRoot = detector.DetectGitRootDirectory(cd);
105+
106+
gitRoot.ShouldBeNull("Should not be a git repository");
107+
}
59108
}

Xenial.RTool.Tests/ReleaseApplicationTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO.Abstractions.TestingHelpers;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using System.IO.Abstractions.TestingHelpers;
72

83
using FakeItEasy;
94

Xenial.RTool/GitRepositoryDetector.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Xenial.RTool;
44

5-
public sealed record GitRepositoryDetector(IFileSystem FileSystem) : IGitRepositoryDetector
5+
public sealed record GitRepositoryDetector(IFileSystem FileSystem)
6+
: IGitRepositoryDetector,
7+
IGitRepositoryRootDetector
68
{
79
public bool DetectGitRepository(string? cd = null)
810
{
@@ -29,4 +31,30 @@ static bool Locate(IDirectoryInfo? directoryInfo)
2931

3032
return Locate(directoryInfo);
3133
}
34+
35+
public string? DetectGitRootDirectory(string? cd = null)
36+
{
37+
cd = cd ?? Environment.CurrentDirectory;
38+
var directoryInfo = FileSystem.DirectoryInfo.FromDirectoryName(cd);
39+
40+
static string? Locate(IDirectoryInfo? directoryInfo)
41+
{
42+
if (directoryInfo is null)
43+
{
44+
return null;
45+
}
46+
47+
foreach (var file in directoryInfo.EnumerateDirectories("*.*", SearchOption.TopDirectoryOnly))
48+
{
49+
if (file.Name.Equals(".git", StringComparison.InvariantCultureIgnoreCase))
50+
{
51+
return directoryInfo.FullName;
52+
}
53+
}
54+
55+
return Locate(directoryInfo.Parent);
56+
}
57+
58+
return Locate(directoryInfo);
59+
}
3260
}

Xenial.RTool/IGitRepositoryDetector.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
public interface IGitRepositoryDetector
44
{
55
bool DetectGitRepository(string? cd = null);
6+
}
7+
public interface IGitRepositoryRootDetector
8+
{
9+
string? DetectGitRootDirectory(string? cd = null);
610
}

0 commit comments

Comments
 (0)