Browse Source

Update create repo

pull/31/head
Unknown 11 years ago
parent
commit
b4727c7601
  1. 2
      gogs.go
  2. 52
      models/repo.go
  3. 2
      modules/auth/repo.go
  4. 6
      routers/repo/repo.go
  5. 2
      serve.go

2
gogs.go

@ -20,7 +20,7 @@ import (
// Test that go1.1 tag above is included in builds. main.go refers to this definition. // Test that go1.1 tag above is included in builds. main.go refers to this definition.
const go11tag = true const go11tag = true
const APP_VER = "0.0.3.0310" const APP_VER = "0.0.3.0311"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

52
models/repo.go

@ -12,6 +12,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"unicode/utf8"
git "github.com/libgit2/git2go" 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. // 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) isExist, err := IsRepositoryExist(user, repoName)
if err != nil { if err != nil {
return nil, err return nil, err
@ -67,11 +68,6 @@ func CreateRepository(user *User, repoName, desc string, private bool, initReadm
return nil, ErrRepoAlreadyExist return nil, ErrRepoAlreadyExist
} }
f := RepoPath(user.Name, repoName)
err = initRepository(f, initReadme, repoLang)
if err != nil {
return nil, err
}
repo := &Repository{ repo := &Repository{
OwnerId: user.Id, OwnerId: user.Id,
Name: repoName, Name: repoName,
@ -79,6 +75,11 @@ func CreateRepository(user *User, repoName, desc string, private bool, initReadm
Description: desc, Description: desc,
Private: private, Private: private,
} }
f := RepoPath(user.Name, repoName)
if err = initRepository(f, user, repo, initReadme, repoLang); err != nil {
return nil, err
}
session := orm.NewSession() session := orm.NewSession()
defer session.Close() defer session.Close()
session.Begin() session.Begin()
@ -127,30 +128,35 @@ func CreateRepository(user *User, repoName, desc string, private bool, initReadm
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(f string, initReadme bool, repoLang string) error { func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang string) error {
readme := "README" fileName := map[string]string{
workdir := os.TempDir() "readme": "README.md",
"gitign": ".gitignore",
}
workdir := os.TempDir() + fmt.Sprintf("%d", time.Now().Nanosecond())
sig := &git.Signature{ sig := &git.Signature{
Name: "Rand Om Hacker", Name: user.Name,
Email: "random@hacker.com", Email: user.Email,
When: time.Now(), When: time.Now(),
} }
// README // README
err := ioutil.WriteFile(filepath.Join(workdir, readme), defaultReadme := repo.Name + "\n" + strings.Repeat("=",
[]byte(defaultREADME), 0644) utf8.RuneCountInString(repo.Name)) + "\n\n" + repo.Description
if err != nil { if err := ioutil.WriteFile(filepath.Join(workdir, fileName["readme"]),
[]byte(defaultReadme), 0644); err != nil {
return err return err
} }
// .gitignore // .gitignore
// TODO: // if err := ioutil.WriteFile(filepath.Join(workdir, gitIgn),
// []byte(defaultREADME), 0644); err != nil {
// return err
// }
// LICENSE
rp, err := git.InitRepository(f, true) rp, err := git.InitRepository(f, true)
if err != nil { if err != nil {
@ -163,8 +169,7 @@ func initRepository(f string, initReadme bool, repoLang string) error {
return err return err
} }
err = idx.AddByPath(readme) if err = idx.AddByPath(fileName["readme"]); err != nil {
if err != nil {
return err return err
} }
@ -173,14 +178,13 @@ func initRepository(f string, initReadme bool, repoLang string) error {
return err return err
} }
message := "add readme" message := "Init commit"
tree, err := rp.LookupTree(treeId) tree, err := rp.LookupTree(treeId)
if err != nil { if err != nil {
return err return err
} }
_, err = rp.CreateCommit("HEAD", sig, sig, message, tree) if _, err = rp.CreateCommit("HEAD", sig, sig, message, tree); err != nil {
if err != nil {
return err return err
} }

2
modules/auth/repo.go

@ -18,7 +18,7 @@ import (
type CreateRepoForm struct { type CreateRepoForm struct {
UserId int64 `form:"userId"` UserId int64 `form:"userId"`
RepoName string `form:"repo" binding:"Required"` RepoName string `form:"repo" binding:"Required;AlphaDash"`
Visibility string `form:"visibility"` Visibility string `form:"visibility"`
Description string `form:"desc" binding:"MaxSize(100)"` Description string `form:"desc" binding:"MaxSize(100)"`
Language string `form:"language"` Language string `form:"language"`

6
routers/repo/repo.go

@ -42,11 +42,11 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b
return return
} }
} }
fmt.Println(models.RepoPath(user.Name, form.RepoName))
if err == nil { if err == nil {
if _, err = models.CreateRepository(user, if _, err = models.CreateRepository(user,
form.RepoName, form.Description, form.Visibility == "private", form.RepoName, form.Description, form.Language,
form.InitReadme == "true", form.Language); err == nil { form.Visibility == "private", form.InitReadme == "true"); 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)

2
serve.go

@ -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, true, "") _, err := models.CreateRepository(user, repoName, "", "", false, true)
if err != nil { if err != nil {
println("Create repository failed") println("Create repository failed")
return return

Loading…
Cancel
Save