77 "bytes"
88 "context"
99 "fmt"
10- "io"
1110 "os"
1211 "path/filepath"
1312 "time"
@@ -19,15 +18,11 @@ import (
1918// BatchChecker provides a reader for check-attribute content that can be long running
2019type BatchChecker struct {
2120 // params
22- Attributes []string
23- Repo * git.Repository
24- Treeish string
21+ attributesNum int
22+ repo * git.Repository
2523
26- stdinReader io.ReadCloser
2724 stdinWriter * os.File
2825 stdOut * nulSeparatedAttributeWriter
29- cmd * git.Command
30- env []string
3126 ctx context.Context
3227 cancel context.CancelFunc
3328}
@@ -46,31 +41,41 @@ func NewBatchChecker(repo *git.Repository, treeish string, attributes ...string)
4641 cmd .AddArguments ("--stdin" )
4742
4843 checker := & BatchChecker {
49- Attributes : attributes ,
50- Repo : repo ,
51- Treeish : treeish ,
52- ctx : ctx ,
44+ attributesNum : len (attributes ),
45+ repo : repo ,
46+ ctx : ctx ,
5347 cancel : func () {
5448 cancel ()
5549 cleanup ()
5650 },
57- cmd : cmd ,
58- env : envs ,
5951 }
6052
61- checker . stdinReader , checker . stdinWriter , err = os .Pipe ()
53+ stdinReader , stdinWriter , err : = os .Pipe ()
6254 if err != nil {
6355 checker .cancel ()
6456 return nil , err
6557 }
58+ checker .stdinWriter = stdinWriter
6659
6760 lw := new (nulSeparatedAttributeWriter )
6861 lw .attributes = make (chan attributeTriple , 5 )
6962 lw .closed = make (chan struct {})
7063 checker .stdOut = lw
7164
7265 go func () {
73- err := checker .run (ctx )
66+ defer func () {
67+ _ = stdinReader .Close ()
68+ _ = lw .Close ()
69+ }()
70+ stdErr := new (bytes.Buffer )
71+ err := cmd .Run (ctx , & git.RunOpts {
72+ Env : envs ,
73+ Dir : repo .Path ,
74+ Stdin : stdinReader ,
75+ Stdout : lw ,
76+ Stderr : stdErr ,
77+ })
78+
7479 if err != nil && ! git .IsErrCanceledOrKilled (err ) {
7580 log .Error ("Attribute checker for commit %s exits with error: %v" , treeish , err )
7681 }
@@ -80,30 +85,11 @@ func NewBatchChecker(repo *git.Repository, treeish string, attributes ...string)
8085 return checker , nil
8186}
8287
83- func (c * BatchChecker ) run (ctx context.Context ) error {
84- defer func () {
85- _ = c .stdinReader .Close ()
86- _ = c .stdOut .Close ()
87- }()
88- stdErr := new (bytes.Buffer )
89- err := c .cmd .Run (ctx , & git.RunOpts {
90- Env : c .env ,
91- Dir : c .Repo .Path ,
92- Stdin : c .stdinReader ,
93- Stdout : c .stdOut ,
94- Stderr : stdErr ,
95- })
96- if err != nil && ! git .IsErrCanceledOrKilled (err ) {
97- return fmt .Errorf ("failed to run attr-check. Error: %w\n Stderr: %s" , err , stdErr .String ())
98- }
99- return nil
100- }
101-
10288// CheckPath check attr for given path
10389func (c * BatchChecker ) CheckPath (path string ) (rs Attributes , err error ) {
10490 defer func () {
10591 if err != nil && err != c .ctx .Err () {
106- log .Error ("Unexpected error when checking path %s in %s, error: %v" , path , filepath .Base (c .Repo .Path ), err )
92+ log .Error ("Unexpected error when checking path %s in %s, error: %v" , path , filepath .Base (c .repo .Path ), err )
10793 }
10894 }()
10995
@@ -125,7 +111,7 @@ func (c *BatchChecker) CheckPath(path string) (rs Attributes, err error) {
125111 stdOutClosed = true
126112 default :
127113 }
128- debugMsg := fmt .Sprintf ("check path %q in repo %q" , path , filepath .Base (c .Repo .Path ))
114+ debugMsg := fmt .Sprintf ("check path %q in repo %q" , path , filepath .Base (c .repo .Path ))
129115 debugMsg += fmt .Sprintf (", stdOut: tmp=%q, pos=%d, closed=%v" , string (c .stdOut .tmp ), c .stdOut .pos , stdOutClosed )
130116 // FIXME:
131117 //if c.cmd.cmd != nil {
@@ -136,7 +122,7 @@ func (c *BatchChecker) CheckPath(path string) (rs Attributes, err error) {
136122 }
137123
138124 rs = make (map [string ]Attribute )
139- for range c . Attributes {
125+ for i := 0 ; i < c . attributesNum ; i ++ {
140126 select {
141127 case <- time .After (5 * time .Second ):
142128 // There is a strange "hang" problem in gitdiff.GetDiff -> CheckPath
0 commit comments