- support tilde in rpm version comparison
[platform/upstream/build.git] / vc
1 #!/bin/bash
2 # use this script to edit *.changes files
3 #
4 # based on changelog edit script from xqf
5 #
6 # Copyright (C) 2002 Ludwig Nussel
7 # Copyright (C) 2009 SUSE Linux Products GmbH, Nuernberg, Germany.
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22
23 shopt -s nullglob
24
25 if [ -z "$mailaddr" ]; then
26         domain=`dnsdomainname`
27         [ -z "$domain" ] && domain=localhost
28         mailaddr="$USER@$domain"
29 fi
30
31 EDITOR=${EDITOR:-vim}
32 date=`LC_ALL=POSIX TZ=UTC date`
33
34 if ! which mktemp > /dev/null 2>&1; then
35         echo "mktemp is required for this script to work"
36         exit 1
37 fi
38
39 while [ -n "$1" ]; do
40         case "$1" in
41                 -m)
42                         if [ $just_edit ]; then
43                                 echo "You cannot use -m and -e together!"
44                                 exit 1
45                         fi
46                         message="$2"
47                         shift 2
48                         ;;
49                 -e)
50                         if [ -n "${message}" ]; then
51                                 echo "You cannot use -m and -e together!"
52                                 exit 1
53                         fi
54                         just_edit=true
55                         shift 1
56                         ;;
57                 --help)
58                         echo "Usage: $0 [-m MESSAGE|-e] [filename[.changes]|path [file_with_comment]]"
59                         echo
60                         echo "Will use '$mailaddr' for changelog entries"
61                         echo
62                         echo "Options:"
63                         echo "    -m MESSAGE    add MESSAGE to changes (not open an editor)"
64                         echo "    -e            just open changes (cannot be used with -m)"
65                         exit 0
66                         ;;
67                 *) break ;;
68         esac
69 done
70
71 changelog="$1"
72 content="$2"
73 pkgpath=
74 if [ -n "$changelog" -a -d "$changelog" ]; then
75         pkgpath="$changelog/"
76         changelog=''
77 fi
78
79 if [ -n "$changelog" ]; then
80         if [ "${changelog%.changes}" = "$changelog" ]; then
81                 changelog="$changelog.changes"
82         fi
83 else
84         changelog=($pkgpath*.changes)
85         if [ "${#changelog[@]}" -eq 1 ]; then
86                 changelog="$changelog"
87         elif [ -n "$changelog" ]; then
88                 echo "Choose one of ${changelog[@]}"
89                 exit 1
90         fi
91 fi
92
93 if [ -z "$changelog" ]; then
94         changelog=($pkgpath*.spec)
95         if [ "${#changelog[@]}" -eq 1 ]; then
96                 changelog=${changelog%.spec}.changes
97         elif [ -n "$changelog" ]; then
98                 echo "Choose one of ${changelog[@]}"
99                 exit 1
100         fi
101 fi
102
103 if [ -z "$changelog" ]; then
104         echo "no .changes and no .spec file found"
105         exit 1
106 fi
107
108 if [ ! -e "$changelog" ]; then
109         touch $changelog
110 fi
111
112 tmpfile=`mktemp -q $changelog.vctmp.XXXXXX`
113 if [ $? -ne 0 ]; then
114         echo "$0: Can't create temp file, exiting..."
115         exit 1
116 fi
117 trap "rm -f \"$tmpfile\"" EXIT
118
119 set +e
120
121 {
122         if [ ! $just_edit ]; then
123                 echo "-------------------------------------------------------------------"
124                 echo "$date - $mailaddr"
125                 echo
126         fi
127         if [ -n "$message" ]; then
128                 echo "- $message"
129                 echo
130         elif [ -n "$content" ]; then
131                 cat "$content"
132                 echo
133         elif [ ! $just_edit ]; then
134                 echo "- "
135                 echo
136         fi
137         cat $changelog
138 } >> "$tmpfile"
139
140 if [ -z "$message" ]; then
141         set -- `md5sum "$tmpfile"`
142         chksum="$1"
143         $EDITOR +4 "$tmpfile"
144         set -- `md5sum "$tmpfile"`
145         if [ -z "$content" -a "$chksum" == "$1" ]; then
146                 echo "no changes made"
147                 exit 0
148         fi
149 fi
150 mode=`stat -c "%a" "$changelog"`
151 mv "$tmpfile" "$changelog"
152 chmod $mode "$changelog"