Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit02bed28

Browse files
authored
Merge pull request#966 from aymanbagabas/taghash-checkout
git: worktree checkout tag hash id (#959)
2 parentsc8348a6 +1e2b0d6 commit02bed28

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
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
typeCheckoutOptionsstruct {
327-
// Hash is the hash ofthe 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 ofa commitor tagto be checked out. If used, HEAD
328+
//will bein detached mode. If Create is not used, Branch and Hash are
329+
//mutuallyexclusive.
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: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,36 +227,33 @@ 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-
returnopts.Hash,nil
232-
}
233-
234-
b,err:=w.r.Reference(opts.Branch,true)
235-
iferr!=nil {
236-
returnplumbing.ZeroHash,err
237-
}
230+
hash:=opts.Hash
231+
ifhash.IsZero() {
232+
b,err:=w.r.Reference(opts.Branch,true)
233+
iferr!=nil {
234+
returnplumbing.ZeroHash,err
235+
}
238236

239-
if!b.Name().IsTag() {
240-
returnb.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
iferr!=nil {
245242
returnplumbing.ZeroHash,err
246243
}
247244

248245
switcho:=o.(type) {
249246
case*object.Tag:
250247
ifo.TargetType!=plumbing.CommitObject {
251-
returnplumbing.ZeroHash,fmt.Errorf("unsupported tagobjecttarget %q",o.TargetType)
248+
returnplumbing.ZeroHash,fmt.Errorf("%w: tag target %q",object.ErrUnsupportedObject,o.TargetType)
252249
}
253250

254251
returno.Target,nil
255252
case*object.Commit:
256253
returno.Hash,nil
257254
}
258255

259-
returnplumbing.ZeroHash,fmt.Errorf("unsupported tag target%q",o.Type())
256+
returnplumbing.ZeroHash,fmt.Errorf("%w:%q",object.ErrUnsupportedObject,o.Type())
260257
}
261258

262259
func (w*Worktree)setHEADToCommit(commit plumbing.Hash)error {

‎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
returnhash,nil
264264
}
265265
returnh.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
iftesting.Short() {
891926
c.Skip("skipping test in short mode.")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp