From 3b8657d9177ad2f3dca5ecf0e1f8f62c20e776f7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 25 Feb 2014 14:01:52 +0800 Subject: [PATCH] finished serve command --- models/models.go | 8 +++++--- serve.go | 34 +++++++++++++++------------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/models/models.go b/models/models.go index 0f8fcac1a8..20f1e02311 100644 --- a/models/models.go +++ b/models/models.go @@ -13,7 +13,6 @@ import ( "github.com/lunny/xorm" "github.com/gogits/gogs/utils" - "github.com/gogits/gogs/utils/log" ) var ( @@ -72,11 +71,14 @@ func setEngine() { 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 - orm.ShowErr = true + //orm.ShowErr = true //x.ShowSQL = true - log.Trace("Initialized database -> %s", dbName) + //log.Trace("Initialized database -> %s", dbName) RepoRootPath = utils.Cfg.MustValue("repository", "ROOT") } diff --git a/serve.go b/serve.go index b0e258e3f4..b33b05386d 100644 --- a/serve.go +++ b/serve.go @@ -9,13 +9,13 @@ import ( "github.com/codegangsta/cli" "github.com/gogits/gogs/models" - "github.com/gogits/gogs/utils/log" ) var ( 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{ @@ -26,9 +26,9 @@ var ( var CmdServ = cli.Command{ Name: "serv", - Usage: "just run", + Usage: "This command just should be called by ssh shell", Description: ` -gogs serv`, +gogs serv provide access auth for repositories`, Action: runServ, Flags: []cli.Flag{ //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") 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 } - //println(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 { println("Unavilable repository", args) return @@ -80,13 +79,11 @@ func runServ(*cli.Context) { isWrite := In(verb, COMMANDS_WRITE) isRead := In(verb, COMMANDS_READONLY) - //println("repoPath:", models.RepoPath(user.Name, repoName)) - switch { case isWrite: has, err := models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb]) if err != nil { - fmt.Println("Inernel error:", err) + println("Inernel error:", err) return } if !has { @@ -96,13 +93,13 @@ func runServ(*cli.Context) { case isRead: has, err := models.HasAccess(user.Name, repoName, COMMANDS_READONLY[verb]) if err != nil { - fmt.Println("Inernel error") + println("Inernel error") return } if !has { has, err = models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb]) if err != nil { - fmt.Println("Inernel error") + println("Inernel error") 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.Stdin = os.Stdin gitcmd.Stderr = os.Stderr err = gitcmd.Run() if err != nil { - log.Error("execute command error: %s", err) + println("execute command error:", err.Error()) } }