gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package internal
  4. import (
  5. "strings"
  6. "code.gitea.io/gitea/modules/indexer/internal"
  7. "code.gitea.io/gitea/modules/log"
  8. )
  9. const filenameMatchNumberOfLines = 7 // Copied from GitHub search
  10. func FilenameIndexerID(repoID int64, filename string) string {
  11. return internal.Base36(repoID) + "_" + filename
  12. }
  13. func ParseIndexerID(indexerID string) (int64, string) {
  14. index := strings.IndexByte(indexerID, '_')
  15. if index == -1 {
  16. log.Error("Unexpected ID in repo indexer: %s", indexerID)
  17. }
  18. repoID, _ := internal.ParseBase36(indexerID[:index])
  19. return repoID, indexerID[index+1:]
  20. }
  21. func FilenameOfIndexerID(indexerID string) string {
  22. index := strings.IndexByte(indexerID, '_')
  23. if index == -1 {
  24. log.Error("Unexpected ID in repo indexer: %s", indexerID)
  25. }
  26. return indexerID[index+1:]
  27. }
  28. // FilenameMatchIndexPos returns the boundaries of its first seven lines.
  29. func FilenameMatchIndexPos(content string) (int, int) {
  30. count := 1
  31. for i, c := range content {
  32. if c == '\n' {
  33. count++
  34. if count == filenameMatchNumberOfLines {
  35. return 0, i
  36. }
  37. }
  38. }
  39. return 0, len(content)
  40. }