Skip to content

Commit 2e16e2f

Browse files
author
gitopia1wqynht5cs3lp3mxrr5l33x34tr3qd5hcu0s8du
committed
Merge pull request #11 from git-remote-gitopia/faza/optimize-git-fetch
2 parents 9cfeba2 + a014afd commit 2e16e2f

File tree

3 files changed

+90
-59
lines changed

3 files changed

+90
-59
lines changed

cmd/git-remote-gitopia/gitopia.go

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,36 +145,60 @@ func (h *GitopiaHandler) List(remote *core.Remote, forPush bool) ([]string, erro
145145
return out, nil
146146
}
147147

148-
func (h *GitopiaHandler) Fetch(remote *core.Remote, sha, ref string) error {
148+
func (h *GitopiaHandler) Fetch(remote *core.Remote, refsToFetch []core.RefToFetch) error {
149149
remoteURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, h.remoteRepository.Id)
150150

151-
force := false
152-
if strings.HasPrefix(ref, "+") {
153-
ref = strings.TrimPrefix(ref, "+")
154-
force = true
155-
}
151+
if !remote.Force {
152+
args := []string{
153+
"fetch",
154+
"--no-write-fetch-head",
155+
remoteURL,
156+
}
157+
for _, ref := range refsToFetch {
158+
args = append(args, ref.Ref)
159+
}
160+
cmd, outPipe := core.GitCommand("git", args...)
161+
if err := cmd.Start(); err != nil {
162+
out, e := io.ReadAll(outPipe)
163+
return errors.Wrapf(err, `error fetching from remote repository.
164+
output %s, output read error %s`, string(out), e.Error())
165+
}
166+
defer core.CleanUpProcessGroup(cmd)
167+
cmd.Wait()
156168

157-
args := []string{
158-
"fetch",
159-
remoteURL,
160-
ref,
161-
}
162-
if force {
163-
args = append(args, "--force")
169+
return nil
164170
}
165-
cmd, outPipe := core.GitCommand("git", args...)
166-
if err := cmd.Start(); err != nil {
167-
out, e := io.ReadAll(outPipe)
168-
return errors.Wrapf(err, `error fetching from remote repository.
171+
172+
for _, ref := range refsToFetch {
173+
force := false
174+
if strings.HasPrefix(ref.Ref, "+") {
175+
ref.Ref = strings.TrimPrefix(ref.Ref, "+")
176+
force = true
177+
}
178+
179+
args := []string{
180+
"fetch",
181+
"--no-write-fetch-head",
182+
remoteURL,
183+
ref.Ref,
184+
}
185+
if force {
186+
args = append(args, "--force")
187+
}
188+
cmd, outPipe := core.GitCommand("git", args...)
189+
if err := cmd.Start(); err != nil {
190+
out, e := io.ReadAll(outPipe)
191+
return errors.Wrapf(err, `error fetching from remote repository.
169192
output %s, output read error %s`, string(out), e.Error())
193+
}
194+
defer core.CleanUpProcessGroup(cmd)
195+
cmd.Wait()
170196
}
171-
defer core.CleanUpProcessGroup(cmd)
172-
cmd.Wait()
173197

174198
return nil
175199
}
176200

177-
func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush) (*[]string, error) {
201+
func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush) ([]string, error) {
178202
var err error
179203

180204
if h.wallet == nil {
@@ -213,7 +237,7 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
213237
var setTags []gitopiaTypes.MsgMultiSetTag_Tag
214238
var deleteBranches, deleteTags []string
215239
var res []string
216-
240+
217241
for _, ref := range refsToPush {
218242
if ref.Local == "" {
219243
if strings.HasPrefix(ref.Remote, branchPrefix) {
@@ -340,7 +364,7 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
340364
return nil, err
341365
}
342366

343-
return &res, nil
367+
return res, nil
344368
}
345369

346370
func (h *GitopiaHandler) havePushPermission(walletAddress string) (havePermission bool, err error) {

core/remote.go

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414

1515
type RemoteHandler interface {
1616
List(remote *Remote, forPush bool) ([]string, error)
17-
Fetch(remote *Remote, sha, ref string) error
18-
Push(remote *Remote, refsToPush []RefToPush) (*[]string, error)
17+
Fetch(remote *Remote, refsToFetch []RefToFetch) error
18+
Push(remote *Remote, refsToPush []RefToPush) ([]string, error)
1919

2020
Initialize(remote *Remote) error
2121
}
@@ -30,15 +30,20 @@ type Remote struct {
3030

3131
Handler RemoteHandler
3232

33-
todo []func() (string, error)
34-
pushList []func() (*[]string, error)
33+
todo []func() (string, error)
34+
Force bool
3535
}
3636

3737
type RefToPush struct {
3838
Local string
3939
Remote string
4040
}
4141

42+
type RefToFetch struct {
43+
Sha string
44+
Ref string
45+
}
46+
4247
func NewRemote(handler RemoteHandler, reader io.Reader, writer io.Writer, logger *log.Logger) (*Remote, error) {
4348
localDir, err := GetLocalDir()
4449
if err != nil {
@@ -82,33 +87,19 @@ func (r *Remote) Printf(format string, a ...interface{}) (n int, err error) {
8287
return fmt.Fprintf(r.writer, format, a...)
8388
}
8489

85-
func (r *Remote) push(refsToPush []RefToPush) {
86-
r.pushList = append(r.pushList, func() (*[]string, error) {
87-
locals, err := r.Handler.Push(r, refsToPush)
88-
if err != nil {
89-
return nil, err
90-
}
91-
92-
return locals, nil
93-
94-
//return fmt.Sprintf("ok %s\n", done), nil, nil
95-
})
90+
func (r *Remote) push(refsToPush []RefToPush) ([]string, error) {
91+
return r.Handler.Push(r, refsToPush)
9692
}
9793

98-
func (r *Remote) fetch(sha, ref string) {
99-
r.todo = append(r.todo, func() (string, error) {
100-
err := r.Handler.Fetch(r, sha, ref)
101-
if err != nil {
102-
return "", err
103-
}
104-
105-
return "", nil
106-
})
94+
func (r *Remote) fetch(refsToFetch []RefToFetch) error {
95+
return r.Handler.Fetch(r, refsToFetch)
10796
}
10897

10998
func (r *Remote) ProcessCommands() error {
11099
reader := bufio.NewReader(r.reader)
111100
var refsToPush []RefToPush
101+
var refsToFetch []RefToFetch
102+
112103
prevCommand := ""
113104
loop:
114105
for {
@@ -143,21 +134,26 @@ loop:
143134
//r.push(refs[0], refs[1])
144135
case strings.HasPrefix(command, "fetch "):
145136
parts := strings.Split(command, " ")
146-
r.fetch(parts[1], parts[2])
137+
138+
if strings.HasPrefix(parts[2], "+") {
139+
r.Force = true
140+
}
141+
142+
refsToFetch = append(refsToFetch, RefToFetch{
143+
Sha: parts[1],
144+
Ref: parts[2],
145+
})
146+
// r.fetch(parts[1], parts[2])
147147
case command == "":
148148
fallthrough
149149
case command == "\n":
150150
if strings.HasPrefix(prevCommand, "push ") {
151-
r.push(refsToPush)
152-
var locals *[]string
153-
for _, task := range r.pushList {
154-
locals, err = task()
155-
if err != nil {
156-
return err
157-
}
151+
locals, err := r.push(refsToPush)
152+
if err != nil {
153+
return err
158154
}
159155
if locals != nil {
160-
for _, local := range *locals {
156+
for _, local := range locals {
161157
r.Printf("ok %s\n", local)
162158
}
163159
r.Printf("\n")
@@ -167,6 +163,18 @@ loop:
167163
break loop
168164
}
169165

166+
if strings.HasPrefix(prevCommand, "fetch ") {
167+
err = r.fetch(refsToFetch)
168+
if err != nil {
169+
return err
170+
}
171+
172+
r.Printf("\n")
173+
174+
r.todo = nil
175+
break loop
176+
}
177+
170178
for _, task := range r.todo {
171179
resp, err := task()
172180
if err != nil {

core/util.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ func GitCommand(name string, args ...string) (*exec.Cmd, io.Reader) {
2323
cmd := exec.Command(name, args...)
2424
cmd.Env = os.Environ()
2525

26-
r, _ := cmd.StdoutPipe()
27-
cmd.Stderr = cmd.Stdout
28-
29-
return cmd, r
26+
cmd.Stdout = os.Stdout
27+
cmd.Stderr = os.Stderr
28+
return cmd, nil
3029
}
3130

3231
func CleanUpProcessGroup(cmd *exec.Cmd) {

0 commit comments

Comments
 (0)