From: Simon Ruderich Date: Sun, 22 Feb 2026 18:07:38 +0000 (+0100) Subject: Add post-commit mode to run in local repository X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=7c8cc6aa936c2a9ceae2b471028072ac1d65980b;p=punyci%2Fpunyci.git Add post-commit mode to run in local repository --- diff --git a/git.go b/git.go index 742b0e1..0d2c040 100644 --- a/git.go +++ b/git.go @@ -71,6 +71,25 @@ func HookGetPushOptions() []string { return res } +func RepoGetBranchOid() (string, string, error) { + cmd := exec.Command("git", "branch", "--show-current") + x, err := cmd.Output() + if err != nil { + return "", "", fmt.Errorf("failed to get branch: %v: %v", cmd, err) + } + branch := strings.TrimSpace(string(x)) + + cmd = exec.Command("git", "rev-parse", "--verify", "--end-of-options", + branch) + x, err = cmd.Output() + if err != nil { + return "", "", fmt.Errorf("failed to get oid: %v: %v", cmd, err) + } + oid := strings.TrimSpace(string(x)) + + return oid, branch, nil +} + // RepoGetConfig reads the punyci configuration from the git repo in the // current working directory. func RepoGetConfig(ref string) (*Config, error) { diff --git a/main.go b/main.go index a70dd4d..021ae71 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,8 @@ type Job struct { func main() { usage := func() { log.SetFlags(0) - log.Fatalf("usage: %s post-receive", os.Args[0]) + log.Fatalf("usage: %[1]s post-receive\n"+ + " %[1]s post-commit", os.Args[0]) } if len(os.Args) != 2 { usage() @@ -45,6 +46,11 @@ func main() { if err != nil { log.Fatal(err) } + case "post-commit": + err := postCommit() + if err != nil { + log.Fatal(err) + } case "internal-post-receive": err := internalPostReceive() if err != nil { @@ -74,6 +80,24 @@ func postReceive() error { } pushOptions := HookGetPushOptions() + return foreground(oid, branch, pushOptions) +} + +func postCommit() error { + oid, branch, err := RepoGetBranchOid() + if err != nil { + return err + } + + err = os.Chdir(".git") + if err != nil { + return err + } + + return foreground(oid, branch, nil) +} + +func foreground(oid, branch string, pushOptions []string) error { cfg, err := RepoGetConfig(oid) if err != nil { return err @@ -150,6 +174,10 @@ func internalPostReceive() error { return err } name := filepath.Base(cwd) + if name == ".git" { + // For "post-commit" + name = filepath.Base(filepath.Dir(cwd)) + } tmp, err := os.MkdirTemp("", "punyci-") if err != nil {