diff --git a/gogs.go b/gogs.go index 9ec326bc8..1ea903263 100644 --- a/gogs.go +++ b/gogs.go @@ -20,7 +20,7 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.0.3.0310" +const APP_VER = "0.0.3.0311" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/repo.go b/models/repo.go index fa4ae0ad5..308d5f3be 100644 --- a/models/repo.go +++ b/models/repo.go @@ -12,6 +12,7 @@ import ( "path/filepath" "strings" "time" + "unicode/utf8" git "github.com/libgit2/git2go" @@ -59,7 +60,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, initReadme bool, repoLang string) (*Repository, error) { +func CreateRepository(user *User, repoName, desc, repoLang string, private bool, initReadme bool) (*Repository, error) { isExist, err := IsRepositoryExist(user, repoName) if err != nil { return nil, err @@ -67,11 +68,6 @@ func CreateRepository(user *User, repoName, desc string, private bool, initReadm return nil, ErrRepoAlreadyExist } - f := RepoPath(user.Name, repoName) - err = initRepository(f, initReadme, repoLang) - if err != nil { - return nil, err - } repo := &Repository{ OwnerId: user.Id, Name: repoName, @@ -79,6 +75,11 @@ func CreateRepository(user *User, repoName, desc string, private bool, initReadm Description: desc, Private: private, } + + f := RepoPath(user.Name, repoName) + if err = initRepository(f, user, repo, initReadme, repoLang); err != nil { + return nil, err + } session := orm.NewSession() defer session.Close() session.Begin() @@ -127,30 +128,35 @@ func CreateRepository(user *User, repoName, desc string, private bool, initReadm return repo, nil } -var ( - defaultREADME = "readme first" -) - // InitRepository initializes README and .gitignore if needed. -func initRepository(f string, initReadme bool, repoLang string) error { - readme := "README" - workdir := os.TempDir() +func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang string) error { + fileName := map[string]string{ + "readme": "README.md", + "gitign": ".gitignore", + } + workdir := os.TempDir() + fmt.Sprintf("%d", time.Now().Nanosecond()) sig := &git.Signature{ - Name: "Rand Om Hacker", - Email: "random@hacker.com", + Name: user.Name, + Email: user.Email, When: time.Now(), } // README - err := ioutil.WriteFile(filepath.Join(workdir, readme), - []byte(defaultREADME), 0644) - if err != nil { + 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 - // TODO: + // if err := ioutil.WriteFile(filepath.Join(workdir, gitIgn), + // []byte(defaultREADME), 0644); err != nil { + // return err + // } + + // LICENSE rp, err := git.InitRepository(f, true) if err != nil { @@ -163,8 +169,7 @@ func initRepository(f string, initReadme bool, repoLang string) error { return err } - err = idx.AddByPath(readme) - if err != nil { + if err = idx.AddByPath(fileName["readme"]); err != nil { return err } @@ -173,14 +178,13 @@ func initRepository(f string, initReadme bool, repoLang string) error { return err } - message := "add readme" + message := "Init commit" tree, err := rp.LookupTree(treeId) if err != nil { return err } - _, err = rp.CreateCommit("HEAD", sig, sig, message, tree) - if err != nil { + if _, err = rp.CreateCommit("HEAD", sig, sig, message, tree); err != nil { return err } diff --git a/modules/auth/repo.go b/modules/auth/repo.go index 6656654f5..e55ccb289 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -18,7 +18,7 @@ import ( type CreateRepoForm struct { UserId int64 `form:"userId"` - RepoName string `form:"repo" binding:"Required"` + RepoName string `form:"repo" binding:"Required;AlphaDash"` Visibility string `form:"visibility"` Description string `form:"desc" binding:"MaxSize(100)"` Language string `form:"language"` diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 0e9ced426..221d4ba1f 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -42,11 +42,11 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b return } } - fmt.Println(models.RepoPath(user.Name, form.RepoName)) + if err == nil { if _, err = models.CreateRepository(user, - form.RepoName, form.Description, form.Visibility == "private", - form.InitReadme == "true", form.Language); err == nil { + form.RepoName, form.Description, form.Language, + form.Visibility == "private", form.InitReadme == "true"); 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 83ce06e88..8ef5690b9 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, true, "") + _, err := models.CreateRepository(user, repoName, "", "", false, true) if err != nil { println("Create repository failed") return