2017-02-23 09:10:44 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-02-25 20:24:40 +05:30
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2017-02-23 09:10:44 +05:30
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-05-04 11:12:02 +05:30
|
|
|
"path/filepath"
|
2017-02-25 20:24:40 +05:30
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-02-23 09:10:44 +05:30
|
|
|
|
2017-02-25 20:24:40 +05:30
|
|
|
"code.gitea.io/git"
|
2017-02-23 09:10:44 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
2017-02-25 20:24:40 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-05-04 11:12:02 +05:30
|
|
|
"code.gitea.io/gitea/modules/private"
|
2017-02-25 20:24:40 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-02-23 09:10:44 +05:30
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// CmdHook represents the available hooks sub-command.
|
|
|
|
CmdHook = cli.Command{
|
|
|
|
Name: "hook",
|
|
|
|
Usage: "Delegate commands to corresponding Git hooks",
|
|
|
|
Description: "This should only be called by Git",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
subcmdHookPreReceive,
|
|
|
|
subcmdHookUpadte,
|
|
|
|
subcmdHookPostReceive,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
subcmdHookPreReceive = cli.Command{
|
|
|
|
Name: "pre-receive",
|
|
|
|
Usage: "Delegate pre-receive Git hook",
|
|
|
|
Description: "This command should only be called by Git",
|
|
|
|
Action: runHookPreReceive,
|
|
|
|
}
|
|
|
|
subcmdHookUpadte = cli.Command{
|
|
|
|
Name: "update",
|
|
|
|
Usage: "Delegate update Git hook",
|
|
|
|
Description: "This command should only be called by Git",
|
|
|
|
Action: runHookUpdate,
|
|
|
|
}
|
|
|
|
subcmdHookPostReceive = cli.Command{
|
|
|
|
Name: "post-receive",
|
|
|
|
Usage: "Delegate post-receive Git hook",
|
|
|
|
Description: "This command should only be called by Git",
|
|
|
|
Action: runHookPostReceive,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-04 11:12:02 +05:30
|
|
|
func hookSetup(logPath string) {
|
|
|
|
setting.NewContext()
|
|
|
|
log.NewGitLogger(filepath.Join(setting.LogRootPath, logPath))
|
|
|
|
models.LoadConfigs()
|
|
|
|
}
|
|
|
|
|
2017-02-23 09:10:44 +05:30
|
|
|
func runHookPreReceive(c *cli.Context) error {
|
|
|
|
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-25 20:24:40 +05:30
|
|
|
|
2017-03-01 20:31:03 +05:30
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
} else if c.GlobalIsSet("config") {
|
|
|
|
setting.CustomConf = c.GlobalString("config")
|
|
|
|
}
|
|
|
|
|
2017-05-04 11:12:02 +05:30
|
|
|
hookSetup("hooks/pre-receive.log")
|
2017-02-23 09:10:44 +05:30
|
|
|
|
2017-02-25 20:24:40 +05:30
|
|
|
// the environment setted on serv command
|
|
|
|
repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
|
|
|
|
isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
|
2017-09-14 13:46:22 +05:30
|
|
|
username := os.Getenv(models.EnvRepoUsername)
|
|
|
|
reponame := os.Getenv(models.EnvRepoName)
|
|
|
|
userIDStr := os.Getenv(models.EnvPusherID)
|
|
|
|
repoPath := models.RepoPath(username, reponame)
|
2017-02-25 20:24:40 +05:30
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
buf.Write(scanner.Bytes())
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
|
|
|
|
// TODO: support news feeds for wiki
|
|
|
|
if isWiki {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := bytes.Fields(scanner.Bytes())
|
|
|
|
if len(fields) != 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:46:22 +05:30
|
|
|
oldCommitID := string(fields[0])
|
2017-02-25 20:24:40 +05:30
|
|
|
newCommitID := string(fields[1])
|
|
|
|
refFullName := string(fields[2])
|
|
|
|
|
|
|
|
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
|
2017-05-04 11:12:02 +05:30
|
|
|
protectBranch, err := private.GetProtectedBranchBy(repoID, branchName)
|
2017-02-25 20:24:40 +05:30
|
|
|
if err != nil {
|
|
|
|
log.GitLogger.Fatal(2, "retrieve protected branches information failed")
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:46:22 +05:30
|
|
|
if protectBranch != nil && protectBranch.IsProtected() {
|
|
|
|
// detect force push
|
|
|
|
if git.EmptySHA != oldCommitID {
|
2017-10-29 01:06:20 +05:30
|
|
|
output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
|
2017-09-14 13:46:22 +05:30
|
|
|
if err != nil {
|
|
|
|
fail("Internal error", "Fail to detect force push: %v", err)
|
|
|
|
} else if len(output) > 0 {
|
|
|
|
fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check and deletion
|
|
|
|
if newCommitID == git.EmptySHA {
|
|
|
|
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
|
|
|
|
} else {
|
|
|
|
userID, _ := strconv.ParseInt(userIDStr, 10, 64)
|
|
|
|
canPush, err := private.CanUserPush(protectBranch.ID, userID)
|
|
|
|
if err != nil {
|
|
|
|
fail("Internal error", "Fail to detect user can push: %v", err)
|
|
|
|
} else if !canPush {
|
2017-05-04 11:12:02 +05:30
|
|
|
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
|
|
|
|
}
|
2017-03-01 20:31:03 +05:30
|
|
|
}
|
2017-02-25 20:24:40 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 09:10:44 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runHookUpdate(c *cli.Context) error {
|
|
|
|
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-01 20:31:03 +05:30
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
} else if c.GlobalIsSet("config") {
|
|
|
|
setting.CustomConf = c.GlobalString("config")
|
|
|
|
}
|
|
|
|
|
2017-05-04 11:12:02 +05:30
|
|
|
hookSetup("hooks/update.log")
|
2017-02-23 09:10:44 +05:30
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runHookPostReceive(c *cli.Context) error {
|
|
|
|
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-01 20:31:03 +05:30
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
} else if c.GlobalIsSet("config") {
|
|
|
|
setting.CustomConf = c.GlobalString("config")
|
|
|
|
}
|
|
|
|
|
2017-05-04 11:12:02 +05:30
|
|
|
hookSetup("hooks/post-receive.log")
|
2017-02-23 09:10:44 +05:30
|
|
|
|
2017-02-25 20:24:40 +05:30
|
|
|
// the environment setted on serv command
|
|
|
|
repoUser := os.Getenv(models.EnvRepoUsername)
|
|
|
|
isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
|
|
|
|
repoName := os.Getenv(models.EnvRepoName)
|
|
|
|
pusherID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
|
|
|
|
pusherName := os.Getenv(models.EnvPusherName)
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
buf.Write(scanner.Bytes())
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
|
|
|
|
// TODO: support news feeds for wiki
|
|
|
|
if isWiki {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := bytes.Fields(scanner.Bytes())
|
|
|
|
if len(fields) != 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
oldCommitID := string(fields[0])
|
|
|
|
newCommitID := string(fields[1])
|
|
|
|
refFullName := string(fields[2])
|
|
|
|
|
2017-05-04 11:12:02 +05:30
|
|
|
if err := private.PushUpdate(models.PushUpdateOptions{
|
2017-02-25 20:24:40 +05:30
|
|
|
RefFullName: refFullName,
|
|
|
|
OldCommitID: oldCommitID,
|
|
|
|
NewCommitID: newCommitID,
|
|
|
|
PusherID: pusherID,
|
|
|
|
PusherName: pusherName,
|
|
|
|
RepoUserName: repoUser,
|
|
|
|
RepoName: repoName,
|
|
|
|
}); err != nil {
|
|
|
|
log.GitLogger.Error(2, "Update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 09:10:44 +05:30
|
|
|
return nil
|
|
|
|
}
|