2024-02-22 19:14:05 +00:00
|
|
|
//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
|
|
|
|
}
|
2024-02-22 21:04:08 +00:00
|
|
|
return gitService(ctx, "upload-pack", string(c), "--stateless-rpc", "--advertise-refs")
|
2024-02-22 19:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2024-02-22 21:04:08 +00:00
|
|
|
"-c", "protocol.version=2",
|
2024-02-22 19:14:05 +00:00
|
|
|
"-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)
|
2024-02-22 21:04:08 +00:00
|
|
|
cmd.Env = append(os.Environ(), fmt.Sprintf("UGIT_REPODIR=%s", repoDir), "GIT_PROTOCOL=version=2")
|
2024-02-22 19:14:05 +00:00
|
|
|
cmd.Stdin = ctx
|
|
|
|
cmd.Stdout = ctx
|
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
}
|