github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_commit_gogit.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 // Copyright 2015 The Gogs Authors. All rights reserved. 7 8 //go:build gogit 9 10 package git 11 12 import ( 13 "fmt" 14 "strings" 15 16 "github.com/go-git/go-git/v5/plumbing" 17 "github.com/go-git/go-git/v5/plumbing/object" 18 ) 19 20 // GetRefCommitID returns the last commit ID string of given reference (branch or tag). 21 func (repo *Repository) GetRefCommitID(name string) (string, error) { 22 ref, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true) 23 if err != nil { 24 if err == plumbing.ErrReferenceNotFound { 25 return "", ErrNotExist{ 26 ID: name, 27 } 28 } 29 return "", err 30 } 31 32 return ref.Hash().String(), nil 33 } 34 35 // SetReference sets the commit ID string of given reference (e.g. branch or tag). 36 func (repo *Repository) SetReference(name, commitID string) error { 37 return repo.gogitRepo.Storer.SetReference(plumbing.NewReferenceFromStrings(name, commitID)) 38 } 39 40 // RemoveReference removes the given reference (e.g. branch or tag). 41 func (repo *Repository) RemoveReference(name string) error { 42 return repo.gogitRepo.Storer.RemoveReference(plumbing.ReferenceName(name)) 43 } 44 45 // ConvertToSHA1 returns a Hash object from a potential ID string 46 func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) { 47 if len(commitID) == 40 { 48 sha1, err := NewIDFromString(commitID) 49 if err == nil { 50 return sha1, nil 51 } 52 } 53 54 actualCommitID, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify", commitID).RunStdString(&RunOpts{Dir: repo.Path}) 55 if err != nil { 56 if strings.Contains(err.Error(), "unknown revision or path") || 57 strings.Contains(err.Error(), "fatal: Needed a single revision") { 58 return SHA1{}, ErrNotExist{commitID, ""} 59 } 60 return SHA1{}, err 61 } 62 63 return NewIDFromString(actualCommitID) 64 } 65 66 // IsCommitExist returns true if given commit exists in current repository. 67 func (repo *Repository) IsCommitExist(name string) bool { 68 hash := plumbing.NewHash(name) 69 _, err := repo.gogitRepo.CommitObject(hash) 70 return err == nil 71 } 72 73 func convertPGPSignatureForTag(t *object.Tag) *CommitGPGSignature { 74 if t.PGPSignature == "" { 75 return nil 76 } 77 78 var w strings.Builder 79 var err error 80 81 if _, err = fmt.Fprintf(&w, 82 "object %s\ntype %s\ntag %s\ntagger ", 83 t.Target.String(), t.TargetType.Bytes(), t.Name); err != nil { 84 return nil 85 } 86 87 if err = t.Tagger.Encode(&w); err != nil { 88 return nil 89 } 90 91 if _, err = fmt.Fprintf(&w, "\n\n"); err != nil { 92 return nil 93 } 94 95 if _, err = fmt.Fprintf(&w, t.Message); err != nil { 96 return nil 97 } 98 99 return &CommitGPGSignature{ 100 Signature: t.PGPSignature, 101 Payload: strings.TrimSpace(w.String()) + "\n", 102 } 103 } 104 105 func (repo *Repository) getCommit(id SHA1) (*Commit, error) { 106 var tagObject *object.Tag 107 108 gogitCommit, err := repo.gogitRepo.CommitObject(id) 109 if err == plumbing.ErrObjectNotFound { 110 tagObject, err = repo.gogitRepo.TagObject(id) 111 if err == plumbing.ErrObjectNotFound { 112 return nil, ErrNotExist{ 113 ID: id.String(), 114 } 115 } 116 if err == nil { 117 gogitCommit, err = repo.gogitRepo.CommitObject(tagObject.Target) 118 } 119 // if we get a plumbing.ErrObjectNotFound here then the repository is broken and it should be 500 120 } 121 if err != nil { 122 return nil, err 123 } 124 125 commit := convertCommit(gogitCommit) 126 commit.repo = repo 127 128 if tagObject != nil { 129 commit.CommitMessage = strings.TrimSpace(tagObject.Message) 130 commit.Author = &tagObject.Tagger 131 commit.Signature = convertPGPSignatureForTag(tagObject) 132 } 133 134 tree, err := gogitCommit.Tree() 135 if err != nil { 136 return nil, err 137 } 138 139 commit.Tree.ID = tree.Hash 140 commit.Tree.gogitTree = tree 141 142 return commit, nil 143 }