Use git command instead of exec.Cmd in blame (#22098)

extract from #18147

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Lunny Xiao 2023-01-03 16:17:13 +08:00 committed by GitHub
parent c59e1537a8
commit efa708501b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 156 deletions

View file

@ -9,10 +9,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"os/exec"
"regexp" "regexp"
"code.gitea.io/gitea/modules/process"
) )
// BlamePart represents block of blame - continuous lines with one sha // BlamePart represents block of blame - continuous lines with one sha
@ -23,12 +20,11 @@ type BlamePart struct {
// BlameReader returns part of file blame one by one // BlameReader returns part of file blame one by one
type BlameReader struct { type BlameReader struct {
cmd *exec.Cmd cmd *Command
output io.ReadCloser output io.WriteCloser
reader *bufio.Reader reader io.ReadCloser
done chan error
lastSha *string lastSha *string
cancel context.CancelFunc // Cancels the context that this reader runs in
finished process.FinishedFunc // Tells the process manager we're finished and it can remove the associated process from the process table
} }
var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})") var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})")
@ -37,7 +33,7 @@ var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})")
func (r *BlameReader) NextPart() (*BlamePart, error) { func (r *BlameReader) NextPart() (*BlamePart, error) {
var blamePart *BlamePart var blamePart *BlamePart
reader := r.reader reader := bufio.NewReader(r.reader)
if r.lastSha != nil { if r.lastSha != nil {
blamePart = &BlamePart{*r.lastSha, make([]string, 0)} blamePart = &BlamePart{*r.lastSha, make([]string, 0)}
@ -99,51 +95,41 @@ func (r *BlameReader) NextPart() (*BlamePart, error) {
// Close BlameReader - don't run NextPart after invoking that // Close BlameReader - don't run NextPart after invoking that
func (r *BlameReader) Close() error { func (r *BlameReader) Close() error {
defer r.finished() // Only remove the process from the process table when the underlying command is closed err := <-r.done
r.cancel() // However, first cancel our own context early _ = r.reader.Close()
_ = r.output.Close() _ = r.output.Close()
return err
if err := r.cmd.Wait(); err != nil {
return fmt.Errorf("Wait: %w", err)
}
return nil
} }
// CreateBlameReader creates reader for given repository, commit and file // CreateBlameReader creates reader for given repository, commit and file
func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*BlameReader, error) { func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*BlameReader, error) {
return createBlameReader(ctx, repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file) cmd := NewCommandContextNoGlobals(ctx, "blame", "--porcelain").
} AddDynamicArguments(commitID).
AddDashesAndList(file).
func createBlameReader(ctx context.Context, dir string, command ...string) (*BlameReader, error) { SetDescription(fmt.Sprintf("GetBlame [repo_path: %s]", repoPath))
// Here we use the provided context - this should be tied to the request performing the blame so that it does not hang around. reader, stdout, err := os.Pipe()
ctx, cancel, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("GetBlame [repo_path: %s]", dir))
cmd := exec.CommandContext(ctx, command[0], command[1:]...)
cmd.Dir = dir
cmd.Stderr = os.Stderr
process.SetSysProcAttribute(cmd)
stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
defer finished() return nil, err
return nil, fmt.Errorf("StdoutPipe: %w", err)
} }
if err = cmd.Start(); err != nil { done := make(chan error, 1)
defer finished()
_ = stdout.Close()
return nil, fmt.Errorf("Start: %w", err)
}
reader := bufio.NewReader(stdout) go func(cmd *Command, dir string, stdout io.WriteCloser, done chan error) {
if err := cmd.Run(&RunOpts{
UseContextTimeout: true,
Dir: dir,
Stdout: stdout,
Stderr: os.Stderr,
}); err == nil {
stdout.Close()
}
done <- err
}(cmd, repoPath, stdout, done)
return &BlameReader{ return &BlameReader{
cmd: cmd, cmd: cmd,
output: stdout, output: stdout,
reader: reader, reader: reader,
cancel: cancel, done: done,
finished: finished,
}, nil }, nil
} }

View file

@ -5,139 +5,36 @@ package git
import ( import (
"context" "context"
"os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const exampleBlame = `
4b92a6c2df28054ad766bc262f308db9f6066596 1 1 1
author Unknown
author-mail <joe2010xtmf@163.com>
author-time 1392833071
author-tz -0500
committer Unknown
committer-mail <joe2010xtmf@163.com>
committer-time 1392833071
committer-tz -0500
summary Add code of delete user
previous be0ba9ea88aff8a658d0495d36accf944b74888d gogs.go
filename gogs.go
// Copyright 2014 The Gogs Authors. All rights reserved.
ce21ed6c3490cdfad797319cbb1145e2330a8fef 2 2 1
author Joubert RedRat
author-mail <eu+github@redrat.com.br>
author-time 1482322397
author-tz -0200
committer Lunny Xiao
committer-mail <xiaolunwen@gmail.com>
committer-time 1482322397
committer-tz +0800
summary Remove remaining Gogs reference on locales and cmd (#430)
previous 618407c018cdf668ceedde7454c42fb22ba422d8 main.go
filename main.go
// Copyright 2016 The Gitea Authors. All rights reserved.
4b92a6c2df28054ad766bc262f308db9f6066596 2 3 2
author Unknown
author-mail <joe2010xtmf@163.com>
author-time 1392833071
author-tz -0500
committer Unknown
committer-mail <joe2010xtmf@163.com>
committer-time 1392833071
committer-tz -0500
summary Add code of delete user
previous be0ba9ea88aff8a658d0495d36accf944b74888d gogs.go
filename gogs.go
// Use of this source code is governed by a MIT-style
4b92a6c2df28054ad766bc262f308db9f6066596 3 4
author Unknown
author-mail <joe2010xtmf@163.com>
author-time 1392833071
author-tz -0500
committer Unknown
committer-mail <joe2010xtmf@163.com>
committer-time 1392833071
committer-tz -0500
summary Add code of delete user
previous be0ba9ea88aff8a658d0495d36accf944b74888d gogs.go
filename gogs.go
// license that can be found in the LICENSE file.
e2aa991e10ffd924a828ec149951f2f20eecead2 6 6 2
author Lunny Xiao
author-mail <xiaolunwen@gmail.com>
author-time 1478872595
author-tz +0800
committer Sandro Santilli
committer-mail <strk@kbt.io>
committer-time 1478872595
committer-tz +0100
summary ask for go get from code.gitea.io/gitea and change gogs to gitea on main file (#146)
previous 5fc370e332171b8658caed771b48585576f11737 main.go
filename main.go
// Gitea (git with a cup of tea) is a painless self-hosted Git Service.
e2aa991e10ffd924a828ec149951f2f20eecead2 7 7
package main // import "code.gitea.io/gitea"
`
func TestReadingBlameOutput(t *testing.T) { func TestReadingBlameOutput(t *testing.T) {
tempFile, err := os.CreateTemp("", ".txt")
if err != nil {
panic(err)
}
defer tempFile.Close()
if _, err = tempFile.WriteString(exampleBlame); err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
blameReader, err := createBlameReader(ctx, "", "cat", tempFile.Name()) blameReader, err := CreateBlameReader(ctx, "./tests/repos/repo5_pulls", "f32b0a9dfd09a60f616f29158f772cedd89942d2", "README.md")
if err != nil { assert.NoError(t, err)
panic(err)
}
defer blameReader.Close() defer blameReader.Close()
parts := []*BlamePart{ parts := []*BlamePart{
{ {
"4b92a6c2df28054ad766bc262f308db9f6066596", "72866af952e98d02a73003501836074b286a78f6",
[]string{ []string{
"// Copyright 2014 The Gogs Authors. All rights reserved.", "# test_repo",
"Test repository for testing migration from github to gitea",
}, },
}, },
{ {
"ce21ed6c3490cdfad797319cbb1145e2330a8fef", "f32b0a9dfd09a60f616f29158f772cedd89942d2",
[]string{ []string{},
"// Copyright 2016 The Gitea Authors. All rights reserved.",
}, },
},
{
"4b92a6c2df28054ad766bc262f308db9f6066596",
[]string{
"// Use of this source code is governed by a MIT-style",
"// license that can be found in the LICENSE file.",
"",
},
},
{
"e2aa991e10ffd924a828ec149951f2f20eecead2",
[]string{
"// Gitea (git with a cup of tea) is a painless self-hosted Git Service.",
"package main // import \"code.gitea.io/gitea\"",
},
},
nil,
} }
for _, part := range parts { for _, part := range parts {
actualPart, err := blameReader.NextPart() actualPart, err := blameReader.NextPart()
if err != nil { assert.NoError(t, err)
panic(err)
}
assert.Equal(t, part, actualPart) assert.Equal(t, part, actualPart)
} }
} }