Browse Source

finished serve command

for-closed-social
Lunny Xiao 10 years ago
parent
commit
3b8657d917
2 changed files with 20 additions and 22 deletions
  1. +5
    -3
      models/models.go
  2. +15
    -19
      serve.go

+ 5
- 3
models/models.go View File

@ -13,7 +13,6 @@ import (
"github.com/lunny/xorm" "github.com/lunny/xorm"
"github.com/gogits/gogs/utils" "github.com/gogits/gogs/utils"
"github.com/gogits/gogs/utils/log"
) )
var ( var (
@ -72,11 +71,14 @@ func setEngine() {
os.Exit(2) os.Exit(2)
} }
//TODO: for serv command, MUST remove the output to os.stdout, so
// use log file to instead print to stdout
//x.ShowDebug = true //x.ShowDebug = true
orm.ShowErr = true
//orm.ShowErr = true
//x.ShowSQL = true //x.ShowSQL = true
log.Trace("Initialized database -> %s", dbName)
//log.Trace("Initialized database -> %s", dbName)
RepoRootPath = utils.Cfg.MustValue("repository", "ROOT") RepoRootPath = utils.Cfg.MustValue("repository", "ROOT")
} }

+ 15
- 19
serve.go View File

@ -9,13 +9,13 @@ import (
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/utils/log"
) )
var ( var (
COMMANDS_READONLY = map[string]int{ COMMANDS_READONLY = map[string]int{
"git-upload-pack": models.AU_WRITABLE,
"git upload-pack": models.AU_WRITABLE,
"git-upload-pack": models.AU_WRITABLE,
"git upload-pack": models.AU_WRITABLE,
"git-upload-archive": models.AU_WRITABLE,
} }
COMMANDS_WRITE = map[string]int{ COMMANDS_WRITE = map[string]int{
@ -26,9 +26,9 @@ var (
var CmdServ = cli.Command{ var CmdServ = cli.Command{
Name: "serv", Name: "serv",
Usage: "just run",
Usage: "This command just should be called by ssh shell",
Description: ` Description: `
gogs serv`,
gogs serv provide access auth for repositories`,
Action: runServ, Action: runServ,
Flags: []cli.Flag{ Flags: []cli.Flag{
//cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"}, //cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"},
@ -61,14 +61,13 @@ func runServ(*cli.Context) {
cmd := os.Getenv("SSH_ORIGINAL_COMMAND") cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
if cmd == "" { if cmd == "" {
fmt.Printf("Hi %s! You've successfully authenticated, but Gogits does not provide shell access.\n", user.Name)
println("Hi %s! You've successfully authenticated, but Gogits does not provide shell access.\n", user.Name)
return return
} }
//println(cmd)
verb, args := parseCmd(cmd) verb, args := parseCmd(cmd)
rr := strings.SplitN(strings.Trim(args, "'"), "/", 2)
rRepo := strings.Trim(args, "'")
rr := strings.SplitN(rRepo, "/", 2)
if len(rr) != 2 { if len(rr) != 2 {
println("Unavilable repository", args) println("Unavilable repository", args)
return return
@ -80,13 +79,11 @@ func runServ(*cli.Context) {
isWrite := In(verb, COMMANDS_WRITE) isWrite := In(verb, COMMANDS_WRITE)
isRead := In(verb, COMMANDS_READONLY) isRead := In(verb, COMMANDS_READONLY)
//println("repoPath:", models.RepoPath(user.Name, repoName))
switch { switch {
case isWrite: case isWrite:
has, err := models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb]) has, err := models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb])
if err != nil { if err != nil {
fmt.Println("Inernel error:", err)
println("Inernel error:", err)
return return
} }
if !has { if !has {
@ -96,13 +93,13 @@ func runServ(*cli.Context) {
case isRead: case isRead:
has, err := models.HasAccess(user.Name, repoName, COMMANDS_READONLY[verb]) has, err := models.HasAccess(user.Name, repoName, COMMANDS_READONLY[verb])
if err != nil { if err != nil {
fmt.Println("Inernel error")
println("Inernel error")
return return
} }
if !has { if !has {
has, err = models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb]) has, err = models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb])
if err != nil { if err != nil {
fmt.Println("Inernel error")
println("Inernel error")
return return
} }
} }
@ -134,16 +131,15 @@ func runServ(*cli.Context) {
} }
} }
fullPath := models.RepoPath(user.Name, repoName)
newcmd := fmt.Sprintf("%s '%s'", verb, fullPath)
//println(newcmd)
gitcmd := exec.Command("git", "shell", "-c", newcmd)
gitcmd := exec.Command(verb, rRepo)
gitcmd.Dir = models.RepoRootPath
gitcmd.Stdout = os.Stdout gitcmd.Stdout = os.Stdout
gitcmd.Stdin = os.Stdin
gitcmd.Stderr = os.Stderr gitcmd.Stderr = os.Stderr
err = gitcmd.Run() err = gitcmd.Run()
if err != nil { if err != nil {
log.Error("execute command error: %s", err)
println("execute command error:", err.Error())
} }
} }

Loading…
Cancel
Save