doc-diff 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/sh
  2. #
  3. # Build two documentation trees and diff the resulting formatted output.
  4. # Compared to a source diff, this can reveal mistakes in the formatting.
  5. # For example:
  6. #
  7. # ./doc-diff origin/master HEAD
  8. #
  9. # would show the differences introduced by a branch based on master.
  10. OPTIONS_SPEC="\
  11. doc-diff [options] <from> <to> [-- <diff-options>]
  12. doc-diff (-c|--clean)
  13. --
  14. j=n parallel argument to pass to make
  15. f force rebuild; do not rely on cached results
  16. c,clean cleanup temporary working files
  17. from-asciidoc use asciidoc with the 'from'-commit
  18. from-asciidoctor use asciidoctor with the 'from'-commit
  19. asciidoc use asciidoc with both commits
  20. to-asciidoc use asciidoc with the 'to'-commit
  21. to-asciidoctor use asciidoctor with the 'to'-commit
  22. asciidoctor use asciidoctor with both commits
  23. cut-footer cut away footer
  24. "
  25. SUBDIRECTORY_OK=1
  26. . "$(git --exec-path)/git-sh-setup"
  27. parallel=
  28. force=
  29. clean=
  30. from_program=
  31. to_program=
  32. cut_footer=
  33. while test $# -gt 0
  34. do
  35. case "$1" in
  36. -j)
  37. parallel=$2; shift ;;
  38. -c|--clean)
  39. clean=t ;;
  40. -f)
  41. force=t ;;
  42. --from-asciidoctor)
  43. from_program=-asciidoctor ;;
  44. --to-asciidoctor)
  45. to_program=-asciidoctor ;;
  46. --asciidoctor)
  47. from_program=-asciidoctor
  48. to_program=-asciidoctor ;;
  49. --from-asciidoc)
  50. from_program=-asciidoc ;;
  51. --to-asciidoc)
  52. to_program=-asciidoc ;;
  53. --asciidoc)
  54. from_program=-asciidoc
  55. to_program=-asciidoc ;;
  56. --cut-footer)
  57. cut_footer=-cut-footer ;;
  58. --)
  59. shift; break ;;
  60. *)
  61. usage ;;
  62. esac
  63. shift
  64. done
  65. tmp="$(git rev-parse --show-toplevel)/Documentation/tmp-doc-diff" || exit 1
  66. if test -n "$clean"
  67. then
  68. test $# -eq 0 || usage
  69. git worktree remove --force "$tmp/worktree" 2>/dev/null
  70. rm -rf "$tmp"
  71. exit 0
  72. fi
  73. if test -z "$parallel"
  74. then
  75. parallel=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
  76. if test $? != 0 || test -z "$parallel"
  77. then
  78. parallel=1
  79. fi
  80. fi
  81. test $# -gt 1 || usage
  82. from=$1; shift
  83. to=$1; shift
  84. from_oid=$(git rev-parse --verify "$from") || exit 1
  85. to_oid=$(git rev-parse --verify "$to") || exit 1
  86. if test -n "$force"
  87. then
  88. rm -rf "$tmp"
  89. fi
  90. # We'll do both builds in a single worktree, which lets "make" reuse
  91. # results that don't differ between the two trees.
  92. if ! test -d "$tmp/worktree"
  93. then
  94. git worktree add -f --detach "$tmp/worktree" "$from" &&
  95. dots=$(echo "$tmp/worktree" | sed 's#[^/]*#..#g') &&
  96. ln -s "$dots/config.mak" "$tmp/worktree/config.mak"
  97. fi
  98. construct_makemanflags () {
  99. if test "$1" = "-asciidoc"
  100. then
  101. echo USE_ASCIIDOCTOR=
  102. elif test "$1" = "-asciidoctor"
  103. then
  104. echo USE_ASCIIDOCTOR=YesPlease
  105. fi
  106. }
  107. from_makemanflags=$(construct_makemanflags "$from_program") &&
  108. to_makemanflags=$(construct_makemanflags "$to_program") &&
  109. from_dir=$from_oid$from_program$cut_footer &&
  110. to_dir=$to_oid$to_program$cut_footer &&
  111. # generate_render_makefile <srcdir> <dstdir>
  112. generate_render_makefile () {
  113. find "$1" -type f |
  114. while read src
  115. do
  116. dst=$2/${src#$1/}
  117. printf 'all: %s\n' "$dst"
  118. printf '%s: %s\n' "$dst" "$src"
  119. printf '\t@echo >&2 " RENDER $(notdir $@)" && \\\n'
  120. printf '\tmkdir -p $(dir $@) && \\\n'
  121. printf '\tMANWIDTH=80 man $< >$@+ && \\\n'
  122. printf '\tmv $@+ $@\n'
  123. done
  124. }
  125. # render_tree <committish_oid> <directory_name> <makemanflags>
  126. render_tree () {
  127. # Skip install-man entirely if we already have an installed directory.
  128. # We can't rely on make here, since "install-man" unconditionally
  129. # copies the files (spending effort, but also updating timestamps that
  130. # we then can't rely on during the render step). We use "mv" to make
  131. # sure we don't get confused by a previous run that failed partway
  132. # through.
  133. oid=$1 &&
  134. dname=$2 &&
  135. makemanflags=$3 &&
  136. if ! test -d "$tmp/installed/$dname"
  137. then
  138. git -C "$tmp/worktree" checkout --detach "$oid" &&
  139. make -j$parallel -C "$tmp/worktree" \
  140. $makemanflags \
  141. GIT_VERSION=omitted \
  142. SOURCE_DATE_EPOCH=0 \
  143. DESTDIR="$tmp/installed/$dname+" \
  144. install-man &&
  145. mv "$tmp/installed/$dname+" "$tmp/installed/$dname"
  146. fi &&
  147. # As with "installed" above, we skip the render if it's already been
  148. # done. So using make here is primarily just about running in
  149. # parallel.
  150. if ! test -d "$tmp/rendered/$dname"
  151. then
  152. generate_render_makefile "$tmp/installed/$dname" \
  153. "$tmp/rendered/$dname+" |
  154. make -j$parallel -f - &&
  155. mv "$tmp/rendered/$dname+" "$tmp/rendered/$dname"
  156. if test "$cut_footer" = "-cut-footer"
  157. then
  158. for f in $(find "$tmp/rendered/$dname" -type f)
  159. do
  160. head -n -2 "$f" | sed -e '${/^$/d}' >"$f+" &&
  161. mv "$f+" "$f" ||
  162. return 1
  163. done
  164. fi
  165. fi
  166. }
  167. render_tree $from_oid $from_dir $from_makemanflags &&
  168. render_tree $to_oid $to_dir $to_makemanflags &&
  169. git -C $tmp/rendered diff --no-index "$@" $from_dir $to_dir