| 1 | # Adding Custom Transfer Agents to LFS |
| 2 | |
| 3 | ## Introduction |
| 4 | |
| 5 | Git LFS supports multiple ways to transfer (upload and download) files. In the |
| 6 | core client, the basic way to do this is via a one-off HTTP request via the URL |
| 7 | returned from the LFS API for a given object. The core client also supports |
| 8 | extensions to allow resuming of downloads (via `Range` headers) and uploads (via |
| 9 | the [tus.io](http://tus.io) protocol). |
| 10 | |
| 11 | Some people might want to be able to transfer content in other ways, however. |
| 12 | To enable this, git-lfs allows configuring Custom Transfers, which are |
| 13 | simply processes which must adhere to the protocol defined later in this |
| 14 | document. git-lfs will invoke the process at the start of all transfers, |
| 15 | and will communicate with the process via stdin/stdout for each transfer. |
| 16 | |
| 17 | ## Custom Transfer Type Selection |
| 18 | |
| 19 | In the LFS API request, the client includes a list of transfer types it |
| 20 | supports. When replying, the API server will pick one of these and make any |
| 21 | necessary adjustments to the returned object actions, in case the picked |
| 22 | transfer type needs custom details about how to do each transfer. |
| 23 | |
| 24 | ## Using a Custom Transfer Type without the API server |
| 25 | |
| 26 | In some cases the transfer agent can figure out by itself how and where |
| 27 | the transfers should be made, without having to query the API server. |
| 28 | In this case it's possible to use the custom transfer agent directly, |
| 29 | without querying the server, by using the following config option: |
| 30 | |
| 31 | * `lfs.standalonetransferagent`, `lfs.<url>.standalonetransferagent` |
| 32 | |
| 33 | Specifies a custom transfer agent to be used if the API server URL matches as |
| 34 | in `git config --get-urlmatch lfs.standalonetransferagent <apiurl>`. |
| 35 | `git-lfs` will not contact the API server. It instead sets stage 2 transfer |
| 36 | actions to `null`. `lfs.<url>.standalonetransferagent` can be used to |
| 37 | configure a custom transfer agent for individual remotes. |
| 38 | `lfs.standalonetransferagent` unconditionally configures a custom transfer |
| 39 | agent for all remotes. The custom transfer agent must be specified in |
| 40 | a `lfs.customtransfer.<name>` settings group, or else must be the internal |
| 41 | `lfs-standalone-file` transfer adapter. Other values will be ignored. |
| 42 | |
| 43 | ## Defining a Custom Transfer Type |
| 44 | |
| 45 | A custom transfer process is defined under a settings group called |
| 46 | `lfs.customtransfer.<name>`, where `<name>` is an identifier (see |
| 47 | [Naming](#naming) below). |
| 48 | |
| 49 | * `lfs.customtransfer.<name>.path` |
| 50 | |
| 51 | `path` should point to the process you wish to invoke. This will be invoked at |
| 52 | the start of all transfers (possibly many times, see the `concurrent` option |
| 53 | below) and the protocol over stdin/stdout is defined below in the |
| 54 | [Protocol](#protocol) section. |
| 55 | |
| 56 | * `lfs.customtransfer.<name>.args` |
| 57 | |
| 58 | If the custom transfer process requires any arguments, these can be provided |
| 59 | here. Typically you would only need this if your process was multi-purpose or |
| 60 | particularly flexible, most of the time you won't need it. Note that this |
| 61 | string will be expanded by the shell. |
| 62 | |
| 63 | * `lfs.customtransfer.<name>.concurrent` |
| 64 | |
| 65 | If true (the default), git-lfs will invoke the custom transfer process |
| 66 | multiple times in parallel, according to `lfs.concurrenttransfers`, splitting |
| 67 | the transfer workload between the processes. |
| 68 | |
| 69 | If you would prefer that only one instance of the transfer process is invoked, |
| 70 | maybe because you want to do your own parallelism internally (e.g. slicing |
| 71 | files into parts), set this to false. |
| 72 | |
| 73 | * `lfs.customtransfer.<name>.direction` |
| 74 | |
| 75 | Specifies which direction the custom transfer process supports, either |
| 76 | `download`, `upload`, or `both`. The default if unspecified is `both`. |
| 77 | |
| 78 | ## Naming |
| 79 | |
| 80 | Each custom transfer must have a name which is unique to the underlying |
| 81 | mechanism, and the client and the server must agree on that name. The client |
| 82 | will advertise this name to the server as a supported transfer approach, and if |
| 83 | the server supports it, it will return relevant object action links. Because |
| 84 | these may be very different from standard HTTP URLs it's important that the |
| 85 | client and server agree on the name. |
| 86 | |
| 87 | For example, let's say I've implemented a custom transfer process which uses |
| 88 | NFS. I could call this transfer type `nfs` - although it's not specific to my |
| 89 | configuration exactly, it is specific to the way NFS works, and the server will |
| 90 | need to give me different URLs. Assuming I define my transfer like this, and the |
| 91 | server supports it, I might start getting object action links back like |
| 92 | `nfs://<host>/path/to/object` |
| 93 | |
| 94 | ## Protocol |
| 95 | |
| 96 | The git-lfs client communicates with the custom transfer process via the stdin |
| 97 | and stdout streams. No file content is communicated on these streams, only |
| 98 | request / response metadata. The metadata exchanged is always in JSON format. |
| 99 | External files will be referenced when actual content is exchanged. |
| 100 | |
| 101 | ### Line Delimited JSON |
| 102 | |
| 103 | Because multiple JSON messages will be exchanged on the same stream it's useful |
| 104 | to delimit them explicitly rather than have the parser find the closing `}` in |
| 105 | an arbitrary stream, therefore each JSON structure will be sent and received on |
| 106 | a **single line** as per [Line Delimited |
| 107 | JSON](https://en.wikipedia.org/wiki/JSON_Streaming#Line_delimited_JSON_2). |
| 108 | |
| 109 | In other words when git-lfs sends a JSON message to the custom transfer it will |
| 110 | be on a single line, with a line feed at the end. The transfer process must |
| 111 | respond the same way by writing a JSON structure back to stdout with a single |
| 112 | line feed at the end (and flush the output). |
| 113 | |
| 114 | ### Protocol Stages |
| 115 | |
| 116 | The protocol consists of 3 stages: |
| 117 | |
| 118 | #### Stage 1: Initiation |
| 119 | |
| 120 | Immediately after invoking a custom transfer process, git-lfs sends initiation |
| 121 | data to the process over stdin. This tells the process useful information about |
| 122 | the configuration. |
| 123 | |
| 124 | The message will look like this: |
| 125 | |
| 126 | ```json |
| 127 | { "event": "init", "operation": "download", "remote": "origin", "concurrent": true, "concurrenttransfers": 3 } |
| 128 | ``` |
| 129 | |
| 130 | * `event`: Always `init` to identify this message |
| 131 | * `operation`: will be `upload` or `download` depending on transfer direction |
| 132 | * `remote`: The Git remote. It can be a remote name like `origin` or an URL |
| 133 | like `ssh://git.example.com//path/to/repo`. A standalone transfer agent can |
| 134 | use it to determine the location of remote files. |
| 135 | * `concurrent`: reflects the value of `lfs.customtransfer.<name>.concurrent`, in |
| 136 | case the process needs to know |
| 137 | * `concurrenttransfers`: reflects the value of `lfs.concurrenttransfers`, for if |
| 138 | the transfer process wants to implement its own concurrency and wants to |
| 139 | respect this setting. |
| 140 | |
| 141 | The transfer process should use the information it needs from the initiation |
| 142 | structure, and also perform any one-off setup tasks it needs to do. It should |
| 143 | then respond on stdout with a simple empty confirmation structure, as follows: |
| 144 | |
| 145 | ```json |
| 146 | { } |
| 147 | ``` |
| 148 | |
| 149 | Or if there was an error: |
| 150 | |
| 151 | ```json |
| 152 | { "error": { "code": 32, "message": "Some init failure message" } } |
| 153 | ``` |
| 154 | |
| 155 | #### Stage 2: 0..N Transfers |
| 156 | |
| 157 | After the initiation exchange, git-lfs will send any number of transfer |
| 158 | requests to the stdin of the transfer process, in a serial sequence. Once a |
| 159 | transfer request is sent to the process, it awaits a completion response before |
| 160 | sending the next request. |
| 161 | |
| 162 | ##### Uploads |
| 163 | |
| 164 | For uploads the request sent from git-lfs to the transfer process will look |
| 165 | like this: |
| 166 | |
| 167 | ```json |
| 168 | { "event": "upload", "oid": "bf3e3e2af9366a3b704ae0c31de5afa64193ebabffde2091936ad2e7510bc03a", "size": 346232, "path": "/path/to/file.png", "action": { "href": "nfs://server/path", "header": { "key": "value" } } } |
| 169 | ``` |
| 170 | |
| 171 | * `event`: Always `upload` to identify this message |
| 172 | * `oid`: the identifier of the LFS object |
| 173 | * `size`: the size of the LFS object |
| 174 | * `path`: the file which the transfer process should read the upload data from |
| 175 | * `action`: the `upload` action copied from the response from the batch API. |
| 176 | This contains `href` and `header` contents, which are named per HTTP |
| 177 | conventions, but can be interpreted however the custom transfer agent wishes |
| 178 | (this is an NFS example, but it doesn't even have to be an URL). Generally, |
| 179 | `href` will give the primary connection details, with `header` containing any |
| 180 | miscellaneous information needed. `action` is `null` for standalone transfer |
| 181 | agents. |
| 182 | |
| 183 | The transfer process should post one or more [progress messages](#progress) and |
| 184 | then a final completion message as follows: |
| 185 | |
| 186 | ```json |
| 187 | { "event": "complete", "oid": "bf3e3e2af9366a3b704ae0c31de5afa64193ebabffde2091936ad2e7510bc03a" } |
| 188 | ``` |
| 189 | |
| 190 | * `event`: Always `complete` to identify this message |
| 191 | * `oid`: the identifier of the LFS object |
| 192 | |
| 193 | Or if there was an error in the transfer: |
| 194 | |
| 195 | ```json |
| 196 | { "event": "complete", "oid": "bf3e3e2af9366a3b704ae0c31de5afa64193ebabffde2091936ad2e7510bc03a", "error": { "code": 2, "message": "Explain what happened to this transfer" } } |
| 197 | ``` |
| 198 | |
| 199 | * `event`: Always `complete` to identify this message |
| 200 | * `oid`: the identifier of the LFS object |
| 201 | * `error`: Should contain a `code` and `message` explaining the error |
| 202 | |
| 203 | ##### Downloads |
| 204 | |
| 205 | For downloads the request sent from git-lfs to the transfer process will look |
| 206 | like this: |
| 207 | |
| 208 | ```json |
| 209 | { "event": "download", "oid": "22ab5f63670800cc7be06dbed816012b0dc411e774754c7579467d2536a9cf3e", "size": 21245, "action": { "href": "nfs://server/path", "header": { "key": "value" } } } |
| 210 | ``` |
| 211 | |
| 212 | * `event`: Always `download` to identify this message |
| 213 | * `oid`: the identifier of the LFS object |
| 214 | * `size`: the size of the LFS object |
| 215 | * `action`: the `download` action copied from the response from the batch API. |
| 216 | This contains `href` and `header` contents, which are named per HTTP |
| 217 | conventions, but can be interpreted however the custom transfer agent wishes |
| 218 | (this is an NFS example, but it doesn't even have to be an URL). Generally, |
| 219 | `href` will give the primary connection details, with `header` containing any |
| 220 | miscellaneous information needed. `action` is `null` for standalone transfer |
| 221 | agents. |
| 222 | |
| 223 | Note there is no file path included in the download request; the transfer |
| 224 | process should create a file itself and return the path in the final response |
| 225 | after completion (see below). |
| 226 | |
| 227 | The transfer process should post one or more [progress messages](#progress) and |
| 228 | then a final completion message as follows: |
| 229 | |
| 230 | ```json |
| 231 | { "event": "complete", "oid": "22ab5f63670800cc7be06dbed816012b0dc411e774754c7579467d2536a9cf3e", "path": "/path/to/file.png" } |
| 232 | ``` |
| 233 | |
| 234 | * `event`: Always `complete` to identify this message |
| 235 | * `oid`: the identifier of the LFS object |
| 236 | * `path`: the path to a file containing the downloaded data, which the transfer |
| 237 | process relinquishes control of to git-lfs. git-lfs will move the file into |
| 238 | LFS storage. |
| 239 | |
| 240 | Or, if there was a failure transferring this item: |
| 241 | |
| 242 | ```json |
| 243 | { "event": "complete", "oid": "22ab5f63670800cc7be06dbed816012b0dc411e774754c7579467d2536a9cf3e", "error": { "code": 2, "message": "Explain what happened to this transfer" } } |
| 244 | ``` |
| 245 | |
| 246 | * `event`: Always `complete` to identify this message |
| 247 | * `oid`: the identifier of the LFS object |
| 248 | * `error`: Should contain a `code` and `message` explaining the error |
| 249 | |
| 250 | Errors for a single transfer request should not terminate the process. The error |
| 251 | should be returned in the response structure instead. |
| 252 | |
| 253 | The custom transfer adapter does not need to check the SHA of the file content |
| 254 | it has downloaded, git-lfs will do that before moving the final content into |
| 255 | the LFS store. |
| 256 | |
| 257 | ##### Progress |
| 258 | |
| 259 | In order to support progress reporting while data is uploading / downloading, |
| 260 | the transfer process should post messages to stdout as follows before sending |
| 261 | the final completion message: |
| 262 | |
| 263 | ```json |
| 264 | { "event": "progress", "oid": "22ab5f63670800cc7be06dbed816012b0dc411e774754c7579467d2536a9cf3e", "bytesSoFar": 1234, "bytesSinceLast": 64 } |
| 265 | ``` |
| 266 | |
| 267 | * `event`: Always `progress` to identify this message |
| 268 | * `oid`: the identifier of the LFS object |
| 269 | * `bytesSoFar`: the total number of bytes transferred so far |
| 270 | * `bytesSinceLast`: the number of bytes transferred since the last progress |
| 271 | message |
| 272 | |
| 273 | The transfer process should post these messages such that the last one sent |
| 274 | has `bytesSoFar` equal to the file size on success. |
| 275 | |
| 276 | #### Stage 3: Finish & Cleanup |
| 277 | |
| 278 | When all transfers have been processed, git-lfs will send the following message |
| 279 | to the stdin of the transfer process: |
| 280 | |
| 281 | ```json |
| 282 | { "event": "terminate" } |
| 283 | ``` |
| 284 | |
| 285 | On receiving this message the transfer process should clean up and terminate. |
| 286 | No response is expected. |
| 287 | |
| 288 | ## Error handling |
| 289 | |
| 290 | Any unexpected fatal errors in the transfer process (not errors specific to a |
| 291 | transfer request) should set the exit code to non-zero and print information to |
| 292 | stderr. Otherwise the exit code should be 0 even if some transfers failed. |
| 293 | |
| 294 | ## A Note On Verify Actions |
| 295 | |
| 296 | You may have noticed that that only the `upload` and `download` actions are |
| 297 | passed to the custom transfer agent for processing, what about the `verify` |
| 298 | action, if the API returns one? |
| 299 | |
| 300 | Custom transfer agents do not handle the verification process, only the |
| 301 | upload and download of content. The verify link is typically used to notify |
| 302 | a system *other* than the actual content store after an upload was completed, |
| 303 | therefore it makes more sense for that to be handled via the normal API process. |