2014-03-15 10:53:13 +01: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-14 17:18:09 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2014-03-17 11:46:54 +01:00
|
|
|
"fmt"
|
2014-03-14 17:18:09 +01:00
|
|
|
"path"
|
2014-03-17 05:57:18 +01:00
|
|
|
"strings"
|
2014-03-14 17:18:09 +01:00
|
|
|
"time"
|
|
|
|
|
2014-03-17 05:57:18 +01:00
|
|
|
"github.com/Unknwon/com"
|
|
|
|
|
2014-03-17 04:43:22 +01:00
|
|
|
"github.com/gogits/git"
|
2014-03-14 17:18:09 +01:00
|
|
|
)
|
|
|
|
|
2014-03-17 05:57:18 +01:00
|
|
|
type Commit struct {
|
|
|
|
Author string
|
|
|
|
Email string
|
|
|
|
Date time.Time
|
|
|
|
SHA string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2014-03-17 11:46:54 +01:00
|
|
|
var (
|
|
|
|
ErrRepoFileNotLoaded = fmt.Errorf("repo file not loaded")
|
|
|
|
)
|
|
|
|
|
2014-03-14 17:18:09 +01:00
|
|
|
type RepoFile struct {
|
2014-03-15 15:43:55 +01:00
|
|
|
*git.TreeEntry
|
2014-03-14 17:18:09 +01:00
|
|
|
Path string
|
|
|
|
Message string
|
|
|
|
Created time.Time
|
2014-03-15 16:38:11 +01:00
|
|
|
Size int64
|
2014-03-17 11:46:54 +01:00
|
|
|
Repo *git.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *RepoFile) LookupBlob() (*git.Blob, error) {
|
|
|
|
if file.Repo == nil {
|
|
|
|
return nil, ErrRepoFileNotLoaded
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.Repo.LookupBlob(file.Id)
|
2014-03-14 17:18:09 +01:00
|
|
|
}
|
|
|
|
|
2014-03-17 04:43:22 +01:00
|
|
|
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
|
|
|
|
}
|
2014-03-14 17:18:09 +01:00
|
|
|
|
2014-03-17 04:43:22 +01:00
|
|
|
func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile, error) {
|
|
|
|
repo, err := git.OpenRepository(RepoPath(userName, reposName))
|
2014-03-14 17:18:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ref, err := repo.LookupReference("refs/heads/" + branchName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastCommit, err := repo.LookupCommit(ref.Oid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:27:24 +01:00
|
|
|
var repodirs []*RepoFile
|
2014-03-14 17:18:09 +01:00
|
|
|
var repofiles []*RepoFile
|
|
|
|
lastCommit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int {
|
|
|
|
if dirname == rpath {
|
2014-03-15 16:38:11 +01:00
|
|
|
size, err := repo.ObjectSize(entry.Id)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
2014-03-15 15:27:24 +01:00
|
|
|
switch entry.Filemode {
|
|
|
|
case git.FileModeBlob, git.FileModeBlobExec:
|
|
|
|
repofiles = append(repofiles, &RepoFile{
|
2014-03-15 15:43:55 +01:00
|
|
|
entry,
|
2014-03-15 15:27:24 +01:00
|
|
|
path.Join(dirname, entry.Name),
|
|
|
|
lastCommit.Message(),
|
|
|
|
lastCommit.Committer.When,
|
2014-03-15 16:38:11 +01:00
|
|
|
size,
|
2014-03-17 11:46:54 +01:00
|
|
|
repo,
|
2014-03-15 15:27:24 +01:00
|
|
|
})
|
|
|
|
case git.FileModeTree:
|
|
|
|
repodirs = append(repodirs, &RepoFile{
|
2014-03-15 15:43:55 +01:00
|
|
|
entry,
|
2014-03-15 15:27:24 +01:00
|
|
|
path.Join(dirname, entry.Name),
|
|
|
|
lastCommit.Message(),
|
|
|
|
lastCommit.Committer.When,
|
2014-03-15 16:38:11 +01:00
|
|
|
size,
|
2014-03-17 11:46:54 +01:00
|
|
|
repo,
|
2014-03-15 15:27:24 +01:00
|
|
|
})
|
|
|
|
}
|
2014-03-14 17:18:09 +01:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
|
2014-03-15 15:27:24 +01:00
|
|
|
return append(repodirs, repofiles...), nil
|
2014-03-14 17:18:09 +01:00
|
|
|
}
|
2014-03-17 05:57:18 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|