From efdaf6ee1536f043d9e242dc16a096c99ec1bfda Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 22 Mar 2014 00:48:26 +0800 Subject: [PATCH] add http protocol clone support --- models/repo.go | 11 +++++++++++ models/user.go | 6 ++---- routers/repo/single.go | 25 +++++++++++++++++++++++++ web.go | 2 ++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/models/repo.go b/models/repo.go index 4b6dedaf90..cf1e1df5c4 100644 --- a/models/repo.go +++ b/models/repo.go @@ -257,6 +257,17 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep return err } + // hook/post-update + pu2, err := os.OpenFile(filepath.Join(repoPath, "hooks", "post-receive"), os.O_CREATE|os.O_WRONLY, 0777) + if err != nil { + return err + } + defer pu2.Close() + // TODO: Windows .bat + if _, err = pu2.WriteString("#!/usr/bin/env bash\ngit update-server-info\n"); err != nil { + return err + } + // Initialize repository according to user's choice. fileName := map[string]string{} if initReadme { diff --git a/models/user.go b/models/user.go index 76cf2d20ce..69608ec277 100644 --- a/models/user.go +++ b/models/user.go @@ -231,10 +231,8 @@ func UserPath(userName string) string { func GetUserByKeyId(keyId int64) (*User, error) { user := new(User) - rawSql := "SELECT a.* FROM user AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?" - if base.Cfg.MustValue("database", "DB_TYPE") == "postgres" { - rawSql = "SELECT a.* FROM \"user\" AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?" - } + rawSql := "SELECT a.* FROM `user` AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?" + has, err := orm.Sql(rawSql, keyId).Get(user) if err != nil { return nil, err diff --git a/routers/repo/single.go b/routers/repo/single.go index c10d30a7d6..064150a234 100644 --- a/routers/repo/single.go +++ b/routers/repo/single.go @@ -5,11 +5,13 @@ package repo import ( + "path" "strings" "github.com/codegangsta/martini" "github.com/gogits/git" + "github.com/gogits/webdav" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" @@ -181,6 +183,29 @@ func Single(ctx *middleware.Context, params martini.Params) { ctx.HTML(200, "repo/single", ctx.Data) } +func Http(ctx *middleware.Context, params martini.Params) { + /*if !ctx.Repo.IsValid { + return + }*/ + + // TODO: access check + + username := params["username"] + reponame := params["reponame"] + if strings.HasSuffix(reponame, ".git") { + reponame = reponame[:len(reponame)-4] + } + + prefix := path.Join("/", username, params["reponame"]) + server := &webdav.Server{ + Fs: webdav.Dir(models.RepoPath(username, reponame)), + TrimPrefix: prefix, + Listings: true, + } + + server.ServeHTTP(ctx.ResponseWriter, ctx.Req) +} + func Setting(ctx *middleware.Context, params martini.Params) { if !ctx.Repo.IsOwner { ctx.Error(404) diff --git a/web.go b/web.go index ceb193e6fd..f083b5508c 100644 --- a/web.go +++ b/web.go @@ -116,6 +116,8 @@ func runWeb(*cli.Context) { m.Get("/:username/:reponame", ignSignIn, middleware.RepoAssignment(true), repo.Single) + m.Any("/:username/:reponame/**", ignSignIn, repo.Http) + if martini.Env == martini.Dev { m.Get("/template/**", dev.TemplatePreview) }