Skip to content

Commit c161f7c

Browse files
committed
optimize git fetch
1 parent 9cfeba2 commit c161f7c

File tree

2 files changed

+85
-32
lines changed

2 files changed

+85
-32
lines changed

cmd/git-remote-gitopia/gitopia.go

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,31 +145,53 @@ 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+
remoteURL,
155+
}
156+
for _, ref := range refsToFetch {
157+
args = append(args, ref.Ref)
158+
}
159+
cmd, outPipe := core.GitCommand("git", args...)
160+
if err := cmd.Start(); err != nil {
161+
out, e := io.ReadAll(outPipe)
162+
return errors.Wrapf(err, `error fetching from remote repository.
163+
output %s, output read error %s`, string(out), e.Error())
164+
}
165+
defer core.CleanUpProcessGroup(cmd)
166+
cmd.Wait()
156167

157-
args := []string{
158-
"fetch",
159-
remoteURL,
160-
ref,
161-
}
162-
if force {
163-
args = append(args, "--force")
168+
return nil
164169
}
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.
170+
171+
for _, ref := range refsToFetch {
172+
force := false
173+
if strings.HasPrefix(ref.Ref, "+") {
174+
ref.Ref = strings.TrimPrefix(ref.Ref, "+")
175+
force = true
176+
}
177+
178+
args := []string{
179+
"fetch",
180+
remoteURL,
181+
ref.Ref,
182+
}
183+
if force {
184+
args = append(args, "--force")
185+
}
186+
cmd, outPipe := core.GitCommand("git", args...)
187+
if err := cmd.Start(); err != nil {
188+
out, e := io.ReadAll(outPipe)
189+
return errors.Wrapf(err, `error fetching from remote repository.
169190
output %s, output read error %s`, string(out), e.Error())
191+
}
192+
defer core.CleanUpProcessGroup(cmd)
193+
cmd.Wait()
170194
}
171-
defer core.CleanUpProcessGroup(cmd)
172-
cmd.Wait()
173195

174196
return nil
175197
}
@@ -213,7 +235,7 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
213235
var setTags []gitopiaTypes.MsgMultiSetTag_Tag
214236
var deleteBranches, deleteTags []string
215237
var res []string
216-
238+
217239
for _, ref := range refsToPush {
218240
if ref.Local == "" {
219241
if strings.HasPrefix(ref.Remote, branchPrefix) {

core/remote.go

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

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

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

3131
Handler RemoteHandler
3232

33-
todo []func() (string, error)
34-
pushList []func() (*[]string, error)
33+
todo []func() (string, error)
34+
pushList []func() (*[]string, error)
35+
fetchList []func() error
36+
Force bool
3537
}
3638

3739
type RefToPush struct {
3840
Local string
3941
Remote string
4042
}
4143

44+
type RefToFetch struct {
45+
Sha string
46+
Ref string
47+
}
48+
4249
func NewRemote(handler RemoteHandler, reader io.Reader, writer io.Writer, logger *log.Logger) (*Remote, error) {
4350
localDir, err := GetLocalDir()
4451
if err != nil {
@@ -95,20 +102,17 @@ func (r *Remote) push(refsToPush []RefToPush) {
95102
})
96103
}
97104

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
105+
func (r *Remote) fetch(refsToFetch []RefToFetch) {
106+
r.fetchList = append(r.fetchList, func() error {
107+
return r.Handler.Fetch(r, refsToFetch)
106108
})
107109
}
108110

109111
func (r *Remote) ProcessCommands() error {
110112
reader := bufio.NewReader(r.reader)
111113
var refsToPush []RefToPush
114+
var refsToFetch []RefToFetch
115+
112116
prevCommand := ""
113117
loop:
114118
for {
@@ -143,7 +147,16 @@ loop:
143147
//r.push(refs[0], refs[1])
144148
case strings.HasPrefix(command, "fetch "):
145149
parts := strings.Split(command, " ")
146-
r.fetch(parts[1], parts[2])
150+
151+
if strings.HasPrefix(parts[2], "+") {
152+
r.Force = true
153+
}
154+
155+
refsToFetch = append(refsToFetch, RefToFetch{
156+
Sha: parts[1],
157+
Ref: parts[2],
158+
})
159+
// r.fetch(parts[1], parts[2])
147160
case command == "":
148161
fallthrough
149162
case command == "\n":
@@ -167,6 +180,24 @@ loop:
167180
break loop
168181
}
169182

183+
if strings.HasPrefix(prevCommand, "fetch ") {
184+
r.fetch(refsToFetch)
185+
for _, task := range r.fetchList {
186+
err = task()
187+
if err != nil {
188+
return err
189+
}
190+
}
191+
192+
for range refsToFetch {
193+
r.Printf("ok \n")
194+
}
195+
r.Printf("\n")
196+
197+
r.todo = nil
198+
break loop
199+
}
200+
170201
for _, task := range r.todo {
171202
resp, err := task()
172203
if err != nil {

0 commit comments

Comments
 (0)