github.com/bir3/gocompiler@v0.3.205/src/cmd/gocmd/internal/vcweb/git.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package vcweb
     6  
     7  import (
     8  	"log"
     9  	"net/http"
    10  	"net/http/cgi"
    11  	"os/exec"
    12  	"runtime"
    13  	"sync"
    14  )
    15  
    16  type gitHandler struct {
    17  	once       sync.Once
    18  	gitPath    string
    19  	gitPathErr error
    20  }
    21  
    22  func (h *gitHandler) Available() bool {
    23  	if runtime.GOOS == "plan9" {
    24  		// The Git command is usually not the real Git on Plan 9.
    25  		// See https://golang.org/issues/29640.
    26  		return false
    27  	}
    28  	h.once.Do(func() {
    29  		h.gitPath, h.gitPathErr = exec.LookPath("git")
    30  	})
    31  	return h.gitPathErr == nil
    32  }
    33  
    34  func (h *gitHandler) Handler(dir string, env []string, logger *log.Logger) (http.Handler, error) {
    35  	if !h.Available() {
    36  		return nil, ServerNotInstalledError{name: "git"}
    37  	}
    38  
    39  	handler := &cgi.Handler{
    40  		Path:   h.gitPath,
    41  		Logger: logger,
    42  		Args:   []string{"http-backend"},
    43  		Dir:    dir,
    44  		Env: append(env[:len(env):len(env)],
    45  			"GIT_PROJECT_ROOT="+dir,
    46  			"GIT_HTTP_EXPORT_ALL=1",
    47  		),
    48  	}
    49  
    50  	return handler, nil
    51  }