Skip to content

Commit

Permalink
Merge pull request #554 from dotnet/fix553
Browse files Browse the repository at this point in the history
Use git "commondir" for reference lookups
  • Loading branch information
AArnott committed Dec 22, 2020
2 parents 7566c2b + 9c55c34 commit bd32874
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
18 changes: 15 additions & 3 deletions src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ public void VersionJsonWithSingleIntegerForVersion()
this.Logger.WriteLine(ex.ToString());
}

[Fact]
public void Worktree_Support()
[Theory, CombinatorialData]
public void Worktree_Support(bool detachedHead)
{
var workingCopyVersion = new VersionOptions
{
Expand All @@ -431,10 +431,22 @@ public void Worktree_Support()

string workTreePath = this.CreateDirectoryForNewRepo();
Directory.Delete(workTreePath);
this.LibGit2Repository.Worktrees.Add("HEAD~1", "myworktree", workTreePath, isLocked: false);
if (detachedHead)
{
this.LibGit2Repository.Worktrees.Add("HEAD~1", "myworktree", workTreePath, isLocked: false);
}
else
{
this.LibGit2Repository.Branches.Add("wtbranch", "HEAD~1");
this.LibGit2Repository.Worktrees.Add("wtbranch", "myworktree", workTreePath, isLocked: false);
}

var context = this.CreateGitContext(workTreePath);
var oracleWorkTree = new VersionOracle(context);
Assert.Equal(oracleOriginal.Version, oracleWorkTree.Version);

Assert.True(context.TrySelectCommit("HEAD"));
Assert.True(context.TrySelectCommit(this.LibGit2Repository.Head.Tip.Sha));
}

[Fact]
Expand Down
37 changes: 14 additions & 23 deletions src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public class GitRepository : IDisposable
/// <summary>
/// Creates a new instance of the <see cref="GitRepository"/> class.
/// </summary>
/// <param name="workingDirectory">
/// The current working directory. This can be a subdirectory of the Git repository.
/// </param>
/// <param name="workingDirectory"><inheritdoc cref="GitRepository(string, string, string, string)" path="/param[@name='workingDirectory']" /></param>
/// <returns>
/// A <see cref="GitRepository"/> which represents the git repository, or <see langword="null"/>
/// if no git repository was found.
Expand Down Expand Up @@ -93,18 +91,10 @@ public class GitRepository : IDisposable
/// <summary>
/// Creates a new instance of the <see cref="GitRepository"/> class.
/// </summary>
/// <param name="workingDirectory">
/// The current working directory. This can be a subdirectory of the Git repository.
/// </param>
/// <param name="gitDirectory">
/// The directory in which git metadata (such as refs,...) is stored.
/// </param>
/// <param name="commonDirectory">
/// The common Git directory, in which Git objects are stored.
/// </param>
/// <param name="objectDirectory">
/// The object directory in which Git objects are stored.
/// </param>
/// <param name="workingDirectory"><inheritdoc cref="GitRepository(string, string, string, string)" path="/param[@name='workingDirectory']" /></param>
/// <param name="gitDirectory"><inheritdoc cref="GitRepository(string, string, string, string)" path="/param[@name='gitDirectory']" /> </param>
/// <param name="commonDirectory"><inheritdoc cref="GitRepository(string, string, string, string)" path="/param[@name='commonDirectory']" /></param>
/// <param name="objectDirectory"><inheritdoc cref="GitRepository(string, string, string, string)" path="/param[@name='objectDirectory']" /></param>
public static GitRepository Create(string workingDirectory, string gitDirectory, string commonDirectory, string objectDirectory)
{
return new GitRepository(workingDirectory, gitDirectory, commonDirectory, objectDirectory);
Expand All @@ -117,10 +107,10 @@ public static GitRepository Create(string workingDirectory, string gitDirectory,
/// The current working directory. This can be a subdirectory of the Git repository.
/// </param>
/// <param name="gitDirectory">
/// The directory in which git metadata (such as refs,...) is stored.
/// The directory in which the git HEAD file is stored. This is the .git directory unless the working directory is a worktree.
/// </param>
/// <param name="commonDirectory">
/// The common Git directory, in which Git objects are stored.
/// The common Git directory, which is parent to the objects, refs, and other directories.
/// </param>
/// <param name="objectDirectory">
/// The object directory in which Git objects are stored.
Expand Down Expand Up @@ -191,7 +181,8 @@ public GitRepository(string workingDirectory, string gitDirectory, string common
public string WorkingDirectory { get; private set; }

/// <summary>
/// Gets the path to the Git directory, in which metadata (e.g. references and configuration) is stored.
/// Gets the path to the Git directory, in which at minimum HEAD is stored.
/// Use <see cref="CommonDirectory"/> for all other metadata (e.g. references, configuration).
/// </summary>
public string GitDirectory { get; private set; }

Expand Down Expand Up @@ -333,9 +324,9 @@ public GitCommit GetCommit(GitObjectId sha, bool readAuthor = false)
else
{
// Look for simple names for branch or tag.
possibleLooseFileMatches.Add(Path.Combine(this.GitDirectory, "refs", "heads", objectish));
possibleLooseFileMatches.Add(Path.Combine(this.GitDirectory, "refs", "tags", objectish));
possibleLooseFileMatches.Add(Path.Combine(this.GitDirectory, "refs", "remotes", objectish));
possibleLooseFileMatches.Add(Path.Combine(this.CommonDirectory, "refs", "heads", objectish));
possibleLooseFileMatches.Add(Path.Combine(this.CommonDirectory, "refs", "tags", objectish));
possibleLooseFileMatches.Add(Path.Combine(this.CommonDirectory, "refs", "remotes", objectish));
}

if (possibleLooseFileMatches.FirstOrDefault(File.Exists) is string existingPath)
Expand All @@ -344,7 +335,7 @@ public GitCommit GetCommit(GitObjectId sha, bool readAuthor = false)
}

// Match in packed-refs file.
string packedRefPath = Path.Combine(this.GitDirectory, "packed-refs");
string packedRefPath = Path.Combine(this.CommonDirectory, "packed-refs");
if (File.Exists(packedRefPath))
{
using var refReader = File.OpenText(packedRefPath);
Expand Down Expand Up @@ -653,7 +644,7 @@ private GitObjectId ResolveReference(object reference)
{
if (reference is string)
{
if (!FileHelpers.TryOpen(Path.Combine(this.GitDirectory, (string)reference), out FileStream? stream))
if (!FileHelpers.TryOpen(Path.Combine(this.CommonDirectory, (string)reference), out FileStream? stream))
{
return GitObjectId.Empty;
}
Expand Down

0 comments on commit bd32874

Please sign in to comment.