Skip to content

Commit 8268649

Browse files
committed
fix
1 parent 900ac62 commit 8268649

File tree

6 files changed

+81
-35
lines changed

6 files changed

+81
-35
lines changed

cmd/hook.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,9 @@ Gitea or set your environment appropriately.`, "")
591591
// S: ... ...
592592
// S: flush-pkt
593593
hookOptions := private.HookOptions{
594-
UserName: pusherName,
595-
UserID: pusherID,
594+
UserName: pusherName,
595+
UserID: pusherID,
596+
GitPushOptions: make(map[string]string),
596597
}
597598
hookOptions.OldCommitIDs = make([]string, 0, hookBatchSize)
598599
hookOptions.NewCommitIDs = make([]string, 0, hookBatchSize)
@@ -617,8 +618,6 @@ Gitea or set your environment appropriately.`, "")
617618
hookOptions.RefFullNames = append(hookOptions.RefFullNames, git.RefName(t[2]))
618619
}
619620

620-
hookOptions.GitPushOptions = make(map[string]string)
621-
622621
if hasPushOptions {
623622
for {
624623
rs, err = readPktLine(ctx, reader, pktLineTypeUnknow)
@@ -629,11 +628,7 @@ Gitea or set your environment appropriately.`, "")
629628
if rs.Type == pktLineTypeFlush {
630629
break
631630
}
632-
633-
kv := strings.SplitN(string(rs.Data), "=", 2)
634-
if len(kv) == 2 {
635-
hookOptions.GitPushOptions[kv[0]] = kv[1]
636-
}
631+
hookOptions.GitPushOptions.AddFromKeyValue(string(rs.Data))
637632
}
638633
}
639634

modules/private/hook.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import (
77
"context"
88
"fmt"
99
"net/url"
10-
"strconv"
1110
"time"
1211

1312
"code.gitea.io/gitea/modules/git"
14-
"code.gitea.io/gitea/modules/optional"
1513
"code.gitea.io/gitea/modules/repository"
1614
"code.gitea.io/gitea/modules/setting"
1715
)
@@ -24,25 +22,6 @@ const (
2422
GitPushOptionCount = "GIT_PUSH_OPTION_COUNT"
2523
)
2624

27-
// GitPushOptions is a wrapper around a map[string]string
28-
type GitPushOptions map[string]string
29-
30-
// GitPushOptions keys
31-
const (
32-
GitPushOptionRepoPrivate = "repo.private"
33-
GitPushOptionRepoTemplate = "repo.template"
34-
)
35-
36-
// Bool checks for a key in the map and parses as a boolean
37-
func (g GitPushOptions) Bool(key string) optional.Option[bool] {
38-
if val, ok := g[key]; ok {
39-
if b, err := strconv.ParseBool(val); err == nil {
40-
return optional.Some(b)
41-
}
42-
}
43-
return optional.None[bool]()
44-
}
45-
4625
// HookOptions represents the options for the Hook calls
4726
type HookOptions struct {
4827
OldCommitIDs []string

modules/private/pushoptions.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package private
5+
6+
import (
7+
"strconv"
8+
"strings"
9+
10+
"code.gitea.io/gitea/modules/optional"
11+
)
12+
13+
// GitPushOptions is a wrapper around a map[string]string
14+
type GitPushOptions map[string]string
15+
16+
// GitPushOptions keys
17+
const (
18+
GitPushOptionRepoPrivate = "repo.private"
19+
GitPushOptionRepoTemplate = "repo.template"
20+
GitPushOptionForcePush = "force-push"
21+
)
22+
23+
// Bool checks for a key in the map and parses as a boolean
24+
// An option without value is considered true, eg: "-o force-push" or "-o repo.private"
25+
func (g GitPushOptions) Bool(key string) optional.Option[bool] {
26+
if val, ok := g[key]; ok {
27+
if val == "" {
28+
return optional.Some(true)
29+
}
30+
if b, err := strconv.ParseBool(val); err == nil {
31+
return optional.Some(b)
32+
}
33+
}
34+
return optional.None[bool]()
35+
}
36+
37+
// AddFromKeyValue adds a key value pair to the map by "key=value" format or "key" for empty value
38+
func (g GitPushOptions) AddFromKeyValue(line string) {
39+
kv := strings.SplitN(line, "=", 2)
40+
if len(kv) == 2 {
41+
g[kv[0]] = kv[1]
42+
} else {
43+
g[kv[0]] = ""
44+
}
45+
}

modules/private/pushoptions_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package private
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestGitPushOptions(t *testing.T) {
13+
o := GitPushOptions{}
14+
15+
v := o.Bool("no-such")
16+
assert.False(t, false, v.Has())
17+
18+
o.AddFromKeyValue("opt1=val1")
19+
o.AddFromKeyValue("opt2=false")
20+
o.AddFromKeyValue("opt3=true")
21+
o.AddFromKeyValue("opt4")
22+
23+
assert.Equal(t, "val1", o["opt1"])
24+
assert.True(t, o.Bool("opt2").Has())
25+
assert.False(t, false, o.Bool("opt2").Value())
26+
assert.True(t, true, o.Bool("opt3").Value())
27+
assert.True(t, true, o.Bool("opt4").Value())
28+
}

routers/private/hook_post_receive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
208208
return
209209
}
210210

211-
cols := make([]string, 0, len(opts.GitPushOptions))
211+
cols := make([]string, 0, 2)
212212

213213
if isPrivate.Has() {
214214
repo.IsPrivate = isPrivate.Value()

services/agit/agit.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99
"os"
10-
"strconv"
1110
"strings"
1211

1312
issues_model "code.gitea.io/gitea/models/issues"
@@ -24,10 +23,10 @@ import (
2423
// ProcReceive handle proc receive work
2524
func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
2625
results := make([]private.HookProcReceiveRefResult, 0, len(opts.OldCommitIDs))
26+
forcePush := opts.GitPushOptions.Bool(private.GitPushOptionForcePush)
2727
topicBranch := opts.GitPushOptions["topic"]
28-
forcePush, _ := strconv.ParseBool(opts.GitPushOptions["force-push"])
2928
title := strings.TrimSpace(opts.GitPushOptions["title"])
30-
description := strings.TrimSpace(opts.GitPushOptions["description"]) // TODO: Add more options?
29+
description := strings.TrimSpace(opts.GitPushOptions["description"])
3130
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
3231
userName := strings.ToLower(opts.UserName)
3332

@@ -178,7 +177,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
178177
continue
179178
}
180179

181-
if !forcePush {
180+
if !forcePush.Value() {
182181
output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1").
183182
AddDynamicArguments(oldCommitID, "^"+opts.NewCommitIDs[i]).
184183
RunStdString(&git.RunOpts{Dir: repo.RepoPath(), Env: os.Environ()})

0 commit comments

Comments
 (0)