init commit

This commit is contained in:
2026-05-07 10:27:16 +02:00
parent dea186c995
commit 9ad2672a66
36 changed files with 965 additions and 15 deletions
+19 -7
View File
@@ -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
}