@@ -6,7 +6,6 @@ package attribute
6
6
import (
7
7
"bytes"
8
8
"context"
9
- "errors"
10
9
"fmt"
11
10
"io"
12
11
"os"
@@ -39,70 +38,48 @@ func NewBatchChecker(repo *git.Repository, treeish string, attributes ...string)
39
38
if len (attributes ) == 0 {
40
39
attributes = LinguistAttributes
41
40
}
41
+ cmd , envs , cleanup , err := checkAttrCommand (repo , treeish , nil , attributes )
42
+ if err != nil {
43
+ cancel ()
44
+ return nil , err
45
+ }
46
+ cmd .AddArguments ("--stdin" )
47
+
42
48
checker := & BatchChecker {
43
49
Attributes : attributes ,
44
50
Repo : repo ,
45
51
Treeish : treeish ,
46
52
ctx : ctx ,
47
- cancel : cancel ,
53
+ cancel : func () {
54
+ cancel ()
55
+ cleanup ()
56
+ },
57
+ cmd : cmd ,
58
+ env : envs ,
48
59
}
49
60
50
- if err := checker .init (); err != nil {
51
- log . Error ( "Unable to open attribute checker for commit %s, error: %v" , treeish , err )
52
- checker .Close ()
61
+ checker . stdinReader , checker .stdinWriter , err = os . Pipe ()
62
+ if err != nil {
63
+ checker .cancel ()
53
64
return nil , err
54
65
}
55
66
67
+ lw := new (nulSeparatedAttributeWriter )
68
+ lw .attributes = make (chan attributeTriple , 5 )
69
+ lw .closed = make (chan struct {})
70
+ checker .stdOut = lw
71
+
56
72
go func () {
57
73
err := checker .run (ctx )
58
74
if err != nil && ! git .IsErrCanceledOrKilled (err ) {
59
75
log .Error ("Attribute checker for commit %s exits with error: %v" , treeish , err )
60
76
}
61
- cancel ()
77
+ checker . cancel ()
62
78
}()
63
79
64
80
return checker , nil
65
81
}
66
82
67
- // init initializes the AttributeChecker
68
- func (c * BatchChecker ) init () error {
69
- if len (c .Attributes ) == 0 {
70
- lw := new (nulSeparatedAttributeWriter )
71
- lw .attributes = make (chan attributeTriple )
72
- lw .closed = make (chan struct {})
73
-
74
- c .stdOut = lw
75
- c .stdOut .Close ()
76
- return errors .New ("no provided Attributes to check" )
77
- }
78
-
79
- cmd , envs , cancel , err := checkAttrCommand (c .Repo , c .Treeish , nil , c .Attributes )
80
- if err != nil {
81
- c .cancel ()
82
- return err
83
- }
84
- c .cmd = cmd
85
- c .env = envs
86
- c .cancel = func () {
87
- cancel ()
88
- c .cancel ()
89
- }
90
-
91
- c .cmd .AddArguments ("--stdin" )
92
-
93
- c .stdinReader , c .stdinWriter , err = os .Pipe ()
94
- if err != nil {
95
- c .cancel ()
96
- return err
97
- }
98
-
99
- lw := new (nulSeparatedAttributeWriter )
100
- lw .attributes = make (chan attributeTriple , 5 )
101
- lw .closed = make (chan struct {})
102
- c .stdOut = lw
103
- return nil
104
- }
105
-
106
83
func (c * BatchChecker ) run (ctx context.Context ) error {
107
84
defer func () {
108
85
_ = c .stdinReader .Close ()
0 commit comments