|
| 1 | +// Copyright 2023 The Gogs Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package git |
| 6 | + |
| 7 | +import ( |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// UpdateServerInfoOptions contains optional arguments for updating auxiliary |
| 12 | +// info file on the server side. |
| 13 | +// |
| 14 | +// Docs: https://git-scm.com/docs/git-update-server-info |
| 15 | +type UpdateServerInfoOptions struct { |
| 16 | + // Indicates whether to overwrite the existing server info. |
| 17 | + Force bool |
| 18 | + // The timeout duration before giving up for each shell command execution. The |
| 19 | + // default timeout duration will be used when not supplied. |
| 20 | + Timeout time.Duration |
| 21 | + // The additional options to be passed to the underlying git. |
| 22 | + CommandOptions |
| 23 | +} |
| 24 | + |
| 25 | +// UpdateServerInfo updates the auxiliary info file on the server side for the |
| 26 | +// repository in given path. |
| 27 | +func UpdateServerInfo(path string, opts ...UpdateServerInfoOptions) error { |
| 28 | + var opt UpdateServerInfoOptions |
| 29 | + if len(opts) > 0 { |
| 30 | + opt = opts[0] |
| 31 | + } |
| 32 | + cmd := NewCommand("update-server-info").AddOptions(opt.CommandOptions) |
| 33 | + if opt.Force { |
| 34 | + cmd.AddArgs("--force") |
| 35 | + } |
| 36 | + _, err := cmd.RunInDirWithTimeout(opt.Timeout, path) |
| 37 | + return err |
| 38 | +} |
| 39 | + |
| 40 | +// ReceivePackOptions contains optional arguments for receiving the info pushed |
| 41 | +// to the repository. |
| 42 | +// |
| 43 | +// Docs: https://git-scm.com/docs/git-receive-pack |
| 44 | +type ReceivePackOptions struct { |
| 45 | + // Indicates whether to suppress the log output. |
| 46 | + Quiet bool |
| 47 | + // Indicates whether to generate the "info/refs" used by the "git http-backend". |
| 48 | + HTTPBackendInfoRefs bool |
| 49 | + // The timeout duration before giving up for each shell command execution. The |
| 50 | + // default timeout duration will be used when not supplied. |
| 51 | + Timeout time.Duration |
| 52 | + // The additional options to be passed to the underlying git. |
| 53 | + CommandOptions |
| 54 | +} |
| 55 | + |
| 56 | +// ReceivePack receives what is pushed into the repository in given path. |
| 57 | +func ReceivePack(path string, opts ...ReceivePackOptions) ([]byte, error) { |
| 58 | + var opt ReceivePackOptions |
| 59 | + if len(opts) > 0 { |
| 60 | + opt = opts[0] |
| 61 | + } |
| 62 | + cmd := NewCommand("receive-pack").AddOptions(opt.CommandOptions) |
| 63 | + if opt.Quiet { |
| 64 | + cmd.AddArgs("--quiet") |
| 65 | + } |
| 66 | + if opt.HTTPBackendInfoRefs { |
| 67 | + cmd.AddArgs("--http-backend-info-refs") |
| 68 | + } |
| 69 | + cmd.AddArgs(".") |
| 70 | + return cmd.RunInDirWithTimeout(opt.Timeout, path) |
| 71 | +} |
| 72 | + |
| 73 | +// UploadPackOptions contains optional arguments for sending the packfile to the |
| 74 | +// client. |
| 75 | +// |
| 76 | +// Docs: https://git-scm.com/docs/git-upload-pack |
| 77 | +type UploadPackOptions struct { |
| 78 | + // Indicates whether to quit after a single request/response exchange. |
| 79 | + StatelessRPC bool |
| 80 | + // Indicates whether to not try "<directory>/.git/" if "<directory>" is not a |
| 81 | + // Git directory. |
| 82 | + Strict bool |
| 83 | + // Indicates whether to generate the "info/refs" used by the "git http-backend". |
| 84 | + HTTPBackendInfoRefs bool |
| 85 | + // The timeout duration before giving up for each shell command execution. The |
| 86 | + // default timeout duration will be used when not supplied. |
| 87 | + Timeout time.Duration |
| 88 | + // The additional options to be passed to the underlying git. |
| 89 | + CommandOptions |
| 90 | +} |
| 91 | + |
| 92 | +// UploadPack sends the packfile to the client for the repository in given path. |
| 93 | +func UploadPack(path string, opts ...UploadPackOptions) ([]byte, error) { |
| 94 | + var opt UploadPackOptions |
| 95 | + if len(opts) > 0 { |
| 96 | + opt = opts[0] |
| 97 | + } |
| 98 | + cmd := NewCommand("upload-pack").AddOptions(opt.CommandOptions) |
| 99 | + if opt.StatelessRPC { |
| 100 | + cmd.AddArgs("--stateless-rpc") |
| 101 | + } |
| 102 | + if opt.Strict { |
| 103 | + cmd.AddArgs("--strict") |
| 104 | + } |
| 105 | + if opt.Timeout > 0 { |
| 106 | + cmd.AddArgs("--timeout", opt.Timeout.String()) |
| 107 | + } |
| 108 | + if opt.HTTPBackendInfoRefs { |
| 109 | + cmd.AddArgs("--http-backend-info-refs") |
| 110 | + } |
| 111 | + cmd.AddArgs(".") |
| 112 | + return cmd.RunInDirWithTimeout(opt.Timeout, path) |
| 113 | +} |
0 commit comments