aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2014-03-11 18:10:19 +0800
committerLunny Xiao <xiaolunwen@gmail.com>2014-03-11 18:10:19 +0800
commit23400dd0a27e73d22725757ccddb45f369bdf9d8 (patch)
treec4b83d56d71c80c8a4e9234391aa09fc3932e4e3 /models
parentb5cc4078a9c3f11f37f4d01f77428a5dc226baa4 (diff)
add GetReposFiles
Diffstat (limited to 'models')
-rw-r--r--models/repo.go103
-rw-r--r--models/user.go10
2 files changed, 86 insertions, 27 deletions
diff --git a/models/repo.go b/models/repo.go
index d4f30455..810f49b8 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -141,43 +141,52 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv
// InitRepository initializes README and .gitignore if needed.
func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang, license string) error {
- fileName := map[string]string{
- "readme": "README.md",
- "gitign": ".gitignore",
- "license": "LICENSE",
+ fileName := map[string]string{}
+
+ if initReadme {
+ fileName["readme"] = "README.md"
+ }
+ if repoLang != "" {
+ fileName["gitign"] = ".gitignore"
}
+ if license != "" {
+ fileName["license"] = "LICENSE"
+ }
+
workdir := os.TempDir() + fmt.Sprintf("%d", time.Now().Nanosecond())
os.MkdirAll(workdir, os.ModePerm)
- sig := &git.Signature{
- Name: user.Name,
- Email: user.Email,
- When: time.Now(),
- }
+ sig := user.NewGitSig()
// README
- defaultReadme := repo.Name + "\n" + strings.Repeat("=",
- utf8.RuneCountInString(repo.Name)) + "\n\n" + repo.Description
- if err := ioutil.WriteFile(filepath.Join(workdir, fileName["readme"]),
- []byte(defaultReadme), 0644); err != nil {
- return err
+ if initReadme {
+ defaultReadme := repo.Name + "\n" + strings.Repeat("=",
+ utf8.RuneCountInString(repo.Name)) + "\n\n" + repo.Description
+ if err := ioutil.WriteFile(filepath.Join(workdir, fileName["readme"]),
+ []byte(defaultReadme), 0644); err != nil {
+ return err
+ }
}
- // .gitignore
- filePath := "conf/gitignore/" + repoLang
- if com.IsFile(filePath) {
- if _, err := com.Copy(filePath,
- filepath.Join(workdir, fileName["gitign"])); err != nil {
- return err
+ if repoLang != "" {
+ // .gitignore
+ filePath := "conf/gitignore/" + repoLang
+ if com.IsFile(filePath) {
+ if _, err := com.Copy(filePath,
+ filepath.Join(workdir, fileName["gitign"])); err != nil {
+ return err
+ }
}
}
- // LICENSE
- filePath = "conf/license/" + license
- if com.IsFile(filePath) {
- if _, err := com.Copy(filePath,
- filepath.Join(workdir, fileName["license"])); err != nil {
- return err
+ if license != "" {
+ // LICENSE
+ filePath := "conf/license/" + license
+ if com.IsFile(filePath) {
+ if _, err := com.Copy(filePath,
+ filepath.Join(workdir, fileName["license"])); err != nil {
+ return err
+ }
}
}
@@ -227,6 +236,48 @@ func GetRepositoryCount(user *User) (int64, error) {
return orm.Count(&Repository{OwnerId: user.Id})
}
+const (
+ RFile = iota + 1
+ RDir
+)
+
+type RepoFile struct {
+ Type int
+ Name string
+
+ Created time.Time
+}
+
+func GetReposFiles(userName, reposName, treeName, rpath string) ([]RepoFile, error) {
+ f := RepoPath(userName, reposName)
+ repo, err := git.OpenRepository(f)
+ if err != nil {
+ return nil, err
+ }
+
+ obj, err := repo.RevparseSingle("HEAD")
+ if err != nil {
+ return nil, err
+ }
+ lastCommit := obj.(*git.Commit)
+ var repofiles []RepoFile
+ tree, err := lastCommit.Tree()
+ if err != nil {
+ return nil, err
+ }
+ var i uint64 = 0
+ for ; i < tree.EntryCount(); i++ {
+ entry := tree.EntryByIndex(i)
+ repofiles = append(repofiles, RepoFile{
+ entry.Filemode,
+ entry.Name,
+ time.Now(),
+ })
+ }
+
+ return repofiles, nil
+}
+
func StarReposiory(user *User, repoName string) error {
return nil
}
diff --git a/models/user.go b/models/user.go
index 95cebb18..05169e46 100644
--- a/models/user.go
+++ b/models/user.go
@@ -13,8 +13,8 @@ import (
"time"
"github.com/dchest/scrypt"
-
"github.com/gogits/gogs/modules/base"
+ git "github.com/libgit2/git2go"
)
var UserPasswdSalt string
@@ -98,6 +98,14 @@ func IsEmailUsed(email string) (bool, error) {
return orm.Get(&User{Email: email})
}
+func (user *User) NewGitSig() *git.Signature {
+ return &git.Signature{
+ Name: user.Name,
+ Email: user.Email,
+ When: time.Now(),
+ }
+}
+
// RegisterUser creates record of a new user.
func RegisterUser(user *User) (err error) {
isExist, err := IsUserExist(user.Name)