You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gitea/update.go

147 lines
3.2 KiB

11 years ago
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
11 years ago
package main
11 years ago
import (
"os"
11 years ago
"os/exec"
"strings"
"strconv"
"container/list"
11 years ago
"github.com/codegangsta/cli"
11 years ago
//"github.com/gogits/gogs/modules/log"
11 years ago
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
11 years ago
"github.com/qiniu/log"
11 years ago
"github.com/gogits/git"
11 years ago
)
11 years ago
var CmdUpdate = cli.Command{
Name: "update",
Usage: "This command just should be called by ssh shell",
Description: `
gogs serv provide access auth for repositories`,
Action: runUpdate,
Flags: []cli.Flag{},
}
11 years ago
// for command: ./gogs update
11 years ago
func runUpdate(c *cli.Context) {
11 years ago
//level := "0"
//os.MkdirAll("log", os.ModePerm)
//log.NewLogger(10000, "file", fmt.Sprintf(`{"level":%s,"filename":"%s"}`, level, "log/serv.log"))
//log.Info("start update logging...")
11 years ago
w, _ := os.Create("update.log")
defer w.Close()
log.SetOutput(w)
11 years ago
args := c.Args()
if len(args) != 3 {
log.Error("received less 3 parameters")
return
11 years ago
}
11 years ago
refName := args[0]
oldCommitId := args[1]
newCommitId := args[2]
11 years ago
userName := os.Getenv("userName")
11 years ago
userId := os.Getenv("userId")
11 years ago
//repoId := os.Getenv("repoId")
11 years ago
repoName := os.Getenv("repoName")
11 years ago
11 years ago
log.Info("username", userName)
11 years ago
log.Info("repoName", repoName)
11 years ago
f := models.RepoPath(userName, repoName)
11 years ago
log.Debug("f", f)
11 years ago
gitUpdate := exec.Command("git", "update-server-info")
gitUpdate.Dir = f
gitUpdate.Run()
11 years ago
repo, err := git.OpenRepository(f)
if err != nil {
log.Error("runUpdate.Open repoId: %v", err)
11 years ago
return
}
11 years ago
ref, err := repo.LookupReference(refName)
11 years ago
if err != nil {
log.Error("runUpdate.Ref repoId: %v", err)
11 years ago
return
}
11 years ago
oldOid, err := git.NewOidFromString(oldCommitId)
if err != nil {
log.Error("runUpdate.Ref repoId: %v", err)
return
}
oldCommit, err := repo.LookupCommit(oldOid)
if err != nil {
log.Error("runUpdate.Ref repoId: %v", err)
return
}
newOid, err := git.NewOidFromString(newCommitId)
if err != nil {
log.Error("runUpdate.Ref repoId: %v", err)
return
}
newCommit, err := repo.LookupCommit(newOid)
if err != nil {
log.Error("runUpdate.Ref repoId: %v", err)
return
}
var l *list.List
// if a new branch
if strings.HasPrefix(oldCommitId, "0000000") {
l, err = ref.AllCommits()
} else {
l = ref.CommitsBetween(newCommit, oldCommit)
}
11 years ago
if err != nil {
log.Error("runUpdate.Commit repoId: %v", err)
11 years ago
return
}
11 years ago
sUserId, err := strconv.Atoi(userId)
11 years ago
if err != nil {
11 years ago
log.Error("runUpdate.Parse userId: %v", err)
return
}
11 years ago
repos, err := models.GetRepositoryByName(int64(sUserId), repoName)
11 years ago
if err != nil {
11 years ago
log.Error("runUpdate.GetRepositoryByName userId: %v", err)
11 years ago
return
}
11 years ago
commits := make([][]string, 0)
11 years ago
var maxCommits = 3
for e := l.Front(); e != nil; e = e.Next() {
commit := e.Value.(*git.Commit)
commits = append(commits, []string{commit.Id().String(), commit.Message()})
if len(commits) >= maxCommits {
break
}
}
//commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
11 years ago
if err = models.CommitRepoAction(int64(sUserId), userName,
11 years ago
repos.Id, repoName, refName, &base.PushCommits{l.Len(), commits}); err != nil {
11 years ago
log.Error("runUpdate.models.CommitRepoAction: %v", err)
11 years ago
}
11 years ago
}