Skip to content

Commit

Permalink
Support getting git information from a sparse-checkout repo
Browse files Browse the repository at this point in the history
Add tests for getting git info
  • Loading branch information
Nate McMaster committed Mar 7, 2018
1 parent e9aafe2 commit 3933770
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@

*.sh eol=lf
*.bin binary
*.zip binary
35 changes: 32 additions & 3 deletions modules/BuildTools.Tasks/Utilities/GitRepoInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,41 @@ private static string ResolveHashFromBranch(string gitDir, string branch)
}

var branchFile = Path.Combine(gitDir, "refs", "heads", branch);
if (!File.Exists(branchFile))
if (File.Exists(branchFile))
{
throw new FileNotFoundException("Unable to determine current git commit hash");
return File.ReadAllText(branchFile).Trim();
}

return File.ReadAllText(branchFile).Trim();
var packedRefs = Path.Combine(gitDir, "packed-refs");
if (File.Exists(packedRefs))
{
var lines = File.ReadAllLines(packedRefs);
foreach (var line in lines)
{
if (line.Length == 0)
{
continue;
}

if (line[0] == '#' || line[0] == '^')
{
continue;
}

var split = line.Split(new[] { ' ' }, 2);
if (split.Length < 2)
{
continue;
}

if (string.Equals("refs/heads/" + branch, split[1], StringComparison.Ordinal))
{
return split[0];
}
}
}

throw new FileNotFoundException("Unable to determine current git commit hash");
}

private static DirectoryInfo GetRepositoryRoot(string start)
Expand Down
119 changes: 119 additions & 0 deletions test/BuildTools.Tasks.Tests/GetGitCommitInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.IO.Compression;
using BuildTools.Tasks.Tests;
using Xunit;

namespace Microsoft.AspNetCore.BuildTools.Tests
{
public class GetGitCommitInfoTests : IDisposable
{
private readonly string _tempDir;

public GetGitCommitInfoTests()
{
_tempDir = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName());
Directory.CreateDirectory(_tempDir);
}

public void Dispose()
{
Directory.Delete(_tempDir, recursive: true);
}

private string CreateTestRepro(string name)
{
ZipFile.ExtractToDirectory(Path.Combine(AppContext.BaseDirectory, "Resources", name), _tempDir, overwriteFiles: true);
return Path.Combine(_tempDir, Path.GetFileNameWithoutExtension(name));
}

[Fact]
public void ItFindsCommitHashFromSparseCheckout()
{
var engine = new MockEngine();
var task = new GetGitCommitInfo
{
BuildEngine = engine,
WorkingDirectory = CreateTestRepro("SparseCheckout.zip"),
};
Assert.True(task.Execute(), "Task should pass");

Assert.Equal("7896eb0373dac70940819ef6a6494fdeb4880391", task.CommitHash);
Assert.Equal("dev", task.Branch);
}

[Fact]
public void ItFindsCommitHash()
{
var engine = new MockEngine();
var task = new GetGitCommitInfo
{
BuildEngine = engine,
WorkingDirectory = CreateTestRepro("SimpleGitRepo.zip"),
};
Assert.True(task.Execute(), "Task should pass");

Assert.Equal("9c03629e11679f5f3d9dbbd27286239588e0d296", task.CommitHash);
Assert.Equal("master", task.Branch);
}

[Fact]
public void ItFindsCommitHashInWorktree()
{
var root = CreateTestRepro("WorktreeRepo.zip");

var engine = new MockEngine();
var task = new GetGitCommitInfo
{
BuildEngine = engine,
WorkingDirectory = Path.Combine(root, "SourceRoot"),
};

Assert.True(task.Execute(), "Task should pass");
Assert.Equal("27a9c92f96a117ff926c12beb9d4ea8d0f127e42", task.CommitHash);
Assert.Equal("master", task.Branch);

engine = new MockEngine();
task = new GetGitCommitInfo
{
BuildEngine = engine,
WorkingDirectory = Path.Combine(root, "Worktree1"),
};

Assert.True(task.Execute(), "Task should pass");
Assert.Equal("51bd19b3825fbc3e96fbdabc9f2cfa9972999bfa", task.CommitHash);
Assert.Equal("worktree1", task.Branch);
}

[Fact]
public void ItFindsCommitHashInSubmodule()
{
var root = CreateTestRepro("SubmoduleRepo.zip");

var engine = new MockEngine();
var task = new GetGitCommitInfo
{
BuildEngine = engine,
WorkingDirectory = root,
};

Assert.True(task.Execute(), "Task should pass");
Assert.Equal("91314ecce9e7d3cbd1d3f4d1f35664f52a301479", task.CommitHash);
Assert.Equal("master", task.Branch);

engine = new MockEngine();
task = new GetGitCommitInfo
{
BuildEngine = engine,
WorkingDirectory = Path.Combine(root, "modules", "submodule1"),
};

Assert.True(task.Execute(), "Task should pass");
Assert.Equal("599e691c41f502ed9e062b1822ce13b673fc916e", task.CommitHash);
Assert.Equal("dev", task.Branch);
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 3933770

Please sign in to comment.