Skip to content

Commit efa7085

Browse files
lunnywxiaoguang
andauthored
Use git command instead of exec.Cmd in blame (#22098)
extract from #18147 Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent c59e153 commit efa7085

File tree

2 files changed

+39
-156
lines changed

2 files changed

+39
-156
lines changed

modules/git/blame.go

+31-45
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"os"
12-
"os/exec"
1312
"regexp"
14-
15-
"code.gitea.io/gitea/modules/process"
1613
)
1714

1815
// BlamePart represents block of blame - continuous lines with one sha
@@ -23,12 +20,11 @@ type BlamePart struct {
2320

2421
// BlameReader returns part of file blame one by one
2522
type BlameReader struct {
26-
cmd *exec.Cmd
27-
output io.ReadCloser
28-
reader *bufio.Reader
29-
lastSha *string
30-
cancel context.CancelFunc // Cancels the context that this reader runs in
31-
finished process.FinishedFunc // Tells the process manager we're finished and it can remove the associated process from the process table
23+
cmd *Command
24+
output io.WriteCloser
25+
reader io.ReadCloser
26+
done chan error
27+
lastSha *string
3228
}
3329

3430
var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})")
@@ -37,7 +33,7 @@ var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})")
3733
func (r *BlameReader) NextPart() (*BlamePart, error) {
3834
var blamePart *BlamePart
3935

40-
reader := r.reader
36+
reader := bufio.NewReader(r.reader)
4137

4238
if r.lastSha != nil {
4339
blamePart = &BlamePart{*r.lastSha, make([]string, 0)}
@@ -99,51 +95,41 @@ func (r *BlameReader) NextPart() (*BlamePart, error) {
9995

10096
// Close BlameReader - don't run NextPart after invoking that
10197
func (r *BlameReader) Close() error {
102-
defer r.finished() // Only remove the process from the process table when the underlying command is closed
103-
r.cancel() // However, first cancel our own context early
104-
98+
err := <-r.done
99+
_ = r.reader.Close()
105100
_ = r.output.Close()
106-
107-
if err := r.cmd.Wait(); err != nil {
108-
return fmt.Errorf("Wait: %w", err)
109-
}
110-
111-
return nil
101+
return err
112102
}
113103

114104
// CreateBlameReader creates reader for given repository, commit and file
115105
func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*BlameReader, error) {
116-
return createBlameReader(ctx, repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file)
117-
}
118-
119-
func createBlameReader(ctx context.Context, dir string, command ...string) (*BlameReader, error) {
120-
// Here we use the provided context - this should be tied to the request performing the blame so that it does not hang around.
121-
ctx, cancel, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("GetBlame [repo_path: %s]", dir))
122-
123-
cmd := exec.CommandContext(ctx, command[0], command[1:]...)
124-
cmd.Dir = dir
125-
cmd.Stderr = os.Stderr
126-
process.SetSysProcAttribute(cmd)
127-
128-
stdout, err := cmd.StdoutPipe()
106+
cmd := NewCommandContextNoGlobals(ctx, "blame", "--porcelain").
107+
AddDynamicArguments(commitID).
108+
AddDashesAndList(file).
109+
SetDescription(fmt.Sprintf("GetBlame [repo_path: %s]", repoPath))
110+
reader, stdout, err := os.Pipe()
129111
if err != nil {
130-
defer finished()
131-
return nil, fmt.Errorf("StdoutPipe: %w", err)
112+
return nil, err
132113
}
133114

134-
if err = cmd.Start(); err != nil {
135-
defer finished()
136-
_ = stdout.Close()
137-
return nil, fmt.Errorf("Start: %w", err)
138-
}
115+
done := make(chan error, 1)
139116

140-
reader := bufio.NewReader(stdout)
117+
go func(cmd *Command, dir string, stdout io.WriteCloser, done chan error) {
118+
if err := cmd.Run(&RunOpts{
119+
UseContextTimeout: true,
120+
Dir: dir,
121+
Stdout: stdout,
122+
Stderr: os.Stderr,
123+
}); err == nil {
124+
stdout.Close()
125+
}
126+
done <- err
127+
}(cmd, repoPath, stdout, done)
141128

142129
return &BlameReader{
143-
cmd: cmd,
144-
output: stdout,
145-
reader: reader,
146-
cancel: cancel,
147-
finished: finished,
130+
cmd: cmd,
131+
output: stdout,
132+
reader: reader,
133+
done: done,
148134
}, nil
149135
}

modules/git/blame_test.go

+8-111
Original file line numberDiff line numberDiff line change
@@ -5,139 +5,36 @@ package git
55

66
import (
77
"context"
8-
"os"
98
"testing"
109

1110
"github.com/stretchr/testify/assert"
1211
)
1312

14-
const exampleBlame = `
15-
4b92a6c2df28054ad766bc262f308db9f6066596 1 1 1
16-
author Unknown
17-
author-mail <joe2010xtmf@163.com>
18-
author-time 1392833071
19-
author-tz -0500
20-
committer Unknown
21-
committer-mail <joe2010xtmf@163.com>
22-
committer-time 1392833071
23-
committer-tz -0500
24-
summary Add code of delete user
25-
previous be0ba9ea88aff8a658d0495d36accf944b74888d gogs.go
26-
filename gogs.go
27-
// Copyright 2014 The Gogs Authors. All rights reserved.
28-
ce21ed6c3490cdfad797319cbb1145e2330a8fef 2 2 1
29-
author Joubert RedRat
30-
author-mail <eu+github@redrat.com.br>
31-
author-time 1482322397
32-
author-tz -0200
33-
committer Lunny Xiao
34-
committer-mail <xiaolunwen@gmail.com>
35-
committer-time 1482322397
36-
committer-tz +0800
37-
summary Remove remaining Gogs reference on locales and cmd (#430)
38-
previous 618407c018cdf668ceedde7454c42fb22ba422d8 main.go
39-
filename main.go
40-
// Copyright 2016 The Gitea Authors. All rights reserved.
41-
4b92a6c2df28054ad766bc262f308db9f6066596 2 3 2
42-
author Unknown
43-
author-mail <joe2010xtmf@163.com>
44-
author-time 1392833071
45-
author-tz -0500
46-
committer Unknown
47-
committer-mail <joe2010xtmf@163.com>
48-
committer-time 1392833071
49-
committer-tz -0500
50-
summary Add code of delete user
51-
previous be0ba9ea88aff8a658d0495d36accf944b74888d gogs.go
52-
filename gogs.go
53-
// Use of this source code is governed by a MIT-style
54-
4b92a6c2df28054ad766bc262f308db9f6066596 3 4
55-
author Unknown
56-
author-mail <joe2010xtmf@163.com>
57-
author-time 1392833071
58-
author-tz -0500
59-
committer Unknown
60-
committer-mail <joe2010xtmf@163.com>
61-
committer-time 1392833071
62-
committer-tz -0500
63-
summary Add code of delete user
64-
previous be0ba9ea88aff8a658d0495d36accf944b74888d gogs.go
65-
filename gogs.go
66-
// license that can be found in the LICENSE file.
67-
68-
e2aa991e10ffd924a828ec149951f2f20eecead2 6 6 2
69-
author Lunny Xiao
70-
author-mail <xiaolunwen@gmail.com>
71-
author-time 1478872595
72-
author-tz +0800
73-
committer Sandro Santilli
74-
committer-mail <strk@kbt.io>
75-
committer-time 1478872595
76-
committer-tz +0100
77-
summary ask for go get from code.gitea.io/gitea and change gogs to gitea on main file (#146)
78-
previous 5fc370e332171b8658caed771b48585576f11737 main.go
79-
filename main.go
80-
// Gitea (git with a cup of tea) is a painless self-hosted Git Service.
81-
e2aa991e10ffd924a828ec149951f2f20eecead2 7 7
82-
package main // import "code.gitea.io/gitea"
83-
`
84-
8513
func TestReadingBlameOutput(t *testing.T) {
86-
tempFile, err := os.CreateTemp("", ".txt")
87-
if err != nil {
88-
panic(err)
89-
}
90-
91-
defer tempFile.Close()
92-
93-
if _, err = tempFile.WriteString(exampleBlame); err != nil {
94-
panic(err)
95-
}
9614
ctx, cancel := context.WithCancel(context.Background())
9715
defer cancel()
9816

99-
blameReader, err := createBlameReader(ctx, "", "cat", tempFile.Name())
100-
if err != nil {
101-
panic(err)
102-
}
17+
blameReader, err := CreateBlameReader(ctx, "./tests/repos/repo5_pulls", "f32b0a9dfd09a60f616f29158f772cedd89942d2", "README.md")
18+
assert.NoError(t, err)
10319
defer blameReader.Close()
10420

10521
parts := []*BlamePart{
10622
{
107-
"4b92a6c2df28054ad766bc262f308db9f6066596",
108-
[]string{
109-
"// Copyright 2014 The Gogs Authors. All rights reserved.",
110-
},
111-
},
112-
{
113-
"ce21ed6c3490cdfad797319cbb1145e2330a8fef",
114-
[]string{
115-
"// Copyright 2016 The Gitea Authors. All rights reserved.",
116-
},
117-
},
118-
{
119-
"4b92a6c2df28054ad766bc262f308db9f6066596",
23+
"72866af952e98d02a73003501836074b286a78f6",
12024
[]string{
121-
"// Use of this source code is governed by a MIT-style",
122-
"// license that can be found in the LICENSE file.",
123-
"",
25+
"# test_repo",
26+
"Test repository for testing migration from github to gitea",
12427
},
12528
},
12629
{
127-
"e2aa991e10ffd924a828ec149951f2f20eecead2",
128-
[]string{
129-
"// Gitea (git with a cup of tea) is a painless self-hosted Git Service.",
130-
"package main // import \"code.gitea.io/gitea\"",
131-
},
30+
"f32b0a9dfd09a60f616f29158f772cedd89942d2",
31+
[]string{},
13232
},
133-
nil,
13433
}
13534

13635
for _, part := range parts {
13736
actualPart, err := blameReader.NextPart()
138-
if err != nil {
139-
panic(err)
140-
}
37+
assert.NoError(t, err)
14138
assert.Equal(t, part, actualPart)
14239
}
14340
}

0 commit comments

Comments
 (0)