A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.9 KiB

Use native git variants by default with go-git variants as build tag (#13673) * Move last commit cache back into modules/git Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from the interface for last commit cache Signed-off-by: Andrew Thornton <art27@cantab.net> * move cacheref to last_commit_cache Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from routers/private/hook Signed-off-by: Andrew Thornton <art27@cantab.net> * Move FindLFSFiles to pipeline Signed-off-by: Andrew Thornton <art27@cantab.net> * Make no-go-git variants Signed-off-by: Andrew Thornton <art27@cantab.net> * Submodule RefID Signed-off-by: Andrew Thornton <art27@cantab.net> * fix issue with GetCommitsInfo Signed-off-by: Andrew Thornton <art27@cantab.net> * fix GetLastCommitForPaths Signed-off-by: Andrew Thornton <art27@cantab.net> * Improve efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * More efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * even faster Signed-off-by: Andrew Thornton <art27@cantab.net> * Reduce duplication * As per @lunny Signed-off-by: Andrew Thornton <art27@cantab.net> * attempt to fix drone Signed-off-by: Andrew Thornton <art27@cantab.net> * fix test-tags Signed-off-by: Andrew Thornton <art27@cantab.net> * default to use no-go-git variants and add gogit build tag Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @6543 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
2 years ago
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // +build !gogit
  5. package git
  6. import (
  7. "bufio"
  8. "io"
  9. "strings"
  10. )
  11. // GetRefsFiltered returns all references of the repository that matches patterm exactly or starting with.
  12. func (repo *Repository) GetRefsFiltered(pattern string) ([]*Reference, error) {
  13. stdoutReader, stdoutWriter := io.Pipe()
  14. defer func() {
  15. _ = stdoutReader.Close()
  16. _ = stdoutWriter.Close()
  17. }()
  18. go func() {
  19. stderrBuilder := &strings.Builder{}
  20. err := NewCommand("for-each-ref").RunInDirPipeline(repo.Path, stdoutWriter, stderrBuilder)
  21. if err != nil {
  22. _ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
  23. } else {
  24. _ = stdoutWriter.Close()
  25. }
  26. }()
  27. refs := make([]*Reference, 0)
  28. bufReader := bufio.NewReader(stdoutReader)
  29. for {
  30. // The output of for-each-ref is simply a list:
  31. // <sha> SP <type> TAB <ref> LF
  32. sha, err := bufReader.ReadString(' ')
  33. if err == io.EOF {
  34. break
  35. }
  36. if err != nil {
  37. return nil, err
  38. }
  39. sha = sha[:len(sha)-1]
  40. typ, err := bufReader.ReadString('\t')
  41. if err == io.EOF {
  42. // This should not happen, but we'll tolerate it
  43. break
  44. }
  45. if err != nil {
  46. return nil, err
  47. }
  48. typ = typ[:len(typ)-1]
  49. refName, err := bufReader.ReadString('\n')
  50. if err == io.EOF {
  51. // This should not happen, but we'll tolerate it
  52. break
  53. }
  54. if err != nil {
  55. return nil, err
  56. }
  57. refName = refName[:len(refName)-1]
  58. // refName cannot be HEAD but can be remotes or stash
  59. if strings.HasPrefix(refName, "/refs/remotes/") || refName == "/refs/stash" {
  60. continue
  61. }
  62. if pattern == "" || strings.HasPrefix(refName, pattern) {
  63. r := &Reference{
  64. Name: refName,
  65. Object: MustIDFromString(sha),
  66. Type: typ,
  67. repo: repo,
  68. }
  69. refs = append(refs, r)
  70. }
  71. }
  72. return refs, nil
  73. }