init commit
This commit is contained in:
@@ -71,12 +71,11 @@ func (h *GitHTTPHandler) ServeGit(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Build PATH_INFO: /{reponame}.git/{suffix}
|
||||
// chi wildcard gives us everything after /{owner}/{repoGit}
|
||||
suffix := chi.URLParam(r, "*")
|
||||
// Strip the /{owner}/{repoGit} prefix from the raw URL path to get the suffix.
|
||||
prefix := "/" + owner + "/" + repoGit
|
||||
suffix := strings.TrimPrefix(r.URL.Path, prefix)
|
||||
if suffix == "" {
|
||||
suffix = "/"
|
||||
} else if !strings.HasPrefix(suffix, "/") {
|
||||
suffix = "/" + suffix
|
||||
}
|
||||
pathInfo := "/" + repoGit + suffix
|
||||
|
||||
|
||||
@@ -45,12 +45,16 @@ func run(repoPath string, args ...string) ([]byte, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// IsEmpty returns true when the repo has no commits yet.
|
||||
// Must use --verify: without it, rev-parse exits 0 in a bare empty repo
|
||||
// because HEAD is a valid symbolic ref even before the first commit.
|
||||
// IsEmpty returns true when the repo has no commits on any branch.
|
||||
// Uses for-each-ref so it doesn't depend on HEAD pointing to a valid branch
|
||||
// (bare repos created without --initial-branch default to master, but clients
|
||||
// may push to main, leaving HEAD dangling).
|
||||
func IsEmpty(repoPath string) bool {
|
||||
_, err := run(repoPath, "rev-parse", "--verify", "HEAD")
|
||||
return err != nil
|
||||
out, err := run(repoPath, "for-each-ref", "--count=1", "refs/heads/")
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
return len(strings.TrimSpace(string(out))) == 0
|
||||
}
|
||||
|
||||
func Init(path string) error {
|
||||
@@ -59,10 +63,18 @@ func Init(path string) error {
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("git init --bare: %w: %s", err, out)
|
||||
}
|
||||
// Allow pushes over HTTP (git http-backend checks this config key)
|
||||
env := []string{"GIT_TERMINAL_PROMPT=0", "HOME=/tmp"}
|
||||
|
||||
// Point HEAD at main so clients pushing 'main' don't leave a dangling HEAD.
|
||||
head := exec.Command("git", "-C", path, "symbolic-ref", "HEAD", "refs/heads/main")
|
||||
head.Env = env
|
||||
_ = head.Run()
|
||||
|
||||
// Allow pushes over HTTP (git http-backend checks this config key).
|
||||
cfg := exec.Command("git", "-C", path, "config", "http.receivepack", "true")
|
||||
cfg.Env = []string{"GIT_TERMINAL_PROMPT=0", "HOME=/tmp"}
|
||||
cfg.Env = env
|
||||
_ = cfg.Run()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user