Merge branch 'jt/test-protocol-version'
[git] / Documentation / doc-diff
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
11 OPTIONS_SPEC="\
12 doc-diff [options] <from> <to> [-- <diff-options>]
13 doc-diff (-c|--clean)
14 --
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 "
19 SUBDIRECTORY_OK=1
20 . "$(git --exec-path)/git-sh-setup"
21
22 parallel=
23 force=
24 clean=
25 while test $# -gt 0
26 do
27         case "$1" in
28         -j)
29                 parallel=$2; shift ;;
30         -c|--clean)
31                 clean=t ;;
32         -f)
33                 force=t ;;
34         --)
35                 shift; break ;;
36         *)
37                 usage ;;
38         esac
39         shift
40 done
41
42 tmp="$(git rev-parse --show-toplevel)/Documentation/tmp-doc-diff" || exit 1
43
44 if test -n "$clean"
45 then
46         test $# -eq 0 || usage
47         git worktree remove --force "$tmp/worktree" 2>/dev/null
48         rm -rf "$tmp"
49         exit 0
50 fi
51
52 if test -z "$parallel"
53 then
54         parallel=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
55         if test $? != 0 || test -z "$parallel"
56         then
57                 parallel=1
58         fi
59 fi
60
61 test $# -gt 1 || usage
62 from=$1; shift
63 to=$1; shift
64
65 from_oid=$(git rev-parse --verify "$from") || exit 1
66 to_oid=$(git rev-parse --verify "$to") || exit 1
67
68 if test -n "$force"
69 then
70         rm -rf "$tmp"
71 fi
72
73 # We'll do both builds in a single worktree, which lets "make" reuse
74 # results that don't differ between the two trees.
75 if ! test -d "$tmp/worktree"
76 then
77         git worktree add -f --detach "$tmp/worktree" "$from" &&
78         dots=$(echo "$tmp/worktree" | sed 's#[^/]*#..#g') &&
79         ln -s "$dots/config.mak" "$tmp/worktree/config.mak"
80 fi
81
82 # generate_render_makefile <srcdir> <dstdir>
83 generate_render_makefile () {
84         find "$1" -type f |
85         while read src
86         do
87                 dst=$2/${src#$1/}
88                 printf 'all:: %s\n' "$dst"
89                 printf '%s: %s\n' "$dst" "$src"
90                 printf '\t@echo >&2 "  RENDER $(notdir $@)" && \\\n'
91                 printf '\tmkdir -p $(dir $@) && \\\n'
92                 printf '\tMANWIDTH=80 man $< >$@+ && \\\n'
93                 printf '\tmv $@+ $@\n'
94         done
95 }
96
97 # render_tree <committish_oid>
98 render_tree () {
99         # Skip install-man entirely if we already have an installed directory.
100         # We can't rely on make here, since "install-man" unconditionally
101         # copies the files (spending effort, but also updating timestamps that
102         # we then can't rely on during the render step). We use "mv" to make
103         # sure we don't get confused by a previous run that failed partway
104         # through.
105         if ! test -d "$tmp/installed/$1"
106         then
107                 git -C "$tmp/worktree" checkout --detach "$1" &&
108                 make -j$parallel -C "$tmp/worktree" \
109                         GIT_VERSION=omitted \
110                         SOURCE_DATE_EPOCH=0 \
111                         DESTDIR="$tmp/installed/$1+" \
112                         install-man &&
113                 mv "$tmp/installed/$1+" "$tmp/installed/$1"
114         fi &&
115
116         # As with "installed" above, we skip the render if it's already been
117         # done.  So using make here is primarily just about running in
118         # parallel.
119         if ! test -d "$tmp/rendered/$1"
120         then
121                 generate_render_makefile "$tmp/installed/$1" "$tmp/rendered/$1+" |
122                 make -j$parallel -f - &&
123                 mv "$tmp/rendered/$1+" "$tmp/rendered/$1"
124         fi
125 }
126
127 render_tree $from_oid &&
128 render_tree $to_oid &&
129 git -C $tmp/rendered diff --no-index "$@" $from_oid $to_oid