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