mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-29 00:59:39 +09:00
refactor: drop two unmaintained dependencies, rename the byte size helpers (#39083)
This commit is contained in:
@@ -16,8 +16,6 @@ import (
|
||||
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/util"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
// EncodeSha256 string to sha256 hex value.
|
||||
@@ -92,11 +90,6 @@ func CreateTimeLimitCode[T time.Time | string](data string, minutes int, startTi
|
||||
return code
|
||||
}
|
||||
|
||||
// FileSize calculates the file size and generate user-friendly string.
|
||||
func FileSize(s int64) string {
|
||||
return humanize.IBytes(uint64(s))
|
||||
}
|
||||
|
||||
// StringsToInt64s converts a slice of string to a slice of int64.
|
||||
func StringsToInt64s(strs []string) ([]int64, error) {
|
||||
if strs == nil {
|
||||
|
||||
@@ -76,23 +76,6 @@ JWT_SECRET = %s
|
||||
})
|
||||
}
|
||||
|
||||
func TestFileSize(t *testing.T) {
|
||||
var size int64 = 512
|
||||
assert.Equal(t, "512 B", FileSize(size))
|
||||
size *= 1024
|
||||
assert.Equal(t, "512 KiB", FileSize(size))
|
||||
size *= 1024
|
||||
assert.Equal(t, "512 MiB", FileSize(size))
|
||||
size *= 1024
|
||||
assert.Equal(t, "512 GiB", FileSize(size))
|
||||
size *= 1024
|
||||
assert.Equal(t, "512 TiB", FileSize(size))
|
||||
size *= 1024
|
||||
assert.Equal(t, "512 PiB", FileSize(size))
|
||||
size *= 4
|
||||
assert.Equal(t, "2.0 EiB", FileSize(size))
|
||||
}
|
||||
|
||||
func TestStringsToInt64s(t *testing.T) {
|
||||
testSuccess := func(input []string, expected []int64) {
|
||||
result, err := StringsToInt64s(input)
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
|
||||
"github.com/blevesearch/bleve/v2"
|
||||
"github.com/blevesearch/bleve/v2/mapping"
|
||||
"github.com/ethantkoenig/rupture"
|
||||
)
|
||||
|
||||
var _ internal.Indexer = &Indexer{}
|
||||
@@ -69,9 +68,7 @@ func (i *Indexer) Init(_ context.Context) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if err = rupture.WriteIndexMetadata(i.indexDir, &rupture.IndexMetadata{
|
||||
Version: i.version,
|
||||
}); err != nil {
|
||||
if err = writeIndexMetadataVersion(i.indexDir, i.version); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
|
||||
@@ -6,21 +6,47 @@ package bleve
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"unicode"
|
||||
|
||||
"gitea.dev/modules/json"
|
||||
"gitea.dev/modules/log"
|
||||
"gitea.dev/modules/setting"
|
||||
|
||||
"github.com/blevesearch/bleve/v2"
|
||||
unicode_tokenizer "github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode"
|
||||
"github.com/blevesearch/bleve/v2/index/upsidedown"
|
||||
"github.com/ethantkoenig/rupture"
|
||||
)
|
||||
|
||||
const (
|
||||
maxFuzziness = 2
|
||||
maxFuzziness = 2
|
||||
indexMetadataFilename = "rupture_meta.json" // named after the former "rupture" dependency, renaming it would force a reindex
|
||||
)
|
||||
|
||||
type indexMetadata struct {
|
||||
Version int `json:"version"`
|
||||
}
|
||||
|
||||
func readIndexMetadataVersion(path string) (int, error) {
|
||||
data, err := os.ReadFile(filepath.Join(path, indexMetadataFilename))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return 0, nil
|
||||
} else if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var metadata indexMetadata
|
||||
if err := json.Unmarshal(data, &metadata); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return metadata.Version, nil
|
||||
}
|
||||
|
||||
func writeIndexMetadataVersion(path string, version int) error {
|
||||
data, _ := json.Marshal(indexMetadata{Version: version}) // marshalling a single int cannot fail
|
||||
return os.WriteFile(filepath.Join(path, indexMetadataFilename), data, 0o666)
|
||||
}
|
||||
|
||||
// openIndexer open the index at the specified path, checking for metadata
|
||||
// updates and bleve version updates. If index needs to be created (or
|
||||
// re-created), returns (nil, nil)
|
||||
@@ -32,14 +58,14 @@ func openIndexer(path string, latestVersion int) (bleve.Index, int, error) {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
metadata, err := rupture.ReadIndexMetadata(path)
|
||||
version, err := readIndexMetadataVersion(path)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if metadata.Version < latestVersion {
|
||||
if version < latestVersion {
|
||||
// the indexer is using a previous version, so we should delete it and
|
||||
// re-populate
|
||||
return nil, metadata.Version, os.RemoveAll(path)
|
||||
return nil, version, os.RemoveAll(path)
|
||||
}
|
||||
|
||||
index, err := bleve.Open(path)
|
||||
|
||||
@@ -5,14 +5,33 @@ package bleve
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/test"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIndexMetadataVersion(t *testing.T) {
|
||||
indexDir := t.TempDir()
|
||||
version, err := readIndexMetadataVersion(indexDir)
|
||||
require.NoError(t, err)
|
||||
assert.Zero(t, version)
|
||||
|
||||
require.NoError(t, writeIndexMetadataVersion(indexDir, 42))
|
||||
data, err := os.ReadFile(filepath.Join(indexDir, indexMetadataFilename))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, `{"version":42}`, string(data))
|
||||
|
||||
version, err = readIndexMetadataVersion(indexDir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 42, version)
|
||||
}
|
||||
|
||||
func TestBleveGuessFuzzinessByKeyword(t *testing.T) {
|
||||
defer test.MockVariableValue(&setting.Indexer.TypeBleveMaxFuzzniess, 2)()
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"gitea.dev/modules/util"
|
||||
)
|
||||
|
||||
// Package registry settings
|
||||
@@ -100,7 +100,7 @@ func mustBytes(section ConfigSection, key string) int64 {
|
||||
if value == noLimit {
|
||||
return -1
|
||||
}
|
||||
bytes, err := humanize.ParseBytes(value)
|
||||
bytes, err := util.ParseByteSize(value)
|
||||
if err != nil || bytes > math.MaxInt64 {
|
||||
return -1
|
||||
}
|
||||
|
||||
@@ -56,10 +56,10 @@ func newFuncMapWebPage() template.FuncMap {
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// time / number / format
|
||||
"ShortSha": base.ShortSha,
|
||||
"FileSize": base.FileSize,
|
||||
"CountFmt": countFmt,
|
||||
"Sec2Hour": util.SecToHours,
|
||||
"ShortSha": base.ShortSha,
|
||||
"FormatByteSize": util.FormatByteSize,
|
||||
"CountFmt": countFmt,
|
||||
"Sec2Hour": util.SecToHours,
|
||||
|
||||
"TimeEstimateString": timeEstimateString,
|
||||
|
||||
|
||||
@@ -87,8 +87,8 @@ func mailBodyFuncMap() template.FuncMap {
|
||||
"JsonUtils": NewJsonUtils,
|
||||
|
||||
// time / number / format
|
||||
"ShortSha": base.ShortSha,
|
||||
"FileSize": base.FileSize,
|
||||
"ShortSha": base.ShortSha,
|
||||
"FormatByteSize": util.FormatByteSize,
|
||||
|
||||
// setting
|
||||
"AppName": func() string {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package util
|
||||
|
||||
import "github.com/dustin/go-humanize"
|
||||
|
||||
// FormatByteSize formats a byte count as a user-friendly string, e.g. "1.5 MiB".
|
||||
func FormatByteSize(size int64) string {
|
||||
return humanize.IBytes(uint64(size))
|
||||
}
|
||||
|
||||
// ParseByteSize parses a byte size such as "100MB" or "1.5 GiB".
|
||||
func ParseByteSize(value string) (uint64, error) {
|
||||
return humanize.ParseBytes(value)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFormatByteSize(t *testing.T) {
|
||||
var size int64 = 512
|
||||
assert.Equal(t, "512 B", FormatByteSize(size))
|
||||
size *= 1024
|
||||
assert.Equal(t, "512 KiB", FormatByteSize(size))
|
||||
size *= 1024
|
||||
assert.Equal(t, "512 MiB", FormatByteSize(size))
|
||||
size *= 1024
|
||||
assert.Equal(t, "512 GiB", FormatByteSize(size))
|
||||
size *= 1024
|
||||
assert.Equal(t, "512 TiB", FormatByteSize(size))
|
||||
size *= 1024
|
||||
assert.Equal(t, "512 PiB", FormatByteSize(size))
|
||||
size *= 4
|
||||
assert.Equal(t, "2.0 EiB", FormatByteSize(size))
|
||||
}
|
||||
Reference in New Issue
Block a user