xref: /netbsd-src/external/bsd/am-utils/dist/commit (revision a53f50b9b44dc9467ccc9c464999b1d1c509cb0c)
1*a53f50b9Schristos#! /bin/sh
2*a53f50b9Schristos
3*a53f50b9Schristos# commit version 0.9.2
4*a53f50b9Schristos
5*a53f50b9Schristos# Copyright (C) 1999, Free Software Foundation
6*a53f50b9Schristos
7*a53f50b9Schristos# This script is Free Software, and it can be copied, distributed and
8*a53f50b9Schristos# modified as defined in the GNU General Public License.  A copy of
9*a53f50b9Schristos# its license can be downloaded from http://www.gnu.org/copyleft/gpl.html
10*a53f50b9Schristos
11*a53f50b9Schristos# Originally by Gary V. Vaughan <gvaughan@oranda.demon.co.uk>
12*a53f50b9Schristos# Heavily modified by Alexandre Oliva <oliva@dcc.unicamp.br>
13*a53f50b9Schristos
14*a53f50b9Schristos# This scripts eases checking in changes to CVS-maintained projects
15*a53f50b9Schristos# with ChangeLog files.  It will check that there have been no
16*a53f50b9Schristos# conflicting commits in the CVS repository and print which files it
17*a53f50b9Schristos# is going to commit to stderr.  A list of files to compare and to
18*a53f50b9Schristos# check in can be given in the command line.  If it is not given, all
19*a53f50b9Schristos# files in the current directory (and below, unless `-l' is given) are
20*a53f50b9Schristos# considered for check in.
21*a53f50b9Schristos
22*a53f50b9Schristos# The commit message will be extracted from the differences between
23*a53f50b9Schristos# the local ChangeLog and the one in the repository (unless a message
24*a53f50b9Schristos# was specified with `-m' or `-F').  An empty message is not accepted
25*a53f50b9Schristos# (but a blank line is).  If the message is acceptable, it will be
26*a53f50b9Schristos# presented for verification (and possible edition) using the $PAGER
27*a53f50b9Schristos# environment variable (or `more', if it is not set, or `cat', if the
28*a53f50b9Schristos# `-f' switch is given).  If $PAGER exits successfully, the modified
29*a53f50b9Schristos# files (at that moment) are checked in, unless `-n' was specified, in
30*a53f50b9Schristos# which case nothing is checked in.
31*a53f50b9Schristos
32*a53f50b9Schristos# usage: commit [-v] [-h] [-f] [-l] [-n] [-q] [-z N]
33*a53f50b9Schristos#               [-m msg|-F msg_file] [--] [file|dir ...]
34*a53f50b9Schristos
35*a53f50b9Schristos# -f      --fast        don't check (unless *followed* by -n), and just
36*a53f50b9Schristos#         --force       display commit message instead of running $PAGER
37*a53f50b9Schristos# -l      --local       don't descend into subdirectories
38*a53f50b9Schristos# -m msg  --message=msg set commit message
39*a53f50b9Schristos#         --msg=msg     same as -m
40*a53f50b9Schristos# -F file --file=file   read commit message from file
41*a53f50b9Schristos# -n      --dry-run     don't commit anything
42*a53f50b9Schristos# -q      --quiet       run cvs in quiet mode
43*a53f50b9Schristos# -zN     --compress=N  set compression level (0-9, 0=none, 9=max)
44*a53f50b9Schristos# -v      --version     print version information
45*a53f50b9Schristos# -h,-?   --help        print short or long help message
46*a53f50b9Schristos
47*a53f50b9Schristosname=commit
48*a53f50b9Schristoscvsopt=
49*a53f50b9Schristosupdateopt=
50*a53f50b9Schristoscommitopt=
51*a53f50b9Schristosdry_run=false
52*a53f50b9Schristoscommit=:
53*a53f50b9Schristosupdate=:
54*a53f50b9Schristoslog_file="${TMPDIR-/tmp}/commitlog.$$"
55*a53f50b9Schristos
56*a53f50b9Schristosrm -f "$log_file"
57*a53f50b9Schristostrap 'rm -f "$log_file"; exit 1' 1 2 15
58*a53f50b9Schristos
59*a53f50b9Schristos# this just eases exit handling
60*a53f50b9Schristosmain_repeat=":"
61*a53f50b9Schristoswhile $main_repeat; do
62*a53f50b9Schristos
63*a53f50b9Schristosrepeat="test $# -gt 0"
64*a53f50b9Schristoswhile $repeat; do
65*a53f50b9Schristos    case "$1" in
66*a53f50b9Schristos    -f|--force|--fast)
67*a53f50b9Schristos	update=false
68*a53f50b9Schristos	PAGER=cat
69*a53f50b9Schristos	shift
70*a53f50b9Schristos	;;
71*a53f50b9Schristos    -l|--local)
72*a53f50b9Schristos	updateopt="$updateopt -l"
73*a53f50b9Schristos	commitopt="$commitopt -l"
74*a53f50b9Schristos	shift
75*a53f50b9Schristos	;;
76*a53f50b9Schristos    -m|--message|--msg)
77*a53f50b9Schristos	if test $# = 1; then
78*a53f50b9Schristos	    echo "$name: missing argument for $1" >&2
79*a53f50b9Schristos	    break
80*a53f50b9Schristos	fi
81*a53f50b9Schristos	if test -f "$log_file"; then
82*a53f50b9Schristos	    echo "$name: you can have at most one of -m and -F" >&2
83*a53f50b9Schristos	    break
84*a53f50b9Schristos	fi
85*a53f50b9Schristos	shift
86*a53f50b9Schristos	echo "$1" > "$log_file"
87*a53f50b9Schristos	shift
88*a53f50b9Schristos	;;
89*a53f50b9Schristos    -F|--file)
90*a53f50b9Schristos	if test -f "$log_file"; then
91*a53f50b9Schristos	    echo "$name: you can have at most one of -m and -F" >&2
92*a53f50b9Schristos	    break
93*a53f50b9Schristos	fi
94*a53f50b9Schristos	if test $# = 1; then
95*a53f50b9Schristos	    echo "$name: missing argument for $1" >&2
96*a53f50b9Schristos	    break
97*a53f50b9Schristos	fi
98*a53f50b9Schristos	shift
99*a53f50b9Schristos	if cat < "$1" > "$log_file"; then :; else
100*a53f50b9Schristos	    break
101*a53f50b9Schristos	fi
102*a53f50b9Schristos	shift
103*a53f50b9Schristos	;;
104*a53f50b9Schristos    -n|--dry-run)
105*a53f50b9Schristos	commit=false
106*a53f50b9Schristos	update=true
107*a53f50b9Schristos	shift
108*a53f50b9Schristos	;;
109*a53f50b9Schristos    -q|--quiet)
110*a53f50b9Schristos	cvsopt="$cvsopt -q"
111*a53f50b9Schristos	shift
112*a53f50b9Schristos	;;
113*a53f50b9Schristos    -z|--compress)
114*a53f50b9Schristos	if test $# = 1; then
115*a53f50b9Schristos	    echo "$name: missing argument for $1" >&2
116*a53f50b9Schristos	    break
117*a53f50b9Schristos	fi
118*a53f50b9Schristos	case "$2" in
119*a53f50b9Schristos	[0-9]) :;;
120*a53f50b9Schristos	*)  echo "$name: invalid argument for $1" >&2
121*a53f50b9Schristos	    break
122*a53f50b9Schristos	    ;;
123*a53f50b9Schristos	esac
124*a53f50b9Schristos	cvsopt="$cvsopt -z$2"
125*a53f50b9Schristos	shift
126*a53f50b9Schristos	shift
127*a53f50b9Schristos	;;
128*a53f50b9Schristos
129*a53f50b9Schristos    -m*|-F*|-z*)
130*a53f50b9Schristos	opt=`echo "$1" | sed '1s/^\(..\).*$/\1/;q'`
131*a53f50b9Schristos	arg=`echo "$1" | sed '1s/^-[a-zA-Z0-9]//'`
132*a53f50b9Schristos	shift
133*a53f50b9Schristos	set -- "$opt" "$arg" ${1+"$@"}
134*a53f50b9Schristos	;;
135*a53f50b9Schristos    --message=*|--msg=*|--file=*|--compress=*)
136*a53f50b9Schristos	opt=`echo "$1" | sed '1s/^\(--[^=]*\)=.*/\1/;q'`
137*a53f50b9Schristos    	arg=`echo "$1" | sed '1s/^--[^=]*=//'`
138*a53f50b9Schristos	shift
139*a53f50b9Schristos	set -- "$opt" "$arg" ${1+"$@"}
140*a53f50b9Schristos	;;
141*a53f50b9Schristos
142*a53f50b9Schristos    -v|--version)
143*a53f50b9Schristos	sed '/^# '$name' version /,/^# Heavily modified by/ { s/^# //; p; }; d' < $0
144*a53f50b9Schristos	exit 0
145*a53f50b9Schristos	;;
146*a53f50b9Schristos    -\?|-h)
147*a53f50b9Schristos	sed '/^# usage:/,/# -h/ { s/^# //; p; }; d' < $0 &&
148*a53f50b9Schristos	echo
149*a53f50b9Schristos	echo "run \`$name --help | more' for full usage"
150*a53f50b9Schristos	exit 0
151*a53f50b9Schristos	;;
152*a53f50b9Schristos    --help)
153*a53f50b9Schristos	sed '/^# '$name' version /,/^[^#]/ { /^[^#]/ d; s/^# //; p; }; d' < $0
154*a53f50b9Schristos	exit 0
155*a53f50b9Schristos	;;
156*a53f50b9Schristos    --)
157*a53f50b9Schristos	shift
158*a53f50b9Schristos	repeat=false
159*a53f50b9Schristos	;;
160*a53f50b9Schristos    -*)
161*a53f50b9Schristos	echo "$name: invalid flag $1" >&2
162*a53f50b9Schristos	break
163*a53f50b9Schristos	;;
164*a53f50b9Schristos    *)
165*a53f50b9Schristos	repeat=false
166*a53f50b9Schristos	;;
167*a53f50b9Schristos    esac
168*a53f50b9Schristosdone
169*a53f50b9Schristos# might have used break 2 within the previous loop, but so what
170*a53f50b9Schristos$repeat && break
171*a53f50b9Schristos
172*a53f50b9Schristos$update && \
173*a53f50b9Schristosif echo "$name: checking for conflicts..." >&2
174*a53f50b9Schristos   (cvs $cvsopt -q -n update $updateopt ${1+"$@"} 2>/dev/null \
175*a53f50b9Schristos    | while read line; do
176*a53f50b9Schristos	echo "$line"
177*a53f50b9Schristos	echo "$line" >&3
178*a53f50b9Schristos      done | grep '^C') 3>&1 >/dev/null; then
179*a53f50b9Schristos  echo "$name: some conflicts were found, aborting..." >&2
180*a53f50b9Schristos  break
181*a53f50b9Schristosfi
182*a53f50b9Schristos
183*a53f50b9Schristosif test ! -f "$log_file"; then
184*a53f50b9Schristos  echo "$name: checking commit message..." >&2
185*a53f50b9Schristos  cvs $cvsopt diff -u ChangeLog \
186*a53f50b9Schristos  | while read line; do
187*a53f50b9Schristos      case "$line" in
188*a53f50b9Schristos      "--- ChangeLog"*) :;;
189*a53f50b9Schristos      "-"*)
190*a53f50b9Schristos	echo "$name: *** Warning: the following line in ChangeLog diff is suspicious:" >&2
191*a53f50b9Schristos	echo "$line" | sed 's/^.//' >&2;;
192*a53f50b9Schristos      "+ "*)
193*a53f50b9Schristos	echo "$name: *** Warning: lines should start with tabs, not spaces; ignoring line:" >&2
194*a53f50b9Schristos	echo "$line" | sed 's/^.//' >&2;;
195*a53f50b9Schristos      "+") echo;;
196*a53f50b9Schristos      "+	"*) echo "$line";;
197*a53f50b9Schristos      esac
198*a53f50b9Schristos    done \
199*a53f50b9Schristos  | sed -e 's,\+	,,' -e '/./p' -e '/./d' -e '1d' -e '$d' > "$log_file" \
200*a53f50b9Schristos  || break
201*a53f50b9Schristos# The sed script above removes "+TAB" from the beginning of a line, then
202*a53f50b9Schristos# deletes the first and/or the last line, when they happen to be empty
203*a53f50b9Schristosfi
204*a53f50b9Schristos
205*a53f50b9Schristosif grep '[^ 	]' < "$log_file" > /dev/null; then :; else
206*a53f50b9Schristos  echo "$name: empty commit message, aborting" >&2
207*a53f50b9Schristos  break
208*a53f50b9Schristosfi
209*a53f50b9Schristos
210*a53f50b9Schristosif grep '^$' < "$log_file" > /dev/null; then
211*a53f50b9Schristos  echo "$name: *** Warning: blank lines should not appear within a commit messages." >&2
212*a53f50b9Schristos  echo "$name: *** They should be used to separate distinct commits." >&2
213*a53f50b9Schristosfi
214*a53f50b9Schristos
215*a53f50b9Schristos${PAGER-more} "$log_file" || break
216*a53f50b9Schristos
217*a53f50b9Schristossleep 1 # give the user some time for a ^C
218*a53f50b9Schristos
219*a53f50b9Schristos# Do not check for empty $log_file again, even though the user might have
220*a53f50b9Schristos# zeroed it out.  If s/he did, it was probably intentional.
221*a53f50b9Schristos
222*a53f50b9Schristosif $commit; then
223*a53f50b9Schristos  cvs $cvsopt commit $commitopt -F $log_file ${1+"$@"} || break
224*a53f50b9Schristosfi
225*a53f50b9Schristos
226*a53f50b9Schristosmain_repeat=false
227*a53f50b9Schristosdone
228*a53f50b9Schristos
229*a53f50b9Schristosrm -f "$log_file"
230*a53f50b9Schristos
231*a53f50b9Schristos# if main_repeat was not set to `false', we failed
232*a53f50b9Schristos$main_repeat && exit 1
233*a53f50b9Schristosexit 0
234