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

161 lines
3.6 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 (
"container/list"
"os"
"os/exec"
"path"
"strconv"
"strings"
"github.com/codegangsta/cli"
qlog "github.com/qiniu/log"
"github.com/gogits/git"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
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{},
}
func newUpdateLogger(execDir string) {
logPath := execDir + "/log/update.log"
os.MkdirAll(path.Dir(logPath), os.ModePerm)
f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm)
if err != nil {
qlog.Fatal(err)
}
qlog.SetOutput(f)
qlog.Info("Start logging update...")
}
11 years ago
// for command: ./gogs update
11 years ago
func runUpdate(c *cli.Context) {
execDir, _ := base.ExecDir()
newUpdateLogger(execDir)
11 years ago
base.NewConfigContext()
models.LoadModelsConfig()
if models.UseSQLite3 {
os.Chdir(execDir)
}
models.SetEngine()
11 years ago
args := c.Args()
if len(args) != 3 {
qlog.Fatal("received less 3 parameters")
11 years ago
}
11 years ago
refName := args[0]
11 years ago
if refName == "" {
qlog.Fatal("refName is empty, shouldn't use")
11 years ago
}
11 years ago
oldCommitId := args[1]
newCommitId := args[2]
11 years ago
isNew := strings.HasPrefix(oldCommitId, "0000000")
if isNew &&
strings.HasPrefix(newCommitId, "0000000") {
qlog.Fatal("old rev and new rev both 000000")
11 years ago
}
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
f := models.RepoPath(userName, repoName)
gitUpdate := exec.Command("git", "update-server-info")
gitUpdate.Dir = f
gitUpdate.Run()
11 years ago
repo, err := git.OpenRepository(f)
if err != nil {
qlog.Fatalf("runUpdate.Open repoId: %v", err)
11 years ago
}
11 years ago
newOid, err := git.NewOidFromString(newCommitId)
if err != nil {
qlog.Fatalf("runUpdate.Ref repoId: %v", err)
11 years ago
}
newCommit, err := repo.LookupCommit(newOid)
if err != nil {
qlog.Fatalf("runUpdate.Ref repoId: %v", err)
11 years ago
}
var l *list.List
// if a new branch
11 years ago
if isNew {
l, err = repo.CommitsBefore(newCommit.Id())
if err != nil {
qlog.Fatalf("Find CommitsBefore erro:", err)
11 years ago
}
11 years ago
} else {
11 years ago
oldOid, err := git.NewOidFromString(oldCommitId)
if err != nil {
qlog.Fatalf("runUpdate.Ref repoId: %v", err)
11 years ago
}
oldCommit, err := repo.LookupCommit(oldOid)
if err != nil {
qlog.Fatalf("runUpdate.Ref repoId: %v", err)
11 years ago
}
l = repo.CommitsBetween(newCommit, oldCommit)
11 years ago
}
11 years ago
if err != nil {
qlog.Fatalf("runUpdate.Commit repoId: %v", err)
11 years ago
}
11 years ago
sUserId, err := strconv.Atoi(userId)
11 years ago
if err != nil {
qlog.Fatalf("runUpdate.Parse userId: %v", err)
11 years ago
}
11 years ago
repos, err := models.GetRepositoryByName(int64(sUserId), repoName)
11 years ago
if err != nil {
qlog.Fatalf("runUpdate.GetRepositoryByName userId: %v", err)
11 years ago
}
11 years ago
11 years ago
commits := make([]*base.PushCommit, 0)
11 years ago
var maxCommits = 3
11 years ago
var actEmail string
11 years ago
for e := l.Front(); e != nil; e = e.Next() {
commit := e.Value.(*git.Commit)
11 years ago
if actEmail == "" {
actEmail = commit.Committer.Email
}
11 years ago
commits = append(commits,
&base.PushCommit{commit.Id().String(),
commit.Message(),
commit.Author.Email,
commit.Author.Name})
11 years ago
if len(commits) >= maxCommits {
break
}
}
//commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
11 years ago
if err = models.CommitRepoAction(int64(sUserId), userName, actEmail,
11 years ago
repos.Id, repoName, git.BranchName(refName), &base.PushCommits{l.Len(), commits}); err != nil {
qlog.Fatalf("runUpdate.models.CommitRepoAction: %v", err)
11 years ago
}
11 years ago
}