Skip to content

Commit bd32874

Browse files
authored
Merge pull request #554 from dotnet/fix553
Use git "commondir" for reference lookups
2 parents 7566c2b + 9c55c34 commit bd32874

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ public void VersionJsonWithSingleIntegerForVersion()
417417
this.Logger.WriteLine(ex.ToString());
418418
}
419419

420-
[Fact]
421-
public void Worktree_Support()
420+
[Theory, CombinatorialData]
421+
public void Worktree_Support(bool detachedHead)
422422
{
423423
var workingCopyVersion = new VersionOptions
424424
{
@@ -431,10 +431,22 @@ public void Worktree_Support()
431431

432432
string workTreePath = this.CreateDirectoryForNewRepo();
433433
Directory.Delete(workTreePath);
434-
this.LibGit2Repository.Worktrees.Add("HEAD~1", "myworktree", workTreePath, isLocked: false);
434+
if (detachedHead)
435+
{
436+
this.LibGit2Repository.Worktrees.Add("HEAD~1", "myworktree", workTreePath, isLocked: false);
437+
}
438+
else
439+
{
440+
this.LibGit2Repository.Branches.Add("wtbranch", "HEAD~1");
441+
this.LibGit2Repository.Worktrees.Add("wtbranch", "myworktree", workTreePath, isLocked: false);
442+
}
443+
435444
var context = this.CreateGitContext(workTreePath);
436445
var oracleWorkTree = new VersionOracle(context);
437446
Assert.Equal(oracleOriginal.Version, oracleWorkTree.Version);
447+
448+
Assert.True(context.TrySelectCommit("HEAD"));
449+
Assert.True(context.TrySelectCommit(this.LibGit2Repository.Head.Tip.Sha));
438450
}
439451

440452
[Fact]

src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public class GitRepository : IDisposable
3333
/// <summary>
3434
/// Creates a new instance of the <see cref="GitRepository"/> class.
3535
/// </summary>
36-
/// <param name="workingDirectory">
37-
/// The current working directory. This can be a subdirectory of the Git repository.
38-
/// </param>
36+
/// <param name="workingDirectory"><inheritdoc cref="GitRepository(string, string, string, string)" path="/param[@name='workingDirectory']" /></param>
3937
/// <returns>
4038
/// A <see cref="GitRepository"/> which represents the git repository, or <see langword="null"/>
4139
/// if no git repository was found.
@@ -93,18 +91,10 @@ public class GitRepository : IDisposable
9391
/// <summary>
9492
/// Creates a new instance of the <see cref="GitRepository"/> class.
9593
/// </summary>
96-
/// <param name="workingDirectory">
97-
/// The current working directory. This can be a subdirectory of the Git repository.
98-
/// </param>
99-
/// <param name="gitDirectory">
100-
/// The directory in which git metadata (such as refs,...) is stored.
101-
/// </param>
102-
/// <param name="commonDirectory">
103-
/// The common Git directory, in which Git objects are stored.
104-
/// </param>
105-
/// <param name="objectDirectory">
106-
/// The object directory in which Git objects are stored.
107-
/// </param>
94+
/// <param name="workingDirectory"><inheritdoc cref="GitRepository(string, string, string, string)" path="/param[@name='workingDirectory']" /></param>
95+
/// <param name="gitDirectory"><inheritdoc cref="GitRepository(string, string, string, string)" path="/param[@name='gitDirectory']" /> </param>
96+
/// <param name="commonDirectory"><inheritdoc cref="GitRepository(string, string, string, string)" path="/param[@name='commonDirectory']" /></param>
97+
/// <param name="objectDirectory"><inheritdoc cref="GitRepository(string, string, string, string)" path="/param[@name='objectDirectory']" /></param>
10898
public static GitRepository Create(string workingDirectory, string gitDirectory, string commonDirectory, string objectDirectory)
10999
{
110100
return new GitRepository(workingDirectory, gitDirectory, commonDirectory, objectDirectory);
@@ -117,10 +107,10 @@ public static GitRepository Create(string workingDirectory, string gitDirectory,
117107
/// The current working directory. This can be a subdirectory of the Git repository.
118108
/// </param>
119109
/// <param name="gitDirectory">
120-
/// The directory in which git metadata (such as refs,...) is stored.
110+
/// The directory in which the git HEAD file is stored. This is the .git directory unless the working directory is a worktree.
121111
/// </param>
122112
/// <param name="commonDirectory">
123-
/// The common Git directory, in which Git objects are stored.
113+
/// The common Git directory, which is parent to the objects, refs, and other directories.
124114
/// </param>
125115
/// <param name="objectDirectory">
126116
/// The object directory in which Git objects are stored.
@@ -191,7 +181,8 @@ public GitRepository(string workingDirectory, string gitDirectory, string common
191181
public string WorkingDirectory { get; private set; }
192182

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

@@ -333,9 +324,9 @@ public GitCommit GetCommit(GitObjectId sha, bool readAuthor = false)
333324
else
334325
{
335326
// Look for simple names for branch or tag.
336-
possibleLooseFileMatches.Add(Path.Combine(this.GitDirectory, "refs", "heads", objectish));
337-
possibleLooseFileMatches.Add(Path.Combine(this.GitDirectory, "refs", "tags", objectish));
338-
possibleLooseFileMatches.Add(Path.Combine(this.GitDirectory, "refs", "remotes", objectish));
327+
possibleLooseFileMatches.Add(Path.Combine(this.CommonDirectory, "refs", "heads", objectish));
328+
possibleLooseFileMatches.Add(Path.Combine(this.CommonDirectory, "refs", "tags", objectish));
329+
possibleLooseFileMatches.Add(Path.Combine(this.CommonDirectory, "refs", "remotes", objectish));
339330
}
340331

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

346337
// Match in packed-refs file.
347-
string packedRefPath = Path.Combine(this.GitDirectory, "packed-refs");
338+
string packedRefPath = Path.Combine(this.CommonDirectory, "packed-refs");
348339
if (File.Exists(packedRefPath))
349340
{
350341
using var refReader = File.OpenText(packedRefPath);
@@ -653,7 +644,7 @@ private GitObjectId ResolveReference(object reference)
653644
{
654645
if (reference is string)
655646
{
656-
if (!FileHelpers.TryOpen(Path.Combine(this.GitDirectory, (string)reference), out FileStream? stream))
647+
if (!FileHelpers.TryOpen(Path.Combine(this.CommonDirectory, (string)reference), out FileStream? stream))
657648
{
658649
return GitObjectId.Empty;
659650
}

0 commit comments

Comments
 (0)