1
0
Fork 0
forked from forgejo/forgejo

Monitor all git commands; move blame to git package and replace git as a variable (#6864)

* monitor all git commands; move blame to git package and replace git as a variable

* use git command but not other commands

* fix build

* move exec.Command to git.NewCommand

* fix fmt

* remove unrelated changes

* remove unrelated changes

* refactor IsEmpty and add tests

* fix tests

* fix tests

* fix tests

* fix tests

* remove gitLogger

* fix fmt

* fix isEmpty

* fix lint

* fix tests
This commit is contained in:
Lunny Xiao 2019-06-27 02:15:26 +08:00 committed by techknowlogick
parent 161e12e157
commit edc94c7041
34 changed files with 750 additions and 74 deletions

View file

@ -50,7 +50,7 @@ func (t *TemporaryUploadRepository) Close() {
func (t *TemporaryUploadRepository) Clone(branch string) error {
if _, stderr, err := process.GetManager().ExecTimeout(5*time.Minute,
fmt.Sprintf("Clone (git clone -s --bare): %s", t.basePath),
"git", "clone", "-s", "--bare", "-b", branch, t.repo.RepoPath(), t.basePath); err != nil {
git.GitExecutable, "clone", "-s", "--bare", "-b", branch, t.repo.RepoPath(), t.basePath); err != nil {
if matched, _ := regexp.MatchString(".*Remote branch .* not found in upstream origin.*", stderr); matched {
return git.ErrBranchNotExist{
Name: branch,
@ -79,7 +79,7 @@ func (t *TemporaryUploadRepository) SetDefaultIndex() error {
if _, stderr, err := process.GetManager().ExecDir(5*time.Minute,
t.basePath,
fmt.Sprintf("SetDefaultIndex (git read-tree HEAD): %s", t.basePath),
"git", "read-tree", "HEAD"); err != nil {
git.GitExecutable, "read-tree", "HEAD"); err != nil {
return fmt.Errorf("SetDefaultIndex: %v %s", err, stderr)
}
return nil
@ -101,7 +101,7 @@ func (t *TemporaryUploadRepository) LsFiles(filenames ...string) ([]string, erro
}
}
cmd := exec.CommandContext(ctx, "git", cmdArgs...)
cmd := exec.CommandContext(ctx, git.GitExecutable, cmdArgs...)
desc := fmt.Sprintf("lsFiles: (git ls-files) %v", cmdArgs)
cmd.Dir = t.basePath
cmd.Stdout = stdOut
@ -146,7 +146,7 @@ func (t *TemporaryUploadRepository) RemoveFilesFromIndex(filenames ...string) er
defer cancel()
cmdArgs := []string{"update-index", "--remove", "-z", "--index-info"}
cmd := exec.CommandContext(ctx, "git", cmdArgs...)
cmd := exec.CommandContext(ctx, git.GitExecutable, cmdArgs...)
desc := fmt.Sprintf("removeFilesFromIndex: (git update-index) %v", filenames)
cmd.Dir = t.basePath
cmd.Stdout = stdOut
@ -174,7 +174,7 @@ func (t *TemporaryUploadRepository) HashObject(content io.Reader) (string, error
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
hashCmd := exec.CommandContext(ctx, "git", "hash-object", "-w", "--stdin")
hashCmd := exec.CommandContext(ctx, git.GitExecutable, "hash-object", "-w", "--stdin")
hashCmd.Dir = t.basePath
hashCmd.Stdin = content
stdOutBuffer := new(bytes.Buffer)
@ -203,7 +203,7 @@ func (t *TemporaryUploadRepository) AddObjectToIndex(mode, objectHash, objectPat
if _, stderr, err := process.GetManager().ExecDir(5*time.Minute,
t.basePath,
fmt.Sprintf("addObjectToIndex (git update-index): %s", t.basePath),
"git", "update-index", "--add", "--replace", "--cacheinfo", mode, objectHash, objectPath); err != nil {
git.GitExecutable, "update-index", "--add", "--replace", "--cacheinfo", mode, objectHash, objectPath); err != nil {
if matched, _ := regexp.MatchString(".*Invalid path '.*", stderr); matched {
return models.ErrFilePathInvalid{
Message: objectPath,
@ -220,7 +220,7 @@ func (t *TemporaryUploadRepository) WriteTree() (string, error) {
treeHash, stderr, err := process.GetManager().ExecDir(5*time.Minute,
t.basePath,
fmt.Sprintf("WriteTree (git write-tree): %s", t.basePath),
"git", "write-tree")
git.GitExecutable, "write-tree")
if err != nil {
return "", fmt.Errorf("git write-tree: %s", stderr)
}
@ -240,7 +240,7 @@ func (t *TemporaryUploadRepository) GetLastCommitByRef(ref string) (string, erro
treeHash, stderr, err := process.GetManager().ExecDir(5*time.Minute,
t.basePath,
fmt.Sprintf("GetLastCommit (git rev-parse %s): %s", ref, t.basePath),
"git", "rev-parse", ref)
git.GitExecutable, "rev-parse", ref)
if err != nil {
return "", fmt.Errorf("git rev-parse %s: %s", ref, stderr)
}
@ -267,7 +267,7 @@ func (t *TemporaryUploadRepository) CommitTree(author, committer *models.User, t
t.basePath,
fmt.Sprintf("commitTree (git commit-tree): %s", t.basePath),
env,
"git", "commit-tree", treeHash, "-p", "HEAD", "-m", message)
git.GitExecutable, "commit-tree", treeHash, "-p", "HEAD", "-m", message)
if err != nil {
return "", fmt.Errorf("git commit-tree: %s", stderr)
}
@ -283,7 +283,7 @@ func (t *TemporaryUploadRepository) Push(doer *models.User, commitHash string, b
t.basePath,
fmt.Sprintf("actuallyPush (git push): %s", t.basePath),
env,
"git", "push", t.repo.RepoPath(), strings.TrimSpace(commitHash)+":refs/heads/"+strings.TrimSpace(branch)); err != nil {
git.GitExecutable, "push", t.repo.RepoPath(), strings.TrimSpace(commitHash)+":refs/heads/"+strings.TrimSpace(branch)); err != nil {
return fmt.Errorf("git push: %s", stderr)
}
return nil
@ -297,7 +297,7 @@ func (t *TemporaryUploadRepository) DiffIndex() (diff *models.Diff, err error) {
stdErr := new(bytes.Buffer)
cmd := exec.CommandContext(ctx, "git", "diff-index", "--cached", "-p", "HEAD")
cmd := exec.CommandContext(ctx, git.GitExecutable, "diff-index", "--cached", "-p", "HEAD")
cmd.Dir = t.basePath
cmd.Stderr = stdErr
@ -341,7 +341,7 @@ func (t *TemporaryUploadRepository) CheckAttribute(attribute string, args ...str
}
}
cmd := exec.CommandContext(ctx, "git", cmdArgs...)
cmd := exec.CommandContext(ctx, git.GitExecutable, cmdArgs...)
desc := fmt.Sprintf("checkAttr: (git check-attr) %s %v", attribute, cmdArgs)
cmd.Dir = t.basePath
cmd.Stdout = stdOut