3 # Build two documentation trees and diff the resulting formatted output.
 
   4 # Compared to a source diff, this can reveal mistakes in the formatting.
 
   7 #   ./doc-diff origin/master HEAD
 
   9 # would show the differences introduced by a branch based on master.
 
  12 doc-diff [options] <from> <to> [-- <diff-options>]
 
  15 j=n                     parallel argument to pass to make
 
  16 f                       force rebuild; do not rely on cached results
 
  17 c,clean                 cleanup temporary working files
 
  18 from-asciidoc           use asciidoc with the 'from'-commit
 
  19 from-asciidoctor        use asciidoctor with the 'from'-commit
 
  20 asciidoc                use asciidoc with both commits
 
  21 to-asciidoc             use asciidoc with the 'to'-commit
 
  22 to-asciidoctor          use asciidoctor with the 'to'-commit
 
  23 asciidoctor             use asciidoctor with both commits
 
  24 cut-footer              cut away footer
 
  27 . "$(git --exec-path)/git-sh-setup"
 
  45                 from_program=-asciidoctor ;;
 
  47                 to_program=-asciidoctor ;;
 
  49                 from_program=-asciidoctor
 
  50                 to_program=-asciidoctor ;;
 
  52                 from_program=-asciidoc ;;
 
  54                 to_program=-asciidoc ;;
 
  56                 from_program=-asciidoc
 
  57                 to_program=-asciidoc ;;
 
  59                 cut_footer=-cut-footer ;;
 
  68 tmp="$(git rev-parse --show-toplevel)/Documentation/tmp-doc-diff" || exit 1
 
  72         test $# -eq 0 || usage
 
  73         git worktree remove --force "$tmp/worktree" 2>/dev/null
 
  78 if test -z "$parallel"
 
  80         parallel=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
 
  81         if test $? != 0 || test -z "$parallel"
 
  87 test $# -gt 1 || usage
 
  91 from_oid=$(git rev-parse --verify "$from") || exit 1
 
  92 to_oid=$(git rev-parse --verify "$to") || exit 1
 
  99 # We'll do both builds in a single worktree, which lets "make" reuse
 
 100 # results that don't differ between the two trees.
 
 101 if ! test -d "$tmp/worktree"
 
 103         git worktree add -f --detach "$tmp/worktree" "$from" &&
 
 104         dots=$(echo "$tmp/worktree" | sed 's#[^/]*#..#g') &&
 
 105         ln -s "$dots/config.mak" "$tmp/worktree/config.mak"
 
 108 construct_makemanflags () {
 
 109         if test "$1" = "-asciidoc"
 
 111                 echo USE_ASCIIDOCTOR=
 
 112         elif test "$1" = "-asciidoctor"
 
 114                 echo USE_ASCIIDOCTOR=YesPlease
 
 118 from_makemanflags=$(construct_makemanflags "$from_program") &&
 
 119 to_makemanflags=$(construct_makemanflags "$to_program") &&
 
 121 from_dir=$from_oid$from_program$cut_footer &&
 
 122 to_dir=$to_oid$to_program$cut_footer &&
 
 124 # generate_render_makefile <srcdir> <dstdir>
 
 125 generate_render_makefile () {
 
 130                 printf 'all:: %s\n' "$dst"
 
 131                 printf '%s: %s\n' "$dst" "$src"
 
 132                 printf '\t@echo >&2 "  RENDER $(notdir $@)" && \\\n'
 
 133                 printf '\tmkdir -p $(dir $@) && \\\n'
 
 134                 printf '\tMANWIDTH=80 man $< >$@+ && \\\n'
 
 135                 printf '\tmv $@+ $@\n'
 
 139 # render_tree <committish_oid> <directory_name> <makemanflags>
 
 141         # Skip install-man entirely if we already have an installed directory.
 
 142         # We can't rely on make here, since "install-man" unconditionally
 
 143         # copies the files (spending effort, but also updating timestamps that
 
 144         # we then can't rely on during the render step). We use "mv" to make
 
 145         # sure we don't get confused by a previous run that failed partway
 
 150         if ! test -d "$tmp/installed/$dname"
 
 152                 git -C "$tmp/worktree" checkout --detach "$oid" &&
 
 153                 make -j$parallel -C "$tmp/worktree" \
 
 155                         GIT_VERSION=omitted \
 
 156                         SOURCE_DATE_EPOCH=0 \
 
 157                         DESTDIR="$tmp/installed/$dname+" \
 
 159                 mv "$tmp/installed/$dname+" "$tmp/installed/$dname"
 
 162         # As with "installed" above, we skip the render if it's already been
 
 163         # done.  So using make here is primarily just about running in
 
 165         if ! test -d "$tmp/rendered/$dname"
 
 167                 generate_render_makefile "$tmp/installed/$dname" \
 
 168                         "$tmp/rendered/$dname+" |
 
 169                 make -j$parallel -f - &&
 
 170                 mv "$tmp/rendered/$dname+" "$tmp/rendered/$dname"
 
 172                 if test "$cut_footer" = "-cut-footer"
 
 174                         for f in $(find "$tmp/rendered/$dname" -type f)
 
 176                                 head -n -2 "$f" | sed -e '${/^$/d}' >"$f+" &&
 
 184 render_tree $from_oid $from_dir $from_makemanflags &&
 
 185 render_tree $to_oid $to_dir $to_makemanflags &&
 
 186 git -C $tmp/rendered diff --no-index "$@" $from_dir $to_dir