www.eamoncaddigan.net

Content and configuration for https://www.eamoncaddigan.net
git clone https://git.eamoncaddigan.net/www.eamoncaddigan.net.git
Log | Files | Refs | Submodules | README

commit 0ad76244be3f6c641cc123adb7101cbbfe3a393d
parent 7e1c2d8f4816ad1e6b00c01128d8d49bf109a4cd
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Wed,  4 Dec 2024 07:41:33 -0800

Update the December Adventure log for day 4

Diffstat:
Acontent/december-adventure/2024-04/index.md | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+), 0 deletions(-)

diff --git a/content/december-adventure/2024-04/index.md b/content/december-adventure/2024-04/index.md @@ -0,0 +1,57 @@ +--- +title: "2024, Day 4: Git pre-push hook for Hugo, part 2" +date: 2024-12-04T14:48:25-08:00 +description: "Prevent pushing drafts to origin" +draft: false +--- + +Well this is kludgey and frail, but I've worked out a pre-push hook that +should stop me from pushing (most) drafts to my website (which is published +when I push my repository to origin/main, hence the check on line 12). + +```bash +#!/bin/sh +# Prevent a push to origin/main if a suspected draft post is found + +remote="$1" +url="$2" + +z40=0000000000000000000000000000000000000000 + + +while read local_ref local_sha remote_ref remote_sha +do + if [ "$remote $remote_ref" = "origin refs/heads/main" ] + then + if [ "$remote_sha" = $z40 ] + then + # New branch, examine all commits + range="$local_sha" + else + # Update to existing branch, examine new commits + range="$remote_sha..$local_sha" + fi + + git diff -U0 $range | grep -q '^\+draft: true' + if [ $? -eq 0 ] + then + echo >&2 "Found a possible draft, suppress with 'git push --no-verify'" + exit 1 + fi + + fi +done + +exit 0 +``` + +This will allow a push if there are drafts in the commit history, but the +final commit shouldn't have any draft posts. There are plenty of ways that +this could break, but I expect it to suffice for now. + +I do welcome suggestions for improvement! A better script would only check +Markdown files, and only inspect the YAML header, and would catch things +like multiline parameters. TBH I probably wouldn't even share something like +this if I wasn't working on #DecemberAdventure. But I suspect that I'll +refine it if and when I'm annoyed by any misses (false positives and +negatives alike), so maybe it'll become decent someday.