Skip to content

Commit 18622a0

Browse files
authored
probe if sha before exec git (#21467)
1 parent 11ac14c commit 18622a0

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Diff for: modules/git/repo_commit.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Co
154154
// then let's iterate over them
155155
if len(opts.Keywords) > 0 {
156156
for _, v := range opts.Keywords {
157-
// ignore anything below 4 characters as too unspecific
158-
if len(v) >= 4 {
157+
// ignore anything not matching a valid sha pattern
158+
if IsValidSHAPattern(v) {
159159
// create new git log command with 1 commit limit
160160
hashCmd := NewCommand(repo.Ctx, "log", "-1", prettyLogFormat)
161161
// add previous arguments except for --grep and --all

Diff for: modules/git/sha1_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2022 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+
5+
package git
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestIsValidSHAPattern(t *testing.T) {
14+
assert.True(t, IsValidSHAPattern("fee1"))
15+
assert.True(t, IsValidSHAPattern("abc000"))
16+
assert.True(t, IsValidSHAPattern("9023902390239023902390239023902390239023"))
17+
assert.False(t, IsValidSHAPattern("90239023902390239023902390239023902390239023"))
18+
assert.False(t, IsValidSHAPattern("abc"))
19+
assert.False(t, IsValidSHAPattern("123g"))
20+
assert.False(t, IsValidSHAPattern("some random text"))
21+
}

0 commit comments

Comments
 (0)