Skip to content

Commit a3813a0

Browse files
committed
added back unmerged update command on go-gitea#1006
1 parent 0e6b9ea commit a3813a0

File tree

2 files changed

+37
-92
lines changed

2 files changed

+37
-92
lines changed

cmd/hook.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ package cmd
77
import (
88
"fmt"
99
"os"
10+
"strconv"
11+
"strings"
1012

13+
"code.gitea.io/git"
1114
"code.gitea.io/gitea/models"
15+
"code.gitea.io/gitea/modules/log"
16+
"code.gitea.io/gitea/modules/setting"
1217

1318
"github.com/urfave/cli"
1419
)
@@ -65,29 +70,52 @@ func runHookPreReceive(c *cli.Context) error {
6570
}
6671

6772
func runHookUpdate(c *cli.Context) error {
68-
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
69-
return nil
73+
if c.IsSet("config") {
74+
setting.CustomConf = c.String("config")
7075
}
7176

7277
if err := setup("hooks/update.log"); err != nil {
7378
fail("Hook update init failed", fmt.Sprintf("setup: %v", err))
7479
}
7580

81+
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
82+
log.GitLogger.Trace("SSH_ORIGINAL_COMMAND is empty")
83+
return nil
84+
}
85+
7686
args := c.Args()
7787
if len(args) != 3 {
78-
fail("Arguments received are not equal to three", "Arguments received are not equal to three")
88+
log.GitLogger.Fatal(2, "Arguments received are not equal to three")
7989
} else if len(args[0]) == 0 {
80-
fail("First argument 'refName' is empty", "First argument 'refName' is empty")
90+
log.GitLogger.Fatal(2, "First argument 'refName' is empty, shouldn't use")
91+
}
92+
93+
// protected branch check
94+
branchName := strings.TrimPrefix(args[0], git.BranchPrefix)
95+
repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
96+
log.GitLogger.Trace("pushing to %d %v", repoID, branchName)
97+
accessMode := models.ParseAccessMode(os.Getenv(models.ProtectedBranchAccessMode))
98+
// skip admin or owner AccessMode
99+
if accessMode == models.AccessModeWrite {
100+
protectBranch, err := models.GetProtectedBranchBy(repoID, branchName)
101+
if err != nil {
102+
log.GitLogger.Fatal(2, "retrieve protected branches information failed")
103+
}
104+
105+
if protectBranch != nil {
106+
log.GitLogger.Fatal(2, "protected branches can not be pushed to")
107+
}
81108
}
82109

83-
uuid := os.Getenv(envUpdateTaskUUID)
84-
if err := models.AddUpdateTask(&models.UpdateTask{
85-
UUID: uuid,
110+
task := models.UpdateTask{
111+
UUID: os.Getenv(envUpdateTaskUUID),
86112
RefName: args[0],
87113
OldCommitID: args[1],
88114
NewCommitID: args[2],
89-
}); err != nil {
90-
fail("Internal error", "Fail to add update task '%s': %v", uuid, err)
115+
}
116+
117+
if err := models.AddUpdateTask(&task); err != nil {
118+
log.GitLogger.Fatal(2, "AddUpdateTask: %v", err)
91119
}
92120

93121
return nil

cmd/update.go

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)