Skip to content

Commit b08fef2

Browse files
committed
Fix git clone in case of lfs repository
Also handle exit status of git command process
1 parent bb5fd4c commit b08fef2

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

cmd/git-remote-gitopia/gitopia.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7-
"io"
8-
"io/ioutil"
97
"math"
108
"os"
9+
"path"
1110
"strings"
1211

1312
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
@@ -123,25 +122,27 @@ func (h *GitopiaHandler) List(remote *core.Remote, forPush bool) ([]string, erro
123122

124123
out = append(out, fmt.Sprintf("@refs/heads/%s HEAD", h.remoteRepository.DefaultBranch))
125124

126-
if _, err := os.Stat(".lfsconfig"); errors.Is(err, os.ErrNotExist) {
125+
dir := os.Getenv("GIT_DIR")
126+
if strings.HasSuffix(dir, "/.git") {
127+
dir = strings.TrimSuffix(dir, "/.git")
128+
}
129+
130+
lfsConfigPath := path.Join(dir, ".lfsconfig")
131+
if _, err := os.Stat(lfsConfigPath); os.IsNotExist(err) {
127132
lfsURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, h.remoteRepository.Id)
128133

129134
args := []string{
130135
"config",
131-
"--file=.lfsconfig",
136+
fmt.Sprintf("--file=%s", lfsConfigPath),
132137
"lfs.url",
133138
lfsURL,
134139
}
135140

136-
cmd, pipe := core.GitCommand("git", args...)
137-
if err := cmd.Start(); err != nil {
138-
return nil, err
141+
cmd, _ := core.GitCommand("git", args...)
142+
if err := cmd.Run(); err != nil {
143+
return nil, errors.Wrap(err, "error creating .lfsconfig")
139144
}
140145
defer core.CleanUpProcessGroup(cmd)
141-
142-
if _, err := io.Copy(ioutil.Discard, pipe); err != nil {
143-
return nil, err
144-
}
145146
}
146147

147148
return out, nil
@@ -164,16 +165,12 @@ func (h *GitopiaHandler) Fetch(remote *core.Remote, sha, ref string) error {
164165
if force {
165166
args = append(args, "--force")
166167
}
167-
cmd, pipe := core.GitCommand("git", args...)
168-
if err := cmd.Start(); err != nil {
169-
return err
168+
cmd, _ := core.GitCommand("git", args...)
169+
if err := cmd.Run(); err != nil {
170+
return errors.Wrap(err, "error fetching from remote repository")
170171
}
171172
defer core.CleanUpProcessGroup(cmd)
172173

173-
if _, err := io.Copy(ioutil.Discard, pipe); err != nil {
174-
return err
175-
}
176-
177174
return nil
178175
}
179176

@@ -269,16 +266,12 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
269266
if force {
270267
args = append(args, "--force")
271268
}
272-
cmd, pipe := core.GitCommand("git", args...)
273-
if err := cmd.Start(); err != nil {
274-
return nil, err
269+
cmd, _ := core.GitCommand("git", args...)
270+
if err := cmd.Run(); err != nil {
271+
return nil, errors.Wrap(err, "error pushing to remote repository")
275272
}
276273
defer core.CleanUpProcessGroup(cmd)
277274

278-
if _, err := io.Copy(ioutil.Discard, pipe); err != nil {
279-
return nil, err
280-
}
281-
282275
// Update ref on gitopia
283276
if strings.HasPrefix(ref.Local, branchPrefix) {
284277
localCommitHash, err := remote.Repo.ResolveRevision(plumbing.Revision(ref.Local))

0 commit comments

Comments
 (0)