summaryrefslogtreecommitdiff
path: root/bin/pre-generate
blob: c2d3516d878e8551538787dc5e444bebaf0c0fff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/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

	rm -rf -- "$workdir"
	git worktree prune
	git branch -D pre-generated.tmp &>/dev/null || true

	unset GIT_INDEX_FILE
	git worktree add -b pre-generated.tmp "${gitdir}/pre-generated" master
	(
		unset GIT_DIR GIT_WORK_TREE
		cd "$workdir"

		msg="$(git log -n1 master --pretty=format:%B)"
		export GIT_AUTHOR_NAME='Maker'
		export GIT_AUTHOR_EMAIL='maker@andrewdm.me'

		make -j1
		echo '!/out/' >> .gitignore

		git add .
		git commit -m "make: $msg"

		git checkout pre-generated # Ensure it exists locally
		git pull --no-edit -s ours # Avoid conflicts

		# What we want is
		#
		#    git merge --no-edit -s theirs pre-generated.tmp
		#
		# Unfortunately, there is no 'theirs' strategy; so we
		# have to switch branches and do it backward with the
		# 'ours' strategry, switch back, then merge the merge
		# commit.
		git checkout pre-generated.tmp
		git merge --no-edit -s ours pre-generated
		git checkout pre-generated
		git merge pre-generated.tmp

		git branch -d pre-generated.tmp
	)
	rm -rf -- "$workdir"
	git worktree prune
fi