1#!/usr/bin/env bash
2
3# Ensures that the working tree is clean, and Documentation/ is up to
4# date, and then commits Documentation/*.1 to the man branch, and
5# Documentation/*.html to the html branch.
6
7set -uexo pipefail
8
9test "$#" -eq 2
10
11# Must be full ref name, i.e. refs/heads/man, etc.
12man_ref="$1"
13html_ref="$2"
14
15git diff-index --quiet HEAD --  # no uncommitted changes
16git rev-parse --verify "$man_ref"
17git rev-parse --verify "$html_ref"
18echo "$man_ref" | grep -qE '^refs/heads'
19echo "$html_ref" | grep -qE '^refs/heads'
20
21"${MAKE:-make}"
22
23mkdir -p t/tmp
24tmpdir="$(mktemp -d "t/tmp/update-doc-branches-XXXXXX")"
25trap "$(printf 'rm -rf %q' "$tmpdir")" EXIT
26tmpidx="$tmpdir/git-index.tmp"
27
28for fmt in man html; do
29    rm -f "$tmpidx"
30    for f in $(git ls-files 'Documentation/*.md'); do
31        base="$(basename "$f" .md)"
32        if test "$fmt" = man; then
33            ref="$man_ref"
34            GIT_INDEX_FILE="$tmpidx" git add -f "Documentation/$base.1"
35        else
36            ref="$html_ref"
37            GIT_INDEX_FILE="$tmpidx" git add -f "Documentation/$base.html"
38        fi
39    done
40    msg="Update $fmt pages for $(git describe --always)"
41    tree=$(GIT_INDEX_FILE="$tmpidx" git write-tree --prefix=Documentation)
42    commit=$(echo "$msg" | git commit-tree "$tree" -p refs/heads/"$fmt")
43    git update-ref "$ref" "$commit"
44done
45