diff options
author | Keith Dahlby <dahlbyk@gmail.com> | 2012-04-21 04:44:27 +0400 |
---|---|---|
committer | Keith Dahlby <dahlbyk@gmail.com> | 2012-04-21 04:44:27 +0400 |
commit | 186ca1782f4346b4ed9f904103a393f1abfa2c1e (patch) | |
tree | 616bfabb7e0e0c3bf78c2c4e6f06446df0fcb4c5 | |
parent | d1993af681c2edd61cdc1440d2cb7d5de83522de (diff) |
Move Checkout() to Repository
Closes #107
-rw-r--r-- | LibGit2Sharp.Tests/BranchFixture.cs | 10 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/CommitFixture.cs | 8 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/ResetFixture.cs | 4 | ||||
-rw-r--r-- | LibGit2Sharp/BranchCollection.cs | 15 | ||||
-rw-r--r-- | LibGit2Sharp/Repository.cs | 22 |
5 files changed, 35 insertions, 24 deletions
diff --git a/LibGit2Sharp.Tests/BranchFixture.cs b/LibGit2Sharp.Tests/BranchFixture.cs index 8105ba65..c100e9d2 100644 --- a/LibGit2Sharp.Tests/BranchFixture.cs +++ b/LibGit2Sharp.Tests/BranchFixture.cs @@ -311,7 +311,7 @@ namespace LibGit2Sharp.Tests Branch master = repo.Branches["master"]; master.IsCurrentRepositoryHead.ShouldBeTrue(); - Branch test = repo.Branches.Checkout(name); + Branch test = repo.Checkout(name); repo.Info.IsHeadDetached.ShouldBeFalse(); test.IsRemote.ShouldBeFalse(); @@ -333,7 +333,7 @@ namespace LibGit2Sharp.Tests Branch master = repo.Branches["master"]; master.IsCurrentRepositoryHead.ShouldBeTrue(); - Branch detachedHead = repo.Branches.Checkout(commitPointer); + Branch detachedHead = repo.Checkout(commitPointer); repo.Info.IsHeadDetached.ShouldBeTrue(); @@ -355,7 +355,7 @@ namespace LibGit2Sharp.Tests { using (var repo = new Repository(BareTestRepoPath)) { - Assert.Throws<LibGit2Exception>(() => repo.Branches.Checkout("i-do-not-exist")); + Assert.Throws<LibGit2Exception>(() => repo.Checkout("i-do-not-exist")); } } @@ -364,8 +364,8 @@ namespace LibGit2Sharp.Tests { using (var repo = new Repository(BareTestRepoPath)) { - Assert.Throws<ArgumentException>(() => repo.Branches.Checkout(string.Empty)); - Assert.Throws<ArgumentNullException>(() => repo.Branches.Checkout(null)); + Assert.Throws<ArgumentException>(() => repo.Checkout(string.Empty)); + Assert.Throws<ArgumentNullException>(() => repo.Checkout(null)); } } diff --git a/LibGit2Sharp.Tests/CommitFixture.cs b/LibGit2Sharp.Tests/CommitFixture.cs index c62781b2..d0eed21c 100644 --- a/LibGit2Sharp.Tests/CommitFixture.cs +++ b/LibGit2Sharp.Tests/CommitFixture.cs @@ -27,11 +27,11 @@ namespace LibGit2Sharp.Tests { using (var repo = new Repository(BareTestRepoPath)) { - repo.Branches.Checkout("test"); + repo.Checkout("test"); repo.Commits.Count().ShouldEqual(2); repo.Commits.First().Id.Sha.ShouldEqual("e90810b8df3e80c413d903f631643c716887138d"); - repo.Branches.Checkout("master"); + repo.Checkout("master"); repo.Commits.Count().ShouldEqual(7); repo.Commits.First().Id.Sha.ShouldEqual("4c062a6361ae6959e06292c1fa5e2822d9c96345"); } @@ -225,7 +225,7 @@ namespace LibGit2Sharp.Tests using (var repoClone = new Repository(path.RepositoryPath)) { string headSha = repoClone.Head.Tip.Sha; - repoClone.Branches.Checkout(headSha); + repoClone.Checkout(headSha); AssertEnumerationOfCommitsInRepo(repoClone, repo => new Filter { Since = repo.Head }, @@ -518,7 +518,7 @@ namespace LibGit2Sharp.Tests commit2.Parents.First().Id.ShouldEqual(commit.Id); Branch firstCommitBranch = repo.CreateBranch("davidfowl-rules", commit.Id.Sha); //TODO: This cries for a shortcut method :-/ - repo.Branches.Checkout(firstCommitBranch.Name); //TODO: This cries for a shortcut method :-/ + repo.Checkout(firstCommitBranch.Name); File.WriteAllText(filePath, "davidfowl commits!\n"); diff --git a/LibGit2Sharp.Tests/ResetFixture.cs b/LibGit2Sharp.Tests/ResetFixture.cs index 75c6b1e7..6c2355d2 100644 --- a/LibGit2Sharp.Tests/ResetFixture.cs +++ b/LibGit2Sharp.Tests/ResetFixture.cs @@ -84,7 +84,7 @@ namespace LibGit2Sharp.Tests Branch branch = repo.Branches["mybranch"]; string branchIdentifier = branchIdentifierRetriever(branch); - repo.Branches.Checkout(branchIdentifier); + repo.Checkout(branchIdentifier); repo.Info.IsHeadDetached.ShouldEqual(shouldHeadBeDetached); string expectedHeadName = expectedHeadNameRetriever(branch); @@ -122,7 +122,7 @@ namespace LibGit2Sharp.Tests repo.Commit("Update file", shiftedSignature, shiftedSignature); repo.CreateBranch("mybranch"); - repo.Branches.Checkout("mybranch"); + repo.Checkout("mybranch"); repo.Index.RetrieveStatus().IsDirty.ShouldBeFalse(); } diff --git a/LibGit2Sharp/BranchCollection.cs b/LibGit2Sharp/BranchCollection.cs index 422d7ee4..22954329 100644 --- a/LibGit2Sharp/BranchCollection.cs +++ b/LibGit2Sharp/BranchCollection.cs @@ -67,21 +67,10 @@ namespace LibGit2Sharp /// </summary> /// <param name = "shaOrReferenceName">The sha of the commit, a canonical reference name or the name of the branch to checkout.</param> /// <returns></returns> + [Obsolete("This method will be removed in the next release. Please use Repository.Checkout() instead.")] public Branch Checkout(string shaOrReferenceName) { - // TODO: This does not yet checkout (write) the working directory - - Branch branch = this[shaOrReferenceName]; - - if (branch != null) - { - repo.Refs.UpdateTarget("HEAD", branch.CanonicalName); - return branch; - } - - ObjectId commitId = repo.LookupCommit(shaOrReferenceName).Id; - repo.Refs.UpdateTarget("HEAD", commitId.Sha); - return repo.Head; + return repo.Checkout(shaOrReferenceName); } /// <summary> diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 9aea29a9..e6f2ab39 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -342,6 +342,28 @@ namespace LibGit2Sharp } /// <summary> + /// Checkout the specified branch, reference or SHA. + /// </summary> + /// <param name = "shaOrReferenceName">The sha of the commit, a canonical reference name or the name of the branch to checkout.</param> + /// <returns>The new HEAD.</returns> + public Branch Checkout(string shaOrReferenceName) + { + // TODO: This does not yet checkout (write) the working directory + + var branch = Branches[shaOrReferenceName]; + + if (branch != null) + { + Refs.UpdateTarget("HEAD", branch.CanonicalName); + return branch; + } + + var commitId = LookupCommit(shaOrReferenceName).Id; + Refs.UpdateTarget("HEAD", commitId.Sha); + return Head; + } + + /// <summary> /// Sets the current <see cref = "Head" /> to the specified commit and optionally resets the <see cref = "Index" /> and /// the content of the working tree to match. /// </summary> |