mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package utils
|
|
|
|
import (
|
|
"errors"
|
|
|
|
git_model "github.com/hanzoai/git/models/git"
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
"github.com/hanzoai/git/modules/git"
|
|
"github.com/hanzoai/git/modules/gitrepo"
|
|
"github.com/hanzoai/git/modules/reqctx"
|
|
"github.com/hanzoai/git/services/context"
|
|
)
|
|
|
|
type RefCommit struct {
|
|
InputRef string
|
|
RefName git.RefName
|
|
Commit *git.Commit
|
|
CommitID string
|
|
}
|
|
|
|
// ResolveRefCommit resolve ref to a commit if exist
|
|
func ResolveRefCommit(ctx reqctx.RequestContext, repo *repo_model.Repository, inputRef string, minCommitIDLen ...int) (_ *RefCommit, err error) {
|
|
gitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, repo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
refCommit := RefCommit{InputRef: inputRef}
|
|
if exist, _ := git_model.IsBranchExist(ctx, repo.ID, inputRef); exist {
|
|
refCommit.RefName = git.RefNameFromBranch(inputRef)
|
|
} else if gitrepo.IsTagExist(ctx, repo, inputRef) {
|
|
refCommit.RefName = git.RefNameFromTag(inputRef)
|
|
} else if git.IsStringLikelyCommitID(git.ObjectFormatFromName(repo.ObjectFormatName), inputRef, minCommitIDLen...) {
|
|
refCommit.RefName = git.RefNameFromCommit(inputRef)
|
|
}
|
|
if refCommit.RefName == "" {
|
|
return nil, git.ErrNotExist{ID: inputRef}
|
|
}
|
|
if refCommit.Commit, err = gitRepo.GetCommit(refCommit.RefName.String()); err != nil {
|
|
return nil, err
|
|
}
|
|
refCommit.CommitID = refCommit.Commit.ID.String()
|
|
return &refCommit, nil
|
|
}
|
|
|
|
func NewRefCommit(refName git.RefName, commit *git.Commit) *RefCommit {
|
|
return &RefCommit{InputRef: refName.ShortName(), RefName: refName, Commit: commit, CommitID: commit.ID.String()}
|
|
}
|
|
|
|
// GetGitRefs return git references based on filter
|
|
func GetGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) {
|
|
if ctx.Repo.GitRepo == nil {
|
|
return nil, "", errors.New("no open git repo found in context")
|
|
}
|
|
if len(filter) > 0 {
|
|
filter = "refs/" + filter
|
|
}
|
|
refs, err := ctx.Repo.GitRepo.GetRefsFiltered(filter)
|
|
return refs, "GetRefsFiltered", err
|
|
}
|