|
@ -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) |
|
|
|
|
|
isRead := In(verb, COMMANDS_READONLY) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// access check
|
|
|
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 { |
|
|
|
|
|
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 { |
|
|
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)) |
|
|
|
|
|
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() |
|
|
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()) |
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if !strings.HasPrefix(cmd, "git-receive-pack") { |
|
|
|
|
|
|
|
|
if isRead { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// update
|
|
|
|
|
|
//w, _ := os.Create("serve.log")
|
|
|
|
|
|
//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) |
|
|