@@ -65,11 +65,8 @@ type Repository struct {
65
65
}
66
66
67
67
func NewRepository (path string ) (* Repository , error ) {
68
- command := exec .Command (
69
- "git" , "-C" , path ,
70
- "rev-parse" , "--git-dir" ,
71
- )
72
- out , err := command .Output ()
68
+ cmd := exec .Command ("git" , "-C" , path , "rev-parse" , "--git-dir" )
69
+ out , err := cmd .Output ()
73
70
if err != nil {
74
71
switch err := err .(type ) {
75
72
case * exec.Error :
@@ -90,13 +87,22 @@ func NewRepository(path string) (*Repository, error) {
90
87
return nil , err
91
88
}
92
89
}
93
- gitDir := filepath .Join (path , string (bytes .TrimSpace (out )))
90
+ gitDir := string (bytes .TrimSpace (out ))
91
+ if ! filepath .IsAbs (gitDir ) {
92
+ gitDir = filepath .Join (path , gitDir )
93
+ }
94
94
repo := & Repository {
95
95
path : gitDir ,
96
96
}
97
97
return repo , nil
98
98
}
99
99
100
+ func (repo * Repository ) gitCommand (args ... string ) * exec.Cmd {
101
+ cmd := exec .Command ("git" , args ... )
102
+ cmd .Env = append (os .Environ (), "GIT_DIR=" + repo .path )
103
+ return cmd
104
+ }
105
+
100
106
func (repo * Repository ) Path () string {
101
107
return repo .path
102
108
}
@@ -113,7 +119,7 @@ type Reference struct {
113
119
}
114
120
115
121
type ReferenceIter struct {
116
- command * exec.Cmd
122
+ cmd * exec.Cmd
117
123
out io.ReadCloser
118
124
f * bufio.Reader
119
125
errChan <- chan error
@@ -122,25 +128,24 @@ type ReferenceIter struct {
122
128
// NewReferenceIter returns an iterator that iterates over all of the
123
129
// references in `repo`.
124
130
func (repo * Repository ) NewReferenceIter () (* ReferenceIter , error ) {
125
- command := exec .Command (
126
- "git" , "-C" , repo .path ,
131
+ cmd := repo .gitCommand (
127
132
"for-each-ref" , "--format=%(objectname) %(objecttype) %(objectsize) %(refname)" ,
128
133
)
129
134
130
- out , err := command .StdoutPipe ()
135
+ out , err := cmd .StdoutPipe ()
131
136
if err != nil {
132
137
return nil , err
133
138
}
134
139
135
- command .Stderr = os .Stderr
140
+ cmd .Stderr = os .Stderr
136
141
137
- err = command .Start ()
142
+ err = cmd .Start ()
138
143
if err != nil {
139
144
return nil , err
140
145
}
141
146
142
147
return & ReferenceIter {
143
- command : command ,
148
+ cmd : cmd ,
144
149
out : out ,
145
150
f : bufio .NewReader (out ),
146
151
errChan : make (chan error , 1 ),
@@ -180,49 +185,46 @@ func (iter *ReferenceIter) Next() (Reference, bool, error) {
180
185
181
186
func (l * ReferenceIter ) Close () error {
182
187
err := l .out .Close ()
183
- err2 := l .command .Wait ()
188
+ err2 := l .cmd .Wait ()
184
189
if err == nil {
185
190
err = err2
186
191
}
187
192
return err
188
193
}
189
194
190
195
type BatchObjectIter struct {
191
- command * exec.Cmd
192
- out io.ReadCloser
193
- f * bufio.Reader
196
+ cmd * exec.Cmd
197
+ out io.ReadCloser
198
+ f * bufio.Reader
194
199
}
195
200
196
201
// NewBatchObjectIter returns iterates over objects whose names are
197
202
// fed into its stdin. The output is buffered, so it has to be closed
198
203
// before you can be sure to read all of the objects.
199
204
func (repo * Repository ) NewBatchObjectIter () (* BatchObjectIter , io.WriteCloser , error ) {
200
- command := exec .Command (
201
- "git" , "-C" , repo .path ,
202
- "cat-file" , "--batch" , "--buffer" ,
203
- )
205
+ cmd := repo .gitCommand ("cat-file" , "--batch" , "--buffer" )
204
206
205
- in , err := command .StdinPipe ()
207
+ in , err := cmd .StdinPipe ()
206
208
if err != nil {
207
209
return nil , nil , err
208
210
}
209
211
210
- out , err := command .StdoutPipe ()
212
+ out , err := cmd .StdoutPipe ()
211
213
if err != nil {
212
214
return nil , nil , err
213
215
}
214
216
215
- command .Stderr = os .Stderr
217
+ cmd .Stderr = os .Stderr
216
218
217
- err = command .Start ()
219
+ err = cmd .Start ()
218
220
if err != nil {
219
221
return nil , nil , err
220
222
}
221
223
222
224
return & BatchObjectIter {
223
- command : command ,
224
- out : out ,
225
- f : bufio .NewReader (out ),
225
+ cmd : cmd ,
226
+ out : out ,
227
+ f : bufio .NewReader (out ),
226
228
}, in , nil
227
229
}
228
230
@@ -247,7 +249,7 @@ func (iter *BatchObjectIter) Next() (OID, ObjectType, counts.Count32, []byte, er
247
249
248
250
func (l * BatchObjectIter ) Close () error {
249
251
err := l .out .Close ()
250
- err2 := l .command .Wait ()
252
+ err2 := l .cmd .Wait ()
251
253
if err == nil {
252
254
err = err2
253
255
}
@@ -355,13 +357,13 @@ func parseBatchHeader(spec string, header string) (OID, ObjectType, counts.Count
355
357
}
356
358
357
359
type ObjectIter struct {
358
- command1 * exec.Cmd
359
- command2 * exec.Cmd
360
- in1 io.Writer
361
- out1 io.ReadCloser
362
- out2 io.ReadCloser
363
- f * bufio.Reader
364
- errChan <- chan error
360
+ cmd1 * exec.Cmd
361
+ cmd2 * exec.Cmd
362
+ in1 io.Writer
363
+ out1 io.ReadCloser
364
+ out2 io.ReadCloser
365
+ f * bufio.Reader
366
+ errChan <- chan error
365
367
}
366
368
367
369
// NewObjectIter returns an iterator that iterates over objects in
@@ -371,49 +373,43 @@ type ObjectIter struct {
371
373
func (repo * Repository ) NewObjectIter (args ... string ) (
372
374
* ObjectIter , io.WriteCloser , error ,
373
375
) {
374
- cmdArgs := []string {"-C" , repo .path , "rev-list" , "--objects" }
375
- cmdArgs = append (cmdArgs , args ... )
376
- command1 := exec .Command ("git" , cmdArgs ... )
377
- in1 , err := command1 .StdinPipe ()
376
+ cmd1 := repo .gitCommand (append ([]string {"rev-list" , "--objects" }, args ... )... )
377
+ in1 , err := cmd1 .StdinPipe ()
378
378
if err != nil {
379
379
return nil , nil , err
380
380
}
381
381
382
- out1 , err := command1 .StdoutPipe ()
382
+ out1 , err := cmd1 .StdoutPipe ()
383
383
if err != nil {
384
384
return nil , nil , err
385
385
}
386
386
387
- command1 .Stderr = os .Stderr
387
+ cmd1 .Stderr = os .Stderr
388
388
389
- err = command1 .Start ()
389
+ err = cmd1 .Start ()
390
390
if err != nil {
391
391
return nil , nil , err
392
392
}
393
393
394
- command2 := exec .Command (
395
- "git" , "-C" , repo .path ,
396
- "cat-file" , "--batch-check" , "--buffer" ,
397
- )
398
-
399
- in2 , err := command2 .StdinPipe ()
394
+ cmd2 := repo .gitCommand ("cat-file" , "--batch-check" , "--buffer" )
395
+ in2 , err := cmd2 .StdinPipe ()
400
396
if err != nil {
401
397
out1 .Close ()
402
- command1 .Wait ()
398
+ cmd1 .Wait ()
403
399
return nil , nil , err
404
400
}
405
401
406
- out2 , err := command2 .StdoutPipe ()
402
+ out2 , err := cmd2 .StdoutPipe ()
407
403
if err != nil {
408
404
in2 .Close ()
409
405
out1 .Close ()
410
- command1 .Wait ()
406
+ cmd1 .Wait ()
411
407
return nil , nil , err
412
408
}
413
409
414
- command2 .Stderr = os .Stderr
410
+ cmd2 .Stderr = os .Stderr
415
411
416
- err = command2 .Start ()
412
+ err = cmd2 .Start ()
417
413
if err != nil {
418
414
return nil , nil , err
419
415
}
@@ -444,23 +440,20 @@ func (repo *Repository) NewObjectIter(args ...string) (
444
440
}()
445
441
446
442
return & ObjectIter {
447
- command1 : command1 ,
448
- command2 : command2 ,
449
- out1 : out1 ,
450
- out2 : out2 ,
451
- f : bufio .NewReader (out2 ),
452
- errChan : errChan ,
443
+ cmd1 : cmd1 ,
444
+ cmd2 : cmd2 ,
445
+ out1 : out1 ,
446
+ out2 : out2 ,
447
+ f : bufio .NewReader (out2 ),
448
+ errChan : errChan ,
453
449
}, in1 , nil
454
450
}
455
451
456
452
// CreateObject creates a new Git object, of the specified type, in
457
453
// `Repository`. `writer` is a function that writes the object in `git
458
454
// hash-object` input format. This is used for testing only.
459
455
func (repo * Repository ) CreateObject (t ObjectType , writer func (io.Writer ) error ) (OID , error ) {
460
- cmd := exec .Command (
461
- "git" , "-C" , repo .path ,
462
- "hash-object" , "-w" , "-t" , string (t ), "--stdin" ,
463
- )
456
+ cmd := repo .gitCommand ("hash-object" , "-w" , "-t" , string (t ), "--stdin" )
464
457
in , err := cmd .StdinPipe ()
465
458
if err != nil {
466
459
return OID {}, err
@@ -505,15 +498,9 @@ func (repo *Repository) UpdateRef(refname string, oid OID) error {
505
498
var cmd * exec.Cmd
506
499
507
500
if oid == NullOID {
508
- cmd = exec .Command (
509
- "git" , "-C" , repo .path ,
510
- "update-ref" , "-d" , refname ,
511
- )
501
+ cmd = repo .gitCommand ("update-ref" , "-d" , refname )
512
502
} else {
513
- cmd = exec .Command (
514
- "git" , "-C" , repo .path ,
515
- "update-ref" , refname , oid .String (),
516
- )
503
+ cmd = repo .gitCommand ("update-ref" , refname , oid .String ())
517
504
}
518
505
return cmd .Run ()
519
506
}
@@ -532,11 +519,11 @@ func (l *ObjectIter) Close() error {
532
519
l .out1 .Close ()
533
520
err := <- l .errChan
534
521
l .out2 .Close ()
535
- err2 := l .command1 .Wait ()
522
+ err2 := l .cmd1 .Wait ()
536
523
if err == nil {
537
524
err = err2
538
525
}
539
- err2 = l .command2 .Wait ()
526
+ err2 = l .cmd2 .Wait ()
540
527
if err == nil {
541
528
err = err2
542
529
}
0 commit comments