|
@@ -9,7 +9,6 @@ import (
|
|
|
"fmt"
|
|
|
"io/ioutil"
|
|
|
"os"
|
|
|
- "os/exec"
|
|
|
"path"
|
|
|
"path/filepath"
|
|
|
"sort"
|
|
@@ -25,6 +24,7 @@ import (
|
|
|
"github.com/gogits/gogs/modules/base"
|
|
|
"github.com/gogits/gogs/modules/bin"
|
|
|
"github.com/gogits/gogs/modules/log"
|
|
|
+ "github.com/gogits/gogs/modules/process"
|
|
|
"github.com/gogits/gogs/modules/setting"
|
|
|
)
|
|
|
|
|
@@ -88,13 +88,13 @@ func NewRepoContext() {
|
|
|
zip.Verbose = false
|
|
|
|
|
|
// Check if server has basic git setting.
|
|
|
- stdout, stderr, err := com.ExecCmd("git", "config", "--get", "user.name")
|
|
|
+ stdout, stderr, err := process.Exec("NewRepoContext(get setting)", "git", "config", "--get", "user.name")
|
|
|
if strings.Contains(stderr, "fatal:") {
|
|
|
log.Fatal("repo.NewRepoContext(fail to get git user.name): %s", stderr)
|
|
|
} else if err != nil || len(strings.TrimSpace(stdout)) == 0 {
|
|
|
- if _, stderr, err = com.ExecCmd("git", "config", "--global", "user.email", "[email protected]"); err != nil {
|
|
|
+ if _, stderr, err = process.Exec("NewRepoContext(set email)", "git", "config", "--global", "user.email", "[email protected]"); err != nil {
|
|
|
log.Fatal("repo.NewRepoContext(fail to set git user.email): %s", stderr)
|
|
|
- } else if _, stderr, err = com.ExecCmd("git", "config", "--global", "user.name", "Gogs"); err != nil {
|
|
|
+ } else if _, stderr, err = process.Exec("NewRepoContext(set name)", "git", "config", "--global", "user.name", "Gogs"); err != nil {
|
|
|
log.Fatal("repo.NewRepoContext(fail to set git user.name): %s", stderr)
|
|
|
}
|
|
|
}
|
|
@@ -190,7 +190,9 @@ type Mirror struct {
|
|
|
|
|
|
// MirrorRepository creates a mirror repository from source.
|
|
|
func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error {
|
|
|
- _, stderr, err := com.ExecCmd("git", "clone", "--mirror", url, repoPath)
|
|
|
+ // TODO: need timeout.
|
|
|
+ _, stderr, err := process.Exec(fmt.Sprintf("MirrorRepository: %s/%s", userName, repoName),
|
|
|
+ "git", "clone", "--mirror", url, repoPath)
|
|
|
if err != nil {
|
|
|
return errors.New("git clone --mirror: " + stderr)
|
|
|
}
|
|
@@ -231,9 +233,11 @@ func MirrorUpdate() {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+ // TODO: need timeout.
|
|
|
repoPath := filepath.Join(setting.RepoRootPath, m.RepoName+".git")
|
|
|
- _, stderr, err := com.ExecCmdDir(repoPath, "git", "remote", "update")
|
|
|
- if err != nil {
|
|
|
+ if _, stderr, err := process.ExecDir(
|
|
|
+ repoPath, fmt.Sprintf("MirrorUpdate: %s", repoPath),
|
|
|
+ "git", "remote", "update"); err != nil {
|
|
|
return errors.New("git remote update: " + stderr)
|
|
|
} else if err = git.UnpackRefs(repoPath); err != nil {
|
|
|
return err
|
|
@@ -268,133 +272,34 @@ func MigrateRepository(user *User, name, desc string, private, mirror bool, url
|
|
|
return repo, UpdateRepository(repo)
|
|
|
}
|
|
|
|
|
|
+ // TODO: need timeout.
|
|
|
// Clone from local repository.
|
|
|
- _, stderr, err := com.ExecCmd("git", "clone", repoPath, tmpDir)
|
|
|
+ _, stderr, err := process.Exec(
|
|
|
+ fmt.Sprintf("MigrateRepository(git clone): %s", repoPath),
|
|
|
+ "git", "clone", repoPath, tmpDir)
|
|
|
if err != nil {
|
|
|
return repo, errors.New("git clone: " + stderr)
|
|
|
}
|
|
|
|
|
|
+ // TODO: need timeout.
|
|
|
// Pull data from source.
|
|
|
- _, stderr, err = com.ExecCmdDir(tmpDir, "git", "pull", url)
|
|
|
- if err != nil {
|
|
|
+ if _, stderr, err = process.ExecDir(
|
|
|
+ tmpDir, fmt.Sprintf("MigrateRepository(git pull): %s", repoPath),
|
|
|
+ "git", "pull", url); err != nil {
|
|
|
return repo, errors.New("git pull: " + stderr)
|
|
|
}
|
|
|
|
|
|
+ // TODO: need timeout.
|
|
|
// Push data to local repository.
|
|
|
- if _, stderr, err = com.ExecCmdDir(tmpDir, "git", "push", "origin", "master"); err != nil {
|
|
|
+ if _, stderr, err = process.ExecDir(
|
|
|
+ tmpDir, fmt.Sprintf("MigrateRepository(git push): %s", repoPath),
|
|
|
+ "git", "push", "origin", "master"); err != nil {
|
|
|
return repo, errors.New("git push: " + stderr)
|
|
|
}
|
|
|
|
|
|
return repo, UpdateRepository(repo)
|
|
|
}
|
|
|
|
|
|
-// CreateRepository creates a repository for given user or orgnaziation.
|
|
|
-func CreateRepository(user *User, name, desc, lang, license string, private, mirror, initReadme bool) (*Repository, error) {
|
|
|
- if !IsLegalName(name) {
|
|
|
- return nil, ErrRepoNameIllegal
|
|
|
- }
|
|
|
-
|
|
|
- isExist, err := IsRepositoryExist(user, name)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- } else if isExist {
|
|
|
- return nil, ErrRepoAlreadyExist
|
|
|
- }
|
|
|
-
|
|
|
- repo := &Repository{
|
|
|
- OwnerId: user.Id,
|
|
|
- Name: name,
|
|
|
- LowerName: strings.ToLower(name),
|
|
|
- Description: desc,
|
|
|
- IsPrivate: private,
|
|
|
- IsBare: lang == "" && license == "" && !initReadme,
|
|
|
- }
|
|
|
- if !repo.IsBare {
|
|
|
- repo.DefaultBranch = "master"
|
|
|
- }
|
|
|
-
|
|
|
- repoPath := RepoPath(user.Name, repo.Name)
|
|
|
-
|
|
|
- sess := orm.NewSession()
|
|
|
- defer sess.Close()
|
|
|
- sess.Begin()
|
|
|
-
|
|
|
- if _, err = sess.Insert(repo); err != nil {
|
|
|
- if err2 := os.RemoveAll(repoPath); err2 != nil {
|
|
|
- log.Error("repo.CreateRepository(repo): %v", err)
|
|
|
- return nil, errors.New(fmt.Sprintf(
|
|
|
- "delete repo directory %s/%s failed(1): %v", user.Name, repo.Name, err2))
|
|
|
- }
|
|
|
- sess.Rollback()
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- mode := AU_WRITABLE
|
|
|
- if mirror {
|
|
|
- mode = AU_READABLE
|
|
|
- }
|
|
|
- access := Access{
|
|
|
- UserName: user.LowerName,
|
|
|
- RepoName: strings.ToLower(path.Join(user.Name, repo.Name)),
|
|
|
- Mode: mode,
|
|
|
- }
|
|
|
- if _, err = sess.Insert(&access); err != nil {
|
|
|
- sess.Rollback()
|
|
|
- if err2 := os.RemoveAll(repoPath); err2 != nil {
|
|
|
- log.Error("repo.CreateRepository(access): %v", err)
|
|
|
- return nil, errors.New(fmt.Sprintf(
|
|
|
- "delete repo directory %s/%s failed(2): %v", user.Name, repo.Name, err2))
|
|
|
- }
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?"
|
|
|
- if _, err = sess.Exec(rawSql, user.Id); err != nil {
|
|
|
- sess.Rollback()
|
|
|
- if err2 := os.RemoveAll(repoPath); err2 != nil {
|
|
|
- log.Error("repo.CreateRepository(repo count): %v", err)
|
|
|
- return nil, errors.New(fmt.Sprintf(
|
|
|
- "delete repo directory %s/%s failed(3): %v", user.Name, repo.Name, err2))
|
|
|
- }
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- if err = sess.Commit(); err != nil {
|
|
|
- sess.Rollback()
|
|
|
- if err2 := os.RemoveAll(repoPath); err2 != nil {
|
|
|
- log.Error("repo.CreateRepository(commit): %v", err)
|
|
|
- return nil, errors.New(fmt.Sprintf(
|
|
|
- "delete repo directory %s/%s failed(3): %v", user.Name, repo.Name, err2))
|
|
|
- }
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- if err = WatchRepo(user.Id, repo.Id, true); err != nil {
|
|
|
- log.Error("repo.CreateRepository(WatchRepo): %v", err)
|
|
|
- }
|
|
|
-
|
|
|
- if err = NewRepoAction(user, repo); err != nil {
|
|
|
- log.Error("repo.CreateRepository(NewRepoAction): %v", err)
|
|
|
- }
|
|
|
-
|
|
|
- // No need for init for mirror.
|
|
|
- if mirror {
|
|
|
- return repo, nil
|
|
|
- }
|
|
|
-
|
|
|
- if err = initRepository(repoPath, user, repo, initReadme, lang, license); err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- c := exec.Command("git", "update-server-info")
|
|
|
- c.Dir = repoPath
|
|
|
- if err = c.Run(); err != nil {
|
|
|
- log.Error("repo.CreateRepository(exec update-server-info): %v", err)
|
|
|
- }
|
|
|
-
|
|
|
- return repo, nil
|
|
|
-}
|
|
|
-
|
|
|
// extractGitBareZip extracts git-bare.zip to repository path.
|
|
|
func extractGitBareZip(repoPath string) error {
|
|
|
z, err := zip.Open(path.Join(setting.RepoRootPath, "git-bare.zip"))
|
|
@@ -409,15 +314,22 @@ func extractGitBareZip(repoPath string) error {
|
|
|
// initRepoCommit temporarily changes with work directory.
|
|
|
func initRepoCommit(tmpPath string, sig *git.Signature) (err error) {
|
|
|
var stderr string
|
|
|
- if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "add", "--all"); err != nil {
|
|
|
+ if _, stderr, err = process.ExecDir(
|
|
|
+ tmpPath, fmt.Sprintf("initRepoCommit(git add): %s", tmpPath),
|
|
|
+ "git", "add", "--all"); err != nil {
|
|
|
return errors.New("git add: " + stderr)
|
|
|
}
|
|
|
- if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
|
|
|
+
|
|
|
+ if _, stderr, err = process.ExecDir(
|
|
|
+ tmpPath, fmt.Sprintf("initRepoCommit(git commit): %s", tmpPath),
|
|
|
+ "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
|
|
|
"-m", "Init commit"); err != nil {
|
|
|
return errors.New("git commit: " + stderr)
|
|
|
}
|
|
|
|
|
|
- if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "push", "origin", "master"); err != nil {
|
|
|
+ if _, stderr, err = process.ExecDir(
|
|
|
+ tmpPath, fmt.Sprintf("initRepoCommit(git push): %s", tmpPath),
|
|
|
+ "git", "push", "origin", "master"); err != nil {
|
|
|
return errors.New("git push: " + stderr)
|
|
|
}
|
|
|
return nil
|
|
@@ -475,9 +387,11 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep
|
|
|
tmpDir := filepath.Join(os.TempDir(), base.ToStr(time.Now().Nanosecond()))
|
|
|
os.MkdirAll(tmpDir, os.ModePerm)
|
|
|
|
|
|
- _, stderr, err := com.ExecCmd("git", "clone", repoPath, tmpDir)
|
|
|
+ _, stderr, err := process.Exec(
|
|
|
+ fmt.Sprintf("initRepository(git clone): %s", repoPath),
|
|
|
+ "git", "clone", repoPath, tmpDir)
|
|
|
if err != nil {
|
|
|
- return errors.New("git clone: " + stderr)
|
|
|
+ return errors.New("initRepository(git clone): " + stderr)
|
|
|
}
|
|
|
|
|
|
// README
|
|
@@ -540,6 +454,114 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep
|
|
|
return initRepoCommit(tmpDir, user.NewGitSig())
|
|
|
}
|
|
|
|
|
|
+// CreateRepository creates a repository for given user or orgnaziation.
|
|
|
+func CreateRepository(user *User, name, desc, lang, license string, private, mirror, initReadme bool) (*Repository, error) {
|
|
|
+ if !IsLegalName(name) {
|
|
|
+ return nil, ErrRepoNameIllegal
|
|
|
+ }
|
|
|
+
|
|
|
+ isExist, err := IsRepositoryExist(user, name)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ } else if isExist {
|
|
|
+ return nil, ErrRepoAlreadyExist
|
|
|
+ }
|
|
|
+
|
|
|
+ repo := &Repository{
|
|
|
+ OwnerId: user.Id,
|
|
|
+ Name: name,
|
|
|
+ LowerName: strings.ToLower(name),
|
|
|
+ Description: desc,
|
|
|
+ IsPrivate: private,
|
|
|
+ IsBare: lang == "" && license == "" && !initReadme,
|
|
|
+ }
|
|
|
+ if !repo.IsBare {
|
|
|
+ repo.DefaultBranch = "master"
|
|
|
+ }
|
|
|
+
|
|
|
+ repoPath := RepoPath(user.Name, repo.Name)
|
|
|
+
|
|
|
+ sess := orm.NewSession()
|
|
|
+ defer sess.Close()
|
|
|
+ sess.Begin()
|
|
|
+
|
|
|
+ if _, err = sess.Insert(repo); err != nil {
|
|
|
+ if err2 := os.RemoveAll(repoPath); err2 != nil {
|
|
|
+ log.Error("repo.CreateRepository(repo): %v", err)
|
|
|
+ return nil, errors.New(fmt.Sprintf(
|
|
|
+ "delete repo directory %s/%s failed(1): %v", user.Name, repo.Name, err2))
|
|
|
+ }
|
|
|
+ sess.Rollback()
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ mode := AU_WRITABLE
|
|
|
+ if mirror {
|
|
|
+ mode = AU_READABLE
|
|
|
+ }
|
|
|
+ access := Access{
|
|
|
+ UserName: user.LowerName,
|
|
|
+ RepoName: strings.ToLower(path.Join(user.Name, repo.Name)),
|
|
|
+ Mode: mode,
|
|
|
+ }
|
|
|
+ if _, err = sess.Insert(&access); err != nil {
|
|
|
+ sess.Rollback()
|
|
|
+ if err2 := os.RemoveAll(repoPath); err2 != nil {
|
|
|
+ log.Error("repo.CreateRepository(access): %v", err)
|
|
|
+ return nil, errors.New(fmt.Sprintf(
|
|
|
+ "delete repo directory %s/%s failed(2): %v", user.Name, repo.Name, err2))
|
|
|
+ }
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?"
|
|
|
+ if _, err = sess.Exec(rawSql, user.Id); err != nil {
|
|
|
+ sess.Rollback()
|
|
|
+ if err2 := os.RemoveAll(repoPath); err2 != nil {
|
|
|
+ log.Error("repo.CreateRepository(repo count): %v", err)
|
|
|
+ return nil, errors.New(fmt.Sprintf(
|
|
|
+ "delete repo directory %s/%s failed(3): %v", user.Name, repo.Name, err2))
|
|
|
+ }
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = sess.Commit(); err != nil {
|
|
|
+ sess.Rollback()
|
|
|
+ if err2 := os.RemoveAll(repoPath); err2 != nil {
|
|
|
+ log.Error("repo.CreateRepository(commit): %v", err)
|
|
|
+ return nil, errors.New(fmt.Sprintf(
|
|
|
+ "delete repo directory %s/%s failed(3): %v", user.Name, repo.Name, err2))
|
|
|
+ }
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = WatchRepo(user.Id, repo.Id, true); err != nil {
|
|
|
+ log.Error("repo.CreateRepository(WatchRepo): %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = NewRepoAction(user, repo); err != nil {
|
|
|
+ log.Error("repo.CreateRepository(NewRepoAction): %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // No need for init for mirror.
|
|
|
+ if mirror {
|
|
|
+ return repo, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = initRepository(repoPath, user, repo, initReadme, lang, license); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ _, stderr, err := process.ExecDir(
|
|
|
+ repoPath, fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath),
|
|
|
+ "git", "update-server-info")
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New("CreateRepository(git update-server-info): " + stderr)
|
|
|
+ }
|
|
|
+
|
|
|
+ return repo, nil
|
|
|
+}
|
|
|
+
|
|
|
// GetRepositoriesWithUsers returns given number of repository objects with offset.
|
|
|
// It also auto-gets corresponding users.
|
|
|
func GetRepositoriesWithUsers(num, offset int) ([]*Repository, error) {
|