Diff options
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Keep the age hook quiet when it cannot write
-rwxr-xr-xcustom/hooks/post-receive.cgit-age12
-rwxr-xr-xtests/t0507-hooks.sh23
2 files changed, 30 insertions, 5 deletions
diff --git a/custom/hooks/post-receive.cgit-age b/custom/hooks/post-receive.cgit-age
index a87e325..c2c4092 100755
--- a/custom/hooks/post-receive.cgit-age
+++ b/custom/hooks/post-receive.cgit-age
@@ -2,8 +2,10 @@
# Updates the age-file cgit reads for the idle column. The path below is the
# default age-file, so a cgitrc that changes the setting needs the same change
-# here. To install the hook, copy or link it to hooks/post-receive in each
-# repository.
+# here. A file the pusher cannot write stays as it was, and nothing here
+# complains or exits nonzero, so the pusher is not shown an error over a stale
+# idle column. To install the hook, copy or link it to hooks/post-receive in
+# each repository.
set -eu
@@ -32,10 +34,10 @@ date="$(git for-each-ref --sort=-committerdate --count=1 \
# being handed an empty file.
test -n "$date" || exit 0
-mkdir -p "$(dirname "$agefile")"
+mkdir -p "$(dirname "$agefile")" 2>/dev/null || exit 0
# Write through a temporary file so a reader never sees a half written date.
tmpfile="$agefile.$$"
trap 'rm -f "$tmpfile"' EXIT
-printf '%s\n' "$date" >"$tmpfile"
-mv "$tmpfile" "$agefile"
+{ printf '%s\n' "$date" >"$tmpfile"; } 2>/dev/null || exit 0
+mv "$tmpfile" "$agefile" 2>/dev/null || :
diff --git a/tests/t0507-hooks.sh b/tests/t0507-hooks.sh
index 8a45f81..9211f8b 100755
--- a/tests/t0507-hooks.sh
+++ b/tests/t0507-hooks.sh
@@ -115,6 +115,29 @@ do
"
done
+for shell in $shells
+do
+ test_expect_success SANITY "the age hook shows a pusher no error under $shell" "
+ echo kept >kept &&
+ cp kept plain.git/info/web/last-modified &&
+ test_when_finished 'chmod 755 plain.git/info/web' &&
+ chmod 555 plain.git/info/web &&
+ (cd plain.git && $shell \"\$HOOK_DIRECTORY/post-receive.cgit-age\" 2>../err) &&
+ test_must_be_empty err &&
+ test_cmp kept plain.git/info/web/last-modified
+ "
+done
+
+test_expect_success SANITY 'the age hook shows no error when it cannot make its directory' '
+ git init -q --bare closed.git &&
+ git -C work push -q ../closed.git HEAD:refs/heads/main &&
+ test_when_finished "chmod 755 closed.git/info" &&
+ chmod 555 closed.git/info &&
+ (cd closed.git && sh "$HOOK_DIRECTORY/post-receive.cgit-age" 2>../err) &&
+ test_must_be_empty err &&
+ test_path_is_missing closed.git/info/web
+'
+
# git keeps a shared repository group writable whatever the pusher's umask,
# and the next member to push has to be able to replace the file.
test_expect_success SANITY 'the age hook keeps a shared directory group writable' '