2014-03-16 10:53:41 +04:00
// 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.
2014-03-16 08:18:34 +04:00
package main
2014-03-25 20:00:36 +04:00
import (
2014-03-26 17:22:08 +04:00
"container/list"
2014-03-31 17:26:15 +04:00
"fmt"
2014-03-26 17:22:08 +04:00
"os"
"os/exec"
2014-03-31 17:26:15 +04:00
"path"
2014-03-26 17:22:08 +04:00
"strconv"
"strings"
"github.com/codegangsta/cli"
"github.com/gogits/git"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
2014-03-31 17:26:15 +04:00
"github.com/gogits/gogs/modules/log"
//"github.com/qiniu/log"
2014-03-25 20:00:36 +04:00
)
2014-03-16 08:18:34 +04:00
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 { } ,
}
2014-03-31 17:26:15 +04:00
func newUpdateLogger ( execDir string ) {
level := "0"
logPath := execDir + "/log/update.log"
os . MkdirAll ( path . Dir ( logPath ) , os . ModePerm )
log . NewLogger ( 0 , "file" , fmt . Sprintf ( ` { "level":%s,"filename":"%s"} ` , level , logPath ) )
log . Trace ( "start logging..." )
}
2014-03-18 17:58:58 +04:00
// for command: ./gogs update
2014-03-25 20:00:36 +04:00
func runUpdate ( c * cli . Context ) {
2014-03-31 17:26:15 +04:00
execDir , _ := base . ExecDir ( )
newLogger ( execDir )
2014-03-26 09:38:16 +04:00
base . NewConfigContext ( )
models . LoadModelsConfig ( )
2014-03-31 00:01:50 +04:00
if models . UseSQLite3 {
os . Chdir ( execDir )
}
2014-03-28 15:26:22 +04:00
models . SetEngine ( )
2014-03-23 12:31:13 +04:00
2014-03-26 09:21:09 +04:00
args := c . Args ( )
if len ( args ) != 3 {
log . Error ( "received less 3 parameters" )
return
2014-03-25 20:05:09 +04:00
}
2014-03-26 09:21:09 +04:00
refName := args [ 0 ]
2014-03-28 06:48:36 +04:00
if refName == "" {
log . Error ( "refName is empty, shouldn't use" )
return
}
2014-03-26 09:21:09 +04:00
oldCommitId := args [ 1 ]
newCommitId := args [ 2 ]
2014-03-28 06:48:36 +04:00
isNew := strings . HasPrefix ( oldCommitId , "0000000" )
if isNew &&
strings . HasPrefix ( newCommitId , "0000000" ) {
log . Error ( "old rev and new rev both 000000" )
return
}
2014-03-25 20:16:13 +04:00
userName := os . Getenv ( "userName" )
2014-03-26 09:21:09 +04:00
userId := os . Getenv ( "userId" )
2014-03-25 20:16:13 +04:00
//repoId := os.Getenv("repoId")
2014-03-26 09:21:09 +04:00
repoName := os . Getenv ( "repoName" )
2014-03-16 08:18:34 +04:00
2014-03-26 09:21:09 +04:00
f := models . RepoPath ( userName , repoName )
gitUpdate := exec . Command ( "git" , "update-server-info" )
gitUpdate . Dir = f
gitUpdate . Run ( )
2014-03-16 08:18:34 +04:00
repo , err := git . OpenRepository ( f )
if err != nil {
2014-03-23 12:31:13 +04:00
log . Error ( "runUpdate.Open repoId: %v" , err )
2014-03-16 08:18:34 +04:00
return
}
2014-03-26 09:21:09 +04:00
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
2014-03-28 06:48:36 +04:00
if isNew {
l , err = repo . CommitsBefore ( newCommit . Id ( ) )
if err != nil {
log . Error ( "Find CommitsBefore erro:" , err )
return
}
2014-03-26 09:21:09 +04:00
} else {
2014-03-28 06:48:36 +04:00
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
}
l = repo . CommitsBetween ( newCommit , oldCommit )
2014-03-26 09:21:09 +04:00
}
2014-03-16 08:18:34 +04:00
if err != nil {
2014-03-23 12:31:13 +04:00
log . Error ( "runUpdate.Commit repoId: %v" , err )
2014-03-16 08:18:34 +04:00
return
}
2014-03-16 19:02:59 +04:00
2014-03-16 10:53:41 +04:00
sUserId , err := strconv . Atoi ( userId )
2014-03-16 08:18:34 +04:00
if err != nil {
2014-03-16 10:53:41 +04:00
log . Error ( "runUpdate.Parse userId: %v" , err )
return
}
2014-03-26 09:21:09 +04:00
repos , err := models . GetRepositoryByName ( int64 ( sUserId ) , repoName )
2014-03-16 10:53:41 +04:00
if err != nil {
2014-03-26 09:21:09 +04:00
log . Error ( "runUpdate.GetRepositoryByName userId: %v" , err )
2014-03-16 10:53:41 +04:00
return
}
2014-03-26 09:21:09 +04:00
2014-03-29 15:29:52 +04:00
commits := make ( [ ] * base . PushCommit , 0 )
2014-03-26 09:21:09 +04:00
var maxCommits = 3
2014-03-29 15:59:02 +04:00
var actEmail string
2014-03-26 09:21:09 +04:00
for e := l . Front ( ) ; e != nil ; e = e . Next ( ) {
commit := e . Value . ( * git . Commit )
2014-03-29 15:59:02 +04:00
if actEmail == "" {
actEmail = commit . Committer . Email
}
2014-03-29 15:29:52 +04:00
commits = append ( commits ,
& base . PushCommit { commit . Id ( ) . String ( ) ,
commit . Message ( ) ,
commit . Author . Email ,
commit . Author . Name } )
2014-03-26 09:21:09 +04:00
if len ( commits ) >= maxCommits {
break
}
}
//commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
2014-03-29 15:59:02 +04:00
if err = models . CommitRepoAction ( int64 ( sUserId ) , userName , actEmail ,
2014-03-26 09:45:55 +04:00
repos . Id , repoName , git . BranchName ( refName ) , & base . PushCommits { l . Len ( ) , commits } ) ; err != nil {
2014-03-16 10:53:41 +04:00
log . Error ( "runUpdate.models.CommitRepoAction: %v" , err )
2014-03-26 09:21:09 +04:00
}
2014-03-16 08:18:34 +04:00
}