Root/scripts/schhist2web

Source at commit 56a95040ca2fe2a265bd27fce365515f7656383e created 13 years 7 months ago.
By Werner Almesberger, Generate commit entry and show diffs also for the first commit. Plus cleanup.
1#!/bin/sh
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
43commit_entry()
44{
45    # usage: commit_entry <base-dir> <commit>
46    # note: the repository's base in $dir must be provided by the caller
47
48    local dir=$1 next=$2
49
50    cat <<EOF
51<TABLE bgcolor="$SEP_COLOR" cellspacing=0 width="100%"><TR><TD></TABLE>
52EOF
53    echo "<PRE>"
54    ( cd "$dir" && git show --pretty=short --quiet $next; ) |
55      if [ -z "$SCHHIST_COMMIT_TEMPLATE" ]; then
56    cat
57      else
58    url=`echo "$SCHHIST_COMMIT_TEMPLATE" | sed "s/{}/$next/g"`
59    sed "s|^commit |<A href=\"$url\">commit</a> |"
60      fi |
61      sed '/^<.*>commit</n;s/&/&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
62    echo "</PRE>"
63}
64
65
66usage()
67{
68    cat <<EOF 2>&1
69usage: $0 [-c cache-dir] [-n] [-S] [top-dir] [top-schem] [out-dir]
70
71  top-dir top-level directory of the git archive (default: locate it)
72  top-schem root sheet of the schematics (default: locate it in top-dir)
73  out-dir output directory (default: $OUTDIR)
74  -c cache-dir cache directory (default: same as out-dir)
75  -n don't use previous cache content (rebuild the cache)
76  -S sanitize KiCad profile
77EOF
78    exit 1
79}
80
81
82# --- Parse command-line options ----------------------------------------------
83
84
85no_cache=false
86sanitize=
87
88while true; do
89    case "$1" in
90    -n) no_cache=true
91    shift;;
92    -c) [ -z "$1" ] && usage
93    cache="$1"
94    shift 2;;
95    -S) sanitize=-S
96    shift;;
97    -*) usage;;
98    *) break;;
99    esac
100done
101
102
103# --- Interpret the command-line arguments ------------------------------------
104
105
106if [ ! -z "$1" -a -d "$1/.git" ]; then
107    dir="$1"
108    shift
109else
110    dir=.
111    while [ ! -d $dir/.git ]; do
112    if [ $dir -ef $dir/.. ]; then
113        echo "no .git/ directory found in hierarchy" 1>&2
114        exit 1
115    fi
116    dir=$dir/..
117    done
118    echo "found top-dir: $dir" 1>&2
119fi
120
121if [ ! -z "$1" -a -f "$dir/$1" -a \
122  -f "$dir/${1%.sch}.pro" ]; then
123    sch="$1"
124    shift
125else
126    for n in "$dir"/*.sch; do
127    [ -f "${n%.sch}.pro" ] || continue
128    if [ ! -z "$sch" ]; then
129        echo "multiple choices for top-level .sch file" 1>&2
130        exit 1
131    fi
132    sch="$n"
133    done
134    if [ -z "$sch" -o "$sch" = "$dir/*.sch" ]; then
135    echo "no candidate for top-level .sch file found" 1>&2
136    exit 1
137    fi
138    echo "found root sheet: $sch" 1>&2
139fi
140
141if [ ! -z "$1" ] && [ ! -e "$1" ] || [ -d "$1" -a ! -d "$1"/.git ]; then
142    out="$1"
143    shift
144else
145    out=$OUTDIR
146fi
147[ -z "$cache" ] && cache="$out"
148
149[ -z "$1" ] || usage
150
151
152# --- Set up some variables and the directories for cache and output ----------
153
154
155PATH=`dirname "$0"`:"$PATH"
156first=`gitenealogy "$dir" "$sch" | sed '$s/ .*//p;d'`
157schname=`gitenealogy "$dir" "$sch" | sed '$s/^.* //p;d'`
158
159rm -rf "$out/diff_*" "$out/thumb_*" "$out/names"
160$no_cache && rm -rf "$cache"
161mkdir -p "$out/names"
162mkdir -p "$cache"
163
164ppmmake '#e0e0e0' 5 30 | pnmtopng >"$out"/unchanged.png
165
166
167# --- Generate/update the cache -----------------------------------------------
168
169
170head=
171for n in $first `cd "$dir" && git rev-list --reverse $first..HEAD`; do
172    ( cd "$dir" && git show --pretty=format:'' --name-only $n; ) |
173      egrep -q '\.sch$|\.pro$|\.lib$' || continue
174    echo Processing $n
175    new=`gitenealogy "$dir" "$sch" | sed "/^$n /s///p;d"`
176    if [ ! -z "$new" ]; then
177    echo Name change $schname to $new 1>&2
178    schname="$new"
179    fi
180    trap "rm -rf \"$cache/ppm_$n\" \"$cache/fat_$n\"" 0
181    if [ ! -d "$cache/ppm_$n" ]; then
182    rm -rf "$cache/ppm_$n"
183    mkdir "$cache/ppm_$n"
184    gitsch2ppm $sanitize -k "$dir" "$schname" $n "$cache/ppm_$n" || exit
185    gitsch2ppm -c -w 500 "$dir" "$schname" $n "$cache/fat_$n" || exit
186    fi
187    for m in "$cache/ppm_$n/"*; do
188    [ "$m" = "$cache/ppm_$n/*" ] && break
189    touch "$out/names/"`basename "$m" .ppm`
190    done
191    trap 0
192    head=$n
193done
194
195if [ -z "$head" ]; then
196    echo "no usable head found" 2>&1
197    exit 1
198fi
199
200
201# --- Title of the Web page and table header ----------------------------------
202
203
204index="$out/index.html"
205{
206    cat <<EOF
207<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
208<HTML>
209EOF
210    if [ ! -z "$SCHHIST_TITLE" ]; then
211    echo "<TITLE>$SCHHIST_TITLE</TITLE>"
212    fi
213    echo "<BODY>"
214    if [ ! -z "$SCHHIST_TITLE" ]; then
215    echo "<H1>"
216    [ -z "$SCHHIST_HOME_URL" ] || echo "<A href=\"$SCHHIST_HOME_URL\">"
217    echo "$SCHHIST_TITLE"
218    [ -z "$SCHHIST_HOME_URL" ] || echo "</A>"
219    echo "</H1>"
220    fi
221    cat <<EOF
222<TABLE bgcolor="$BG_COLOR" callpadding=1>
223<TR bgcolor="$FNAME_COLOR">
224EOF
225    for m in `ls -1 "$out/names"`; do
226    echo "<TD><B>$m</B>"
227    done
228} >"$index"
229
230
231# --- Diff all the revisions, newest to oldest --------------------------------
232
233
234next="$head"
235for n in `cd "$dir" && git rev-list $first..HEAD~1` $first; do
236    [ -d "$cache/ppm_$n" ] || continue
237    empty=true
238    s="<TR>"
239    mkdir -p "$out/diff_$next" "$out/thumb_$next"
240    for m in `ls -1 "$out/names"`; do
241    a="$cache/ppm_$n/$m.ppm"
242    fat_a="$cache/fat_$n/$m.ppm"
243    b="$cache/ppm_$next/$m.ppm"
244    fat_b="$cache/fat_$next/$m.ppm"
245    diff="$out/diff_$next/$m.png"
246    thumb="$out/thumb_$next/$m.png"
247
248    if [ -f "$a" -a -f "$b" ]; then
249        s="$s<TD align=\"center\" valign=\"middle\">"
250        if ! pngdiff cat "$diff" "$a" "$b"; then
251        s="$s<IMG src=\"unchanged.png\""
252        continue
253        fi
254        pngdiff shrink "$thumb" -f $THUMB_OPTS "$fat_a" "$fat_b" \
255          "$a" "$b" || exit
256    elif [ -f "$a" ]; then
257        s="$s<TD>"
258        pngdiff cat "$diff" -f -c 1,0,0 "$a" "$a" || exit
259        pngdiff shrink "$thumb" -f $THUMB_OPTS -c 1,0,0 "$fat_a" "$fat_a" \
260          || exit
261    elif [ -f "$b" ]; then
262        s="$s<TD>"
263        pngdiff cat "$diff" -f -c 0,1,0 "$b" "$b" || exit
264        pngdiff shrink "$thumb" -f $THUMB_OPTS -c 0,1,0 "$fat_b" "$fat_b" \
265          || exit
266    else
267        s="$s<TD>"
268        continue
269    fi
270    echo "$s" >>"$index"
271    s=
272    empty=false
273    echo "<A href=\"diff_$next/$m.png\"><IMG src=\"thumb_$next/$m.png\"></A>" >>"$index"
274    done
275    if ! $empty; then
276     echo "$s<TD valign=\"middle\">" >>"$index"
277    commit_entry "$dir" $next >>"$index"
278    fi
279    next=$n
280done
281
282
283# --- Add creation entries for all files in the first commit ------------------
284
285
286if [ -d "$cache/ppm_$next" ]; then # could this ever be false ?
287    empty=true
288    echo "<TR>" >>"$index"
289    mkdir -p "$out/diff_$next" "$out/thumb_$next"
290    for m in `ls -1 "$out/names"`; do
291    ppm="$cache/ppm_$next/$m.ppm"
292    fat="$cache/fat_$next/$m.ppm"
293    diff="$out/diff_$next/$m.png"
294    thumb="$out/thumb_$next/$m.png"
295
296    echo "<TD>" >>"$index"
297    [ -f "$ppm" ] || continue
298    pngdiff cat "$diff" -f -c 0,1,0 "$ppm" "$ppm" || exit
299    pngdiff shrink "$thumb" -f $THUMB_OPTS -c 0,1,0 "$fat" "$fat" \
300          || exit
301    empty=false
302    echo "<A href=\"diff_$next/$m.png\"><IMG src=\"thumb_$next/$m.png\"></A>" >>"$index"
303    done
304    if ! $empty; then
305     echo "<TD valign=\"middle\">" >>"$index"
306    commit_entry "$dir" $next >>"$index"
307    fi
308fi
309
310
311# --- Finish ------------------------------------------------------------------
312
313
314cat <<EOF >>"$index"
315</TABLE>
316<HR>
317`date -u '+%F %X'` UTC
318</BODY>
319</HTML>
320EOF
321

Archive Download this file



interactive