|
|
@ -7,6 +7,7 @@ package models |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"io/ioutil" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
@ -58,7 +59,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) (*Repository, error) { |
|
|
|
func CreateRepository(user *User, repoName, desc string, private bool, initReadme bool, repoLang string) (*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,10 +68,10 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
f := RepoPath(user.Name, repoName) |
|
|
|
f := RepoPath(user.Name, repoName) |
|
|
|
if _, err = git.InitRepository(f, true); err != nil { |
|
|
|
err = initRepository(f, initReadme, repoLang) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
repo := &Repository{ |
|
|
|
repo := &Repository{ |
|
|
|
OwnerId: user.Id, |
|
|
|
OwnerId: user.Id, |
|
|
|
Name: repoName, |
|
|
|
Name: repoName, |
|
|
@ -98,39 +99,91 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit |
|
|
|
Mode: AU_WRITABLE, |
|
|
|
Mode: AU_WRITABLE, |
|
|
|
} |
|
|
|
} |
|
|
|
if _, err = session.Insert(&access); err != nil { |
|
|
|
if _, err = session.Insert(&access); err != nil { |
|
|
|
|
|
|
|
session.Rollback() |
|
|
|
if err2 := os.RemoveAll(f); err2 != nil { |
|
|
|
if err2 := os.RemoveAll(f); err2 != nil { |
|
|
|
return nil, errors.New(fmt.Sprintf( |
|
|
|
return nil, errors.New(fmt.Sprintf( |
|
|
|
"delete repo directory %s/%s failed", user.Name, repoName)) |
|
|
|
"delete repo directory %s/%s failed", user.Name, repoName)) |
|
|
|
} |
|
|
|
} |
|
|
|
session.Rollback() |
|
|
|
|
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil { |
|
|
|
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 { |
|
|
|
if err2 := os.RemoveAll(f); err2 != nil { |
|
|
|
return nil, errors.New(fmt.Sprintf( |
|
|
|
return nil, errors.New(fmt.Sprintf( |
|
|
|
"delete repo directory %s/%s failed", user.Name, repoName)) |
|
|
|
"delete repo directory %s/%s failed", user.Name, repoName)) |
|
|
|
} |
|
|
|
} |
|
|
|
session.Rollback() |
|
|
|
|
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if err = session.Commit(); err != nil { |
|
|
|
if err = session.Commit(); err != nil { |
|
|
|
|
|
|
|
session.Rollback() |
|
|
|
if err2 := os.RemoveAll(f); err2 != nil { |
|
|
|
if err2 := os.RemoveAll(f); err2 != nil { |
|
|
|
return nil, errors.New(fmt.Sprintf( |
|
|
|
return nil, errors.New(fmt.Sprintf( |
|
|
|
"delete repo directory %s/%s failed", user.Name, repoName)) |
|
|
|
"delete repo directory %s/%s failed", user.Name, repoName)) |
|
|
|
} |
|
|
|
} |
|
|
|
session.Rollback() |
|
|
|
|
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
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(repo *Repository, initReadme bool, repoLang string) error { |
|
|
|
func initRepository(f string, initReadme bool, repoLang string) error { |
|
|
|
// README.
|
|
|
|
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
|
|
|
|
// .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 |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|