gitea源码

repo_ref.go 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "context"
  6. "strings"
  7. "code.gitea.io/gitea/modules/git/gitcmd"
  8. "code.gitea.io/gitea/modules/util"
  9. )
  10. // GetRefs returns all references of the repository.
  11. func (repo *Repository) GetRefs() ([]*Reference, error) {
  12. return repo.GetRefsFiltered("")
  13. }
  14. // ListOccurrences lists all refs of the given refType the given commit appears in sorted by creation date DESC
  15. // refType should only be a literal "branch" or "tag" and nothing else
  16. func (repo *Repository) ListOccurrences(ctx context.Context, refType, commitSHA string) ([]string, error) {
  17. cmd := gitcmd.NewCommand()
  18. switch refType {
  19. case "branch":
  20. cmd.AddArguments("branch")
  21. case "tag":
  22. cmd.AddArguments("tag")
  23. default:
  24. return nil, util.NewInvalidArgumentErrorf(`can only use "branch" or "tag" for refType, but got %q`, refType)
  25. }
  26. stdout, _, err := cmd.AddArguments("--no-color", "--sort=-creatordate", "--contains").AddDynamicArguments(commitSHA).RunStdString(ctx, &gitcmd.RunOpts{Dir: repo.Path})
  27. if err != nil {
  28. return nil, err
  29. }
  30. refs := strings.Split(strings.TrimSpace(stdout), "\n")
  31. if refType == "branch" {
  32. return parseBranches(refs), nil
  33. }
  34. return parseTags(refs), nil
  35. }
  36. func parseBranches(refs []string) []string {
  37. results := make([]string, 0, len(refs))
  38. for _, ref := range refs {
  39. if strings.HasPrefix(ref, "* ") { // current branch (main branch)
  40. results = append(results, ref[len("* "):])
  41. } else if strings.HasPrefix(ref, " ") { // all other branches
  42. results = append(results, ref[len(" "):])
  43. } else if ref != "" {
  44. results = append(results, ref)
  45. }
  46. }
  47. return results
  48. }
  49. func parseTags(refs []string) []string {
  50. results := make([]string, 0, len(refs))
  51. for _, ref := range refs {
  52. if ref != "" {
  53. results = append(results, ref)
  54. }
  55. }
  56. return results
  57. }
  58. // UnstableGuessRefByShortName does the best guess to see whether a "short name" provided by user is a branch, tag or commit.
  59. // It could guess wrongly if the input is already ambiguous. For example:
  60. // * "refs/heads/the-name" vs "refs/heads/refs/heads/the-name"
  61. // * "refs/tags/1234567890" vs commit "1234567890"
  62. // In most cases, it SHOULD AVOID using this function, unless there is an irresistible reason (eg: make API friendly to end users)
  63. // If the function is used, the caller SHOULD CHECK the ref type carefully.
  64. func (repo *Repository) UnstableGuessRefByShortName(shortName string) RefName {
  65. if repo.IsBranchExist(shortName) {
  66. return RefNameFromBranch(shortName)
  67. }
  68. if repo.IsTagExist(shortName) {
  69. return RefNameFromTag(shortName)
  70. }
  71. if strings.HasPrefix(shortName, "refs/") {
  72. if repo.IsReferenceExist(shortName) {
  73. return RefName(shortName)
  74. }
  75. }
  76. commit, err := repo.GetCommit(shortName)
  77. if err == nil {
  78. commitIDString := commit.ID.String()
  79. if strings.HasPrefix(commitIDString, shortName) {
  80. return RefName(commitIDString)
  81. }
  82. }
  83. return ""
  84. }