| 1 |
package common |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp" |
| 9 |
"github.com/go-git/go-git/v5/plumbing/transport" |
| 10 |
"github.com/go-git/go-git/v5/utils/ioutil" |
| 11 |
) |
| 12 |
|
| 13 |
// ServerCommand is used for a single server command execution. |
| 14 |
type ServerCommand struct { |
| 15 |
Stderr io.Writer |
| 16 |
Stdout io.WriteCloser |
| 17 |
Stdin io.Reader |
| 18 |
} |
| 19 |
|
| 20 |
func ServeUploadPack(cmd ServerCommand, s transport.UploadPackSession) (err error) { |
| 21 |
ioutil.CheckClose(cmd.Stdout, &err) |
| 22 |
|
| 23 |
ar, err := s.AdvertisedReferences() |
| 24 |
if err != nil { |
| 25 |
return err |
| 26 |
} |
| 27 |
|
| 28 |
if err := ar.Encode(cmd.Stdout); err != nil { |
| 29 |
return err |
| 30 |
} |
| 31 |
|
| 32 |
req := packp.NewUploadPackRequest() |
| 33 |
if err := req.Decode(cmd.Stdin); err != nil { |
| 34 |
return err |
| 35 |
} |
| 36 |
|
| 37 |
var resp *packp.UploadPackResponse |
| 38 |
resp, err = s.UploadPack(context.TODO(), req) |
| 39 |
if err != nil { |
| 40 |
return err |
| 41 |
} |
| 42 |
|
| 43 |
return resp.Encode(cmd.Stdout) |
| 44 |
} |
| 45 |
|
| 46 |
func ServeReceivePack(cmd ServerCommand, s transport.ReceivePackSession) error { |
| 47 |
ar, err := s.AdvertisedReferences() |
| 48 |
if err != nil { |
| 49 |
return fmt.Errorf("internal error in advertised references: %s", err) |
| 50 |
} |
| 51 |
|
| 52 |
if err := ar.Encode(cmd.Stdout); err != nil { |
| 53 |
return fmt.Errorf("error in advertised references encoding: %s", err) |
| 54 |
} |
| 55 |
|
| 56 |
req := packp.NewReferenceUpdateRequest() |
| 57 |
if err := req.Decode(cmd.Stdin); err != nil { |
| 58 |
return fmt.Errorf("error decoding: %s", err) |
| 59 |
} |
| 60 |
|
| 61 |
rs, err := s.ReceivePack(context.TODO(), req) |
| 62 |
if rs != nil { |
| 63 |
if err := rs.Encode(cmd.Stdout); err != nil { |
| 64 |
return fmt.Errorf("error in encoding report status %s", err) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if err != nil { |
| 69 |
return fmt.Errorf("error in receive pack: %s", err) |
| 70 |
} |
| 71 |
|
| 72 |
return nil |
| 73 |
} |