aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authordelvh <dev.lh@web.de>2023-07-27 12:47:41 +0200
committerGitHub <noreply@github.com>2023-07-27 12:47:41 +0200
commitbd6ef718548767ff209048eb8443a067106908bf (patch)
tree58c48bebaea1307368fb41fa13b349e42353ad8d /modules
parentbd7b5e61aa8d8f8a7dfd1ce4b49bf6461fce57b8 (diff)
downloadgitea-bd6ef718548767ff209048eb8443a067106908bf.tar.gz
gitea-bd6ef718548767ff209048eb8443a067106908bf.zip
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>
Diffstat (limited to 'modules')
-rw-r--r--modules/context/context_response.go1
-rw-r--r--modules/git/commit.go26
-rw-r--r--modules/git/repo_ref.go54
3 files changed, 55 insertions, 26 deletions
diff --git a/modules/context/context_response.go b/modules/context/context_response.go
index bb3ccf69ce..9dc6d1fc0e 100644
--- a/modules/context/context_response.go
+++ b/modules/context/context_response.go
@@ -166,6 +166,7 @@ func (ctx *Context) serverErrorInternal(logMsg string, logErr error) {
// NotFoundOrServerError use error check function to determine if the error
// is about not found. It responds with 404 status code for not found error,
// or error context description for logging purpose of 500 server error.
+// TODO: remove the "errCheck" and use util.ErrNotFound to check
func (ctx *Context) NotFoundOrServerError(logMsg string, errCheck func(error) bool, logErr error) {
if errCheck(logErr) {
ctx.notFoundInternal(logMsg, logErr)
diff --git a/modules/git/commit.go b/modules/git/commit.go
index 729e3b4672..c44882d886 100644
--- a/modules/git/commit.go
+++ b/modules/git/commit.go
@@ -20,7 +20,6 @@ import (
// Commit represents a git commit.
type Commit struct {
- Branch string // Branch this commit belongs to
Tree
ID SHA1 // The ID of this commit object
Author *Signature
@@ -432,31 +431,6 @@ func (c *Commit) GetBranchName() (string, error) {
return strings.SplitN(strings.TrimSpace(data), "~", 2)[0], nil
}
-// LoadBranchName load branch name for commit
-func (c *Commit) LoadBranchName() (err error) {
- if len(c.Branch) != 0 {
- return nil
- }
-
- c.Branch, err = c.GetBranchName()
- return err
-}
-
-// GetTagName gets the current tag name for given commit
-func (c *Commit) GetTagName() (string, error) {
- data, _, err := NewCommand(c.repo.Ctx, "describe", "--exact-match", "--tags", "--always").AddDynamicArguments(c.ID.String()).RunStdString(&RunOpts{Dir: c.repo.Path})
- if err != nil {
- // handle special case where there is no tag for this commit
- if strings.Contains(err.Error(), "no tag exactly matches") {
- return "", nil
- }
-
- return "", err
- }
-
- return strings.TrimSpace(data), nil
-}
-
// CommitFileStatus represents status of files in a commit.
type CommitFileStatus struct {
Added []string
diff --git a/modules/git/repo_ref.go b/modules/git/repo_ref.go
index 54e424bb83..8eaa17cb04 100644
--- a/modules/git/repo_ref.go
+++ b/modules/git/repo_ref.go
@@ -3,7 +3,61 @@
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
+}