|
|
@ -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 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|