@@ -34,6 +34,48 @@ type BatchChecker struct {
3434	cancel       context.CancelFunc 
3535}
3636
37+ // NewBatchChecker creates a check attribute reader for the current repository and provided commit ID 
38+ func  NewBatchChecker (repo  * git.Repository , treeish  string , attributes  ... string ) (* BatchChecker , error ) {
39+ 	indexFilename , worktree , deleteTemporaryFile , err  :=  repo .ReadTreeToTemporaryIndex (treeish )
40+ 	if  err  !=  nil  {
41+ 		return  nil , err 
42+ 	}
43+ 
44+ 	if  len (attributes ) ==  0  {
45+ 		attributes  =  LinguistAttributes 
46+ 	}
47+ 
48+ 	ctx , cancel  :=  context .WithCancel (repo .Ctx )
49+ 
50+ 	checker  :=  & BatchChecker {
51+ 		Attributes : attributes ,
52+ 		Repo :       repo ,
53+ 		IndexFile :  indexFilename ,
54+ 		WorkTree :   worktree ,
55+ 		ctx :        ctx ,
56+ 		cancel : func () {
57+ 			cancel ()
58+ 			deleteTemporaryFile ()
59+ 		},
60+ 	}
61+ 
62+ 	if  err  :=  checker .init (ctx ); err  !=  nil  {
63+ 		log .Error ("Unable to open attribute checker for commit %s, error: %v" , treeish , err )
64+ 		checker .Close ()
65+ 		return  nil , err 
66+ 	}
67+ 
68+ 	go  func () {
69+ 		err  :=  checker .run (ctx )
70+ 		if  err  !=  nil  &&  ! git .IsErrCanceledOrKilled (err ) {
71+ 			log .Error ("Attribute checker for commit %s exits with error: %v" , treeish , err )
72+ 		}
73+ 		cancel ()
74+ 	}()
75+ 
76+ 	return  checker , nil 
77+ }
78+ 
3779// init initializes the AttributeChecker 
3880func  (c  * BatchChecker ) init (ctx  context.Context ) error  {
3981	if  len (c .Attributes ) ==  0  {
@@ -46,7 +88,6 @@ func (c *BatchChecker) init(ctx context.Context) error {
4688		return  errors .New ("no provided Attributes to check" )
4789	}
4890
49- 	c .ctx , c .cancel  =  context .WithCancel (ctx )
5091	c .cmd  =  git .NewCommand ("check-attr" , "--stdin" , "-z" )
5192
5293	if  len (c .IndexFile ) >  0  {
@@ -77,13 +118,13 @@ func (c *BatchChecker) init(ctx context.Context) error {
77118	return  nil 
78119}
79120
80- func  (c  * BatchChecker ) run () error  {
121+ func  (c  * BatchChecker ) run (ctx  context. Context ) error  {
81122	defer  func () {
82123		_  =  c .stdinReader .Close ()
83124		_  =  c .stdOut .Close ()
84125	}()
85126	stdErr  :=  new (bytes.Buffer )
86- 	err  :=  c .cmd .Run (c . ctx , & git.RunOpts {
127+ 	err  :=  c .cmd .Run (ctx , & git.RunOpts {
87128		Env :    c .env ,
88129		Dir :    c .Repo .Path ,
89130		Stdin :  c .stdinReader ,
@@ -221,46 +262,3 @@ func (wr *nulSeparatedAttributeWriter) Close() error {
221262	close (wr .closed )
222263	return  nil 
223264}
224- 
225- // NewBatchChecker creates a check attribute reader for the current repository and provided commit ID 
226- func  NewBatchChecker (repo  * git.Repository , commitID  string ) (* BatchChecker , context.CancelFunc , error ) {
227- 	indexFilename , worktree , deleteTemporaryFile , err  :=  repo .ReadTreeToTemporaryIndex (commitID )
228- 	if  err  !=  nil  {
229- 		return  nil , nil , err 
230- 	}
231- 
232- 	checker  :=  & BatchChecker {
233- 		Attributes : []string {
234- 			LinguistVendored ,
235- 			LinguistGenerated ,
236- 			LinguistDocumentation ,
237- 			LinguistDetectable ,
238- 			LinguistLanguage ,
239- 			GitlabLanguage ,
240- 		},
241- 		Repo :      repo ,
242- 		IndexFile : indexFilename ,
243- 		WorkTree :  worktree ,
244- 	}
245- 	ctx , cancel  :=  context .WithCancel (repo .Ctx )
246- 	deferrable  :=  func () {
247- 		_  =  checker .Close ()
248- 		cancel ()
249- 		deleteTemporaryFile ()
250- 	}
251- 	if  err  :=  checker .init (ctx ); err  !=  nil  {
252- 		log .Error ("Unable to open attribute checker for commit %s, error: %v" , commitID , err )
253- 		deferrable ()
254- 		return  nil , nil , err 
255- 	}
256- 
257- 	go  func () {
258- 		err  :=  checker .run ()
259- 		if  err  !=  nil  &&  ! git .IsErrCanceledOrKilled (err ) {
260- 			log .Error ("Attribute checker for commit %s exits with error: %v" , commitID , err )
261- 		}
262- 		cancel ()
263- 	}()
264- 
265- 	return  checker , deferrable , nil 
266- }
0 commit comments