# relpath.awk - the path from one directory to one file, relative to the # directory. Used by `make install' for the hook symlink: # # 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 { 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 }