Home

ugit @2615593c2f59ab25cb74d65d968a33cbc834e9c5 - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / git / protocol_git.go
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//go:build !gogit

package git

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"

	"github.com/go-git/go-git/v5/plumbing/format/pktline"
)

var RequiresHook = true

type CmdProtocol string

func NewProtocol(repoPath string) (Protocoler, error) {
	return CmdProtocol(repoPath), nil
}

func (c CmdProtocol) HTTPInfoRefs(ctx ReadWriteContexter) error {
	pkt := pktline.NewEncoder(ctx)
	if err := pkt.EncodeString("# service=git-upload-pack"); err != nil {
		return err
	}
	if err := pkt.Flush(); err != nil {
		return err
	}
	return gitService(ctx, "upload-pack", string(c), "--stateless-rpc", "--advertise-refs")
}

func (c CmdProtocol) HTTPUploadPack(ctx ReadWriteContexter) error {
	return gitService(ctx, "upload-pack", string(c), "--stateless-rpc")
}

func (c CmdProtocol) SSHUploadPack(ctx ReadWriteContexter) error {
	return gitService(ctx, "upload-pack", string(c))
}

func (c CmdProtocol) SSHReceivePack(ctx ReadWriteContexter, _ *Repo) error {
	return gitService(ctx, "receive-pack", string(c))
}

func gitService(ctx ReadWriteContexter, command, repoDir string, args ...string) error {
	cmd := exec.CommandContext(ctx.Context(), "git")
	cmd.Args = append(cmd.Args, []string{
		"-c", "protocol.version=2",
		"-c", "uploadpack.allowFilter=true",
		"-c", "receive.advertisePushOptions=true",
		"-c", fmt.Sprintf("core.hooksPath=%s", filepath.Join(filepath.Dir(repoDir), "hooks")),
		command,
	}...)
	if len(args) > 0 {
		cmd.Args = append(cmd.Args, args...)
	}
	cmd.Args = append(cmd.Args, repoDir)
	cmd.Env = append(os.Environ(), fmt.Sprintf("UGIT_REPODIR=%s", repoDir), "GIT_PROTOCOL=version=2")
	cmd.Stdin = ctx
	cmd.Stdout = ctx
	fmt.Println(cmd.Env, cmd.String())

	return cmd.Run()
}