Skip to content

Commit fe1b11b

Browse files
GiteaBotwxiaoguang
andauthored
Close stdout correctly for "git blame" (#26470) (#26473)
Backport #26470 by @wxiaoguang Close stdout correctly for "git blame", otherwise the failed "git blame" would cause the request hanging forever. And "os.Stderr" should never (seldom) be used as git command's stderr (there seems some similar problems in code, they could be fixed later). Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 80d7288 commit fe1b11b

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

modules/git/blame.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ package git
55

66
import (
77
"bufio"
8+
"bytes"
89
"context"
910
"fmt"
1011
"io"
1112
"os"
1213
"regexp"
14+
15+
"code.gitea.io/gitea/modules/log"
1316
)
1417

1518
// BlamePart represents block of blame - continuous lines with one sha
@@ -115,15 +118,19 @@ func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*B
115118
done := make(chan error, 1)
116119

117120
go func(cmd *Command, dir string, stdout io.WriteCloser, done chan error) {
118-
if err := cmd.Run(&RunOpts{
121+
stderr := bytes.Buffer{}
122+
// TODO: it doesn't work for directories (the directories shouldn't be "blamed"), and the "err" should be returned by "Read" but not by "Close"
123+
err := cmd.Run(&RunOpts{
119124
UseContextTimeout: true,
120125
Dir: dir,
121126
Stdout: stdout,
122-
Stderr: os.Stderr,
123-
}); err == nil {
124-
stdout.Close()
125-
}
127+
Stderr: &stderr,
128+
})
126129
done <- err
130+
_ = stdout.Close()
131+
if err != nil {
132+
log.Error("Error running git blame (dir: %v): %v, stderr: %v", repoPath, err, stderr.String())
133+
}
127134
}(cmd, repoPath, stdout, done)
128135

129136
bufferedReader := bufio.NewReader(reader)

0 commit comments

Comments
 (0)