Skip to content

Commit d21cf2e

Browse files
Ben Straubnulltoken
Ben Straub
authored andcommitted
Revparse support in Repository.Lookup
Now running lookups through git_revparse_single.
1 parent e7b3e10 commit d21cf2e

9 files changed

+150
-68
lines changed

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ public void CanCreateBranchFromCommit()
8888
}
8989
}
9090

91+
[Fact]
92+
public void CanCreateBranchFromRevparseSpec()
93+
{
94+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
95+
using (var repo = new Repository(path.RepositoryPath))
96+
{
97+
const string name = "revparse_branch";
98+
var target = repo.Lookup<Commit>("master~2");
99+
Branch newBranch = repo.CreateBranch(name, target);
100+
Assert.NotNull(newBranch);
101+
Assert.Equal("9fd738e8f7967c078dceed8190330fc8648ee56a", newBranch.Tip.Sha);
102+
}
103+
}
104+
91105
[Fact]
92106
public void CreatingABranchFromATagPeelsToTheCommit()
93107
{
@@ -150,15 +164,6 @@ public void CreatingBranchWithUnknownShaTargetThrows()
150164
}
151165
}
152166

153-
[Fact]
154-
public void CreatingABranchPointingAtANonCanonicalReferenceThrows()
155-
{
156-
using (var repo = new Repository(BareTestRepoPath))
157-
{
158-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Add("nocanonicaltarget", "br2"));
159-
}
160-
}
161-
162167
[Fact]
163168
public void CreatingBranchWithBadParamsThrows()
164169
{

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,35 @@ public void CanLookupWhithShortIdentifers()
296296
}
297297
}
298298

299+
[Fact]
300+
public void CanLookupUsingRevparseSyntax()
301+
{
302+
using (var repo = new Repository(BareTestRepoPath))
303+
{
304+
Assert.Null(repo.Lookup<Tree>("master^"));
305+
306+
Assert.NotNull(repo.Lookup("master:new.txt"));
307+
Assert.NotNull(repo.Lookup<Blob>("master:new.txt"));
308+
Assert.NotNull(repo.Lookup("master^"));
309+
Assert.NotNull(repo.Lookup<Commit>("master^"));
310+
Assert.NotNull(repo.Lookup("master~3"));
311+
Assert.NotNull(repo.Lookup("HEAD"));
312+
Assert.NotNull(repo.Lookup("refs/heads/br2"));
313+
}
314+
}
315+
316+
[Fact]
317+
public void CanResolveAmbiguousRevparseSpecs()
318+
{
319+
using (var repo = new Repository(BareTestRepoPath))
320+
{
321+
var o1 = repo.Lookup("e90810b"); // This resolves to a tag
322+
Assert.Equal("7b4384978d2493e851f9cca7858815fac9b10980", o1.Sha);
323+
var o2 = repo.Lookup("e90810b8"); // This resolves to a commit
324+
Assert.Equal("e90810b8df3e80c413d903f631643c716887138d", o2.Sha);
325+
}
326+
}
327+
299328
[Fact]
300329
public void LookingUpWithBadParamsThrows()
301330
{
@@ -310,7 +339,7 @@ public void LookingUpWithBadParamsThrows()
310339
}
311340
}
312341

313-
[Fact]
342+
[Fact(Skip = "This test requires an update to libgit2 to pass.")]
314343
public void LookingUpWithATooShortShaThrows()
315344
{
316345
using (var repo = new Repository(BareTestRepoPath))

LibGit2Sharp.Tests/TagFixture.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ public void CanAddALightweightTagFromABranchName()
5353
}
5454
}
5555

56+
[Fact]
57+
public void CanAddALightweightTagFromARevparseSpec()
58+
{
59+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
60+
using (var repo = new Repository(path.RepositoryPath))
61+
{
62+
Tag newTag = repo.Tags.Add("i_am_lightweight", "master^1^2");
63+
Assert.False(newTag.IsAnnotated);
64+
Assert.NotNull(newTag);
65+
Assert.Equal("c47800c7266a2be04c571c04d5a6614691ea99bd", newTag.Target.Sha);
66+
}
67+
}
68+
5669
[Fact]
5770
public void CanAddAndOverwriteALightweightTag()
5871
{
@@ -124,6 +137,19 @@ public void CanAddAnAnnotatedTagFromSha()
124137
}
125138
}
126139

140+
[Fact]
141+
public void CanAddAnAnnotatedTagFromARevparseSpec()
142+
{
143+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
144+
using (var repo = new Repository(path.RepositoryPath))
145+
{
146+
Tag newTag = repo.Tags.Add("unit_test", "master^1^2", signatureTim, "a new tag");
147+
Assert.NotNull(newTag);
148+
Assert.True(newTag.IsAnnotated);
149+
Assert.Equal("c47800c7266a2be04c571c04d5a6614691ea99bd", newTag.Target.Sha);
150+
}
151+
}
152+
127153
[Fact]
128154
// Ported from cgit (https://github.com/git/git/blob/1c08bf50cfcf924094eca56c2486a90e2bf1e6e2/t/t7004-tag.sh#L359)
129155
public void CanAddAnAnnotatedTagWithAnEmptyMessage()
@@ -201,15 +227,6 @@ public void CreatingATagForAnUnknowReferenceThrows()
201227
}
202228
}
203229

204-
[Fact]
205-
public void CreatingATagForANonCanonicalReferenceThrows()
206-
{
207-
using (var repo = new Repository(BareTestRepoPath))
208-
{
209-
Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("noncanonicaltarget", "br2"));
210-
}
211-
}
212-
213230
[Fact]
214231
// Ported from cgit (https://github.com/git/git/blob/1c08bf50cfcf924094eca56c2486a90e2bf1e6e2/t/t7004-tag.sh#L42)
215232
public void CreatingATagForAnUnknowObjectIdThrows()

LibGit2Sharp.Tests/TreeFixture.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,15 @@ public void CanRetrieveTreeEntryPath()
174174
TreeEntry anotherInstance = tree["branch_file.txt"];
175175
Assert.Equal("branch_file.txt", anotherInstance.Path);
176176

177+
// From a rev-parse statement
178+
var revparseTree = repo.Lookup<Tree>("master:1");
179+
TreeEntry yetAnotherInstance = revparseTree["branch_file.txt"];
180+
Assert.Equal(completePath, yetAnotherInstance.Path);
181+
177182
Assert.Equal(tree, subTree);
183+
Assert.Equal(revparseTree, tree);
178184
Assert.Equal(anotherInstance, anInstance);
185+
Assert.Equal(yetAnotherInstance, anotherInstance);
179186
Assert.NotEqual(anotherInstance.Path, anInstance.Path);
180187
Assert.NotSame(anotherInstance, anInstance);
181188
}

LibGit2Sharp/BranchCollection.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ IEnumerator IEnumerable.GetEnumerator()
137137
/// Create a new local branch with the specified name
138138
/// </summary>
139139
/// <param name = "name">The name of the branch.</param>
140-
/// <param name = "shaOrReferenceName">The target which can be sha or a canonical reference name.</param>
140+
/// <param name = "commitish">Revparse spec for the target commit.</param>
141141
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
142142
/// <returns></returns>
143-
public virtual Branch Add(string name, string shaOrReferenceName, bool allowOverwrite = false)
143+
public virtual Branch Add(string name, string commitish, bool allowOverwrite = false)
144144
{
145145
Ensure.ArgumentNotNullOrEmptyString(name, "name");
146146

147-
ObjectId commitId = repo.LookupCommit(shaOrReferenceName).Id;
147+
ObjectId commitId = repo.LookupCommit(commitish).Id;
148148

149149
using (var osw = new ObjectSafeWrapper(commitId, repo))
150150
{
@@ -159,13 +159,13 @@ public virtual Branch Add(string name, string shaOrReferenceName, bool allowOver
159159
/// Create a new local branch with the specified name
160160
/// </summary>
161161
/// <param name = "name">The name of the branch.</param>
162-
/// <param name = "shaOrReferenceName">The target which can be sha or a canonical reference name.</param>
162+
/// <param name = "commitish">Revparse spec for the target commit.</param>
163163
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
164164
/// <returns></returns>
165165
[Obsolete("This method will be removed in the next release. Please use Add() instead.")]
166-
public virtual Branch Create(string name, string shaOrReferenceName, bool allowOverwrite = false)
166+
public virtual Branch Create(string name, string commitish, bool allowOverwrite = false)
167167
{
168-
return Add(name, shaOrReferenceName, allowOverwrite);
168+
return Add(name, commitish, allowOverwrite);
169169
}
170170

171171
/// <summary>

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,12 @@ public static extern int git_repository_set_workdir(
639639
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(FilePathMarshaler))]
640640
public static extern FilePath git_repository_workdir(RepositorySafeHandle repository);
641641

642+
[DllImport(libgit2)]
643+
public static extern int git_revparse_single(
644+
out GitObjectSafeHandle obj,
645+
RepositorySafeHandle repo,
646+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string spec);
647+
642648
[DllImport(libgit2)]
643649
public static extern void git_revwalk_free(IntPtr walker);
644650

LibGit2Sharp/IRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public interface IRepository : IDisposable
6969
/// <summary>
7070
/// Checkout the specified branch, reference or SHA.
7171
/// </summary>
72-
/// <param name = "shaOrReferenceName">The sha of the commit, a canonical reference name or the name of the branch to checkout.</param>
72+
/// <param name = "commitOrBranchSpec">A revparse spec for the commit or branch to checkout.</param>
7373
/// <returns>The new HEAD.</returns>
74-
Branch Checkout(string shaOrReferenceName);
74+
Branch Checkout(string commitOrBranchSpec);
7575

7676
/// <summary>
7777
/// Try to lookup an object by its <see cref = "ObjectId" /> and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
@@ -84,10 +84,10 @@ public interface IRepository : IDisposable
8484
/// <summary>
8585
/// Try to lookup an object by its sha or a reference canonical name and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
8686
/// </summary>
87-
/// <param name = "shaOrReferenceName">The sha or reference canonical name to lookup.</param>
87+
/// <param name = "objectish">A revparse spec for the object to lookup.</param>
8888
/// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
8989
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
90-
GitObject Lookup(string shaOrReferenceName, GitObjectType type = GitObjectType.Any);
90+
GitObject Lookup(string objectish, GitObjectType type = GitObjectType.Any);
9191

9292
/// <summary>
9393
/// Stores the content of the <see cref = "Repository.Index" /> as a new <see cref = "Commit" /> into the repository.

0 commit comments

Comments
 (0)