Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6cfba69

Browse files
committed
git: worktree checkout tag hash id (#959)
Allow checking out a worktree using a tag hash id. Fixes: #959 Supersedes: #964
1 parent 952f1ba commit 6cfba69

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ var (
324324

325325
// CheckoutOptions describes how a checkout operation should be performed.
326326
type CheckoutOptions struct {
327-
// Hash is the hash of the commit to be checked out. If used, HEAD will be
328-
// in detached mode. If Create is not used, Branch and Hash are mutually
329-
// exclusive.
327+
// Hash is the hash of a commit or tag to be checked out. If used, HEAD
328+
// will be in detached mode. If Create is not used, Branch and Hash are
329+
// mutually exclusive.
330330
Hash plumbing.Hash
331331
// Branch to be checked out, if Branch and Hash are empty is set to `master`.
332332
Branch plumbing.ReferenceName

worktree.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,17 @@ func (w *Worktree) createBranch(opts *CheckoutOptions) error {
227227
}
228228

229229
func (w *Worktree) getCommitFromCheckoutOptions(opts *CheckoutOptions) (plumbing.Hash, error) {
230-
if !opts.Hash.IsZero() {
231-
return opts.Hash, nil
232-
}
233-
234-
b, err := w.r.Reference(opts.Branch, true)
235-
if err != nil {
236-
return plumbing.ZeroHash, err
237-
}
230+
hash := opts.Hash
231+
if hash.IsZero() {
232+
b, err := w.r.Reference(opts.Branch, true)
233+
if err != nil {
234+
return plumbing.ZeroHash, err
235+
}
238236

239-
if !b.Name().IsTag() {
240-
return b.Hash(), nil
237+
hash = b.Hash()
241238
}
242239

243-
o, err := w.r.Object(plumbing.AnyObject, b.Hash())
240+
o, err := w.r.Object(plumbing.AnyObject, hash)
244241
if err != nil {
245242
return plumbing.ZeroHash, err
246243
}

worktree_commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,4 @@ func (h *buildTreeHelper) copyTreeToStorageRecursive(parent string, t *object.Tr
263263
return hash, nil
264264
}
265265
return h.s.SetEncodedObject(o)
266-
}
266+
}

worktree_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,41 @@ func (s *WorktreeSuite) TestCheckoutTag(c *C) {
886886
c.Assert(head.Name().String(), Equals, "HEAD")
887887
}
888888

889+
func (s *WorktreeSuite) TestCheckoutTagHash(c *C) {
890+
f := fixtures.ByTag("tags").One()
891+
r := s.NewRepositoryWithEmptyWorktree(f)
892+
w, err := r.Worktree()
893+
c.Assert(err, IsNil)
894+
895+
for _, hash := range []string{
896+
"b742a2a9fa0afcfa9a6fad080980fbc26b007c69", // annotated tag
897+
"ad7897c0fb8e7d9a9ba41fa66072cf06095a6cfc", // commit tag
898+
"f7b877701fbf855b44c0a9e86f3fdce2c298b07f", // lightweight tag
899+
} {
900+
err = w.Checkout(&CheckoutOptions{
901+
Hash: plumbing.NewHash(hash),
902+
})
903+
c.Assert(err, IsNil)
904+
head, err := w.r.Head()
905+
c.Assert(err, IsNil)
906+
c.Assert(head.Name().String(), Equals, "HEAD")
907+
908+
status, err := w.Status()
909+
c.Assert(err, IsNil)
910+
c.Assert(status.IsClean(), Equals, true)
911+
}
912+
913+
for _, hash := range []string{
914+
"fe6cb94756faa81e5ed9240f9191b833db5f40ae", // blob tag
915+
"152175bf7e5580299fa1f0ba41ef6474cc043b70", // tree tag
916+
} {
917+
err = w.Checkout(&CheckoutOptions{
918+
Hash: plumbing.NewHash(hash),
919+
})
920+
c.Assert(err, NotNil)
921+
}
922+
}
923+
889924
func (s *WorktreeSuite) TestCheckoutBisect(c *C) {
890925
if testing.Short() {
891926
c.Skip("skipping test in short mode.")

0 commit comments

Comments
 (0)