2020-12-17 22:00:47 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-08-25 00:47:09 +08:00
|
|
|
//go:build !gogit
|
2020-12-17 22:00:47 +08:00
|
|
|
// +build !gogit
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-02-19 08:40:17 +08:00
|
|
|
"errors"
|
2020-12-17 22:00:47 +08:00
|
|
|
"io"
|
|
|
|
"strings"
|
2021-06-26 00:54:08 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-17 22:00:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResolveReference resolves a name to a reference
|
|
|
|
func (repo *Repository) ResolveReference(name string) (string, error) {
|
2021-12-01 04:06:32 +08:00
|
|
|
stdout, err := NewCommandContext(repo.Ctx, "show-ref", "--hash", name).RunInDir(repo.Path)
|
2020-12-17 22:00:47 +08: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) {
|
2021-12-01 04:06:32 +08:00
|
|
|
wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx)
|
2021-05-10 09:27:03 +08:00
|
|
|
defer cancel()
|
2021-12-17 03:01:14 +08:00
|
|
|
_, err := wr.Write([]byte(name + "\n"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-05-10 09:27:03 +08:00
|
|
|
shaBs, _, _, err := ReadBatchLine(rd)
|
|
|
|
if IsErrNotExist(err) {
|
|
|
|
return "", ErrNotExist{name, ""}
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
|
|
|
|
2021-05-10 09:27:03 +08:00
|
|
|
return string(shaBs), nil
|
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 {
|
2021-12-01 04:06:32 +08:00
|
|
|
_, err := NewCommandContext(repo.Ctx, "cat-file", "-e", name).RunInDir(repo.Path)
|
2020-12-17 22:00:47 +08:00
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
|
2021-12-01 04:06:32 +08:00
|
|
|
wr, rd, cancel := repo.CatFileBatch(repo.Ctx)
|
2021-05-10 09:27:03 +08:00
|
|
|
defer cancel()
|
2020-12-17 22:00:47 +08:00
|
|
|
|
2021-05-10 09:27:03 +08:00
|
|
|
_, _ = wr.Write([]byte(id.String() + "\n"))
|
2021-05-02 09:16:08 +08:00
|
|
|
|
2021-05-10 09:27:03 +08:00
|
|
|
return repo.getCommitFromBatchReader(rd, id)
|
2021-05-02 09:16:08 +08:00
|
|
|
}
|
|
|
|
|
2021-05-10 09:27:03 +08:00
|
|
|
func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Commit, error) {
|
|
|
|
_, typ, size, err := ReadBatchLine(rd)
|
2020-12-17 22:00:47 +08:00
|
|
|
if err != nil {
|
2021-05-10 09:27:03 +08:00
|
|
|
if errors.Is(err, io.EOF) || IsErrNotExist(err) {
|
2021-02-19 08:40:17 +08:00
|
|
|
return nil, ErrNotExist{ID: id.String()}
|
|
|
|
}
|
2020-12-17 22:00:47 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ {
|
2021-02-19 08:40:17 +08:00
|
|
|
case "missing":
|
|
|
|
return nil, ErrNotExist{ID: id.String()}
|
2020-12-17 22:00:47 +08:00
|
|
|
case "tag":
|
|
|
|
// then we need to parse the tag
|
|
|
|
// and load the commit
|
2021-09-22 13:38:34 +08:00
|
|
|
data, err := io.ReadAll(io.LimitReader(rd, size))
|
2021-05-10 09:27:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = rd.Discard(1)
|
2020-12-17 22:00:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tag, err := parseTagData(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tag.repo = repo
|
|
|
|
|
|
|
|
commit, err := tag.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
commit.CommitMessage = strings.TrimSpace(tag.Message)
|
|
|
|
commit.Author = tag.Tagger
|
|
|
|
commit.Signature = tag.Signature
|
|
|
|
|
|
|
|
return commit, nil
|
|
|
|
case "commit":
|
2021-05-10 09:27:03 +08: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 22:00:47 +08:00
|
|
|
default:
|
2021-06-26 00:54:08 +08:00
|
|
|
log.Debug("Unknown typ: %s", typ)
|
2021-05-10 09:27:03 +08:00
|
|
|
_, err = rd.Discard(int(size) + 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-17 22:00:47 +08:00
|
|
|
return nil, ErrNotExist{
|
|
|
|
ID: id.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-10 09:27:03 +08:00
|
|
|
|
|
|
|
// ConvertToSHA1 returns a Hash object from a potential ID string
|
|
|
|
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
|
|
|
|
if len(commitID) == 40 && SHAPattern.MatchString(commitID) {
|
|
|
|
sha1, err := NewIDFromString(commitID)
|
|
|
|
if err == nil {
|
|
|
|
return sha1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 04:06:32 +08:00
|
|
|
wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx)
|
2021-05-10 09:27:03 +08:00
|
|
|
defer cancel()
|
|
|
|
_, err := wr.Write([]byte(commitID + "\n"))
|
|
|
|
if err != nil {
|
|
|
|
return SHA1{}, err
|
|
|
|
}
|
|
|
|
sha, _, _, err := ReadBatchLine(rd)
|
|
|
|
if err != nil {
|
|
|
|
if IsErrNotExist(err) {
|
|
|
|
return SHA1{}, ErrNotExist{commitID, ""}
|
|
|
|
}
|
|
|
|
return SHA1{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return MustIDFromString(string(sha)), nil
|
|
|
|
}
|