@ -10,7 +10,7 @@ import (
"os/exec"
"os/exec"
"strings"
"strings"
"github.com/gogits/git-module"
git "github.com/gogits/git-module"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/log"
)
)
@ -65,94 +65,104 @@ func ListToPushCommits(l *list.List) *PushCommits {
return & PushCommits { l . Len ( ) , commits , "" , nil }
return & PushCommits { l . Len ( ) , commits , "" , nil }
}
}
func Update ( refName , oldCommitID , newCommitID , userName , repoUserName , repoName string , userID int64 ) error {
type PushUpdateOptions struct {
isNew := strings . HasPrefix ( oldCommitID , "0000000" )
RefName string
if isNew &&
OldCommitID string
strings . HasPrefix ( newCommitID , "0000000" ) {
NewCommitID string
return fmt . Errorf ( "old rev and new rev both 000000" )
PusherID int64
PusherName string
RepoUserName string
RepoName string
}
// PushUpdate must be called for any push actions in order to
// generates necessary push action history feeds.
func PushUpdate ( opts PushUpdateOptions ) ( err error ) {
isNewRef := strings . HasPrefix ( opts . OldCommitID , "0000000" )
isDelRef := strings . HasPrefix ( opts . NewCommitID , "0000000" )
if isNewRef && isDelRef {
return fmt . Errorf ( "Old and new revisions both start with 000000" )
}
}
f := RepoPath ( repoUserName , repoName )
repoPath := RepoPath ( opts . RepoUserName , opts . R epoName)
gitUpdate := exec . Command ( "git" , "update-server-info" )
gitUpdate := exec . Command ( "git" , "update-server-info" )
gitUpdate . Dir = f
gitUpdate . Dir = repoPath
gitUpdate . Run ( )
if err = gitUpdate . Run ( ) ; err != nil {
return fmt . Errorf ( "Fail to call 'git update-server-info': %v" , err )
}
isDel := strings . HasPrefix ( newCommitID , "0000000" )
if isDelRef {
if isDel {
log . GitLogger . Info ( "Reference '%s' has been deleted from '%s/%s' by %d" ,
log . GitLogger . Info ( "del rev" , refName , "from" , userName + "/" + repoName + ".git" , "by" , userID )
opts . RefName , opts . RepoUserName , opts . RepoName , opts . PusherName )
return nil
return nil
}
}
gitRepo , err := git . OpenRepository ( f )
gitRepo , err := git . OpenRepository ( repoPath )
if err != nil {
if err != nil {
return fmt . Errorf ( "runUpdate.Open repoId : %v" , err )
return fmt . Errorf ( "OpenRepository : %v" , err )
}
}
u ser, err := GetUserByName ( r epoUserName)
repoU ser, err := GetUserByName ( opts . R epoUserName)
if err != nil {
if err != nil {
return fmt . Errorf ( "runUpdate. GetUserByName: %v" , err )
return fmt . Errorf ( "GetUserByName: %v" , err )
}
}
repo , err := GetRepositoryByName ( u ser. Id , r epoName)
repo , err := GetRepositoryByName ( repoU ser. Id , opts . R epoName)
if err != nil {
if err != nil {
return fmt . Errorf ( "runUpdate. GetRepositoryByName userId : %v" , err )
return fmt . Errorf ( "GetRepositoryByName: %v" , err )
}
}
// Push tags.
// Push tags.
if strings . HasPrefix ( refName , "refs/tags/" ) {
if strings . HasPrefix ( opts . RefName , "refs/tags/" ) {
tagName := git . RefEndName ( refName )
tag , err := gitRepo . GetTag ( git . RefEndName ( opts . RefName ) )
tag , err := gitRepo . GetTag ( tagName )
if err != nil {
if err != nil {
log . GitLogger . Fatal ( 4 , "runUpdate .GetTag: %v", err )
return fmt . Errorf ( "gitRepo .GetTag: %v", err )
}
}
// When tagger isn't available, fall back to get committer email.
var actEmail string
var actEmail string
if tag . Tagger != nil {
if tag . Tagger != nil {
actEmail = tag . Tagger . Email
actEmail = tag . Tagger . Email
} else {
} else {
cmt , err := tag . Commit ( )
cmt , err := tag . Commit ( )
if err != nil {
if err != nil {
log . GitLogger . Fatal ( 4 , "runUpdate.GetTag Commit: %v", err )
return fmt . Errorf ( "tag. Commit: %v", err )
}
}
actEmail = cmt . Committer . Email
actEmail = cmt . Committer . Email
}
}
commit := & PushCommits { }
commit := & PushCommits { }
if err = CommitRepoAction ( opts . PusherID , repoUser . Id , opts . PusherName , actEmail ,
if err = CommitRepoAction ( userID , user . Id , userName , actEmail ,
repo . ID , opts . RepoUserName , opts . RepoName , opts . RefName , commit , opts . OldCommitID , opts . NewCommitID ) ; err != nil {
repo . ID , repoUserName , repoName , refName , commit , oldCommitID , newCommitID ) ; err != nil {
return fmt . Errorf ( "CommitRepoAction (tag): %v" , err )
log . GitLogger . Fatal ( 4 , "CommitRepoAction: %s/%s:%v" , repoUserName , repoName , err )
}
}
return err
return err
}
}
newCommit , err := gitRepo . GetCommit ( n ewCommitID)
newCommit , err := gitRepo . GetCommit ( opts . N ewCommitID)
if err != nil {
if err != nil {
return fmt . Errorf ( "runUpdate GetCommit of newCommitId : %v" , err )
return fmt . Errorf ( "gitRepo.GetCommit : %v" , err )
}
}
// Push new branch.
// Push new branch.
var l * list . List
var l * list . List
if isNew {
if isNewRef {
l , err = newCommit . CommitsBeforeLimit ( 10 )
l , err = newCommit . CommitsBeforeLimit ( 10 )
if err != nil {
if err != nil {
return fmt . Errorf ( "CommitsBefore: %v" , err )
return fmt . Errorf ( "newCommit. CommitsBeforeLimit : %v" , err )
}
}
} else {
} else {
l , err = newCommit . CommitsBeforeUntil ( oldCommitID )
l , err = newCommit . CommitsBeforeUntil ( opts . O ldCommitID )
if err != nil {
if err != nil {
return fmt . Errorf ( "CommitsBeforeUntil: %v" , err )
return fmt . Errorf ( "newCommit. CommitsBeforeUntil: %v" , err )
}
}
}
}
if err != nil {
if err = CommitRepoAction ( opts . PusherID , repoUser . Id , opts . PusherName , repoUser . Email ,
return fmt . Errorf ( "runUpdate.Commit repoId: %v" , err )
repo . ID , opts . RepoUserName , opts . RepoName , opts . RefName , ListToPushCommits ( l ) ,
}
opts . OldCommitID , opts . NewCommitID ) ; err != nil {
return fmt . Errorf ( "CommitRepoAction (branch): %v" , err )
if err = CommitRepoAction ( userID , user . Id , userName , user . Email ,
repo . ID , repoUserName , repoName , refName , ListToPushCommits ( l ) , oldCommitID , newCommitID ) ; err != nil {
return fmt . Errorf ( "runUpdate.models.CommitRepoAction: %s/%s:%v" , repoUserName , repoName , err )
}
}
return nil
return nil
}
}