add README support for creating repository

release/v1.15
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 (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -58,7 +59,7 @@ func IsRepositoryExist(user *User, repoName string) (bool, error) {
}
// 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)
if err != nil {
return nil, err
@ -67,10 +68,10 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit
}
f := RepoPath(user.Name, repoName)
if _, err = git.InitRepository(f, true); err != nil {
err = initRepository(f, initReadme, repoLang)
if err != nil {
return nil, err
}
repo := &Repository{
OwnerId: user.Id,
Name: repoName,
@ -98,39 +99,91 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit
Mode: AU_WRITABLE,
}
if _, err = session.Insert(&access); err != nil {
session.Rollback()
if err2 := os.RemoveAll(f); err2 != nil {
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
}
session.Rollback()
return nil, err
}
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 {
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
}
session.Rollback()
return nil, err
}
if err = session.Commit(); err != nil {
session.Rollback()
if err2 := os.RemoveAll(f); err2 != nil {
return nil, errors.New(fmt.Sprintf(
"delete repo directory %s/%s failed", user.Name, repoName))
}
session.Rollback()
return nil, err
}
return repo, nil
}
var (
defaultREADME = "readme first"
)
// InitRepository initializes README and .gitignore if needed.
func InitRepository(repo *Repository, initReadme bool, repoLang string) error {
// README.
func initRepository(f string, initReadme bool, repoLang string) error {
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
// 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
}

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))
if err == nil {
if repo, err := models.CreateRepository(user,
form.RepoName, form.Description, form.Visibility == "private"); err == nil {
err = models.InitRepository(repo, form.InitReadme == "true", form.Language)
if _, err = models.CreateRepository(user,
form.RepoName, form.Description, form.Visibility == "private",
form.InitReadme == "true", form.Language); err == nil {
if err == nil {
data["RepoName"] = user.Name + "/" + form.RepoName
r.HTML(200, "repo/created", data)

View File

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