57 lines
941 B
Go
57 lines
941 B
Go
// Copyright (C) 2024 Umorpha Systems
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// https://github.com/jonasmalacofilho/git-cache-http-server
|
|
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os/exec"
|
|
)
|
|
|
|
type cache struct {
|
|
dir string
|
|
}
|
|
|
|
func (c cache) fetchRepo(cacheDir, url string) error {
|
|
cmd := exec.Command("git", "fetch",
|
|
"--no-write-fetch-head",
|
|
"--no-recurse-submodules",
|
|
"--no-tags",
|
|
"--prune",
|
|
"--",
|
|
url, "*:"+url2id(url)+"/*")
|
|
cmd.Dir = c.dir
|
|
if err := cmd.Run(); err != nil {
|
|
var eErr *exec.ExitError
|
|
if errors.As(err, &eErr) {
|
|
err = fmt.Errorf("%w: %s", err, eErr.Stderr)
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
|
|
func (c cache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
repoURL, _, _, err := routeGitHTTP(r)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if repoURL == "" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
_ = url2id(repoURL)
|
|
|
|
}
|
|
|
|
func main() {
|
|
|
|
}
|