Skip to content

Commit 186ca17

Browse files
committed
Move Checkout() to Repository
Closes #107
1 parent d1993af commit 186ca17

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public void CanCheckoutAnExistingBranch(string name)
311311
Branch master = repo.Branches["master"];
312312
master.IsCurrentRepositoryHead.ShouldBeTrue();
313313

314-
Branch test = repo.Branches.Checkout(name);
314+
Branch test = repo.Checkout(name);
315315
repo.Info.IsHeadDetached.ShouldBeFalse();
316316

317317
test.IsRemote.ShouldBeFalse();
@@ -333,7 +333,7 @@ public void CanCheckoutAnArbitraryCommit(string commitPointer)
333333
Branch master = repo.Branches["master"];
334334
master.IsCurrentRepositoryHead.ShouldBeTrue();
335335

336-
Branch detachedHead = repo.Branches.Checkout(commitPointer);
336+
Branch detachedHead = repo.Checkout(commitPointer);
337337

338338
repo.Info.IsHeadDetached.ShouldBeTrue();
339339

@@ -355,7 +355,7 @@ public void CheckingOutANonExistingBranchThrows()
355355
{
356356
using (var repo = new Repository(BareTestRepoPath))
357357
{
358-
Assert.Throws<LibGit2Exception>(() => repo.Branches.Checkout("i-do-not-exist"));
358+
Assert.Throws<LibGit2Exception>(() => repo.Checkout("i-do-not-exist"));
359359
}
360360
}
361361

@@ -364,8 +364,8 @@ public void CheckingOutABranchWithBadParamsThrows()
364364
{
365365
using (var repo = new Repository(BareTestRepoPath))
366366
{
367-
Assert.Throws<ArgumentException>(() => repo.Branches.Checkout(string.Empty));
368-
Assert.Throws<ArgumentNullException>(() => repo.Branches.Checkout(null));
367+
Assert.Throws<ArgumentException>(() => repo.Checkout(string.Empty));
368+
Assert.Throws<ArgumentNullException>(() => repo.Checkout(null));
369369
}
370370
}
371371

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public void CanCorrectlyCountCommitsWhenSwitchingToAnotherBranch()
2727
{
2828
using (var repo = new Repository(BareTestRepoPath))
2929
{
30-
repo.Branches.Checkout("test");
30+
repo.Checkout("test");
3131
repo.Commits.Count().ShouldEqual(2);
3232
repo.Commits.First().Id.Sha.ShouldEqual("e90810b8df3e80c413d903f631643c716887138d");
3333

34-
repo.Branches.Checkout("master");
34+
repo.Checkout("master");
3535
repo.Commits.Count().ShouldEqual(7);
3636
repo.Commits.First().Id.Sha.ShouldEqual("4c062a6361ae6959e06292c1fa5e2822d9c96345");
3737
}
@@ -225,7 +225,7 @@ public void CanEnumerateFromDetachedHead()
225225
using (var repoClone = new Repository(path.RepositoryPath))
226226
{
227227
string headSha = repoClone.Head.Tip.Sha;
228-
repoClone.Branches.Checkout(headSha);
228+
repoClone.Checkout(headSha);
229229

230230
AssertEnumerationOfCommitsInRepo(repoClone,
231231
repo => new Filter { Since = repo.Head },
@@ -518,7 +518,7 @@ public void CanCommitALittleBit()
518518
commit2.Parents.First().Id.ShouldEqual(commit.Id);
519519

520520
Branch firstCommitBranch = repo.CreateBranch("davidfowl-rules", commit.Id.Sha); //TODO: This cries for a shortcut method :-/
521-
repo.Branches.Checkout(firstCommitBranch.Name); //TODO: This cries for a shortcut method :-/
521+
repo.Checkout(firstCommitBranch.Name);
522522

523523
File.WriteAllText(filePath, "davidfowl commits!\n");
524524

LibGit2Sharp.Tests/ResetFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private void AssertSoftReset(Func<Branch, string> branchIdentifierRetriever, boo
8484
Branch branch = repo.Branches["mybranch"];
8585

8686
string branchIdentifier = branchIdentifierRetriever(branch);
87-
repo.Branches.Checkout(branchIdentifier);
87+
repo.Checkout(branchIdentifier);
8888
repo.Info.IsHeadDetached.ShouldEqual(shouldHeadBeDetached);
8989

9090
string expectedHeadName = expectedHeadNameRetriever(branch);
@@ -122,7 +122,7 @@ private static void FeedTheRepository(Repository repo)
122122
repo.Commit("Update file", shiftedSignature, shiftedSignature);
123123
repo.CreateBranch("mybranch");
124124

125-
repo.Branches.Checkout("mybranch");
125+
repo.Checkout("mybranch");
126126

127127
repo.Index.RetrieveStatus().IsDirty.ShouldBeFalse();
128128
}

LibGit2Sharp/BranchCollection.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,10 @@ IEnumerator IEnumerable.GetEnumerator()
6767
/// </summary>
6868
/// <param name = "shaOrReferenceName">The sha of the commit, a canonical reference name or the name of the branch to checkout.</param>
6969
/// <returns></returns>
70+
[Obsolete("This method will be removed in the next release. Please use Repository.Checkout() instead.")]
7071
public Branch Checkout(string shaOrReferenceName)
7172
{
72-
// TODO: This does not yet checkout (write) the working directory
73-
74-
Branch branch = this[shaOrReferenceName];
75-
76-
if (branch != null)
77-
{
78-
repo.Refs.UpdateTarget("HEAD", branch.CanonicalName);
79-
return branch;
80-
}
81-
82-
ObjectId commitId = repo.LookupCommit(shaOrReferenceName).Id;
83-
repo.Refs.UpdateTarget("HEAD", commitId.Sha);
84-
return repo.Head;
73+
return repo.Checkout(shaOrReferenceName);
8574
}
8675

8776
/// <summary>

LibGit2Sharp/Repository.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,28 @@ public static string Discover(string startingPath)
341341
return discoveredPath.Native;
342342
}
343343

344+
/// <summary>
345+
/// Checkout the specified branch, reference or SHA.
346+
/// </summary>
347+
/// <param name = "shaOrReferenceName">The sha of the commit, a canonical reference name or the name of the branch to checkout.</param>
348+
/// <returns>The new HEAD.</returns>
349+
public Branch Checkout(string shaOrReferenceName)
350+
{
351+
// TODO: This does not yet checkout (write) the working directory
352+
353+
var branch = Branches[shaOrReferenceName];
354+
355+
if (branch != null)
356+
{
357+
Refs.UpdateTarget("HEAD", branch.CanonicalName);
358+
return branch;
359+
}
360+
361+
var commitId = LookupCommit(shaOrReferenceName).Id;
362+
Refs.UpdateTarget("HEAD", commitId.Sha);
363+
return Head;
364+
}
365+
344366
/// <summary>
345367
/// Sets the current <see cref = "Head" /> to the specified commit and optionally resets the <see cref = "Index" /> and
346368
/// the content of the working tree to match.

0 commit comments

Comments
 (0)