]> ruderich.org/simon Gitweb - punyci/punyci.git/commitdiff
Add post-commit mode to run in local repository
authorSimon Ruderich <simon@ruderich.org>
Sun, 22 Feb 2026 18:07:38 +0000 (19:07 +0100)
committerSimon Ruderich <simon@ruderich.org>
Sun, 22 Feb 2026 18:12:59 +0000 (19:12 +0100)
git.go
main.go

diff --git a/git.go b/git.go
index 742b0e1150d289c23e4b0c9ac8688c17e9dda948..0d2c0406b875aef3da3c19b120cf3dd38f26ed2c 100644 (file)
--- 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 a70dd4d259fb693a1b9819f0a424d415742dd41a..021ae71b4efd69f42187cf4cf9c9e594c4311056 100644 (file)
--- 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 {