@@ -65,10 +65,7 @@ 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
- )
68
+ command := exec .Command ("git" , "-C" , path , "rev-parse" , "--git-dir" )
72
69
out , err := command .Output ()
73
70
if err != nil {
74
71
switch err := err .(type ) {
@@ -100,6 +97,12 @@ func NewRepository(path string) (*Repository, error) {
100
97
return repo , nil
101
98
}
102
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
+
103
106
func (repo * Repository ) Path () string {
104
107
return repo .path
105
108
}
@@ -125,8 +128,7 @@ type ReferenceIter struct {
125
128
// NewReferenceIter returns an iterator that iterates over all of the
126
129
// references in `repo`.
127
130
func (repo * Repository ) NewReferenceIter () (* ReferenceIter , error ) {
128
- command := exec .Command (
129
- "git" , "-C" , repo .path ,
131
+ command := repo .gitCommand (
130
132
"for-each-ref" , "--format=%(objectname) %(objecttype) %(objectsize) %(refname)" ,
131
133
)
132
134
@@ -200,10 +202,7 @@ type BatchObjectIter struct {
200
202
// fed into its stdin. The output is buffered, so it has to be closed
201
203
// before you can be sure to read all of the objects.
202
204
func (repo * Repository ) NewBatchObjectIter () (* BatchObjectIter , io.WriteCloser , error ) {
203
- command := exec .Command (
204
- "git" , "-C" , repo .path ,
205
- "cat-file" , "--batch" , "--buffer" ,
206
- )
205
+ command := repo .gitCommand ("cat-file" , "--batch" , "--buffer" )
207
206
208
207
in , err := command .StdinPipe ()
209
208
if err != nil {
@@ -374,9 +373,7 @@ type ObjectIter struct {
374
373
func (repo * Repository ) NewObjectIter (args ... string ) (
375
374
* ObjectIter , io.WriteCloser , error ,
376
375
) {
377
- cmdArgs := []string {"-C" , repo .path , "rev-list" , "--objects" }
378
- cmdArgs = append (cmdArgs , args ... )
379
- command1 := exec .Command ("git" , cmdArgs ... )
376
+ command1 := repo .gitCommand (append ([]string {"rev-list" , "--objects" }, args ... )... )
380
377
in1 , err := command1 .StdinPipe ()
381
378
if err != nil {
382
379
return nil , nil , err
@@ -394,11 +391,7 @@ func (repo *Repository) NewObjectIter(args ...string) (
394
391
return nil , nil , err
395
392
}
396
393
397
- command2 := exec .Command (
398
- "git" , "-C" , repo .path ,
399
- "cat-file" , "--batch-check" , "--buffer" ,
400
- )
401
-
394
+ command2 := repo .gitCommand ("cat-file" , "--batch-check" , "--buffer" )
402
395
in2 , err := command2 .StdinPipe ()
403
396
if err != nil {
404
397
out1 .Close ()
@@ -460,10 +453,7 @@ func (repo *Repository) NewObjectIter(args ...string) (
460
453
// `Repository`. `writer` is a function that writes the object in `git
461
454
// hash-object` input format. This is used for testing only.
462
455
func (repo * Repository ) CreateObject (t ObjectType , writer func (io.Writer ) error ) (OID , error ) {
463
- cmd := exec .Command (
464
- "git" , "-C" , repo .path ,
465
- "hash-object" , "-w" , "-t" , string (t ), "--stdin" ,
466
- )
456
+ cmd := repo .gitCommand ("hash-object" , "-w" , "-t" , string (t ), "--stdin" )
467
457
in , err := cmd .StdinPipe ()
468
458
if err != nil {
469
459
return OID {}, err
@@ -508,15 +498,9 @@ func (repo *Repository) UpdateRef(refname string, oid OID) error {
508
498
var cmd * exec.Cmd
509
499
510
500
if oid == NullOID {
511
- cmd = exec .Command (
512
- "git" , "-C" , repo .path ,
513
- "update-ref" , "-d" , refname ,
514
- )
501
+ cmd = repo .gitCommand ("update-ref" , "-d" , refname )
515
502
} else {
516
- cmd = exec .Command (
517
- "git" , "-C" , repo .path ,
518
- "update-ref" , refname , oid .String (),
519
- )
503
+ cmd = repo .gitCommand ("update-ref" , refname , oid .String ())
520
504
}
521
505
return cmd .Run ()
522
506
}
0 commit comments