2023-06-23 20:37:56 +08:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/indexer/internal"
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
)
|
|
|
|
|
|
2025-03-13 11:07:48 +08:00
|
|
|
const filenameMatchNumberOfLines = 7 // Copied from GitHub search
|
2024-10-11 20:35:04 -03:00
|
|
|
|
2023-06-23 20:37:56 +08:00
|
|
|
func FilenameIndexerID(repoID int64, filename string) string {
|
|
|
|
|
return internal.Base36(repoID) + "_" + filename
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParseIndexerID(indexerID string) (int64, string) {
|
2025-12-04 10:06:44 +01:00
|
|
|
before, after, ok := strings.Cut(indexerID, "_")
|
|
|
|
|
if !ok {
|
2023-06-23 20:37:56 +08:00
|
|
|
log.Error("Unexpected ID in repo indexer: %s", indexerID)
|
|
|
|
|
}
|
2025-12-04 10:06:44 +01:00
|
|
|
repoID, _ := internal.ParseBase36(before)
|
|
|
|
|
return repoID, after
|
2023-06-23 20:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FilenameOfIndexerID(indexerID string) string {
|
2025-12-04 10:06:44 +01:00
|
|
|
_, after, ok := strings.Cut(indexerID, "_")
|
|
|
|
|
if !ok {
|
2023-06-23 20:37:56 +08:00
|
|
|
log.Error("Unexpected ID in repo indexer: %s", indexerID)
|
|
|
|
|
}
|
2025-12-04 10:06:44 +01:00
|
|
|
return after
|
2023-06-23 20:37:56 +08:00
|
|
|
}
|
2024-10-11 20:35:04 -03:00
|
|
|
|
2025-02-16 18:28:06 +08:00
|
|
|
// FilenameMatchIndexPos returns the boundaries of its first seven lines.
|
2024-10-11 20:35:04 -03:00
|
|
|
func FilenameMatchIndexPos(content string) (int, int) {
|
|
|
|
|
count := 1
|
|
|
|
|
for i, c := range content {
|
|
|
|
|
if c == '\n' {
|
|
|
|
|
count++
|
|
|
|
|
if count == filenameMatchNumberOfLines {
|
|
|
|
|
return 0, i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0, len(content)
|
|
|
|
|
}
|