add README support for creating repository

This commit is contained in:
Lunny Xiao 2014-03-11 12:18:44 +08:00
parent 52837e3d36
commit f9907bbfa4
3 changed files with 65 additions and 12 deletions

View File

@ -7,6 +7,7 @@ package models
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -58,7 +59,7 @@ func IsRepositoryExist(user *User, repoName string) (bool, error) {
} }
// CreateRepository creates a repository for given user or orgnaziation. // CreateRepository creates a repository for given user or orgnaziation.
func CreateRepository(user *User, repoName, desc string, private bool) (*Repository, error) { func CreateRepository(user *User, repoName, desc string, private bool, initReadme bool, repoLang string) (*Repository, error) {
isExist, err := IsRepositoryExist(user, repoName) isExist, err := IsRepositoryExist(user, repoName)
if err != nil { if err != nil {
return nil, err return nil, err
@ -67,10 +68,10 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit
} }
f := RepoPath(user.Name, repoName) f := RepoPath(user.Name, repoName)
if _, err = git.InitRepository(f, true); err != nil { err = initRepository(f, initReadme, repoLang)
if err != nil {
return nil, err return nil, err
} }
repo := &Repository{ repo := &Repository{
OwnerId: user.Id, OwnerId: user.Id,
Name: repoName, Name: repoName,
@ -98,39 +99,91 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit
Mode: AU_WRITABLE, Mode: AU_WRITABLE,
} }
if _, err = session.Insert(&access); err != nil { if _, err = session.Insert(&access); err != nil {
session.Rollback()
if err2 := os.RemoveAll(f); err2 != nil { if err2 := os.RemoveAll(f); err2 != nil {
return nil, errors.New(fmt.Sprintf( return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName)) "delete repo directory %s/%s failed", user.Name, repoName))
} }
session.Rollback()
return nil, err return nil, err
} }
if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil { if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil {
session.Rollback()
if err2 := os.RemoveAll(f); err2 != nil { if err2 := os.RemoveAll(f); err2 != nil {
return nil, errors.New(fmt.Sprintf( return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName)) "delete repo directory %s/%s failed", user.Name, repoName))
} }
session.Rollback()
return nil, err return nil, err
} }
if err = session.Commit(); err != nil { if err = session.Commit(); err != nil {
session.Rollback()
if err2 := os.RemoveAll(f); err2 != nil { if err2 := os.RemoveAll(f); err2 != nil {
return nil, errors.New(fmt.Sprintf( return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName)) "delete repo directory %s/%s failed", user.Name, repoName))
} }
session.Rollback()
return nil, err return nil, err
} }
return repo, nil return repo, nil
} }
var (
defaultREADME = "readme first"
)
// InitRepository initializes README and .gitignore if needed. // InitRepository initializes README and .gitignore if needed.
func InitRepository(repo *Repository, initReadme bool, repoLang string) error { func initRepository(f string, initReadme bool, repoLang string) error {
// README. readme := "README"
workdir := os.TempDir()
sig := &git.Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Now(),
}
// README
err := ioutil.WriteFile(filepath.Join(workdir, readme),
[]byte(defaultREADME), 0644)
if err != nil {
return err
}
// .gitignore // .gitignore
// TODO:
rp, err := git.InitRepository(f, true)
if err != nil {
return err
}
rp.SetWorkdir(workdir, false)
idx, err := rp.Index()
if err != nil {
return err
}
err = idx.AddByPath(readme)
if err != nil {
return err
}
treeId, err := idx.WriteTree()
if err != nil {
return err
}
message := "add readme"
tree, err := rp.LookupTree(treeId)
if err != nil {
return err
}
_, err = rp.CreateCommit("HEAD", sig, sig, message, tree)
if err != nil {
return err
}
return nil return nil
} }

View File

@ -44,9 +44,9 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b
} }
fmt.Println(models.RepoPath(user.Name, form.RepoName)) fmt.Println(models.RepoPath(user.Name, form.RepoName))
if err == nil { if err == nil {
if repo, err := models.CreateRepository(user, if _, err = models.CreateRepository(user,
form.RepoName, form.Description, form.Visibility == "private"); err == nil { form.RepoName, form.Description, form.Visibility == "private",
err = models.InitRepository(repo, form.InitReadme == "true", form.Language) form.InitReadme == "true", form.Language); err == nil {
if err == nil { if err == nil {
data["RepoName"] = user.Name + "/" + form.RepoName data["RepoName"] = user.Name + "/" + form.RepoName
r.HTML(200, "repo/created", data) r.HTML(200, "repo/created", data)

View File

@ -123,7 +123,7 @@ func runServ(*cli.Context) {
println("Repository", user.Name+"/"+repoName, "is not exist") println("Repository", user.Name+"/"+repoName, "is not exist")
return return
} else if isWrite { } else if isWrite {
_, err := models.CreateRepository(user, repoName, "", false) _, err := models.CreateRepository(user, repoName, "", false, true, "")
if err != nil { if err != nil {
println("Create repository failed") println("Create repository failed")
return return