This commit is contained in:
Lunny Xiao 2014-03-17 20:04:58 +08:00
commit a30e72323d
40 changed files with 1416 additions and 479 deletions

View file

@ -44,6 +44,10 @@ func (a Action) GetRepoName() string {
return a.RepoName
}
func (a Action) GetContent() string {
return a.Content
}
// CommitRepoAction records action for commit repository.
func CommitRepoAction(userId int64, userName string,
repoId int64, repoName string, commits [][]string) error {

View file

@ -307,6 +307,9 @@ func DeleteRepository(userId, repoId int64, userName string) (err error) {
}
session := orm.NewSession()
if err = session.Begin(); err != nil {
return err
}
if _, err = session.Delete(&Repository{Id: repoId}); err != nil {
session.Rollback()
return err

View file

@ -5,11 +5,26 @@
package models
import (
"fmt"
"path"
"strings"
"time"
git "github.com/gogits/git"
"github.com/Unknwon/com"
"github.com/gogits/git"
)
type Commit struct {
Author string
Email string
Date time.Time
SHA string
Message string
}
var (
ErrRepoFileNotLoaded = fmt.Errorf("repo file not loaded")
)
type RepoFile struct {
@ -18,6 +33,7 @@ type RepoFile struct {
Message string
Created time.Time
Size int64
Repo *git.Repository
LastCommit string
}
@ -43,10 +59,34 @@ func findTree(repo *git.Repository, tree *git.Tree, rpath string) *git.Tree {
return g
}
func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile, error) {
f := RepoPath(userName, reposName)
func (file *RepoFile) LookupBlob() (*git.Blob, error) {
if file.Repo == nil {
return nil, ErrRepoFileNotLoaded
}
repo, err := git.OpenRepository(f)
return file.Repo.LookupBlob(file.Id)
}
func GetBranches(userName, reposName string) ([]string, error) {
repo, err := git.OpenRepository(RepoPath(userName, reposName))
if err != nil {
return nil, err
}
refs, err := repo.AllReferences()
if err != nil {
return nil, err
}
brs := make([]string, len(refs))
for i, ref := range refs {
brs[i] = ref.Name
}
return brs, nil
}
func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile, error) {
repo, err := git.OpenRepository(RepoPath(userName, reposName))
if err != nil {
return nil, err
}
@ -128,6 +168,7 @@ func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile,
cm.Message(),
cm.Committer.When,
size,
repo,
cm.Id().String(),
}
@ -142,3 +183,33 @@ func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile,
return append(repodirs, repofiles...), nil
}
func GetLastestCommit(userName, repoName string) (*Commit, error) {
stdout, _, err := com.ExecCmd("git", "--git-dir="+RepoPath(userName, repoName), "log", "-1")
if err != nil {
return nil, err
}
commit := new(Commit)
for _, line := range strings.Split(stdout, "\n") {
if len(line) == 0 {
continue
}
switch {
case line[0] == 'c':
commit.SHA = line[7:]
case line[0] == 'A':
infos := strings.SplitN(line, " ", 3)
commit.Author = infos[1]
commit.Email = infos[2][1 : len(infos[2])-1]
case line[0] == 'D':
commit.Date, err = time.Parse("Mon Jan 02 15:04:05 2006 -0700", line[8:])
if err != nil {
return nil, err
}
case line[:4] == " ":
commit.Message = line[4:]
}
}
return commit, nil
}