release/v1.15
FuXiaoHei 2014-03-25 18:45:41 +08:00
commit 94ac5ebb9f
5 changed files with 109 additions and 57 deletions

25
.gitignore vendored
View File

@ -1,3 +1,5 @@
gogs gogs
*.exe *.exe
*.exe~ *.exe~
@ -10,3 +12,26 @@ data/
.idea/ .idea/
*.iml *.iml
public/img/avatar/ public/img/avatar/
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe

40
LICENSE
View File

@ -1,19 +1,27 @@
Copyright (c) 2011 Dmitriy Zaporozhets Copyright (c) 2014
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy Redistribution and use in source and binary forms, with or without
of this software and associated documentation files (the "Software"), to deal modification, are permitted provided that the following conditions are met:
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in * Redistributions of source code must retain the above copyright notice, this
all copies or substantial portions of the Software. list of conditions and the following disclaimer.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * Redistributions in binary form must reproduce the above copyright notice,
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, this list of conditions and the following disclaimer in the documentation
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE and/or other materials provided with the distribution.
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * Neither the name of the {organization} nor the names of its
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN contributors may be used to endorse or promote products derived from
THE SOFTWARE. this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -2,7 +2,7 @@
APP_NAME = Gogs: Go Git Service APP_NAME = Gogs: Go Git Service
APP_LOGO = img/favicon.png APP_LOGO = img/favicon.png
; !!MUST CHANGE TO YOUR USER NAME!! ; !!MUST CHANGE TO YOUR USER NAME!!
RUN_USER = lunny RUN_USER = skyblue
; Either "dev", "prod" or "test", default is "dev" ; Either "dev", "prod" or "test", default is "dev"
RUN_MODE = dev RUN_MODE = dev

View File

@ -251,8 +251,8 @@ var client = &http.Client{}
func (this *thunderTask) fetch() error { func (this *thunderTask) fetch() error {
req, _ := http.NewRequest("GET", this.Url, nil) req, _ := http.NewRequest("GET", this.Url, nil)
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/jpeg,image/png,*/*;q=0.8")
req.Header.Set("Accept-Encoding", "gzip,deflate,sdch") req.Header.Set("Accept-Encoding", "deflate,sdch")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8") req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
req.Header.Set("Cache-Control", "no-cache") req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36") req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36")

View File

@ -13,6 +13,7 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
@ -45,8 +46,10 @@ gogs serv provide access auth for repositories`,
} }
func init() { func init() {
level := "0"
os.MkdirAll("log", os.ModePerm) os.MkdirAll("log", os.ModePerm)
log.NewLogger(10000, "file", fmt.Sprintf(`{"filename":"%s"}`, "log/serv.log")) log.NewLogger(10000, "file", fmt.Sprintf(`{"level":%s,"filename":"%s"}`, level, "log/serv.log"))
log.Info("start logging...")
} }
func parseCmd(cmd string) (string, string) { func parseCmd(cmd string) (string, string) {
@ -109,25 +112,32 @@ func runServ(k *cli.Context) {
repoName = repoName[:len(repoName)-4] repoName = repoName[:len(repoName)-4]
} }
isWrite := In(verb, COMMANDS_WRITE)
isRead := In(verb, COMMANDS_READONLY)
repo, err := models.GetRepositoryByName(user.Id, repoName) repo, err := models.GetRepositoryByName(user.Id, repoName)
var isExist bool = true var isExist bool = true
if err != nil { if err != nil {
if err == models.ErrRepoNotExist { if err == models.ErrRepoNotExist {
isExist = false isExist = false
if isRead {
println("Repository", user.Name+"/"+repoName, "is not exist")
return
}
} else { } else {
println("Unavilable repository", err) println("Get repository error:", err)
log.Error(err.Error())
return return
} }
} }
isWrite := In(verb, COMMANDS_WRITE) // access check
isRead := In(verb, COMMANDS_READONLY)
switch { switch {
case isWrite: case isWrite:
has, err := models.HasAccess(user.Name, repoName, models.AU_WRITABLE) has, err := models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
if err != nil { if err != nil {
println("Inernel error:", err) println("Inernel error:", err)
log.Error(err.Error())
return return
} }
if !has { if !has {
@ -138,12 +148,14 @@ func runServ(k *cli.Context) {
has, err := models.HasAccess(user.Name, repoName, models.AU_READABLE) has, err := models.HasAccess(user.Name, repoName, models.AU_READABLE)
if err != nil { if err != nil {
println("Inernel error") println("Inernel error")
log.Error(err.Error())
return return
} }
if !has { if !has {
has, err = models.HasAccess(user.Name, repoName, models.AU_WRITABLE) has, err = models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
if err != nil { if err != nil {
println("Inernel error") println("Inernel error")
log.Error(err.Error())
return return
} }
} }
@ -156,28 +168,30 @@ func runServ(k *cli.Context) {
return return
} }
var rep *git.Repository
repoPath := models.RepoPath(user.Name, repoName)
if !isExist { if !isExist {
if isRead { if isWrite {
println("Repository", user.Name+"/"+repoName, "is not exist") _, err = models.CreateRepository(user, repoName, "", "", "", false, true)
return
} else if isWrite {
_, err := models.CreateRepository(user, repoName, "", "", "", false, true)
if err != nil { if err != nil {
println("Create repository failed") println("Create repository failed")
log.Error(err.Error())
return return
} }
} }
} }
rep, err := git.OpenRepository(models.RepoPath(user.Name, repoName)) rep, err = git.OpenRepository(repoPath)
if err != nil { if err != nil {
println(err.Error()) println("OpenRepository failed:", err.Error())
return log.Error(err.Error())
} return
}
refs, err := rep.AllReferencesMap() refs, err := rep.AllReferencesMap()
if err != nil { if err != nil {
println(err.Error()) println("Get All References failed:", err.Error())
log.Error(err.Error())
return return
} }
@ -194,17 +208,17 @@ func runServ(k *cli.Context) {
if err = gitcmd.Run(); err != nil { if err = gitcmd.Run(); err != nil {
println("execute command error:", err.Error()) println("execute command error:", err.Error())
} log.Error(err.Error())
if !strings.HasPrefix(cmd, "git-receive-pack") {
return return
} }
// update if isRead {
//w, _ := os.Create("serve.log") return
//defer w.Close() }
//log.SetOutput(w)
time.Sleep(time.Second)
// find push reference name
var t = "ok refs/heads/" var t = "ok refs/heads/"
var i int var i int
var refname string var refname string
@ -220,24 +234,31 @@ func runServ(k *cli.Context) {
refname = l[idx+len(t):] refname = l[idx+len(t):]
} }
} }
if refname == "" {
println("No find any reference name:", b.String())
return
}
var ref *git.Reference var ref *git.Reference
var ok bool var ok bool
var l *list.List var l *list.List
//log.Info("----", refname, "-----") //log.Info("----", refname, "-----")
if ref, ok = refs[refname]; !ok { if ref, ok = refs[refname]; !ok {
// for new branch
refs, err = rep.AllReferencesMap() refs, err = rep.AllReferencesMap()
if err != nil { if err != nil {
println(err.Error()) println("Get All References failed:", err.Error())
log.Error(err.Error())
return return
} }
if ref, ok = refs[refname]; !ok { if ref, ok = refs[refname]; !ok {
log.Trace("unknow reference name -", refname, "-", b.String()) log.Error("unknow reference name -", refname, "-", b.String())
return return
} }
l, err = ref.AllCommits() l, err = ref.AllCommits()
if err != nil { if err != nil {
println(err.Error()) println("Get All Commits failed:", err.Error())
log.Error(err.Error())
return return
} }
} else { } else {
@ -246,20 +267,23 @@ func runServ(k *cli.Context) {
//log.Info("00000", ref.Oid.String()) //log.Info("00000", ref.Oid.String())
last, err = ref.LastCommit() last, err = ref.LastCommit()
if err != nil { if err != nil {
println(err.Error()) println("Get last commit failed:", err.Error())
log.Error(err.Error())
return return
} }
ref2, err := rep.LookupReference(ref.Name) ref2, err := rep.LookupReference(ref.Name)
if err != nil { if err != nil {
println(err.Error()) println("look up reference failed:", err.Error())
log.Error(err.Error())
return return
} }
//log.Info("11111", ref2.Oid.String()) //log.Info("11111", ref2.Oid.String())
before, err := ref2.LastCommit() before, err := ref2.LastCommit()
if err != nil { if err != nil {
println(err.Error()) println("Get last commit failed:", err.Error())
log.Error(err.Error())
return return
} }
//log.Info("----", before.Id(), "-----", last.Id()) //log.Info("----", before.Id(), "-----", last.Id())
@ -280,13 +304,8 @@ func runServ(k *cli.Context) {
repo.Id, repoName, refname, &base.PushCommits{l.Len(), commits}); err != nil { repo.Id, repoName, refname, &base.PushCommits{l.Len(), commits}); err != nil {
log.Error("runUpdate.models.CommitRepoAction: %v", err, commits) log.Error("runUpdate.models.CommitRepoAction: %v", err, commits)
} else { } else {
//log.Info("refname", refname)
//log.Info("Listen: %v", cmd)
//fmt.Println("...", cmd)
//runUpdate(k)
c := exec.Command("git", "update-server-info") c := exec.Command("git", "update-server-info")
c.Dir = models.RepoPath(user.Name, repoName) c.Dir = repoPath
err := c.Run() err := c.Run()
if err != nil { if err != nil {
log.Error("update-server-info: %v", err) log.Error("update-server-info: %v", err)