Skip to content

Commit f837b62

Browse files
committed
git.Repository.gitCommand(): new helper function
Change how `repo.path` is passed to the git command. Instead of invoking the command in that directory, set it as the `GIT_DIR` environment variable.
1 parent 3c0a944 commit f837b62

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

git/git.go

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ type Repository struct {
6565
}
6666

6767
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")
7269
out, err := command.Output()
7370
if err != nil {
7471
switch err := err.(type) {
@@ -100,6 +97,12 @@ func NewRepository(path string) (*Repository, error) {
10097
return repo, nil
10198
}
10299

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+
103106
func (repo *Repository) Path() string {
104107
return repo.path
105108
}
@@ -125,8 +128,7 @@ type ReferenceIter struct {
125128
// NewReferenceIter returns an iterator that iterates over all of the
126129
// references in `repo`.
127130
func (repo *Repository) NewReferenceIter() (*ReferenceIter, error) {
128-
command := exec.Command(
129-
"git", "-C", repo.path,
131+
command := repo.gitCommand(
130132
"for-each-ref", "--format=%(objectname) %(objecttype) %(objectsize) %(refname)",
131133
)
132134

@@ -200,10 +202,7 @@ type BatchObjectIter struct {
200202
// fed into its stdin. The output is buffered, so it has to be closed
201203
// before you can be sure to read all of the objects.
202204
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")
207206

208207
in, err := command.StdinPipe()
209208
if err != nil {
@@ -374,9 +373,7 @@ type ObjectIter struct {
374373
func (repo *Repository) NewObjectIter(args ...string) (
375374
*ObjectIter, io.WriteCloser, error,
376375
) {
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...)...)
380377
in1, err := command1.StdinPipe()
381378
if err != nil {
382379
return nil, nil, err
@@ -394,11 +391,7 @@ func (repo *Repository) NewObjectIter(args ...string) (
394391
return nil, nil, err
395392
}
396393

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")
402395
in2, err := command2.StdinPipe()
403396
if err != nil {
404397
out1.Close()
@@ -460,10 +453,7 @@ func (repo *Repository) NewObjectIter(args ...string) (
460453
// `Repository`. `writer` is a function that writes the object in `git
461454
// hash-object` input format. This is used for testing only.
462455
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")
467457
in, err := cmd.StdinPipe()
468458
if err != nil {
469459
return OID{}, err
@@ -508,15 +498,9 @@ func (repo *Repository) UpdateRef(refname string, oid OID) error {
508498
var cmd *exec.Cmd
509499

510500
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)
515502
} 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())
520504
}
521505
return cmd.Run()
522506
}

0 commit comments

Comments
 (0)