| 1 | // Package git contains various commands that shell out to git |
| 2 | // NOTE: Subject to change, do not rely on this package from outside git-lfs source |
| 3 | package git |
| 4 | |
| 5 | import ( |
| 6 | "io" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/git-lfs/git-lfs/v3/errors" |
| 11 | "github.com/git-lfs/git-lfs/v3/tr" |
| 12 | "github.com/git-lfs/pktline" |
| 13 | "github.com/rubyist/tracerx" |
| 14 | ) |
| 15 | |
| 16 | // FilterProcessScanner provides a scanner-like interface capable of |
| 17 | // initializing the filter process with the Git parent, and scanning for |
| 18 | // requests across the protocol. |
| 19 | // |
| 20 | // Reading a request (and errors) is as follows: |
| 21 | // |
| 22 | // s := NewFilterProcessScanner(os.Stdin, os.Stderr) |
| 23 | // for s.Scan() { |
| 24 | // req := s.Request() |
| 25 | // // ... |
| 26 | // } |
| 27 | // |
| 28 | // if err := s.Err(); err != nil { |
| 29 | // // ... |
| 30 | // } |
| 31 | type FilterProcessScanner struct { |
| 32 | // pl is the *pktline instance used to read and write packets back and |
| 33 | // forth between Git. |
| 34 | pl *pktline.Pktline |
| 35 | |
| 36 | // req is a temporary variable used to hold the value accessible by the |
| 37 | // `Request()` function. It is cleared at the beginning of each `Scan()` |
| 38 | // invocation, and written to at the end of each `Scan()` invocation. |
| 39 | req *Request |
| 40 | // err is a temporary variable used to hold the value accessible by the |
| 41 | // `Request()` function. It is cleared at the beginning of each `Scan()` |
| 42 | // invocation, and written to at the end of each `Scan()` invocation. |
| 43 | err error |
| 44 | } |
| 45 | |
| 46 | // NewFilterProcessScanner constructs a new instance of the |
| 47 | // `*FilterProcessScanner` type which reads packets from the `io.Reader` "r", |
| 48 | // and writes packets to the `io.Writer`, "w". |
| 49 | // |
| 50 | // Both reader and writers SHOULD NOT be `*git.PacketReader` or |
| 51 | // `*git.PacketWriter`s, they will be transparently treated as such. In other |
| 52 | // words, it is safe (and recommended) to pass `os.Stdin` and `os.Stdout` |
| 53 | // directly. |
| 54 | func NewFilterProcessScanner(r io.Reader, w io.Writer) *FilterProcessScanner { |
| 55 | return &FilterProcessScanner{ |
| 56 | pl: pktline.NewPktline(r, w), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Init initializes the filter and ACKs back and forth between the Git LFS |
| 61 | // subprocess and the Git parent process that each is a git-filter-server and |
| 62 | // client respectively. |
| 63 | // |
| 64 | // If either side wrote an invalid sequence of data, or did not meet |
| 65 | // expectations, an error will be returned. If the filter type is not supported, |
| 66 | // an error will be returned. If the pkt-line welcome message was invalid, an |
| 67 | // error will be returned. |
| 68 | // |
| 69 | // If there was an error reading or writing any of the packets below, an error |
| 70 | // will be returned. |
| 71 | func (o *FilterProcessScanner) Init() error { |
| 72 | tracerx.Printf("Initialize filter-process") |
| 73 | reqVer := "version=2" |
| 74 | |
| 75 | initMsg, err := o.pl.ReadPacketText() |
| 76 | if err != nil { |
| 77 | return errors.Wrap(err, tr.Tr.Get("reading filter-process initialization")) |
| 78 | } |
| 79 | if initMsg != "git-filter-client" { |
| 80 | return errors.New(tr.Tr.Get("invalid filter-process pkt-line welcome message: %s", initMsg)) |
| 81 | } |
| 82 | |
| 83 | supVers, err := o.pl.ReadPacketList() |
| 84 | if err != nil { |
| 85 | return errors.Wrap(err, tr.Tr.Get("reading filter-process versions")) |
| 86 | } |
| 87 | if !slices.Contains(supVers, reqVer) { |
| 88 | return errors.New(tr.Tr.Get("filter '%s' not supported (your Git supports: %s)", reqVer, supVers)) |
| 89 | } |
| 90 | |
| 91 | err = o.pl.WritePacketList([]string{"git-filter-server", reqVer}) |
| 92 | if err != nil { |
| 93 | return errors.Wrap(err, tr.Tr.Get("writing filter-process initialization failed")) |
| 94 | } |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | // NegotiateCapabilities executes the process of negotiating capabilities |
| 99 | // between the filter client and server. If we don't support any of the |
| 100 | // capabilities given to LFS by Git, an error will be returned. If there was an |
| 101 | // error reading or writing capabilities between the two, an error will be |
| 102 | // returned. |
| 103 | func (o *FilterProcessScanner) NegotiateCapabilities() ([]string, error) { |
| 104 | reqCaps := []string{"capability=clean", "capability=smudge"} |
| 105 | |
| 106 | supCaps, err := o.pl.ReadPacketList() |
| 107 | if err != nil { |
| 108 | return nil, errors.New(tr.Tr.Get("reading filter-process capabilities failed with %s", err)) |
| 109 | } |
| 110 | |
| 111 | if slices.Contains(supCaps, "capability=delay") { |
| 112 | reqCaps = append(reqCaps, "capability=delay") |
| 113 | } |
| 114 | |
| 115 | for _, reqCap := range reqCaps { |
| 116 | if !slices.Contains(supCaps, reqCap) { |
| 117 | return nil, errors.New(tr.Tr.Get("filter '%s' not supported (your Git supports: %s)", reqCap, supCaps)) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | err = o.pl.WritePacketList(reqCaps) |
| 122 | if err != nil { |
| 123 | return nil, errors.New(tr.Tr.Get("writing filter-process capabilities failed with %s", err)) |
| 124 | } |
| 125 | |
| 126 | return supCaps, nil |
| 127 | } |
| 128 | |
| 129 | // Request represents a single command sent to LFS from the parent Git process. |
| 130 | type Request struct { |
| 131 | // Header maps header strings to values, and is encoded as the first |
| 132 | // part of the packet. |
| 133 | Header map[string]string |
| 134 | // Payload represents the body of the packet, and contains the contents |
| 135 | // of the file in the index. |
| 136 | Payload io.Reader |
| 137 | } |
| 138 | |
| 139 | // Scan scans for the next request, or error and returns whether or not the scan |
| 140 | // was successful, indicating the presence of a valid request. If the Scan |
| 141 | // failed, there was either an error reading the next request (and the results |
| 142 | // of calling `Err()` should be inspected), or the pipe was closed and no more |
| 143 | // requests are present. |
| 144 | // |
| 145 | // Closing the pipe is Git's way to communicate that no more files will be |
| 146 | // filtered. Git expects that the LFS process exits after this event. |
| 147 | func (o *FilterProcessScanner) Scan() bool { |
| 148 | o.req, o.err = nil, nil |
| 149 | |
| 150 | req, err := o.readRequest() |
| 151 | if err != nil { |
| 152 | o.err = err |
| 153 | return false |
| 154 | } |
| 155 | |
| 156 | o.req = req |
| 157 | return true |
| 158 | } |
| 159 | |
| 160 | // Request returns the request read from a call to Scan(). It is available only |
| 161 | // after a call to `Scan()` has completed, and is re-initialized to nil at the |
| 162 | // beginning of the subsequent `Scan()` call. |
| 163 | func (o *FilterProcessScanner) Request() *Request { return o.req } |
| 164 | |
| 165 | // Err returns any error encountered from the last call to Scan(). It is available only |
| 166 | // after a call to `Scan()` has completed, and is re-initialized to nil at the |
| 167 | // beginning of the subsequent `Scan()` call. |
| 168 | func (o *FilterProcessScanner) Err() error { return o.err } |
| 169 | |
| 170 | // readRequest reads the headers of a request and yields an `io.Reader` which |
| 171 | // will read the body of the request. Since the body is _not_ offset, one |
| 172 | // request should be read in its entirety before consuming the next request. |
| 173 | func (o *FilterProcessScanner) readRequest() (*Request, error) { |
| 174 | requestList, err := o.pl.ReadPacketList() |
| 175 | if err != nil { |
| 176 | return nil, err |
| 177 | } |
| 178 | |
| 179 | req := &Request{ |
| 180 | Header: make(map[string]string), |
| 181 | Payload: pktline.NewPktlineReaderFromPktline(o.pl, 65536), |
| 182 | } |
| 183 | |
| 184 | for _, pair := range requestList { |
| 185 | v := strings.SplitN(pair, "=", 2) |
| 186 | if len(v) > 1 { |
| 187 | req.Header[v[0]] = v[1] |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return req, nil |
| 192 | } |
| 193 | |
| 194 | // WriteList writes a list of strings to the underlying pktline data stream in |
| 195 | // pktline format. |
| 196 | func (o *FilterProcessScanner) WriteList(list []string) error { |
| 197 | return o.pl.WritePacketList(list) |
| 198 | } |
| 199 | |
| 200 | func (o *FilterProcessScanner) WriteStatus(status FilterProcessStatus) error { |
| 201 | return o.pl.WritePacketList([]string{"status=" + status.String()}) |
| 202 | } |