func WriteModuleZip(w io.Writer, m module.Version, r vcs.Repository, id vcs.CommitID) error {
	fs, err := r.FileSystem(id)
	if err != nil {
		return err
	}
	z := zip.NewWriter(w)
	err = vfsutil.Walk(fs, "/", func(name string, fi os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if fi.IsDir() {
			return nil
		}
		dst, err := z.Create(m.Path + "@" + m.Version + name)
		if err != nil {
			return err
		}
		src, err := fs.Open(name)
		if err != nil {
			return err
		}
		_, err = io.Copy(dst, src)
		src.Close()
		return err
	})
	if err != nil {
		return err
	}
	err = z.Close()
	return err
}
WriteModuleZip builds a zip archive for module version m
by including all files from repository r at commit id,
and writes the result to w.

WriteModuleZip does not support multi-module repositories.
A go.mod file may be in root, but not in any other directory.

Unlike "golang.org/x/mod/zip".Create, it does not verify
any module zip restrictions. It will produce an invalid
module zip if given a commit containing invalid files.
It should be used on commits that are known to have files
that are all acceptable to include in a module zip.
package code
import (
	"archive/zip"
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"
	"time"
	"github.com/shurcooL/go/vfs/godocfs/vfsutil"
	"github.com/shurcooL/home/internal/mod"
	"github.com/shurcooL/httperror"
	"golang.org/x/mod/modfile"
	"golang.org/x/mod/module"
	"sourcegraph.com/sourcegraph/go-vcs/vcs"
	"sourcegraph.com/sourcegraph/go-vcs/vcs/git"
)
type ModuleHandler struct {
	Code *Service
}
func (h ModuleHandler) ServeModule(w http.ResponseWriter, req *http.Request) error {
	if req.Method != http.MethodGet {
		return httperror.Method{Allowed: []string{http.MethodGet}}
	}
	r, ok := parseModuleProxyRequest(req.URL.Path)
	if !ok {
		return os.ErrNotExist
	}
	unesc, ok := r.Unescape()  
	if !ok {
		return httperror.BadRequest{Err: fmt.Errorf("failed to unescape module path=%q and/or version=%q", r.Module, r.Version)}
	}
	modulePath, typ, version := unesc.Module, unesc.Type, unesc.Version
	d, err := h.Code.GetDirectory(req.Context(), modulePath)
	if err != nil || !d.IsRepoRoot() {
		return os.ErrNotExist
	}
	gitDir := filepath.Join(h.Code.reposDir, filepath.FromSlash(d.RepoRoot))
	if typ == "list" {
		return h.serveList(req.Context(), w, gitDir)
	}
	versionTime, versionRevision, err := mod.ParseV000PseudoVersion(version)
	if err != nil {
		return os.ErrNotExist
	}
	repo, err := git.Open(gitDir)
	if err != nil {
		return err
	}
	defer func() {
		if err := repo.Close(); err != nil {
			log.Println("ModuleHandler.ServeModule: repo.Close:", err)
		}
	}()
	commitID, err := repo.ResolveRevision(versionRevision)
	if err != nil {
		return os.ErrNotExist
	}
	commit, err := repo.GetCommit(commitID)
	if err != nil || commit.Committer == nil || !versionTime.Equal(time.Unix(commit.Committer.Date.Seconds, 0).UTC()) {
		return os.ErrNotExist
	} else if !isCommitOnMaster(req.Context(), gitDir, commit) {
		return os.ErrNotExist
	}
	switch typ {
	case "info":
		return h.serveInfo(w, version, versionTime)
	case "mod":
		return h.serveMod(w, modulePath, repo, commitID)
	case "zip":
		return h.serveZip(w, modulePath, version, repo, commitID)
	default:
		panic("unreachable")
	}
}
func (ModuleHandler) serveList(ctx context.Context, w http.ResponseWriter, gitDir string) error {
	revs, err := listMasterCommits(ctx, gitDir)
	if err != nil {
		return err
	}
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	for i := len(revs) - 1; i >= 0; i-- {
		fmt.Fprintln(w, revs[i].Version)
	}
	return nil
}
func (ModuleHandler) serveInfo(w http.ResponseWriter, version string, time time.Time) error {
	w.Header().Set("Content-Type", "application/json")
	enc := json.NewEncoder(w)
	enc.SetIndent("", "\t")
	err := enc.Encode(mod.RevInfo{
		Version: version,
		Time:    time,
	})
	return err
}
func (ModuleHandler) serveMod(w http.ResponseWriter, modulePath string, repo *git.Repository, commitID vcs.CommitID) error {
	fs, err := repo.FileSystem(commitID)
	if err != nil {
		return err
	}
	f, err := fs.Open("/go.mod")
	if os.IsNotExist(err) {
		f = nil
	} else if err != nil {
		return err
	}
	if f != nil {
		defer f.Close()
	}
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	if f != nil {
		_, err := io.Copy(w, f)
		return err
	} else {
		_, err := fmt.Fprintf(w, "module %s\n", modfile.AutoQuote(modulePath))
		return err
	}
}
func (ModuleHandler) serveZip(w http.ResponseWriter, modulePath, version string, repo *git.Repository, commitID vcs.CommitID) error {
	w.Header().Set("Content-Type", "application/zip")
	return WriteModuleZip(w, module.Version{Path: modulePath, Version: version}, repo, commitID)
}