From f9907bbfa4368be7412ab3ade6be354225409652 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 11 Mar 2014 12:18:44 +0800 Subject: [PATCH] add README support for creating repository --- models/repo.go | 69 +++++++++++++++++++++++++++++++++++++++----- routers/repo/repo.go | 6 ++-- serve.go | 2 +- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/models/repo.go b/models/repo.go index d17f32976a..fa4ae0ad5f 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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 } diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 6f59b408e0..0e9ced4262 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -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) diff --git a/serve.go b/serve.go index 0c0584cd91..83ce06e882 100644 --- a/serve.go +++ b/serve.go @@ -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