git-module

git clone git://git.lin.moe/fork/git-module.git

  1package git
  2
  3import (
  4	"context"
  5	"time"
  6)
  7
  8// UpdateServerInfoOptions contains optional arguments for updating auxiliary
  9// info file on the server side.
 10//
 11// Docs: https://git-scm.com/docs/git-update-server-info
 12type UpdateServerInfoOptions struct {
 13	// Indicates whether to overwrite the existing server info.
 14	Force bool
 15	CommandOptions
 16}
 17
 18// UpdateServerInfo updates the auxiliary info file on the server side for the
 19// repository in given path.
 20func UpdateServerInfo(ctx context.Context, path string, opts ...UpdateServerInfoOptions) error {
 21	var opt UpdateServerInfoOptions
 22	if len(opts) > 0 {
 23		opt = opts[0]
 24	}
 25
 26	args := []string{"update-server-info"}
 27	if opt.Force {
 28		args = append(args, "--force")
 29	}
 30	args = append(args, "--end-of-options")
 31	_, err := exec(ctx, path, args, opt.Envs)
 32	return err
 33}
 34
 35// ReceivePackOptions contains optional arguments for receiving the info pushed
 36// to the repository.
 37//
 38// Docs: https://git-scm.com/docs/git-receive-pack
 39type ReceivePackOptions struct {
 40	// Indicates whether to suppress the log output.
 41	Quiet bool
 42	// Indicates whether to generate the "info/refs" used by the "git http-backend".
 43	HTTPBackendInfoRefs bool
 44	CommandOptions
 45}
 46
 47// ReceivePack receives what is pushed into the repository in given path.
 48func ReceivePack(ctx context.Context, path string, opts ...ReceivePackOptions) ([]byte, error) {
 49	var opt ReceivePackOptions
 50	if len(opts) > 0 {
 51		opt = opts[0]
 52	}
 53
 54	args := []string{"receive-pack"}
 55	if opt.Quiet {
 56		args = append(args, "--quiet")
 57	}
 58	if opt.HTTPBackendInfoRefs {
 59		args = append(args, "--http-backend-info-refs")
 60	}
 61	args = append(args, "--end-of-options", ".")
 62	return exec(ctx, path, args, opt.Envs)
 63}
 64
 65// UploadPackOptions contains optional arguments for sending the packfile to the
 66// client.
 67//
 68// Docs: https://git-scm.com/docs/git-upload-pack
 69type UploadPackOptions struct {
 70	// Indicates whether to quit after a single request/response exchange.
 71	StatelessRPC bool
 72	// Indicates whether to not try "<directory>/.git/" if "<directory>" is not a
 73	// Git directory.
 74	Strict bool
 75	// Indicates whether to generate the "info/refs" used by the "git http-backend".
 76	HTTPBackendInfoRefs bool
 77	// The git-level inactivity timeout passed to git-upload-pack's --timeout flag.
 78	// This is separate from the command execution timeout which is controlled via
 79	// context.Context.
 80	InactivityTimeout time.Duration
 81	CommandOptions
 82}
 83
 84// UploadPack sends the packfile to the client for the repository in given path.
 85func UploadPack(ctx context.Context, path string, opts ...UploadPackOptions) ([]byte, error) {
 86	var opt UploadPackOptions
 87	if len(opts) > 0 {
 88		opt = opts[0]
 89	}
 90
 91	args := []string{"upload-pack"}
 92	if opt.StatelessRPC {
 93		args = append(args, "--stateless-rpc")
 94	}
 95	if opt.Strict {
 96		args = append(args, "--strict")
 97	}
 98	if opt.InactivityTimeout > 0 {
 99		args = append(args, "--timeout", opt.InactivityTimeout.String())
100	}
101	if opt.HTTPBackendInfoRefs {
102		args = append(args, "--http-backend-info-refs")
103	}
104	args = append(args, "--end-of-options", ".")
105	return exec(ctx, path, args, opt.Envs)
106}