code.gitea.io/gitea@v1.22.3/modules/git/repo_commit_gogit.go (about) 1 // Copyright 2015 The Gogs Authors. All rights reserved. 2 // Copyright 2019 The Gitea Authors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 5 //go:build gogit 6 7 package git 8 9 import ( 10 "strings" 11 12 "github.com/go-git/go-git/v5/plumbing" 13 "github.com/go-git/go-git/v5/plumbing/hash" 14 "github.com/go-git/go-git/v5/plumbing/object" 15 ) 16 17 // GetRefCommitID returns the last commit ID string of given reference (branch or tag). 18 func (repo *Repository) GetRefCommitID(name string) (string, error) { 19 ref, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true) 20 if err != nil { 21 if err == plumbing.ErrReferenceNotFound { 22 return "", ErrNotExist{ 23 ID: name, 24 } 25 } 26 return "", err 27 } 28 29 return ref.Hash().String(), nil 30 } 31 32 // SetReference sets the commit ID string of given reference (e.g. branch or tag). 33 func (repo *Repository) SetReference(name, commitID string) error { 34 return repo.gogitRepo.Storer.SetReference(plumbing.NewReferenceFromStrings(name, commitID)) 35 } 36 37 // RemoveReference removes the given reference (e.g. branch or tag). 38 func (repo *Repository) RemoveReference(name string) error { 39 return repo.gogitRepo.Storer.RemoveReference(plumbing.ReferenceName(name)) 40 } 41 42 // ConvertToHash returns a Hash object from a potential ID string 43 func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) { 44 objectFormat, err := repo.GetObjectFormat() 45 if err != nil { 46 return nil, err 47 } 48 if len(commitID) == hash.HexSize && objectFormat.IsValid(commitID) { 49 ID, err := NewIDFromString(commitID) 50 if err == nil { 51 return ID, nil 52 } 53 } 54 55 actualCommitID, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path}) 56 actualCommitID = strings.TrimSpace(actualCommitID) 57 if err != nil { 58 if strings.Contains(err.Error(), "unknown revision or path") || 59 strings.Contains(err.Error(), "fatal: Needed a single revision") { 60 return objectFormat.EmptyObjectID(), ErrNotExist{commitID, ""} 61 } 62 return objectFormat.EmptyObjectID(), err 63 } 64 65 return NewIDFromString(actualCommitID) 66 } 67 68 // IsCommitExist returns true if given commit exists in current repository. 69 func (repo *Repository) IsCommitExist(name string) bool { 70 hash, err := repo.ConvertToGitID(name) 71 if err != nil { 72 return false 73 } 74 _, err = repo.gogitRepo.CommitObject(plumbing.Hash(hash.RawValue())) 75 return err == nil 76 } 77 78 func (repo *Repository) getCommit(id ObjectID) (*Commit, error) { 79 var tagObject *object.Tag 80 81 commitID := plumbing.Hash(id.RawValue()) 82 gogitCommit, err := repo.gogitRepo.CommitObject(commitID) 83 if err == plumbing.ErrObjectNotFound { 84 tagObject, err = repo.gogitRepo.TagObject(commitID) 85 if err == plumbing.ErrObjectNotFound { 86 return nil, ErrNotExist{ 87 ID: id.String(), 88 } 89 } 90 if err == nil { 91 gogitCommit, err = repo.gogitRepo.CommitObject(tagObject.Target) 92 } 93 // if we get a plumbing.ErrObjectNotFound here then the repository is broken and it should be 500 94 } 95 if err != nil { 96 return nil, err 97 } 98 99 commit := convertCommit(gogitCommit) 100 commit.repo = repo 101 102 tree, err := gogitCommit.Tree() 103 if err != nil { 104 return nil, err 105 } 106 107 commit.Tree.ID = ParseGogitHash(tree.Hash) 108 commit.Tree.gogitTree = tree 109 110 return commit, nil 111 }