Root/scripts/schhist2web

Source at commit 78f6cfbfda4942457e7a158a4f9fafd5e1fca067 created 10 years 23 days ago.
By Werner Almesberger, Attempt at avoiding the false changes seen on Xue. Seems that alpha-blending produces minute differences where there should be none.
1#!/bin/bash
2#
3# schhist2web - Web-browseable graphical revision history of schematics
4#
5# Written 2010 by Werner Almesberger
6# Copyright 2010 Werner Almesberger
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13
14
15OUTDIR=_out
16THUMB_OPTS="-w 3 -d 60 -c 0.5,0.5,0.5 -n 1,1,0"
17BG_COLOR="f0f0ff"
18FNAME_COLOR="#b0f0ff"
19SEP_COLOR="#000000"
20
21
22shrink()
23{
24    pnmscale -width 120 "$@" || exit
25}
26
27
28pngdiff()
29{
30    # pngdiff preproc outfile arg ...
31    pp="$1"
32    of="$2"
33    shift 2
34    if ! PATH=$PATH:`dirname $0`/ppmdiff ppmdiff "$@" "$out/_tmp"; then
35    rm -f "$out/_tmp"
36    return 1
37    fi
38    $pp "$out/_tmp" | pnmtopng >"$of"
39    rm "$out/_tmp"
40}
41
42
43symlink()
44{
45    local old=$1 new=$2
46    local src=`dirname "$new"`/$old
47
48    if [ -L "$src" ]; then
49    ln -sf "`readlink \"$src\"`" "$new"
50    else
51    ln -sf "$old" "$new"
52    fi
53}
54
55
56commit_entry()
57{
58    # usage: commit_entry <base-dir> <commit>
59    # note: the repository's base in $dir must be provided by the caller
60
61    local dir=$1 next=$2
62
63    cat <<EOF
64<TABLE bgcolor="$SEP_COLOR" cellspacing=0 width="100%"><TR><TD></TABLE>
65EOF
66    echo "<PRE>"
67    ( cd "$dir" && git show \
68        --pretty=format:"%aN <%aE>%n%ad, %ar%n%n %s" \
69    --quiet $next; ) |
70      sed 's/&/&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' |
71      if [ -z "$SCHHIST_COMMIT_TEMPLATE" ]; then
72    cat
73      else
74    url=`echo "$SCHHIST_COMMIT_TEMPLATE" | sed "s/{}/$next/g"`
75    sed "1s|^|<A href=\"$url\"><B>\&gt;\&gt;\&gt;</B></a> |"
76      fi
77    echo "</PRE>"
78}
79
80
81usage()
82{
83    cat <<EOF 2>&1
84usage: $0 [-c cache-dir] [-n] [-S] [top-dir] [top-schem] [out-dir]
85
86  top-dir top-level directory of the git archive (default: locate it)
87  top-schem root sheet of the schematics (default: locate it in top-dir)
88  out-dir output directory (default: $OUTDIR)
89  -c cache-dir cache directory (default: same as out-dir)
90  -n don't use previous cache content (rebuild the cache)
91  -S sanitize KiCad profile
92EOF
93    exit 1
94}
95
96
97# --- Parse command-line options ----------------------------------------------
98
99
100no_cache=false
101sanitize=
102
103while true; do
104    case "$1" in
105    -n) no_cache=true
106    shift;;
107    -c) [ -z "$1" ] && usage
108    cache="$1"
109    shift 2;;
110    -S) sanitize=-S
111    shift;;
112    -*) usage;;
113    *) break;;
114    esac
115done
116
117
118# --- Interpret the command-line arguments ------------------------------------
119
120
121if [ ! -z "$1" -a -d "$1/.git" ]; then
122    dir="$1"
123    shift
124else
125    dir=.
126    while [ ! -d $dir/.git ]; do
127    if [ $dir -ef $dir/.. ]; then
128        echo "no .git/ directory found in hierarchy" 1>&2
129        exit 1
130    fi
131    dir=$dir/..
132    done
133    echo "found top-dir: $dir" 1>&2
134fi
135
136if [ ! -z "$1" -a -f "$dir/$1" -a \
137  -f "$dir/${1%.sch}.pro" ]; then
138    sch="$1"
139    shift
140else
141    for n in "$dir"/*.sch; do
142    [ -f "${n%.sch}.pro" ] || continue
143    if [ ! -z "$sch" ]; then
144        echo "multiple choices for top-level .sch file" 1>&2
145        exit 1
146    fi
147    sch="$n"
148    done
149    if [ -z "$sch" -o "$sch" = "$dir/*.sch" ]; then
150    echo "no candidate for top-level .sch file found" 1>&2
151    exit 1
152    fi
153    echo "found root sheet: $sch" 1>&2
154fi
155
156if [ ! -z "$1" ] && [ ! -e "$1" ] || [ -d "$1" -a ! -d "$1"/.git ]; then
157    out="$1"
158    shift
159else
160    out=$OUTDIR
161fi
162[ -z "$cache" ] && cache="$out"
163
164[ -z "$1" ] || usage
165
166
167# --- Set up some variables and the directories for cache and output ----------
168
169
170PATH=`dirname "$0"`:"$PATH"
171first=`gitenealogy "$dir" "$sch" | sed '$s/ .*//p;d'`
172schname=`gitenealogy "$dir" "$sch" | sed '$s/^.* //p;d'`
173
174rm -rf "$out/diff_*" "$out/thumb_*" "$out/names"
175$no_cache && rm -rf "$cache"
176mkdir -p "$out/names"
177mkdir -p "$cache"
178
179ppmmake '#e0e0e0' 5 30 | pnmtopng >"$out"/unchanged.png
180
181
182# --- Generate/update the cache -----------------------------------------------
183
184
185head=
186for n in $first `cd "$dir" && git rev-list --reverse $first..HEAD`; do
187    ( cd "$dir" && git show --pretty=format:'' --name-only $n; ) |
188      egrep -q '\.sch$|\.pro$|\.lib$' || continue
189    echo Processing $n
190    new=`gitenealogy "$dir" "$sch" | sed "/^$n /s///p;d"`
191    if [ ! -z "$new" ]; then
192    echo Name change $schname to $new 1>&2
193    schname="$new"
194    fi
195    tmp=`pwd`/_schhist2web
196    trap "rm -rf \"$cache/ppm_$n\" \"$cache/fat_$n\" \"$cache/ps_$n\" \
197      \"$cache/hard_$n\" \"$tmp\"" 0
198    if [ ! -d "$cache/ppm_$n" ]; then
199    rm -rf "$cache/ppm_$n" "$cache/fat_$n" "$cache/ps_$n" "$cache/hard_$n"
200    mkdir "$cache/ppm_$n" "$cache/fat_$n" "$cache/ps_$n" "$cache/hard_$n"
201    #
202    # potential optimization here: remember Postscript files from previous
203    # run (or their md5sum) and check if they have changed. If not, skip
204    # the ghostscript run and just put a symlink, replacing the less
205    # efficient optimization below.
206    #
207    gitsch2ps $sanitize "$dir" "$schname" $n "$tmp" || exit
208    for m in "$tmp"/*.ps; do
209        # Postscript, for making PDFs later
210        ps="$cache/ps_$n/`basename "$m"`"
211        normalizeschps "$m" "$ps" || exit
212
213        # Unadorned pixmap, for comparison
214        ppm="$cache/hard_$n/`basename "$m" .ps`.ppm"
215        schps2ppm -n "$ps" "$ppm" || exit
216
217        # Pixmap with thin lines, for the detail views
218        ppm="$cache/ppm_$n/`basename "$m" .ps`.ppm"
219        normalizeschps -w 120 "$m" | schps2ppm - "$ppm" || exit
220
221        # Pixmap with thick lines, for the thumbnails
222        ppm="$cache/fat_$n/`basename "$m" .ps`.ppm"
223        normalizeschps -w 500 "$m" | schps2ppm - "$ppm" || exit
224    done
225    rm -rf "$tmp"
226    fi
227    for m in "$cache/ppm_$n/"*; do
228    [ "$m" = "$cache/ppm_$n/*" ] && break
229    if [ ! -z "$head" ]; then
230        prev="$cache/ppm_$head"/`basename "$m"`
231        if [ -r "$prev" ] && cmp -s "$prev" "$m"; then
232        symlink "../ppm_$head/`basename \"$m\"`" "$m"
233        symlink "../fat_$head/`basename \"$m\"`" \
234          "$cache/fat_$n/`basename \"$m\"`"
235        fi
236    fi
237    touch "$out/names/`basename \"$m\" .ppm`"
238    done
239    trap 0
240    head=$n
241done
242
243if [ -z "$head" ]; then
244    echo "no usable head found" 2>&1
245    exit 1
246fi
247
248
249# --- Title of the Web page and table header ----------------------------------
250
251
252index="$out/index.html"
253all=
254{
255    cat <<EOF
256<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
257<HTML>
258EOF
259    if [ ! -z "$SCHHIST_TITLE" ]; then
260    echo "<TITLE>$SCHHIST_TITLE</TITLE>"
261    fi
262    echo "<BODY>"
263    if [ ! -z "$SCHHIST_TITLE" ]; then
264    echo "<H1>"
265    [ -z "$SCHHIST_HOME_URL" ] || echo "<A href=\"$SCHHIST_HOME_URL\">"
266    echo "$SCHHIST_TITLE"
267    [ -z "$SCHHIST_HOME_URL" ] || echo "</A>"
268    echo "</H1>"
269    fi
270    cat <<EOF
271<TABLE bgcolor="$BG_COLOR" callpadding=1>
272<TR bgcolor="$FNAME_COLOR">
273EOF
274    while read m; do
275    ps="$cache/ps_$head/$m.ps"
276    if [ -r "$ps" ]; then
277        #
278        # Note: we read from variable ps_$head but we write to constant
279        # pdf_head. We can't use pdf_$head here, because that may just be a
280        # commit with a change and we thus generate a delta PDF below.
281        #
282        mkdir -p "$out/pdf_head"
283        schps2pdf -o "$out/pdf_head/$m.pdf" "$ps" || exit
284        all="$all \"$ps\""
285        echo "<TD><A href=\"pdf_head/$m.pdf\"><B>$m</B></A>"
286    else
287        echo "<TD><B>$m</B>"
288    fi
289    done < <(ls -1 "$out/names")
290    proj=`basename "$sch" .sch`
291    eval schps2pdf -t \""$proj-"\" -o \""$out/pdf_$proj.pdf"\" $all
292    echo "<TD><A href=\"pdf_$proj.pdf\">All sheets</A>"
293} >"$index"
294
295
296# --- Diff all the revisions, newest to oldest --------------------------------
297
298
299next="$head"
300for n in `cd "$dir" && git rev-list $first..HEAD~1` $first; do
301    [ -d "$cache/ppm_$n" ] || continue
302    empty=true
303    s="<TR>"
304    mkdir -p "$out/diff_$next" "$out/thumb_$next"
305    while read m; do
306    a="$cache/ppm_$n/$m.ppm"
307    fat_a="$cache/fat_$n/$m.ppm"
308    hard_a="$cache/hard_$n/$m.ppm"
309    b="$cache/ppm_$next/$m.ppm"
310    fat_b="$cache/fat_$next/$m.ppm"
311    hard_b="$cache/hard_$next/$m.ppm"
312    diff="$out/diff_$next/$m.png"
313    thumb="$out/thumb_$next/$m.png"
314
315    if [ -f "$a" -a -f "$b" ]; then
316        s="$s<TD align=\"center\" valign=\"middle\">"
317        if ! pngdiff cat "$diff" "$a" "$b" "$hard_a" "$hard_b"; then
318        s="$s<IMG src=\"unchanged.png\""
319        continue
320        fi
321        pngdiff shrink "$thumb" -f $THUMB_OPTS "$fat_a" "$fat_b" \
322          "$hard_a" "$hard_b" || exit
323        mkdir -p "$out/pdf_$next"
324        schps2pdf -T BEFORE -T AFTER -o "$out/pdf_$next/$m.pdf" \
325          "$cache/ps_$n/$m.ps" "$cache/ps_$next/$m.ps" || exit
326    elif [ -f "$a" ]; then
327        s="$s<TD>"
328        pngdiff cat "$diff" -f -c 1,0,0 "$a" "$a" || exit
329        pngdiff shrink "$thumb" -f $THUMB_OPTS -c 1,0,0 "$fat_a" "$fat_a" \
330          || exit
331        mkdir -p "$out/pdf_$next"
332        schps2pdf -T DELETED -o "$out/pdf_$next/$m.pdf" \
333          "$cache/ps_$n/$m.ps" || exit
334    elif [ -f "$b" ]; then
335        s="$s<TD>"
336        pngdiff cat "$diff" -f -c 0,1,0 "$b" "$b" || exit
337        pngdiff shrink "$thumb" -f $THUMB_OPTS -c 0,1,0 "$fat_b" "$fat_b" \
338          || exit
339        mkdir -p "$out/pdf_$next"
340        schps2pdf -T NEW -o "$out/pdf_$next/$m.pdf" \
341          "$cache/ps_$next/$m.ps" || exit
342    else
343        s="$s<TD>"
344        continue
345    fi
346    echo "$s" >>"$index"
347    s=
348    empty=false
349    mkdir -p "$out/html_$next"
350    echo "<A href=\"html_$next/$m.html\"><IMG src=\"thumb_$next/$m.png\"></A>" >>"$index"
351    cat <<EOF >"$out/html_$next/$m.html"
352<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
353<HTML>
354<TITLE>$m</TITLE>
355<BODY>
356<A href="../pdf_$next/$m.pdf"><IMG src="../diff_$next/$m.png"></A>
357</BODY>
358EOF
359    done < <(ls -1 "$out/names")
360    if ! $empty; then
361     echo "$s<TD valign=\"middle\">" >>"$index"
362    commit_entry "$dir" $next >>"$index"
363    fi
364    next=$n
365done
366
367
368# --- Add creation entries for all files in the first commit ------------------
369
370
371if [ -d "$cache/ppm_$next" ]; then # could this ever be false ?
372    empty=true
373    echo "<TR>" >>"$index"
374    mkdir -p "$out/diff_$next" "$out/thumb_$next"
375    while read m; do
376    ppm="$cache/ppm_$next/$m.ppm"
377    fat="$cache/fat_$next/$m.ppm"
378    diff="$out/diff_$next/$m.png"
379    thumb="$out/thumb_$next/$m.png"
380
381    echo "<TD>" >>"$index"
382    [ -f "$ppm" ] || continue
383    pngdiff cat "$diff" -f -c 0,1,0 "$ppm" "$ppm" || exit
384    pngdiff shrink "$thumb" -f $THUMB_OPTS -c 0,1,0 "$fat" "$fat" \
385          || exit
386    empty=false
387    echo "<A href=\"diff_$next/$m.png\"><IMG src=\"thumb_$next/$m.png\"></A>" >>"$index"
388    done < <(ls -1 "$out/names")
389    if ! $empty; then
390     echo "<TD valign=\"middle\">" >>"$index"
391    commit_entry "$dir" $next >>"$index"
392    fi
393fi
394
395
396# --- Finish ------------------------------------------------------------------
397
398
399cat <<EOF >>"$index"
400</TABLE>
401<HR>
402`date -u '+%F %X'` UTC
403</BODY>
404</HTML>
405EOF
406

Archive Download this file



interactive