| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/tenseleyFlow/shithub/internal/git/hooks" |
| 16 | "github.com/tenseleyFlow/shithub/internal/infra/config" |
| 17 | "github.com/tenseleyFlow/shithub/internal/infra/db" |
| 18 | "github.com/tenseleyFlow/shithub/internal/infra/storage" |
| 19 | reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc" |
| 20 | ) |
| 21 | |
| 22 | // runHooksReinstall is the implementation of `shithubd hooks reinstall`. |
| 23 | // It owns the deployment-time bootstrap of hook shims — call after |
| 24 | // upgrading the binary so every repo's hook scripts point at the new |
| 25 | // path. |
| 26 | func runHooksReinstall(ctx context.Context, all bool, repoArg string, out io.Writer) error { |
| 27 | cfg, err := config.Load(nil) |
| 28 | if err != nil { |
| 29 | return fmt.Errorf("config: %w", err) |
| 30 | } |
| 31 | root, err := filepath.Abs(cfg.Storage.ReposRoot) |
| 32 | if err != nil || root == "" { |
| 33 | return fmt.Errorf("repos_root unset") |
| 34 | } |
| 35 | rfs, err := storage.NewRepoFS(root) |
| 36 | if err != nil { |
| 37 | return fmt.Errorf("repo fs: %w", err) |
| 38 | } |
| 39 | binPath, err := shithubdBinaryPath() |
| 40 | if err != nil { |
| 41 | return fmt.Errorf("binary path: %w", err) |
| 42 | } |
| 43 | |
| 44 | if !all { |
| 45 | owner, name, ok := strings.Cut(repoArg, "/") |
| 46 | if !ok || owner == "" || name == "" { |
| 47 | return errors.New("hooks reinstall: --repo wants owner/name") |
| 48 | } |
| 49 | gitDir, err := rfs.RepoPath(owner, name) |
| 50 | if err != nil { |
| 51 | return fmt.Errorf("repo path: %w", err) |
| 52 | } |
| 53 | if err := hooks.Install(gitDir, binPath); err != nil { |
| 54 | return fmt.Errorf("install: %w", err) |
| 55 | } |
| 56 | fmt.Fprintf(out, "ok: %s/%s\n", owner, name) |
| 57 | return nil |
| 58 | } |
| 59 | |
| 60 | // --all: enumerate via DB. |
| 61 | dbCtx, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 62 | defer cancel() |
| 63 | pool, err := db.Open(dbCtx, db.Config{URL: cfg.DB.URL, MaxConns: 4}) |
| 64 | if err != nil { |
| 65 | return fmt.Errorf("db: %w", err) |
| 66 | } |
| 67 | defer pool.Close() |
| 68 | rq := reposdb.New() |
| 69 | rows, err := rq.ListAllRepoFullNames(dbCtx, pool) |
| 70 | if err != nil { |
| 71 | return fmt.Errorf("list repos: %w", err) |
| 72 | } |
| 73 | var ok, fail int |
| 74 | for _, r := range rows { |
| 75 | gitDir, err := rfs.RepoPath(r.OwnerUsername, r.Name) |
| 76 | if err != nil { |
| 77 | fmt.Fprintf(out, "skip: %s/%s: %v\n", r.OwnerUsername, r.Name, err) |
| 78 | fail++ |
| 79 | continue |
| 80 | } |
| 81 | if err := hooks.Install(gitDir, binPath); err != nil { |
| 82 | fmt.Fprintf(out, "fail: %s/%s: %v\n", r.OwnerUsername, r.Name, err) |
| 83 | fail++ |
| 84 | continue |
| 85 | } |
| 86 | ok++ |
| 87 | } |
| 88 | fmt.Fprintf(out, "reinstalled hooks on %d repos (%d failed)\n", ok, fail) |
| 89 | if fail > 0 { |
| 90 | return fmt.Errorf("%d failures", fail) |
| 91 | } |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | // shithubdBinaryPath returns the absolute path of the running binary. |
| 96 | // hooks.Install bakes this into the shim so every push exec's the same |
| 97 | // version that wrote the hook. |
| 98 | func shithubdBinaryPath() (string, error) { |
| 99 | exe, err := os.Executable() |
| 100 | if err != nil { |
| 101 | return "", err |
| 102 | } |
| 103 | return filepath.Abs(exe) |
| 104 | } |
| 105 |