2018-11-27 21:52:20 +00:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-11-27 21:52:20 +00:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
2023-07-27 10:47:41 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2024-06-30 03:54:31 +00:00
|
|
|
"fmt"
|
2023-07-27 10:47:41 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
2018-11-27 21:52:20 +00:00
|
|
|
// GetRefs returns all references of the repository.
|
|
|
|
func (repo *Repository) GetRefs() ([]*Reference, error) {
|
|
|
|
return repo.GetRefsFiltered("")
|
|
|
|
}
|
2023-07-27 10:47:41 +00:00
|
|
|
|
|
|
|
// ListOccurrences lists all refs of the given refType the given commit appears in sorted by creation date DESC
|
|
|
|
// refType should only be a literal "branch" or "tag" and nothing else
|
|
|
|
func (repo *Repository) ListOccurrences(ctx context.Context, refType, commitSHA string) ([]string, error) {
|
|
|
|
cmd := NewCommand(ctx)
|
|
|
|
if refType == "branch" {
|
|
|
|
cmd.AddArguments("branch")
|
|
|
|
} else if refType == "tag" {
|
|
|
|
cmd.AddArguments("tag")
|
|
|
|
} else {
|
|
|
|
return nil, util.NewInvalidArgumentErrorf(`can only use "branch" or "tag" for refType, but got %q`, refType)
|
|
|
|
}
|
|
|
|
stdout, _, err := cmd.AddArguments("--no-color", "--sort=-creatordate", "--contains").AddDynamicArguments(commitSHA).RunStdString(&RunOpts{Dir: repo.Path})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
refs := strings.Split(strings.TrimSpace(stdout), "\n")
|
|
|
|
if refType == "branch" {
|
|
|
|
return parseBranches(refs), nil
|
|
|
|
}
|
|
|
|
return parseTags(refs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseBranches(refs []string) []string {
|
|
|
|
results := make([]string, 0, len(refs))
|
|
|
|
for _, ref := range refs {
|
|
|
|
if strings.HasPrefix(ref, "* ") { // current branch (main branch)
|
|
|
|
results = append(results, ref[len("* "):])
|
|
|
|
} else if strings.HasPrefix(ref, " ") { // all other branches
|
|
|
|
results = append(results, ref[len(" "):])
|
|
|
|
} else if ref != "" {
|
|
|
|
results = append(results, ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTags(refs []string) []string {
|
|
|
|
results := make([]string, 0, len(refs))
|
|
|
|
for _, ref := range refs {
|
|
|
|
if ref != "" {
|
|
|
|
results = append(results, ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
2024-06-30 03:54:31 +00:00
|
|
|
|
|
|
|
// ExpandRef expands any partial reference to its full form
|
|
|
|
func (repo *Repository) ExpandRef(ref string) (string, error) {
|
|
|
|
if strings.HasPrefix(ref, "refs/") {
|
|
|
|
return ref, nil
|
|
|
|
} else if strings.HasPrefix(ref, "tags/") || strings.HasPrefix(ref, "heads/") {
|
|
|
|
return "refs/" + ref, nil
|
|
|
|
} else if repo.IsTagExist(ref) {
|
|
|
|
return TagPrefix + ref, nil
|
|
|
|
} else if repo.IsBranchExist(ref) {
|
|
|
|
return BranchPrefix + ref, nil
|
|
|
|
} else if repo.IsCommitExist(ref) {
|
|
|
|
return ref, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("could not expand reference '%s'", ref)
|
|
|
|
}
|