@@ -14,8 +14,8 @@ import (
1414
1515type 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
3737type RefToPush struct {
3838 Local string
3939 Remote string
4040}
4141
42+ type RefToFetch struct {
43+ Sha string
44+ Ref string
45+ }
46+
4247func 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
10998func (r * Remote ) ProcessCommands () error {
11099 reader := bufio .NewReader (r .reader )
111100 var refsToPush []RefToPush
101+ var refsToFetch []RefToFetch
102+
112103 prevCommand := ""
113104loop:
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 {
0 commit comments