2020-12-17 22:00:47 +08:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-17 22:00:47 +08:00
|
|
|
|
2021-08-25 00:47:09 +08:00
|
|
|
//go:build gogit
|
2020-12-17 22:00:47 +08:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2023-12-14 05:02:00 +08:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/hash"
|
2020-12-17 22:00:47 +08:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
|
|
|
|
func (repo *Repository) GetRefCommitID(name string) (string, error) {
|
|
|
|
ref, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true)
|
|
|
|
if err != nil {
|
|
|
|
if err == plumbing.ErrReferenceNotFound {
|
|
|
|
return "", ErrNotExist{
|
|
|
|
ID: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref.Hash().String(), nil
|
|
|
|
}
|
|
|
|
|
2021-12-23 21:44:00 +08:00
|
|
|
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
|
|
|
|
func (repo *Repository) SetReference(name, commitID string) error {
|
|
|
|
return repo.gogitRepo.Storer.SetReference(plumbing.NewReferenceFromStrings(name, commitID))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveReference removes the given reference (e.g. branch or tag).
|
|
|
|
func (repo *Repository) RemoveReference(name string) error {
|
|
|
|
return repo.gogitRepo.Storer.RemoveReference(plumbing.ReferenceName(name))
|
|
|
|
}
|
|
|
|
|
2023-12-14 05:02:00 +08:00
|
|
|
// ConvertToHash returns a Hash object from a potential ID string
|
|
|
|
func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) {
|
|
|
|
objectFormat := repo.objectFormat
|
|
|
|
if len(commitID) == hash.HexSize && objectFormat.IsValid(commitID) {
|
2023-12-19 15:20:47 +08:00
|
|
|
ID, err := NewIDFromString(commitID)
|
2021-05-10 09:27:03 +08:00
|
|
|
if err == nil {
|
2023-12-14 05:02:00 +08:00
|
|
|
return ID, nil
|
2021-05-10 09:27:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 22:44:45 +08:00
|
|
|
actualCommitID, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path})
|
2023-12-14 05:02:00 +08:00
|
|
|
actualCommitID = strings.TrimSpace(actualCommitID)
|
2021-05-10 09:27:03 +08:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "unknown revision or path") ||
|
|
|
|
strings.Contains(err.Error(), "fatal: Needed a single revision") {
|
2023-12-17 19:56:08 +08:00
|
|
|
return objectFormat.EmptyObjectID(), ErrNotExist{commitID, ""}
|
2021-05-10 09:27:03 +08:00
|
|
|
}
|
2023-12-17 19:56:08 +08:00
|
|
|
return objectFormat.EmptyObjectID(), err
|
2021-05-10 09:27:03 +08:00
|
|
|
}
|
|
|
|
|
2023-12-19 15:20:47 +08:00
|
|
|
return NewIDFromString(actualCommitID)
|
2021-05-10 09:27:03 +08:00
|
|
|
}
|
|
|
|
|
2020-12-17 22:00:47 +08:00
|
|
|
// IsCommitExist returns true if given commit exists in current repository.
|
|
|
|
func (repo *Repository) IsCommitExist(name string) bool {
|
2023-12-14 05:02:00 +08:00
|
|
|
hash, err := repo.ConvertToGitID(name)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, err = repo.gogitRepo.CommitObject(plumbing.Hash(hash.RawValue()))
|
2020-12-17 22:00:47 +08:00
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2023-12-14 05:02:00 +08:00
|
|
|
func (repo *Repository) getCommit(id ObjectID) (*Commit, error) {
|
2020-12-17 22:00:47 +08:00
|
|
|
var tagObject *object.Tag
|
|
|
|
|
2023-12-14 05:02:00 +08:00
|
|
|
commitID := plumbing.Hash(id.RawValue())
|
|
|
|
gogitCommit, err := repo.gogitRepo.CommitObject(commitID)
|
2020-12-17 22:00:47 +08:00
|
|
|
if err == plumbing.ErrObjectNotFound {
|
2023-12-14 05:02:00 +08:00
|
|
|
tagObject, err = repo.gogitRepo.TagObject(commitID)
|
2020-12-17 22:00:47 +08:00
|
|
|
if err == plumbing.ErrObjectNotFound {
|
|
|
|
return nil, ErrNotExist{
|
|
|
|
ID: id.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
gogitCommit, err = repo.gogitRepo.CommitObject(tagObject.Target)
|
|
|
|
}
|
|
|
|
// if we get a plumbing.ErrObjectNotFound here then the repository is broken and it should be 500
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
commit := convertCommit(gogitCommit)
|
|
|
|
commit.repo = repo
|
|
|
|
|
|
|
|
tree, err := gogitCommit.Tree()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-14 05:02:00 +08:00
|
|
|
commit.Tree.ID = ParseGogitHash(tree.Hash)
|
2020-12-17 22:00:47 +08:00
|
|
|
commit.Tree.gogitTree = tree
|
|
|
|
|
|
|
|
return commit, nil
|
|
|
|
}
|