summaryrefslogtreecommitdiffstats
path: root/build/relpath.awk
diff options
context:
space:
mode:
authordev <dev@example>2026-11-13 20:34:47 +0000
committerdev <dev@example>2026-11-13 20:34:47 +0000
commit19a3eb704479fa01c383960ca21a057e8c5fbaf7 (patch)
tree565882a47da5ced0f2112a36703a81406f9a0a69 /build/relpath.awk
parente755a181b0e4c952c35d80e2aa1acd584d2e4595 (diff)
downloadaccess-control-19a3eb704479fa01c383960ca21a057e8c5fbaf7.tar.gz
access-control-19a3eb704479fa01c383960ca21a057e8c5fbaf7.zip
Install: the hook is a relative symlink, and the policy is configuration
The hook is a symlink named pre-receive, because that name is what core.hooksPath looks for and what selects authorize mode inside the binary. It was built by cd'ing into the hook directory first, which left the awk script writing the relative path out of reach, and the recipe failed with the link never created. The path is computed where the script is, and the link is made from there. Relative rather than absolute for the reason that matters during an install: a staged tree under DESTDIR, an unpacked prefix, or an image assembled somewhere else must point at the binary that came with them, not at whatever lives at the same absolute path on the machine reading the link. relpath.awk walks both paths component by component - it is not a string-prefix operation, because /usr/local/share and /usr/local/bin share a prefix only up to the point where ".." starts - and falls back to the absolute path when a component cannot be named from where it stands. authz.conf is the example policy, and the installer treats it as configuration: an existing file is an operator's, so it is kept and the new example lands beside it as authz.conf.new rather than over the rules someone wrote down. `make sanitizer' builds both suites under AddressSanitizer and UBSan, which is how the editing bugs in the previous commit were found.
Diffstat (limited to 'build/relpath.awk')
-rw-r--r--build/relpath.awk79
1 files changed, 60 insertions, 19 deletions
diff --git a/build/relpath.awk b/build/relpath.awk
index 6c9a031..ea5ca99 100644
--- a/build/relpath.awk
+++ b/build/relpath.awk
@@ -1,22 +1,63 @@
-# The path from one directory to a file, expressed relative to that directory.
+# relpath.awk - the path from one directory to one file, relative to the
+# directory. Used by `make install' for the hook symlink:
#
-# The pre-receive hook is this same binary reached through a symlink, because
-# that is how git's core.hooksPath finds it. An absolute symlink would be a
-# hook that works at exactly one prefix; a relative one survives a staged
-# install (DESTDIR), a prefix built elsewhere, and a container image that moves
-# /usr/local around - and `ls -l` still says what the hook is.
+# awk -v from=/usr/local/share/git-host/hooks -v to=/usr/local/bin/git-authz \
+# -f build/relpath.awk -> ../../../bin/git-authz
+#
+# A relative link survives being moved: a staged install under DESTDIR, an
+# unpacked prefix, a container image built somewhere else. An absolute one
+# points at whatever happens to live at that path on the machine reading it,
+# which during a staged install is the wrong answer and after a failed one is a
+# broken link nothing explains.
+#
+# POSIX awk, so busybox awk and mawk and gawk all agree.
+
+function split_path(p, out, n, parts, i, depth) {
+ n = split(p, parts, "/")
+ depth = 0
+ for (i = 1; i <= n; i++) {
+ if (parts[i] == "" || parts[i] == ".")
+ continue # empty runs, and "." changes nothing
+ if (parts[i] == "..") {
+ if (depth > 0)
+ depth-- # already inside a component we know
+ else
+ out[++depth] = ".." # above the root we cannot see into
+ continue
+ }
+ out[++depth] = parts[i]
+ }
+ return depth
+}
+
BEGIN {
- n = split(from, f, "/")
- m = split(to, t, "/")
- i = 1
- while (i <= n && i <= m && f[i] == t[i])
- i++
- rel = ""
- for (k = i; k <= n; k++)
- rel = rel (rel == "" ? "" : "/") ".."
- for (k = i; k <= m; k++)
- rel = rel (rel == "" ? "" : "/") t[k]
- if (rel == "")
- rel = "."
- print rel
+ if (from == "" || to == "") {
+ print "relpath: needs -v from=DIR -v to=FILE" > "/dev/stderr"
+ exit 64
+ }
+
+ nf = split_path(from, f)
+ nt = split_path(to, t)
+
+ common = 0
+ while (common < nf && common < nt && f[common + 1] == t[common + 1])
+ common++
+
+ # Anything above a ".." we could not resolve has to be answered with an
+ # absolute path: there is no relative route into a directory we cannot name.
+ if ((nf > common && f[common + 1] == "..") ||
+ (nt > common && t[common + 1] == "..")) {
+ print to
+ exit 0
+ }
+
+ res = ""
+ for (i = common + 1; i <= nf; i++)
+ res = res ".." "/"
+ for (i = common + 1; i <= nt; i++)
+ res = res t[i] "/"
+ sub(/\/$/, "", res)
+ if (res == "")
+ res = "." # the file is the directory's own name
+ print res
}