2014-03-15 05:53:13 -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-14 12:18:09 -04:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
2014-03-15 10:27:24 -04:00
|
|
|
git "github.com/gogits/git"
|
2014-03-14 12:18:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type RepoFile struct {
|
|
|
|
Id *git.Oid
|
|
|
|
Type int
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
Message string
|
|
|
|
Created time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *RepoFile) IsFile() bool {
|
|
|
|
return f.Type == git.FileModeBlob || f.Type == git.FileModeBlobExec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *RepoFile) IsDir() bool {
|
|
|
|
return f.Type == git.FileModeTree
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile, error) {
|
|
|
|
f := RepoPath(userName, reposName)
|
|
|
|
|
|
|
|
repo, err := git.OpenRepository(f)
|
|
|
|
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 10:27:24 -04:00
|
|
|
var repodirs []*RepoFile
|
2014-03-14 12:18:09 -04:00
|
|
|
var repofiles []*RepoFile
|
|
|
|
lastCommit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int {
|
|
|
|
if dirname == rpath {
|
2014-03-15 10:27:24 -04:00
|
|
|
switch entry.Filemode {
|
|
|
|
case git.FileModeBlob, git.FileModeBlobExec:
|
|
|
|
repofiles = append(repofiles, &RepoFile{
|
|
|
|
entry.Id,
|
|
|
|
entry.Filemode,
|
|
|
|
entry.Name,
|
|
|
|
path.Join(dirname, entry.Name),
|
|
|
|
lastCommit.Message(),
|
|
|
|
lastCommit.Committer.When,
|
|
|
|
})
|
|
|
|
case git.FileModeTree:
|
|
|
|
repodirs = append(repodirs, &RepoFile{
|
|
|
|
entry.Id,
|
|
|
|
entry.Filemode,
|
|
|
|
entry.Name,
|
|
|
|
path.Join(dirname, entry.Name),
|
|
|
|
lastCommit.Message(),
|
|
|
|
lastCommit.Committer.When,
|
|
|
|
})
|
|
|
|
}
|
2014-03-14 12:18:09 -04:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
|
2014-03-15 10:27:24 -04:00
|
|
|
return append(repodirs, repofiles...), nil
|
2014-03-14 12:18:09 -04:00
|
|
|
}
|