Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-24 18:56:11 +01:00
This commit is contained in:
FuXiaoHei 2014-03-25 18:45:41 +08:00
commit 94ac5ebb9f
5 changed files with 109 additions and 57 deletions

27
.gitignore vendored
View file

@ -1,3 +1,5 @@
gogs
*.exe
*.exe~
@ -9,4 +11,27 @@ data/
.vendor/
.idea/
*.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
of this software and associated documentation files (the "Software"), to deal
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:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the {organization} nor the names of its
contributors may be used to endorse or promote products derived from
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_LOGO = img/favicon.png
; !!MUST CHANGE TO YOUR USER NAME!!
RUN_USER = lunny
RUN_USER = skyblue
; Either "dev", "prod" or "test", default is "dev"
RUN_MODE = dev
@ -164,4 +164,4 @@ RECEIVERS =
[log.database]
LEVEL =
Driver =
CONN =
CONN =

View file

@ -251,8 +251,8 @@ var client = &http.Client{}
func (this *thunderTask) fetch() error {
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-Encoding", "gzip,deflate,sdch")
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", "deflate,sdch")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
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")

View file

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