1
1
mirror of https://github.com/go-gitea/gitea synced 2024-11-18 16:14:26 +00:00
gitea/modules/git/repo_ref.go
delvh bd6ef71854
Show branches and tags that contain a commit (#25180)
Now, you can see for a commit which existing branches and tags contain it.
You first have to click on the `load branches and tags` button, they are not preloaded by default.
All branches and tags are ordered descending by creation date.
You can even see without much hassle if the given commit is already part of the default branch.

Closes #25152 

## Screenshots

### Initial

![image](https://github.com/go-gitea/gitea/assets/51889757/84db2c0b-aaef-4f69-ab92-0b812793d2ad)

### Loaded

![image](https://github.com/go-gitea/gitea/assets/51889757/a9b84e66-8e44-4c55-b017-c37f4a45f41b)

---------

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2023-07-27 12:47:41 +02:00

64 lines
1.8 KiB
Go

// Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"context"
"strings"
"code.gitea.io/gitea/modules/util"
)
// GetRefs returns all references of the repository.
func (repo *Repository) GetRefs() ([]*Reference, error) {
return repo.GetRefsFiltered("")
}
// 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
}