summaryrefslogtreecommitdiffstats
path: root/src/cmd/go/internal/vcweb/git.go
blob: 5f9864e2dd83180251787d06b2ff4e7ac0ec0c27 (plain)
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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package vcweb

import (
	"log"
	"net/http"
	"net/http/cgi"
	"os/exec"
	"runtime"
	"sync"
)

type gitHandler struct {
	once       sync.Once
	gitPath    string
	gitPathErr error
}

func (h *gitHandler) Available() bool {
	if runtime.GOOS == "plan9" {
		// The Git command is usually not the real Git on Plan 9.
		// See https://golang.org/issues/29640.
		return false
	}
	h.once.Do(func() {
		h.gitPath, h.gitPathErr = exec.LookPath("git")
	})
	return h.gitPathErr == nil
}

func (h *gitHandler) Handler(dir string, env []string, logger *log.Logger) (http.Handler, error) {
	if !h.Available() {
		return nil, ServerNotInstalledError{name: "git"}
	}

	handler := &cgi.Handler{
		Path:   h.gitPath,
		Logger: logger,
		Args:   []string{"http-backend"},
		Dir:    dir,
		Env: append(env[:len(env):len(env)],
			"GIT_PROJECT_ROOT="+dir,
			"GIT_HTTP_EXPORT_ALL=1",
		),
	}

	return handler, nil
}