modules/git: resolve GetFullCommitID via got log
giteabot backport / giteabot (push) Skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Canceled after 0s
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Canceled after 0s
release-nightly / nightly-binary (push) Canceled after 0s
release-nightly / nightly-container (push) Canceled after 0s

Use got log -c <rev> -l1 to resolve short/full commit IDs and refs to a
full commit ID. This keeps the call on got for the commit-resolution path
used by pull/merge/compare flows; unknown revisions still map to
ErrNotExist.

Verified: GOTMPDIR=/home/lotherk/.cache/gotmp go test ./modules/got ./modules/git -count=1
This commit is contained in:
lotherk committed 2026-11-24 06:52:26 +00:00
1 parent 75fed4414d
commit 264453a934
2 files changed
+46 -6

No files matched your search

+12 -6
View File
@@ -12,6 +12,8 @@ import (
"strings"
"gotea.dev/modules/git/gitcmd"
"gotea.dev/modules/git/gitrepo"
"gotea.dev/modules/got"
"gotea.dev/modules/util"
)
@@ -236,17 +238,21 @@ func (c *Commit) GetFileContent(ctx context.Context, gitRepo *Repository, filena
// GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository.
func GetFullCommitID(ctx context.Context, repo RepositoryFacade, shortID string) (string, error) {
commitID, _, err := gitcmd.NewCommand("rev-parse").
AddDynamicArguments(shortID).
WithRepo(repo).
RunStdString(ctx)
commitID, err := got.ResolveCommitID(ctx, gitrepo.RepoLocalPath(repo), shortID)
if err != nil {
if gitcmd.IsErrorExitCode(err, 128) {
if errors.Is(err, got.ErrNotInstalled) {
return "", err
}
if _, ok := errors.AsType[got.ErrRefNotFound](err); ok {
return "", ErrNotExist{shortID, ""}
}
var refNF got.ErrRefNotFound
if errors.As(err, &refNF) {
return "", ErrNotExist{shortID, ""}
}
return "", err
}
return strings.TrimSpace(commitID), nil
return commitID, nil
}
func AddObjectMessageArgument(cmd *gitcmd.Command, typ ObjectType, message string) error {
+34
View File
@@ -124,6 +124,40 @@ func ObjectExists(ctx context.Context, path, name string) (bool, error) {
return false, err
}
// ResolveCommitID returns the full commit ID for a revision argument. It accepts
// full/abbreviated commit IDs, refs and symbolic refs such as HEAD. Only commit
// objects are resolved; callers that need arbitrary object IDs should use
// ObjectExists/another path.
func ResolveCommitID(ctx context.Context, path, rev string) (string, error) {
if strings.TrimSpace(rev) == "" {
return "", fmt.Errorf("empty revision")
}
out, stderr, err := run(ctx, "log", "-r", path, "-c", rev, "-l1")
if err != nil {
msg := strings.ToLower(stderr + out)
if strings.Contains(msg, "not found") || strings.Contains(msg, "no such") ||
strings.Contains(msg, "reference ") || strings.Contains(msg, "unknown") {
return "", ErrRefNotFound{Name: rev}
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {
return "", ErrRefNotFound{Name: rev}
}
return "", err
}
for _, line := range strings.Split(out, "\n") {
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "commit ") {
continue
}
fields := strings.Fields(line)
if len(fields) >= 2 && isObjectID(fields[1]) {
return fields[1], nil
}
}
return "", fmt.Errorf("got log: commit id not found for %q", rev)
}
// ErrRefNotFound reports a missing reference.
type ErrRefNotFound struct {
Name string