| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 9 |
) |
| 10 |
|
| 11 |
var ( |
| 12 |
// ErrInvalidGitProtoRequest is returned by Decode if the input is not a |
| 13 |
// valid git protocol request. |
| 14 |
ErrInvalidGitProtoRequest = fmt.Errorf("invalid git protocol request") |
| 15 |
) |
| 16 |
|
| 17 |
// GitProtoRequest is a command request for the git protocol. |
| 18 |
// It is used to send the command, endpoint, and extra parameters to the |
| 19 |
// remote. |
| 20 |
// See https://git-scm.com/docs/pack-protocol#_git_transport |
| 21 |
type GitProtoRequest struct { |
| 22 |
RequestCommand string |
| 23 |
Pathname string |
| 24 |
|
| 25 |
// Optional |
| 26 |
Host string |
| 27 |
|
| 28 |
// Optional |
| 29 |
ExtraParams []string |
| 30 |
} |
| 31 |
|
| 32 |
// validate validates the request. |
| 33 |
func (g *GitProtoRequest) validate() error { |
| 34 |
if g.RequestCommand == "" { |
| 35 |
return fmt.Errorf("%w: empty request command", ErrInvalidGitProtoRequest) |
| 36 |
} |
| 37 |
|
| 38 |
if g.Pathname == "" { |
| 39 |
return fmt.Errorf("%w: empty pathname", ErrInvalidGitProtoRequest) |
| 40 |
} |
| 41 |
|
| 42 |
return nil |
| 43 |
} |
| 44 |
|
| 45 |
// Encode encodes the request into the writer. |
| 46 |
func (g *GitProtoRequest) Encode(w io.Writer) error { |
| 47 |
if w == nil { |
| 48 |
return ErrNilWriter |
| 49 |
} |
| 50 |
|
| 51 |
if err := g.validate(); err != nil { |
| 52 |
return err |
| 53 |
} |
| 54 |
|
| 55 |
p := pktline.NewEncoder(w) |
| 56 |
req := fmt.Sprintf("%s %s\x00", g.RequestCommand, g.Pathname) |
| 57 |
if host := g.Host; host != "" { |
| 58 |
req += fmt.Sprintf("host=%s\x00", host) |
| 59 |
} |
| 60 |
|
| 61 |
if len(g.ExtraParams) > 0 { |
| 62 |
req += "\x00" |
| 63 |
for _, param := range g.ExtraParams { |
| 64 |
req += param + "\x00" |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if err := p.Encode([]byte(req)); err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
|
| 72 |
return nil |
| 73 |
} |
| 74 |
|
| 75 |
// Decode decodes the request from the reader. |
| 76 |
func (g *GitProtoRequest) Decode(r io.Reader) error { |
| 77 |
s := pktline.NewScanner(r) |
| 78 |
if !s.Scan() { |
| 79 |
err := s.Err() |
| 80 |
if err == nil { |
| 81 |
return ErrInvalidGitProtoRequest |
| 82 |
} |
| 83 |
return err |
| 84 |
} |
| 85 |
|
| 86 |
line := string(s.Bytes()) |
| 87 |
if len(line) == 0 { |
| 88 |
return io.EOF |
| 89 |
} |
| 90 |
|
| 91 |
if line[len(line)-1] != 0 { |
| 92 |
return fmt.Errorf("%w: missing null terminator", ErrInvalidGitProtoRequest) |
| 93 |
} |
| 94 |
|
| 95 |
parts := strings.SplitN(line, " ", 2) |
| 96 |
if len(parts) != 2 { |
| 97 |
return fmt.Errorf("%w: short request", ErrInvalidGitProtoRequest) |
| 98 |
} |
| 99 |
|
| 100 |
g.RequestCommand = parts[0] |
| 101 |
params := strings.Split(parts[1], string(null)) |
| 102 |
if len(params) < 1 { |
| 103 |
return fmt.Errorf("%w: missing pathname", ErrInvalidGitProtoRequest) |
| 104 |
} |
| 105 |
|
| 106 |
g.Pathname = params[0] |
| 107 |
if len(params) > 1 { |
| 108 |
g.Host = strings.TrimPrefix(params[1], "host=") |
| 109 |
} |
| 110 |
|
| 111 |
if len(params) > 2 { |
| 112 |
for _, param := range params[2:] { |
| 113 |
if param != "" { |
| 114 |
g.ExtraParams = append(g.ExtraParams, param) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return nil |
| 120 |
} |