mirror of https://github.com/gogits/gogs.git
Unknwon
8 years ago
22 changed files with 337 additions and 185 deletions
@ -0,0 +1,127 @@
|
||||
// Copyright 2014 The Gogs 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 ( |
||||
"bufio" |
||||
"bytes" |
||||
"os" |
||||
"os/exec" |
||||
"path/filepath" |
||||
|
||||
"github.com/urfave/cli" |
||||
|
||||
"github.com/gogits/gogs/models" |
||||
) |
||||
|
||||
var ( |
||||
CmdHook = cli.Command{ |
||||
Name: "hook", |
||||
Usage: "Delegate commands to corresponding Git hooks", |
||||
Description: "All sub-commands should only be called by Git", |
||||
Flags: []cli.Flag{ |
||||
stringFlag("config, c", "custom/conf/app.ini", "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, |
||||
} |
||||
) |
||||
|
||||
func runHookPreReceive(c *cli.Context) error { |
||||
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { |
||||
return nil |
||||
} |
||||
setup(c, "hooks/pre-receive.log") |
||||
|
||||
buf := bytes.NewBuffer(nil) |
||||
scanner := bufio.NewScanner(os.Stdin) |
||||
for scanner.Scan() { |
||||
buf.Write(scanner.Bytes()) |
||||
buf.WriteByte('\n') |
||||
} |
||||
|
||||
customHooksPath := os.Getenv(_ENV_REPO_CUSTOM_HOOKS_PATH) |
||||
hookCmd := exec.Command(filepath.Join(customHooksPath, "pre-receive")) |
||||
hookCmd.Stdout = os.Stdout |
||||
hookCmd.Stdin = buf |
||||
hookCmd.Stderr = os.Stderr |
||||
if err := hookCmd.Run(); err != nil { |
||||
fail("Internal error", "Fail to execute custom pre-receive hook: %v", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func runHookUpdate(c *cli.Context) error { |
||||
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { |
||||
return nil |
||||
} |
||||
setup(c, "hooks/update.log") |
||||
|
||||
args := c.Args() |
||||
if len(args) != 3 { |
||||
fail("Arguments received are not equal to three", "Arguments received are not equal to three") |
||||
} else if len(args[0]) == 0 { |
||||
fail("First argument 'refName' is empty", "First argument 'refName' is empty") |
||||
} |
||||
|
||||
uuid := os.Getenv(_ENV_UPDATE_TASK_UUID) |
||||
if err := models.AddUpdateTask(&models.UpdateTask{ |
||||
UUID: uuid, |
||||
RefName: args[0], |
||||
OldCommitID: args[1], |
||||
NewCommitID: args[2], |
||||
}); err != nil { |
||||
fail("Internal error", "Fail to add update task '%s': %v", uuid, err) |
||||
} |
||||
|
||||
customHooksPath := os.Getenv(_ENV_REPO_CUSTOM_HOOKS_PATH) |
||||
hookCmd := exec.Command(filepath.Join(customHooksPath, "update"), args...) |
||||
hookCmd.Stdout = os.Stdout |
||||
hookCmd.Stdin = os.Stdin |
||||
hookCmd.Stderr = os.Stderr |
||||
if err := hookCmd.Run(); err != nil { |
||||
fail("Internal error", "Fail to execute custom pre-receive hook: %v", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func runHookPostReceive(c *cli.Context) error { |
||||
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { |
||||
return nil |
||||
} |
||||
setup(c, "hooks/post-receive.log") |
||||
|
||||
customHooksPath := os.Getenv(_ENV_REPO_CUSTOM_HOOKS_PATH) |
||||
hookCmd := exec.Command(filepath.Join(customHooksPath, "post-receive")) |
||||
hookCmd.Stdout = os.Stdout |
||||
hookCmd.Stdin = os.Stdin |
||||
hookCmd.Stderr = os.Stderr |
||||
if err := hookCmd.Run(); err != nil { |
||||
fail("Internal error", "Fail to execute custom post-receive hook: %v", err) |
||||
} |
||||
return nil |
||||
} |
@ -1,58 +0,0 @@
|
||||
// Copyright 2014 The Gogs 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 ( |
||||
"os" |
||||
|
||||
"github.com/urfave/cli" |
||||
log "gopkg.in/clog.v1" |
||||
|
||||
"github.com/gogits/gogs/models" |
||||
"github.com/gogits/gogs/modules/setting" |
||||
) |
||||
|
||||
var CmdUpdate = cli.Command{ |
||||
Name: "update", |
||||
Usage: "This command should only be called by Git hook", |
||||
Description: `Update get pushed info and insert into database`, |
||||
Action: runUpdate, |
||||
Flags: []cli.Flag{ |
||||
stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"), |
||||
}, |
||||
} |
||||
|
||||
func runUpdate(c *cli.Context) error { |
||||
if c.IsSet("config") { |
||||
setting.CustomConf = c.String("config") |
||||
} |
||||
|
||||
setup("update.log") |
||||
|
||||
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { |
||||
log.Trace("SSH_ORIGINAL_COMMAND is empty") |
||||
return nil |
||||
} |
||||
|
||||
args := c.Args() |
||||
if len(args) != 3 { |
||||
log.Fatal(2, "Arguments received are not equal to three") |
||||
} else if len(args[0]) == 0 { |
||||
log.Fatal(2, "First argument 'refName' is empty, shouldn't use") |
||||
} |
||||
|
||||
task := models.UpdateTask{ |
||||
UUID: os.Getenv("uuid"), |
||||
RefName: args[0], |
||||
OldCommitID: args[1], |
||||
NewCommitID: args[2], |
||||
} |
||||
|
||||
if err := models.AddUpdateTask(&task); err != nil { |
||||
log.Fatal(2, "AddUpdateTask: %v", err) |
||||
} |
||||
|
||||
return nil |
||||
} |
@ -0,0 +1,82 @@
|
||||
// Copyright 2017 The Gogs 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 migrations |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io/ioutil" |
||||
"os" |
||||
"path/filepath" |
||||
"strings" |
||||
|
||||
"github.com/Unknwon/com" |
||||
"github.com/go-xorm/xorm" |
||||
|
||||
"github.com/gogits/gogs/modules/setting" |
||||
) |
||||
|
||||
func generateAndMigrateGitHooks(x *xorm.Engine) (err error) { |
||||
type Repository struct { |
||||
ID int64 |
||||
OwnerID int64 |
||||
Name string |
||||
} |
||||
type User struct { |
||||
ID int64 |
||||
Name string |
||||
} |
||||
var ( |
||||
hookNames = []string{"pre-receive", "update", "post-receive"} |
||||
hookTpls = []string{ |
||||
fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' pre-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf), |
||||
fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' update $1 $2 $3\n", setting.ScriptType, setting.AppPath, setting.CustomConf), |
||||
fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' post-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf), |
||||
} |
||||
) |
||||
|
||||
// Cleanup old update.log files.
|
||||
filepath.Walk(setting.LogRootPath, func(path string, info os.FileInfo, err error) error { |
||||
if !info.IsDir() && strings.HasPrefix(filepath.Base(path), "update.log") { |
||||
os.Remove(path) |
||||
} |
||||
return nil |
||||
}) |
||||
|
||||
return x.Where("id > 0").Iterate(new(Repository), |
||||
func(idx int, bean interface{}) error { |
||||
repo := bean.(*Repository) |
||||
user := new(User) |
||||
has, err := x.Where("id = ?", repo.OwnerID).Get(user) |
||||
if err != nil { |
||||
return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err) |
||||
} else if !has { |
||||
return nil |
||||
} |
||||
|
||||
repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git" |
||||
hookDir := filepath.Join(repoPath, "hooks") |
||||
customHookDir := filepath.Join(repoPath, "custom_hooks") |
||||
|
||||
for i, hookName := range hookNames { |
||||
oldHookPath := filepath.Join(hookDir, hookName) |
||||
newHookPath := filepath.Join(customHookDir, hookName) |
||||
|
||||
// Gogs didn't allow user to set custom update hook thus no migration for it.
|
||||
// In case user runs this migration multiple times, and custom hook exists,
|
||||
// we assume it's been migrated already.
|
||||
if hookName != "update" && com.IsFile(oldHookPath) && !com.IsExist(newHookPath) { |
||||
os.MkdirAll(customHookDir, os.ModePerm) |
||||
if err = os.Rename(oldHookPath, newHookPath); err != nil { |
||||
return fmt.Errorf("move hook file to custom directory '%s' -> '%s': %v", oldHookPath, newHookPath, err) |
||||
} |
||||
} |
||||
|
||||
if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), 0777); err != nil { |
||||
return fmt.Errorf("write hook file '%s': %v", oldHookPath, err) |
||||
} |
||||
} |
||||
return nil |
||||
}) |
||||
} |
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue