2020-12-17 07:00:47 -07:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-17 07:00:47 -07:00
|
|
|
|
2021-08-24 10:47:09 -06:00
|
|
|
//go:build !gogit
|
2020-12-17 07:00:47 -07:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-02-18 17:40:17 -07:00
|
|
|
"errors"
|
2020-12-17 07:00:47 -07:00
|
|
|
"io"
|
|
|
|
"strings"
|
2021-06-25 10:54:08 -06:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-17 07:00:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResolveReference resolves a name to a reference
|
|
|
|
func (repo *Repository) ResolveReference(name string) (string, error) {
|
2022-10-23 08:44:45 -06:00
|
|
|
stdout, _, err := NewCommand(repo.Ctx, "show-ref", "--hash").AddDynamicArguments(name).RunStdString(&RunOpts{Dir: repo.Path})
|
2020-12-17 07:00:47 -07:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "not a valid ref") {
|
|
|
|
return "", ErrNotExist{name, ""}
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
stdout = strings.TrimSpace(stdout)
|
|
|
|
if stdout == "" {
|
|
|
|
return "", ErrNotExist{name, ""}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
|
|
|
|
func (repo *Repository) GetRefCommitID(name string) (string, error) {
|
2024-08-20 11:04:57 -06:00
|
|
|
wr, rd, cancel, err := repo.CatFileBatchCheck(repo.Ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-05-09 19:27:03 -06:00
|
|
|
defer cancel()
|
2024-08-20 11:04:57 -06:00
|
|
|
_, err = wr.Write([]byte(name + "\n"))
|
2021-12-16 12:01:14 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-05-09 19:27:03 -06:00
|
|
|
shaBs, _, _, err := ReadBatchLine(rd)
|
|
|
|
if IsErrNotExist(err) {
|
|
|
|
return "", ErrNotExist{name, ""}
|
2020-12-17 07:00:47 -07:00
|
|
|
}
|
|
|
|
|
2021-05-09 19:27:03 -06:00
|
|
|
return string(shaBs), nil
|
2020-12-17 07:00:47 -07:00
|
|
|
}
|
|
|
|
|
2021-12-23 06:44:00 -07:00
|
|
|
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
|
|
|
|
func (repo *Repository) SetReference(name, commitID string) error {
|
2022-10-23 08:44:45 -06:00
|
|
|
_, _, err := NewCommand(repo.Ctx, "update-ref").AddDynamicArguments(name, commitID).RunStdString(&RunOpts{Dir: repo.Path})
|
2021-12-23 06:44:00 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveReference removes the given reference (e.g. branch or tag).
|
|
|
|
func (repo *Repository) RemoveReference(name string) error {
|
2022-10-23 08:44:45 -06:00
|
|
|
_, _, err := NewCommand(repo.Ctx, "update-ref", "--no-deref", "-d").AddDynamicArguments(name).RunStdString(&RunOpts{Dir: repo.Path})
|
2021-12-23 06:44:00 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-17 07:00:47 -07:00
|
|
|
// IsCommitExist returns true if given commit exists in current repository.
|
|
|
|
func (repo *Repository) IsCommitExist(name string) bool {
|
2024-08-20 11:04:57 -06:00
|
|
|
if err := ensureValidGitRepository(repo.Ctx, repo.Path); err != nil {
|
|
|
|
log.Error("IsCommitExist: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2022-10-23 08:44:45 -06:00
|
|
|
_, _, err := NewCommand(repo.Ctx, "cat-file", "-e").AddDynamicArguments(name).RunStdString(&RunOpts{Dir: repo.Path})
|
2020-12-17 07:00:47 -07:00
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:02:00 -07:00
|
|
|
func (repo *Repository) getCommit(id ObjectID) (*Commit, error) {
|
2024-08-20 11:04:57 -06:00
|
|
|
wr, rd, cancel, err := repo.CatFileBatch(repo.Ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-09 19:27:03 -06:00
|
|
|
defer cancel()
|
2020-12-17 07:00:47 -07:00
|
|
|
|
2021-05-09 19:27:03 -06:00
|
|
|
_, _ = wr.Write([]byte(id.String() + "\n"))
|
2021-05-01 19:16:08 -06:00
|
|
|
|
2021-05-09 19:27:03 -06:00
|
|
|
return repo.getCommitFromBatchReader(rd, id)
|
2021-05-01 19:16:08 -06:00
|
|
|
}
|
|
|
|
|
2023-12-13 14:02:00 -07:00
|
|
|
func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id ObjectID) (*Commit, error) {
|
2021-05-09 19:27:03 -06:00
|
|
|
_, typ, size, err := ReadBatchLine(rd)
|
2020-12-17 07:00:47 -07:00
|
|
|
if err != nil {
|
2021-05-09 19:27:03 -06:00
|
|
|
if errors.Is(err, io.EOF) || IsErrNotExist(err) {
|
2021-02-18 17:40:17 -07:00
|
|
|
return nil, ErrNotExist{ID: id.String()}
|
|
|
|
}
|
2020-12-17 07:00:47 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ {
|
2021-02-18 17:40:17 -07:00
|
|
|
case "missing":
|
|
|
|
return nil, ErrNotExist{ID: id.String()}
|
2020-12-17 07:00:47 -07:00
|
|
|
case "tag":
|
|
|
|
// then we need to parse the tag
|
|
|
|
// and load the commit
|
2021-09-21 23:38:34 -06:00
|
|
|
data, err := io.ReadAll(io.LimitReader(rd, size))
|
2021-05-09 19:27:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = rd.Discard(1)
|
2020-12-17 07:00:47 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-13 14:02:00 -07:00
|
|
|
tag, err := parseTagData(id.Type(), data)
|
2020-12-17 07:00:47 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:37:46 -07:00
|
|
|
commit, err := tag.Commit(repo)
|
2020-12-17 07:00:47 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return commit, nil
|
|
|
|
case "commit":
|
2021-05-09 19:27:03 -06:00
|
|
|
commit, err := CommitFromReader(repo, id, io.LimitReader(rd, size))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = rd.Discard(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return commit, nil
|
2020-12-17 07:00:47 -07:00
|
|
|
default:
|
2021-06-25 10:54:08 -06:00
|
|
|
log.Debug("Unknown typ: %s", typ)
|
2024-02-21 20:48:19 -07:00
|
|
|
if err := DiscardFull(rd, size+1); err != nil {
|
2021-05-09 19:27:03 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-17 07:00:47 -07:00
|
|
|
return nil, ErrNotExist{
|
|
|
|
ID: id.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 19:27:03 -06:00
|
|
|
|
2023-12-13 14:02:00 -07:00
|
|
|
// ConvertToGitID returns a GitHash object from a potential ID string
|
|
|
|
func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) {
|
2024-03-11 22:21:27 -06:00
|
|
|
objectFormat, err := repo.GetObjectFormat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(commitID) == objectFormat.FullLength() && objectFormat.IsValid(commitID) {
|
2023-12-19 00:20:47 -07:00
|
|
|
ID, err := NewIDFromString(commitID)
|
2021-05-09 19:27:03 -06:00
|
|
|
if err == nil {
|
2023-12-13 14:02:00 -07:00
|
|
|
return ID, nil
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 11:04:57 -06:00
|
|
|
wr, rd, cancel, err := repo.CatFileBatchCheck(repo.Ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-09 19:27:03 -06:00
|
|
|
defer cancel()
|
2024-03-11 22:21:27 -06:00
|
|
|
_, err = wr.Write([]byte(commitID + "\n"))
|
2021-05-09 19:27:03 -06:00
|
|
|
if err != nil {
|
2023-12-13 14:02:00 -07:00
|
|
|
return nil, err
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
|
|
|
sha, _, _, err := ReadBatchLine(rd)
|
|
|
|
if err != nil {
|
|
|
|
if IsErrNotExist(err) {
|
2023-12-13 14:02:00 -07:00
|
|
|
return nil, ErrNotExist{commitID, ""}
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
2023-12-13 14:02:00 -07:00
|
|
|
return nil, err
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
|
|
|
|
2023-12-19 00:20:47 -07:00
|
|
|
return MustIDFromString(string(sha)), nil
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|