From 87ce1bc0de3bf1368f3160e0ca28bd282384e109 Mon Sep 17 00:00:00 2001
From: Luke Shumaker <lukeshu@lukeshu.com>
Date: Fri, 9 Feb 2018 17:38:43 -0500
Subject: post-commit: backport safety (+worktree!) things from andrewdm.me

This does not change the pre-generate logic to match Andrew's, just the
worktree setup bits.
---
 post-commit | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

(limited to 'post-commit')

diff --git a/post-commit b/post-commit
index d3a81ba..b8f816a 100755
--- a/post-commit
+++ b/post-commit
@@ -1,24 +1,29 @@
 #!/usr/bin/env bash
+# Copyright 2016-2017 Luke Shumaker
+set -e
 
 branch=$(git name-rev --name-only HEAD)
 if [[ $branch == master ]]; then
+	gitdir="$(git rev-parse --git-dir)"
+	workdir="${gitdir}/pre-generated"
+	exec 8>"${workdir}.lock"
+	flock 8
 
-	stash=false
-	if [[ -n "$(git status --porcelain)" ]]; then
-		stash=true
-		git add .
-		git stash
-	fi
+	rm -rf -- "$workdir"
+	git worktree prune
 
-	git checkout pre-generated
-	git merge master -m 'bogus'
-	make --always-make -j12
-	git add .
-	git commit --amend -m "make: $(git log -n1 master --pretty=format:%B)"
-	git checkout master
+	unset GIT_INDEX_FILE
+	git worktree add "${gitdir}/pre-generated" pre-generated
+	(
+		unset GIT_DIR GIT_WORK_TREE
+		cd "$workdir"
 
-	if $stash; then
-		git stash pop
-	fi
+		git merge master -m 'bogus'
+		make --always-make -j12
+		git add .
+		git commit --amend -m "make: $(git log -n1 master --pretty=format:%B)"
+	)
 
+	rm -rf -- "$workdir"
+	git worktree prune
 fi
-- 
cgit v1.2.3-2-g168b