Imported Upstream version 0.18.1.1
[platform/upstream/gettext.git] / gettext-tools / misc / convert-archive.in
1 #! /bin/sh
2 #
3 # Copyright (C) 2009-2010 Free Software Foundation, Inc.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 # Usage: convert-archive FROM TO [FROMFILE [TOFILE]]
20 # where FROM is dir or cvs or git
21 # and   TO   is dir or cvs or git
22 # This will read FROMFILE (default: archive.$FROM.tar.gz)
23 # and produce TOFILE (default: archive.$TO.tar.gz).
24
25 progname=$0
26 package=@PACKAGE@
27 version=@VERSION@
28
29 # func_usage
30 # outputs to stdout the --help usage message.
31 func_usage ()
32 {
33   echo "\
34 Usage: convert-archive FROM TO [FROMFILE [TOFILE]]
35
36 Converts the archive of gettext infrastructure from the FROM format
37 to the TO format.
38 FROMFILE is the original file, defaulting to archive.\$FROM.tar.gz.
39 TOFILE is the destination file, defaulting to archive.\$TO.tar.gz.
40
41 Report bugs to <bug-gnu-gettext@gnu.org>."
42 }
43
44 # func_version
45 # outputs to stdout the --version message.
46 func_version ()
47 {
48   echo "$progname (GNU $package) $version"
49   echo "Copyright (C) 2009-2010 Free Software Foundation, Inc.
50 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
51 This is free software: you are free to change and redistribute it.
52 There is NO WARRANTY, to the extent permitted by law."
53   echo "Written by" "Bruno Haible"
54 }
55
56 # func_fatal_error message
57 # outputs to stderr a fatal error message, and terminates the program.
58 func_fatal_error ()
59 {
60   echo "convert-archive: *** $1" 1>&2
61   echo "convert-archive: *** Stop." 1>&2
62   exit 1
63 }
64
65 # Command-line option processing.
66
67 if ! { { test $# = 2 || test $# = 3 || test $# = 4; } \
68        && case "$1" in dir | cvs | git ) true;; *) false;; esac \
69        && case "$2" in dir | cvs | git ) true;; *) false;; esac; }; then
70   echo "Usage: convert-archive {dir|cvs|git} {dir|cvs|git} [fromfile [tofile]]"
71   exit 1
72 fi
73
74 from="$1"
75 to="$2"
76 fromfile="$3"
77 test -n "$fromfile" || fromfile=`pwd`/archive.$from.tar.gz
78 tofile="$4"
79 test -n "$tofile" || tofile=`pwd`/archive.$to.tar.gz
80
81 unpacked=`pwd`/unpacked-files
82 mkdir "$unpacked" || {
83   if test -d "$unpacked"; then
84     func_fatal_error "directory $unpacked already exists"
85   else
86     func_fatal_error "cannot create directory $unpacked"
87   fi
88 }
89
90 # Unpack the original archive.
91 case "$from" in
92   dir)
93     gzip -d -c < "$fromfile" | (cd "$unpacked" && tar xf -) \
94       || func_fatal_error "file copy failed"
95     ;;
96
97   cvs)
98     # Set variables
99     # - work_dir        directory containing the temporary checkout
100     work_dir=tmpwrk$$
101     mkdir "$work_dir" || {
102       if test -d "$work_dir"; then
103         func_fatal_error "directory $work_dir already exists"
104       else
105         func_fatal_error "cannot create directory $work_dir"
106       fi
107     }
108
109     # Set variables
110     # - cvs_dir         directory containing the temporary repository
111     cvs_dir=tmpcvs$$
112     # Use an umask of 077, to avoid attacks that work by overwriting files in
113     # the "$CVSROOT"/CVSROOT directory.
114     (umask 077 && mkdir "$cvs_dir") || {
115       if test -d "$cvs_dir"; then
116         func_fatal_error "directory $cvs_dir already exists"
117       else
118         func_fatal_error "cannot create directory $cvs_dir"
119       fi
120     }
121     CVSROOT=`pwd`/"$cvs_dir"
122     unset CVS_CLIENT_LOG
123     unset CVS_CLIENT_PORT
124     unset CVS_IGNORE_REMOTE_ROOT
125     unset CVS_LOCAL_BRANCH_NUM
126     unset CVS_NOBASES
127     unset CVS_PASSFILE
128     unset CVS_PASSWORD
129     unset CVS_PROXY_PORT
130     unset CVS_RCMD_PORT
131     unset CVS_RSH
132     unset CVS_SERVER
133     unset CVS_SERVER_SLEEP
134     CVS_SIGN_COMMITS=
135     export CVS_SIGN_COMMITS
136     unset CVS_SSH
137     unset CVS_VERIFY_CHECKOUTS
138     unset CVS_VERIFY_TEMPLATE
139     unset CVSIGNORE
140     unset CVSREAD
141     unset CVSREADONLYFS
142     unset CVSUMASK
143     unset CVSWRAPPERS
144
145     # Need to pass -d "$CVSROOT", because there may be a CVS directory in the
146     # current directory.
147     cvs -d "$CVSROOT" init || func_fatal_error "cvs init failed"
148     gzip -d -c < "$fromfile" | (cd "$cvs_dir" && tar xf -)
149
150     # A witness that contains all versions.
151     # Can be e.g. ABOUT-NLS, intl/ChangeLog, m4/gettext.m4.
152     witness='ABOUT-NLS'
153
154     # Get list of tags or versions.
155     sed_extract_tags_from_log1='/^symbolic names:/{
156 :a
157 /^      /p
158 n
159 ba
160 }
161 /^keyword substitution:/q'
162     sed_extract_tags_from_log2='s/^     \([^:]*\):.*/\1/'
163     rlog_harmless_warning_regex="warning: Unknown phrases like \`commitid \.\.\.;' are present\."
164     tags=`
165       ( LC_ALL=C rlog "$cvs_dir"/archive/$witness,v 2>&1 1>&3 \
166         | grep -v "$rlog_harmless_warning_regex" 1>&2 ) 3>&1 \
167       | sed -n -e "$sed_extract_tags_from_log1" \
168       | sed -e "$sed_extract_tags_from_log2"`
169     test -n "$tags" || func_fatal_error "no release tags found"
170     sed_tag_to_version='s/_/./g'
171     for tag in $tags; do
172       if test $tag != release; then
173         version=`echo "$tag" | sed -e "$sed_tag_to_version"`
174         (cd "$work_dir"
175          cvs -d "$CVSROOT" checkout -r"$tag" archive > /dev/null 2> cvs.err \
176            || { cat cvs.err 1>&2; exit 1; }
177          cat cvs.err | grep -v '^cvs checkout: Updating'
178          find archive -name CVS -type d -print | xargs rm -rf
179          rm -rf CVS
180         ) || func_fatal_error "cvs checkout failed"
181         mv "$work_dir"/archive "$unpacked/$version"
182       fi
183     done
184     rm -rf "$cvs_dir"
185     rm -rf "$work_dir"
186     ;;
187
188   git)
189     # Set variables
190     # - work_dir        directory containing the temporary checkout
191     work_dir=tmpwrk$$
192     mkdir "$work_dir" || {
193       if test -d "$work_dir"; then
194         func_fatal_error "directory $work_dir already exists"
195       else
196         func_fatal_error "cannot create directory $work_dir"
197       fi
198     }
199
200     mkdir "$work_dir/master" || func_fatal_error "mkdir failed"
201     gzip -d -c < "$fromfile" | (cd "$work_dir/master" && tar xf -)
202     cd "$work_dir"
203     tags=`cd master && git tag`
204     test -n "$tags" || func_fatal_error "no release tags found"
205     for tag in $tags; do
206       if test $tag != empty; then
207         version=$tag
208         (cd master && git checkout -q $tag) \
209           || func_fatal_error "git checkout failed"
210         rm -f master/.gitignore
211         mv master/.git .git
212         mkdir "$unpacked/$version" || func_fatal_error "mkdir failed"
213         (cd master && tar cf - .) | (cd "$unpacked/$version" && tar xf -) \
214           || func_fatal_error "file copy failed"
215         mv .git master/.git
216       fi
217     done
218     cd ..
219     rm -rf "$work_dir"
220     ;;
221 esac
222
223 # Find a good 'tar' program.
224 existing_file=gettext-0.10.35/ABOUT-NLS
225 TAR=tar
226 TAR_OPTIONS=
227 for prog in tar gnutar gtar; do
228   if (cd "$unpacked" && $prog cf - --owner=root --group=root "$existing_file") >/dev/null 2>&1; then
229     TAR=$prog
230     TAR_OPTIONS='--owner=root --group=root'
231     break
232   fi
233 done
234
235 # Create the target archive.
236 case "$to" in
237   dir)
238     (cd "$unpacked" && $TAR cf - $TAR_OPTIONS *) \
239       | gzip -c -9 > "$tofile" \
240       || func_fatal_error "archive creation failed"
241     ;;
242
243   cvs)
244     # Set variables
245     # - cvs_dir         directory containing the temporary repository
246     cvs_dir=autopoint-files
247     # Use an umask of 077, to avoid attacks that work by overwriting files in
248     # the "$CVSROOT"/CVSROOT directory.
249     (umask 077 && mkdir "$cvs_dir") || {
250       if test -d "$cvs_dir"; then
251         func_fatal_error "directory $cvs_dir already exists"
252       else
253         func_fatal_error "cannot create directory $cvs_dir"
254       fi
255     }
256     CVSROOT=`pwd`/"$cvs_dir"
257     unset CVS_CLIENT_LOG
258     unset CVS_CLIENT_PORT
259     unset CVS_IGNORE_REMOTE_ROOT
260     unset CVS_PASSFILE
261     unset CVS_PASSWORD
262     unset CVS_RCMD_PORT
263     unset CVS_RSH
264     unset CVS_SERVER
265     unset CVS_SERVER_SLEEP
266     unset CVSIGNORE
267     unset CVSREAD
268     unset CVSUMASK
269     unset CVSWRAPPERS
270
271     # Set a nonstandard variable, for a good-looking cvs history.
272     cvsuser=bruno
273     gcc -shared -fPIC -O cvsuser.c -o cvsuser.so
274     cvsuser_hack=`pwd`/cvsuser.so
275
276     # Need to pass -d "$CVSROOT", because there may be a CVS directory in the
277     # current directory.
278     cvs -d "$CVSROOT" init || func_fatal_error "cvs init failed"
279
280     # Add the contents of the unpacked directory to the repository.
281     (cd "$unpacked"
282      for version in *; do
283        cvsver=`echo "$version" | sed -e 's/\./_/g'`
284        (cd $version
285         CVSUSER=$cvsuser LD_PRELOAD=$cvsuser_hack \
286         cvs -d "$CVSROOT" import -m "Import $version" archive release "$cvsver" > /dev/null
287        ) || func_fatal_error "cvs import failed"
288        # In order to avoid keyword substitution, we have to use "cvs admin"
289        # in a temporary checkout.
290        mkdir tmpcheckout || func_fatal_error "mkdir failed"
291        (cd tmpcheckout \
292         && cvs -d "$CVSROOT" checkout -r"$cvsver" archive > /dev/null \
293         && cvs -d "$CVSROOT" admin -ko `find . -type f -print | sed -e 's,^\./,,' | grep -v '^CVS/' | grep -v '/CVS/'` > /dev/null
294        ) 2> cvs.err || { cat cvs.err 1>&2; func_fatal_error "cvs checkout or admin failed"; }
295        rm -rf tmpcheckout
296      done
297     )
298     (cd "$cvs_dir" && $TAR cf - $TAR_OPTIONS archive) \
299       | gzip -c -9 > "$tofile" \
300       || func_fatal_error "archive creation failed"
301     rm -rf "$cvs_dir" cvsuser.so
302     ;;
303
304   git)
305     git_dir=`pwd`/tmpgit$$
306     mkdir "$git_dir" || func_fatal_error "mkdir failed"
307     unset GIT_CONFIG
308     (cd "$git_dir" && {
309       git init -q
310       git config user.name 'GNU Gettext Build'
311       git config user.email 'bug-gnu-gettext@gnu.org'
312       touch .gitignore
313       git add .
314       git commit --author="Bruno Haible <bruno@clisp.org>" \
315                  --message="Empty directory" 2>&1 \
316         | grep -v '^ '
317       git tag empty
318     }) || func_fatal_error "git init failed"
319     sed_remove_leading_dot='s,^\./,,'
320     sed_remove_git_infrastructure='/^\.git/d'
321     (cd "$unpacked"
322      for version in *; do
323        (cd $version && tar cf - .) | \
324        (cd "$git_dir" && {
325          prev_files=`find . -type f | sed -e "$sed_remove_leading_dot" -e "$sed_remove_git_infrastructure"`
326          if test -n "$prev_files"; then
327            git rm -q $prev_files
328          fi
329          tar xf -
330          git add .
331          git commit --author="Bruno Haible <bruno@clisp.org>" \
332                     --message="Import $version" 2>&1 \
333            | grep -v '^ '
334          git tag "$version"
335        }) || func_fatal_error "file copy into git repository failed"
336      done
337     )
338     (cd "$git_dir" && git reset -q --hard empty && git repack -a -d -q) \
339       || func_fatal_error "git reset or repack failed"
340     (cd "$git_dir" && $TAR cf - $TAR_OPTIONS .git) \
341       | gzip -c -9 > "$tofile" \
342       || func_fatal_error "archive creation failed"
343     rm -rf "$git_dir"
344     ;;
345 esac
346
347 rm -rf "$unpacked"