code.gitea.io/gitea@v1.19.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/object"
    14  )
    15  
    16  // GetRefCommitID returns the last commit ID string of given reference (branch or tag).
    17  func (repo *Repository) GetRefCommitID(name string) (string, error) {
    18  	ref, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true)
    19  	if err != nil {
    20  		if err == plumbing.ErrReferenceNotFound {
    21  			return "", ErrNotExist{
    22  				ID: name,
    23  			}
    24  		}
    25  		return "", err
    26  	}
    27  
    28  	return ref.Hash().String(), nil
    29  }
    30  
    31  // SetReference sets the commit ID string of given reference (e.g. branch or tag).
    32  func (repo *Repository) SetReference(name, commitID string) error {
    33  	return repo.gogitRepo.Storer.SetReference(plumbing.NewReferenceFromStrings(name, commitID))
    34  }
    35  
    36  // RemoveReference removes the given reference (e.g. branch or tag).
    37  func (repo *Repository) RemoveReference(name string) error {
    38  	return repo.gogitRepo.Storer.RemoveReference(plumbing.ReferenceName(name))
    39  }
    40  
    41  // ConvertToSHA1 returns a Hash object from a potential ID string
    42  func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
    43  	if len(commitID) == SHAFullLength {
    44  		sha1, err := NewIDFromString(commitID)
    45  		if err == nil {
    46  			return sha1, nil
    47  		}
    48  	}
    49  
    50  	actualCommitID, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path})
    51  	if err != nil {
    52  		if strings.Contains(err.Error(), "unknown revision or path") ||
    53  			strings.Contains(err.Error(), "fatal: Needed a single revision") {
    54  			return SHA1{}, ErrNotExist{commitID, ""}
    55  		}
    56  		return SHA1{}, err
    57  	}
    58  
    59  	return NewIDFromString(actualCommitID)
    60  }
    61  
    62  // IsCommitExist returns true if given commit exists in current repository.
    63  func (repo *Repository) IsCommitExist(name string) bool {
    64  	hash := plumbing.NewHash(name)
    65  	_, err := repo.gogitRepo.CommitObject(hash)
    66  	return err == nil
    67  }
    68  
    69  func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
    70  	var tagObject *object.Tag
    71  
    72  	gogitCommit, err := repo.gogitRepo.CommitObject(id)
    73  	if err == plumbing.ErrObjectNotFound {
    74  		tagObject, err = repo.gogitRepo.TagObject(id)
    75  		if err == plumbing.ErrObjectNotFound {
    76  			return nil, ErrNotExist{
    77  				ID: id.String(),
    78  			}
    79  		}
    80  		if err == nil {
    81  			gogitCommit, err = repo.gogitRepo.CommitObject(tagObject.Target)
    82  		}
    83  		// if we get a plumbing.ErrObjectNotFound here then the repository is broken and it should be 500
    84  	}
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	commit := convertCommit(gogitCommit)
    90  	commit.repo = repo
    91  
    92  	tree, err := gogitCommit.Tree()
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	commit.Tree.ID = tree.Hash
    98  	commit.Tree.gogitTree = tree
    99  
   100  	return commit, nil
   101  }