Skip to content

Commit b6191ce

Browse files
committed
cleanup git commands
1 parent 2c925bc commit b6191ce

File tree

3 files changed

+16
-40
lines changed

3 files changed

+16
-40
lines changed

cmd/git-gitopia/lfs/init.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package lfs
33
import (
44
"context"
55
"fmt"
6-
"io"
76
"os"
7+
"os/exec"
88
"path"
99
"strings"
1010

@@ -31,22 +31,13 @@ var initCmd = &cobra.Command{
3131

3232
lfsConfigPath := path.Join(dir, ".lfsconfig")
3333
if _, err := os.Stat(lfsConfigPath); os.IsNotExist(err) {
34-
c, buf := core.GitCommand("git", "remote", "get-url", args[0])
35-
if err := c.Start(); err != nil {
36-
return err
37-
}
38-
39-
output, err := io.ReadAll(buf)
34+
c := exec.Command("git", "remote", "get-url", args[0])
35+
output, err := c.Output()
4036
if err != nil {
41-
return err
42-
}
43-
44-
if err := c.Wait(); err != nil {
45-
return err
37+
return errors.Wrap(err, "error reading the remote url")
4638
}
4739

4840
remoteURL := strings.TrimSpace(string(output))
49-
5041
remoteUserId, remoteRepositoryName, err := core.ValidateGitopiaRemoteURL(string(remoteURL))
5142
if err != nil {
5243
return err
@@ -77,14 +68,10 @@ var initCmd = &cobra.Command{
7768
remoteRepository := *res.Repository
7869
lfsURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, remoteRepository.Id)
7970

80-
args := []string{
81-
"config",
71+
cmd := core.GitCommand("git", "config",
8272
fmt.Sprintf("--file=%s", lfsConfigPath),
8373
"lfs.url",
84-
lfsURL,
85-
}
86-
87-
cmd, _ := core.GitCommand("git", args...)
74+
lfsURL)
8875
if err := cmd.Run(); err != nil {
8976
return errors.Wrap(err, "error creating .lfsconfig")
9077
}

cmd/git-remote-gitopia/gitopia.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7-
"io"
87
"math"
98
"os"
109
"strings"
@@ -137,14 +136,10 @@ func (h *GitopiaHandler) Fetch(remote *core.Remote, refsToFetch []core.RefToFetc
137136
for _, ref := range refsToFetch {
138137
args = append(args, ref.Ref)
139138
}
140-
cmd, outPipe := core.GitCommand("git", args...)
141-
if err := cmd.Start(); err != nil {
142-
out, e := io.ReadAll(outPipe)
143-
return errors.Wrapf(err, `error fetching from remote repository.
144-
output %s, output read error %s`, string(out), e.Error())
139+
cmd := core.GitCommand("git", args...)
140+
if err := cmd.Run(); err != nil {
141+
return errors.Wrap(err, "error fetching from remote repository")
145142
}
146-
defer core.CleanUpProcessGroup(cmd)
147-
cmd.Wait()
148143

149144
return nil
150145
}
@@ -165,14 +160,10 @@ func (h *GitopiaHandler) Fetch(remote *core.Remote, refsToFetch []core.RefToFetc
165160
if force {
166161
args = append(args, "--force")
167162
}
168-
cmd, outPipe := core.GitCommand("git", args...)
169-
if err := cmd.Start(); err != nil {
170-
out, e := io.ReadAll(outPipe)
171-
return errors.Wrapf(err, `error fetching from remote repository.
172-
output %s, output read error %s`, string(out), e.Error())
163+
cmd := core.GitCommand("git", args...)
164+
if err := cmd.Run(); err != nil {
165+
return errors.Wrap(err, "error fetching from remote repository")
173166
}
174-
defer core.CleanUpProcessGroup(cmd)
175-
cmd.Wait()
176167
}
177168

178169
return nil
@@ -270,11 +261,10 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
270261
if force {
271262
args = append(args, "--force")
272263
}
273-
cmd, _ := core.GitCommand("git", args...)
264+
cmd := core.GitCommand("git", args...)
274265
if err := cmd.Run(); err != nil {
275266
return nil, errors.Wrap(err, "error pushing to remote repository")
276267
}
277-
defer core.CleanUpProcessGroup(cmd)
278268

279269
// Update ref on gitopia
280270
if strings.HasPrefix(ref.Local, branchPrefix) {

core/util.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package core
22

33
import (
44
"errors"
5-
"io"
65
"os"
76
"os/exec"
87
"path"
@@ -30,13 +29,13 @@ func GetLocalDir() (string, error) {
3029
return localdir, nil
3130
}
3231

33-
func GitCommand(name string, args ...string) (*exec.Cmd, io.Reader) {
32+
func GitCommand(name string, args ...string) *exec.Cmd {
3433
cmd := exec.Command(name, args...)
3534
cmd.Env = os.Environ()
36-
3735
cmd.Stdout = os.Stdout
3836
cmd.Stderr = os.Stderr
39-
return cmd, nil
37+
38+
return cmd
4039
}
4140

4241
func CleanUpProcessGroup(cmd *exec.Cmd) {

0 commit comments

Comments
 (0)