2024-03-24 12:05:00 -04:00
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2024-04-11 07:34:53 -04:00
|
|
|
"bytes"
|
2024-03-24 12:05:00 -04:00
|
|
|
"context"
|
2024-04-11 07:34:53 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
2024-03-24 12:05:00 -04:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-03-24 12:05:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGrepSearch(t *testing.T) {
|
|
|
|
repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "language_stats_repo"))
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-03-24 12:05:00 -04:00
|
|
|
defer repo.Close()
|
|
|
|
|
2024-08-06 01:57:25 -04:00
|
|
|
res, err := GrepSearch(context.Background(), repo, "public", GrepOptions{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-03-24 12:05:00 -04:00
|
|
|
assert.Equal(t, []*GrepResult{
|
|
|
|
{
|
|
|
|
Filename: "java-hello/main.java",
|
2024-08-06 01:57:25 -04:00
|
|
|
LineNumbers: []int{1, 3},
|
|
|
|
LineCodes: []string{
|
|
|
|
"public class HelloWorld",
|
|
|
|
" public static void main(String[] args)",
|
|
|
|
},
|
|
|
|
HighlightedRanges: [][3]int{{0, 0, 6}, {1, 1, 7}},
|
2024-03-24 12:05:00 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Filename: "main.vendor.java",
|
2024-08-06 01:57:25 -04:00
|
|
|
LineNumbers: []int{1, 3},
|
|
|
|
LineCodes: []string{
|
|
|
|
"public class HelloWorld",
|
|
|
|
" public static void main(String[] args)",
|
|
|
|
},
|
|
|
|
HighlightedRanges: [][3]int{{0, 0, 6}, {1, 1, 7}},
|
2024-03-24 12:05:00 -04:00
|
|
|
},
|
|
|
|
}, res)
|
|
|
|
|
2024-08-06 01:57:25 -04:00
|
|
|
res, err = GrepSearch(context.Background(), repo, "void", GrepOptions{MaxResultLimit: 1, ContextLineNumber: 2})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-03-25 09:25:22 -04:00
|
|
|
assert.Equal(t, []*GrepResult{
|
|
|
|
{
|
|
|
|
Filename: "java-hello/main.java",
|
2024-08-06 01:57:25 -04:00
|
|
|
LineNumbers: []int{1, 2, 3, 4, 5},
|
|
|
|
LineCodes: []string{
|
|
|
|
"public class HelloWorld",
|
|
|
|
"{",
|
|
|
|
" public static void main(String[] args)",
|
|
|
|
" {",
|
|
|
|
" System.out.println(\"Hello world!\");",
|
|
|
|
},
|
|
|
|
HighlightedRanges: [][3]int{{2, 15, 19}},
|
2024-03-25 09:25:22 -04:00
|
|
|
},
|
|
|
|
}, res)
|
|
|
|
|
2024-05-20 08:23:27 -04:00
|
|
|
res, err = GrepSearch(context.Background(), repo, "world", GrepOptions{MatchesPerFile: 1})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-05-20 08:23:27 -04:00
|
|
|
assert.Equal(t, []*GrepResult{
|
|
|
|
{
|
2024-08-06 01:57:25 -04:00
|
|
|
Filename: "i-am-a-python.p",
|
|
|
|
LineNumbers: []int{1},
|
|
|
|
LineCodes: []string{"## This is a simple file to do a hello world"},
|
|
|
|
HighlightedRanges: [][3]int{{0, 39, 44}},
|
2024-05-20 08:23:27 -04:00
|
|
|
},
|
|
|
|
{
|
2024-08-06 01:57:25 -04:00
|
|
|
Filename: "java-hello/main.java",
|
|
|
|
LineNumbers: []int{1},
|
|
|
|
LineCodes: []string{"public class HelloWorld"},
|
|
|
|
HighlightedRanges: [][3]int{{0, 18, 23}},
|
2024-05-20 08:23:27 -04:00
|
|
|
},
|
|
|
|
{
|
2024-08-06 01:57:25 -04:00
|
|
|
Filename: "main.vendor.java",
|
|
|
|
LineNumbers: []int{1},
|
|
|
|
LineCodes: []string{"public class HelloWorld"},
|
|
|
|
HighlightedRanges: [][3]int{{0, 18, 23}},
|
2024-05-20 08:23:27 -04:00
|
|
|
},
|
|
|
|
{
|
2024-08-06 01:57:25 -04:00
|
|
|
Filename: "python-hello/hello.py",
|
|
|
|
LineNumbers: []int{1},
|
|
|
|
LineCodes: []string{"## This is a simple file to do a hello world"},
|
|
|
|
HighlightedRanges: [][3]int{{0, 39, 44}},
|
2024-05-20 08:23:27 -04:00
|
|
|
},
|
|
|
|
}, res)
|
|
|
|
|
2024-03-24 12:05:00 -04:00
|
|
|
res, err = GrepSearch(context.Background(), repo, "no-such-content", GrepOptions{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, res)
|
2024-03-24 12:05:00 -04:00
|
|
|
|
|
|
|
res, err = GrepSearch(context.Background(), &Repository{Path: "no-such-git-repo"}, "no-such-content", GrepOptions{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Empty(t, res)
|
2024-03-24 12:05:00 -04:00
|
|
|
}
|
2024-04-11 07:34:53 -04:00
|
|
|
|
2024-08-12 08:05:01 -04:00
|
|
|
func TestGrepDashesAreFine(t *testing.T) {
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
err := InitRepository(DefaultContext, tmpDir, false, Sha1ObjectFormat.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gitRepo, err := openRepositoryWithDefaultContext(tmpDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
require.NoError(t, os.WriteFile(path.Join(tmpDir, "with-dashes"), []byte("--"), 0o666))
|
|
|
|
require.NoError(t, os.WriteFile(path.Join(tmpDir, "without-dashes"), []byte(".."), 0o666))
|
|
|
|
|
|
|
|
err = AddChanges(tmpDir, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = CommitChanges(tmpDir, CommitChangesOptions{Message: "Dashes are cool sometimes"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := GrepSearch(context.Background(), gitRepo, "--", GrepOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, res, 1)
|
|
|
|
assert.Equal(t, "with-dashes", res[0].Filename)
|
|
|
|
}
|
|
|
|
|
2024-08-11 08:24:40 -04:00
|
|
|
func TestGrepNoBinary(t *testing.T) {
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
err := InitRepository(DefaultContext, tmpDir, false, Sha1ObjectFormat.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gitRepo, err := openRepositoryWithDefaultContext(tmpDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
require.NoError(t, os.WriteFile(path.Join(tmpDir, "BINARY"), []byte("I AM BINARY\n\x00\nYOU WON'T SEE ME"), 0o666))
|
|
|
|
require.NoError(t, os.WriteFile(path.Join(tmpDir, "TEXT"), []byte("I AM NOT BINARY\nYOU WILL SEE ME"), 0o666))
|
|
|
|
|
|
|
|
err = AddChanges(tmpDir, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = CommitChanges(tmpDir, CommitChangesOptions{Message: "Binary and text files"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := GrepSearch(context.Background(), gitRepo, "BINARY", GrepOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, res, 1)
|
|
|
|
assert.Equal(t, "TEXT", res[0].Filename)
|
|
|
|
}
|
|
|
|
|
2024-04-11 07:34:53 -04:00
|
|
|
func TestGrepLongFiles(t *testing.T) {
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
err := InitRepository(DefaultContext, tmpDir, false, Sha1ObjectFormat.Name())
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-04-11 07:34:53 -04:00
|
|
|
|
|
|
|
gitRepo, err := openRepositoryWithDefaultContext(tmpDir)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-04-11 07:34:53 -04:00
|
|
|
defer gitRepo.Close()
|
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, os.WriteFile(path.Join(tmpDir, "README.md"), bytes.Repeat([]byte{'a'}, 65*1024), 0o666))
|
2024-04-11 07:34:53 -04:00
|
|
|
|
|
|
|
err = AddChanges(tmpDir, true)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-04-11 07:34:53 -04:00
|
|
|
|
|
|
|
err = CommitChanges(tmpDir, CommitChangesOptions{Message: "Long file"})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-04-11 07:34:53 -04:00
|
|
|
|
|
|
|
res, err := GrepSearch(context.Background(), gitRepo, "a", GrepOptions{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-04-11 07:34:53 -04:00
|
|
|
assert.Len(t, res, 1)
|
|
|
|
assert.Len(t, res[0].LineCodes[0], 65*1024)
|
|
|
|
}
|
2024-05-14 11:41:03 -04:00
|
|
|
|
|
|
|
func TestGrepRefs(t *testing.T) {
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
err := InitRepository(DefaultContext, tmpDir, false, Sha1ObjectFormat.Name())
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-05-14 11:41:03 -04:00
|
|
|
|
|
|
|
gitRepo, err := openRepositoryWithDefaultContext(tmpDir)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-05-14 11:41:03 -04:00
|
|
|
defer gitRepo.Close()
|
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, os.WriteFile(path.Join(tmpDir, "README.md"), []byte{'A'}, 0o666))
|
|
|
|
require.NoError(t, AddChanges(tmpDir, true))
|
2024-05-14 11:41:03 -04:00
|
|
|
|
|
|
|
err = CommitChanges(tmpDir, CommitChangesOptions{Message: "add A"})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-05-14 11:41:03 -04:00
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, gitRepo.CreateTag("v1", "HEAD"))
|
2024-05-14 11:41:03 -04:00
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, os.WriteFile(path.Join(tmpDir, "README.md"), []byte{'A', 'B', 'C', 'D'}, 0o666))
|
|
|
|
require.NoError(t, AddChanges(tmpDir, true))
|
2024-05-14 11:41:03 -04:00
|
|
|
|
|
|
|
err = CommitChanges(tmpDir, CommitChangesOptions{Message: "add BCD"})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-05-14 11:41:03 -04:00
|
|
|
|
|
|
|
res, err := GrepSearch(context.Background(), gitRepo, "a", GrepOptions{RefName: "v1"})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-05-14 11:41:03 -04:00
|
|
|
assert.Len(t, res, 1)
|
2024-07-30 15:41:10 -04:00
|
|
|
assert.Equal(t, "A", res[0].LineCodes[0])
|
2024-05-14 11:41:03 -04:00
|
|
|
}
|