Skip to content

Commit 3eb9255

Browse files
authored
Merge pull request #107 from asherber/submodules
Handle .git files, for submodules
2 parents 50b1458 + 8b7d45f commit 3eb9255

File tree

6 files changed

+114
-6
lines changed

6 files changed

+114
-6
lines changed

src/NerdBank.GitVersioning.Tests/NerdBank.GitVersioning.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Compile Include="VersionExtensionsTests.cs" />
5757
<Compile Include="VersionFileTests.cs" />
5858
<Compile Include="VersionOptionsTests.cs" />
59+
<Compile Include="VersionOracleTests.cs" />
5960
</ItemGroup>
6061
<ItemGroup>
6162
<EmbeddedResource Include="..\Nerdbank.GitVersioning.NuGet\build\*.targets">
@@ -82,6 +83,7 @@
8283
<EmbeddedResource Include="Keys\protectedPair.pfx" />
8384
<None Include="project.json" />
8485
<EmbeddedResource Include="test.proj" />
86+
<EmbeddedResource Include="repos\submodules.7z" />
8587
</ItemGroup>
8688
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
8789
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

src/NerdBank.GitVersioning.Tests/TestUtilities.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Validation;
1+
using SevenZipNET;
2+
using Validation;
3+
24
namespace Nerdbank.GitVersioning.Tests
35
{
46
using System;
@@ -54,5 +56,45 @@ internal static void ExtractEmbeddedResource(string resourcePath, string extract
5456
}
5557
}
5658
}
59+
60+
internal static ExpandedRepo ExtractRepoArchive(string repoArchiveName)
61+
{
62+
string archiveFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
63+
string expandedFolderPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
64+
65+
ExtractEmbeddedResource($"repos.{repoArchiveName}.7z", archiveFilePath);
66+
try
67+
{
68+
var extractor = new SevenZipExtractor(archiveFilePath);
69+
extractor.ExtractAll(expandedFolderPath);
70+
return new ExpandedRepo(expandedFolderPath);
71+
}
72+
finally
73+
{
74+
if (File.Exists(archiveFilePath))
75+
{
76+
File.Delete(archiveFilePath);
77+
}
78+
}
79+
}
80+
81+
internal class ExpandedRepo : IDisposable
82+
{
83+
internal ExpandedRepo(string repoPath)
84+
{
85+
Requires.NotNullOrEmpty(repoPath, nameof(repoPath));
86+
this.RepoPath = repoPath;
87+
}
88+
89+
public string RepoPath { get; private set; }
90+
91+
public void Dispose()
92+
{
93+
if (Directory.Exists(this.RepoPath))
94+
{
95+
DeleteDirectory(this.RepoPath);
96+
}
97+
}
98+
}
5799
}
58100
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.IO;
2+
using LibGit2Sharp;
3+
using Nerdbank.GitVersioning;
4+
using Nerdbank.GitVersioning.Tests;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
public class VersionOracleTests : RepoTestBase
9+
{
10+
public VersionOracleTests(ITestOutputHelper logger)
11+
: base(logger)
12+
{
13+
}
14+
15+
[Fact]
16+
public void Submodule_RecognizedWithCorrectVersion()
17+
{
18+
using (var expandedRepo = TestUtilities.ExtractRepoArchive("submodules"))
19+
{
20+
this.Repo = new Repository(expandedRepo.RepoPath);
21+
22+
var oracleA = VersionOracle.Create(Path.Combine(expandedRepo.RepoPath, "a"));
23+
Assert.Equal("1.3.1", oracleA.SimpleVersion.ToString());
24+
Assert.Equal("e238b03e75", oracleA.GitCommitIdShort);
25+
26+
var oracleB = VersionOracle.Create(Path.Combine(expandedRepo.RepoPath, "b", "projB"));
27+
Assert.Equal("2.5.2", oracleB.SimpleVersion.ToString());
28+
Assert.Equal("3ea7f010c3", oracleB.GitCommitIdShort);
29+
}
30+
}
31+
}

src/NerdBank.GitVersioning.Tests/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"dependencies": {
3+
"7z.NET": "1.0.3",
34
"Microsoft.CodeAnalysis.CSharp": "1.1.1",
45
"System.Collections.Immutable": "1.1.37",
56
"xunit": "2.1.0",
9.1 KB
Binary file not shown.

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,48 @@ private static string FormatBuildMetadataSemVerV1(IEnumerable<string> identifier
264264
private static LibGit2Sharp.Repository OpenGitRepo(string repoRoot)
265265
{
266266
Requires.NotNullOrEmpty(repoRoot, nameof(repoRoot));
267-
while (!Directory.Exists(Path.Combine(repoRoot, ".git")))
267+
var gitDir = FindGitDir(repoRoot);
268+
return gitDir == null ? null : new LibGit2Sharp.Repository(gitDir);
269+
}
270+
271+
private static string FindGitDir(string startingDir)
272+
{
273+
while (startingDir != null)
268274
{
269-
repoRoot = Path.GetDirectoryName(repoRoot);
270-
if (repoRoot == null)
275+
var dirOrFilePath = Path.Combine(startingDir, ".git");
276+
if (Directory.Exists(dirOrFilePath))
271277
{
272-
return null;
278+
return dirOrFilePath;
273279
}
280+
else if (File.Exists(dirOrFilePath))
281+
{
282+
var relativeGitDirPath = ReadGitDirFromFile(dirOrFilePath);
283+
if (!string.IsNullOrWhiteSpace(relativeGitDirPath))
284+
{
285+
var fullGitDirPath = Path.GetFullPath(Path.Combine(startingDir, relativeGitDirPath));
286+
if (Directory.Exists(fullGitDirPath))
287+
{
288+
return fullGitDirPath;
289+
}
290+
}
291+
}
292+
293+
startingDir = Path.GetDirectoryName(startingDir);
294+
}
295+
296+
return null;
297+
}
298+
299+
private static string ReadGitDirFromFile(string fileName)
300+
{
301+
const string expectedPrefix = "gitdir: ";
302+
var firstLineOfFile = File.ReadLines(fileName).FirstOrDefault();
303+
if (firstLineOfFile?.StartsWith(expectedPrefix) ?? false)
304+
{
305+
return firstLineOfFile.Substring(expectedPrefix.Length); // strip off the prefix, leaving just the path
274306
}
275307

276-
return new LibGit2Sharp.Repository(repoRoot);
308+
return null;
277309
}
278310

279311
private static Version GetAssemblyVersion(Version version, VersionOptions versionOptions)

0 commit comments

Comments
 (0)