@@ -8,151 +8,17 @@ import (
88	"context" 
99)
1010
11- type  batchCatFile  struct  {
12- 	cancel  context.CancelFunc 
13- 	Reader  * bufio.Reader 
14- 	Writer  WriteCloserError 
15- }
16- 
17- func  (b  * batchCatFile ) Close () {
18- 	if  b .cancel  !=  nil  {
19- 		b .cancel ()
20- 		b .Reader  =  nil 
21- 		b .Writer  =  nil 
22- 		b .cancel  =  nil 
23- 	}
24- }
25- 
26- type  Batch  interface  {
27- 	Write ([]byte ) (int , error )
28- 	WriteCheck ([]byte ) (int , error )
11+ type  CatFileBatch  interface  {
12+ 	Write ([]byte ) (int , error ) // query object contents 
2913	Reader () * bufio.Reader 
30- 	CheckReader () * bufio.Reader 
31- 	Close ()
32- }
33- 
34- // batchCatFileWithCheck implements the Batch interface using the "cat-file --batch" command and "cat-file --batch-check" command 
35- // ref: https://git-scm.com/docs/git-cat-file#Documentation/git-cat-file.txt---batch 
36- // To align with --batch-command, we creates the two commands both at the same time if git version is lower than 2.36 
37- type  batchCatFileWithCheck  struct  {
38- 	ctx         context.Context 
39- 	repoPath    string 
40- 	batch       * batchCatFile 
41- 	batchCheck  * batchCatFile 
42- }
43- 
44- var  _  Batch  =  & batchCatFileWithCheck {}
45- 
46- // newBatchCatFileWithCheck creates a new batch and a new batch check for the given repository, the Close must be invoked before release the batch 
47- func  newBatchCatFileWithCheck (ctx  context.Context , repoPath  string ) (* batchCatFileWithCheck , error ) {
48- 	// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first! 
49- 	if  err  :=  ensureValidGitRepository (ctx , repoPath ); err  !=  nil  {
50- 		return  nil , err 
51- 	}
52- 
53- 	return  & batchCatFileWithCheck {
54- 		ctx :      ctx ,
55- 		repoPath : repoPath ,
56- 	}, nil 
57- }
58- 
59- func  (b  * batchCatFileWithCheck ) getBatch () * batchCatFile  {
60- 	if  b .batch  !=  nil  {
61- 		return  b .batch 
62- 	}
63- 	b .batch  =  newCatFileBatch (b .ctx , b .repoPath , BatchArg )
64- 	return  b .batch 
65- }
66- 
67- func  (b  * batchCatFileWithCheck ) getBatchCheck () * batchCatFile  {
68- 	if  b .batchCheck  !=  nil  {
69- 		return  b .batchCheck 
70- 	}
71- 	b .batchCheck  =  newCatFileBatch (b .ctx , b .repoPath , BatchCheckArg )
72- 	return  b .batchCheck 
73- }
74- 
75- func  (b  * batchCatFileWithCheck ) Write (bs  []byte ) (int , error ) {
76- 	return  b .getBatch ().Writer .Write (bs )
77- }
78- 
79- func  (b  * batchCatFileWithCheck ) WriteCheck (bs  []byte ) (int , error ) {
80- 	return  b .getBatchCheck ().Writer .Write (bs )
81- }
82- 
83- func  (b  * batchCatFileWithCheck ) Reader () * bufio.Reader  {
84- 	return  b .getBatch ().Reader 
85- }
8614
87- func  (b  * batchCatFileWithCheck ) CheckReader () * bufio.Reader  {
88- 	return  b .getBatchCheck ().Reader 
89- }
90- 
91- func  (b  * batchCatFileWithCheck ) Close () {
92- 	if  b .batch  !=  nil  {
93- 		b .batch .Close ()
94- 		b .batch  =  nil 
95- 	}
96- 	if  b .batchCheck  !=  nil  {
97- 		b .batchCheck .Close ()
98- 		b .batchCheck  =  nil 
99- 	}
100- }
101- 
102- // batchCommandCatFile implements the Batch interface using the "cat-file --batch-command" command 
103- // ref: https://git-scm.com/docs/git-cat-file#Documentation/git-cat-file.txt---batch-command 
104- type  batchCommandCatFile  struct  {
105- 	ctx       context.Context 
106- 	repoPath  string 
107- 	batch     * batchCatFile 
108- }
109- 
110- var  _  Batch  =  & batchCommandCatFile {}
111- 
112- func  newBatchCommandCatFile (ctx  context.Context , repoPath  string ) (* batchCommandCatFile , error ) {
113- 	// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first! 
114- 	if  err  :=  ensureValidGitRepository (ctx , repoPath ); err  !=  nil  {
115- 		return  nil , err 
116- 	}
117- 
118- 	return  & batchCommandCatFile {
119- 		ctx :      ctx ,
120- 		repoPath : repoPath ,
121- 	}, nil 
122- }
123- 
124- func  (b  * batchCommandCatFile ) getBatch () * batchCatFile  {
125- 	if  b .batch  !=  nil  {
126- 		return  b .batch 
127- 	}
128- 	b .batch  =  newCatFileBatch (b .ctx , b .repoPath , BatchCommandArg )
129- 	return  b .batch 
130- }
131- 
132- func  (b  * batchCommandCatFile ) Write (bs  []byte ) (int , error ) {
133- 	return  b .getBatch ().Writer .Write (append ([]byte ("contents " ), bs ... ))
134- }
135- 
136- func  (b  * batchCommandCatFile ) WriteCheck (bs  []byte ) (int , error ) {
137- 	return  b .getBatch ().Writer .Write (append ([]byte ("info " ), bs ... ))
138- }
139- 
140- func  (b  * batchCommandCatFile ) Reader () * bufio.Reader  {
141- 	return  b .getBatch ().Reader 
142- }
143- 
144- func  (b  * batchCommandCatFile ) CheckReader () * bufio.Reader  {
145- 	return  b .getBatch ().Reader 
146- }
15+ 	WriteCheck ([]byte ) (int , error ) // query object info 
16+ 	CheckReader () * bufio.Reader 
14717
148- func  (b  * batchCommandCatFile ) Close () {
149- 	if  b .batch  !=  nil  {
150- 		b .batch .Close ()
151- 		b .batch  =  nil 
152- 	}
18+ 	Close ()
15319}
15420
155- func  NewBatch (ctx  context.Context , repoPath  string ) (Batch , error ) {
21+ func  NewBatch (ctx  context.Context , repoPath  string ) (CatFileBatch , error ) {
15622	if  DefaultFeatures ().SupportCatFileBatchCommand  {
15723		return  newBatchCommandCatFile (ctx , repoPath )
15824	}
0 commit comments