1
This commit is contained in:
+24
-24
@@ -13,20 +13,20 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// RunClient implements the `simplegit hook` subcommand invoked by rendered hook
|
||||
// scripts. It forwards one hook invocation to the daemon's hook bus over
|
||||
// <root>/hook.sock and exits with the daemon's response code. It fails open:
|
||||
// any IPC error (no socket, refused, timeout) exits 0 so the git operation is
|
||||
// never blocked by the event bus.
|
||||
//
|
||||
// Usage: simplegit hook --root=PATH <hook-type> [hook-args...]
|
||||
//
|
||||
// --root is parsed manually (not via flag) so hook args that begin with '-' are
|
||||
// passed through verbatim instead of being mistaken for flags.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func RunClient(args []string) {
|
||||
root, rest := parseRoot(args)
|
||||
if root == "" || len(rest) < 1 {
|
||||
// Malformed invocation (e.g. manual call): fail open, do not break git.
|
||||
|
||||
fmt.Fprintln(os.Stderr, "simplegit hook: usage: simplegit hook --root=PATH <hook-type> [args...]")
|
||||
os.Exit(0)
|
||||
}
|
||||
@@ -47,8 +47,8 @@ func RunClient(args []string) {
|
||||
os.Exit(forward(ev, SockPath(root)))
|
||||
}
|
||||
|
||||
// parseRoot extracts a --root=PATH (or --root PATH) flag from args and returns
|
||||
// it plus the remaining positional args.
|
||||
|
||||
|
||||
func parseRoot(args []string) (root string, rest []string) {
|
||||
for i := 0; i < len(args); i++ {
|
||||
a := args[i]
|
||||
@@ -65,8 +65,8 @@ func parseRoot(args []string) (root string, rest []string) {
|
||||
return root, rest
|
||||
}
|
||||
|
||||
// forward sends ev to the hook socket and returns the daemon's requested exit
|
||||
// code. Returns 0 on any error (fail open).
|
||||
|
||||
|
||||
func forward(ev HookEvent, sock string) int {
|
||||
body, err := json.Marshal(ev)
|
||||
if err != nil {
|
||||
@@ -75,8 +75,8 @@ func forward(ev HookEvent, sock string) int {
|
||||
}
|
||||
conn, err := net.DialTimeout("unix", sock, 5*time.Second)
|
||||
if err != nil {
|
||||
// Daemon not running / socket absent: fail open and stay quiet (common in
|
||||
// isolated mode or during a daemon restart).
|
||||
|
||||
|
||||
return 0
|
||||
}
|
||||
defer conn.Close()
|
||||
@@ -101,11 +101,11 @@ func forward(ev HookEvent, sock string) int {
|
||||
return resp.Exit
|
||||
}
|
||||
|
||||
// resolveRepo turns the hook's repo directory into "ns/name". git sets GIT_DIR
|
||||
// for the hook (relative "." under git http-backend / SSH receive-pack, which
|
||||
// chdir into the repo); cwd is the fallback when GIT_DIR is unset. Both sides
|
||||
// are symlink-resolved so a root passed as /tmp and a cwd that resolves to
|
||||
// /private/tmp (macOS) still match. Returns "" if not under <root>/repos.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func resolveRepo(root, gitDir, cwd string) string {
|
||||
dir := gitDir
|
||||
if dir == "" || !filepath.IsAbs(dir) {
|
||||
@@ -125,8 +125,8 @@ func resolveRepo(root, gitDir, cwd string) string {
|
||||
return strings.TrimSuffix(rel, ".git")
|
||||
}
|
||||
|
||||
// captureGitEnv collects GIT_* env vars (GIT_DIR, GIT_PUSH_CERT_*, etc.) so the
|
||||
// event consumer sees the context git handed the hook.
|
||||
|
||||
|
||||
func captureGitEnv() map[string]string {
|
||||
env := map[string]string{}
|
||||
for _, kv := range os.Environ() {
|
||||
|
||||
Reference in New Issue
Block a user