Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / misc / convert-archive.in
1 #! /bin/sh
2 #
3 # Copyright (C) 2009-2010, 2015 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 dirgz or dirbz2 or dirxz or cvs or git
21 # and   TO   is dir or dirgz or dirbz2 or dirxz 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   :
71 else
72   echo "Usage: convert-archive {dir|cvs|git} {dir|cvs|git} [fromfile [tofile]]"
73   exit 1
74 fi
75
76 from="$1"
77 to="$2"
78 fromfile="$3"
79 test -n "$fromfile" || fromfile=`pwd`/archive.$from.tar.gz
80 tofile="$4"
81 test -n "$tofile" || tofile=`pwd`/archive.$to.tar.gz
82
83 unpacked=`pwd`/unpacked-files
84 mkdir "$unpacked" || {
85   if test -d "$unpacked"; then
86     func_fatal_error "directory $unpacked already exists"
87   else
88     func_fatal_error "cannot create directory $unpacked"
89   fi
90 }
91
92 # Unpack the original archive.
93 case "$from" in
94   dir*)
95     { case "$from" in
96         dir) cat < "$fromfile" ;;
97         dirgz) gzip -d -c < "$fromfile" ;;
98         dirbz2) bzip2 -d -c < "$fromfile" ;;
99         dirxz) xz -d -c < "$fromfile" ;;
100       esac
101     } \
102       | (cd "$unpacked" && tar xf -) \
103       || func_fatal_error "file copy failed"
104     ;;
105
106   cvs)
107     # Set variables
108     # - work_dir        directory containing the temporary checkout
109     work_dir=tmpwrk$$
110     mkdir "$work_dir" || {
111       if test -d "$work_dir"; then
112         func_fatal_error "directory $work_dir already exists"
113       else
114         func_fatal_error "cannot create directory $work_dir"
115       fi
116     }
117
118     # Set variables
119     # - cvs_dir         directory containing the temporary repository
120     cvs_dir=tmpcvs$$
121     # Use an umask of 077, to avoid attacks that work by overwriting files in
122     # the "$CVSROOT"/CVSROOT directory.
123     (umask 077 && mkdir "$cvs_dir") || {
124       if test -d "$cvs_dir"; then
125         func_fatal_error "directory $cvs_dir already exists"
126       else
127         func_fatal_error "cannot create directory $cvs_dir"
128       fi
129     }
130     CVSROOT=`pwd`/"$cvs_dir"
131     unset CVS_CLIENT_LOG
132     unset CVS_CLIENT_PORT
133     unset CVS_IGNORE_REMOTE_ROOT
134     unset CVS_LOCAL_BRANCH_NUM
135     unset CVS_NOBASES
136     unset CVS_PASSFILE
137     unset CVS_PASSWORD
138     unset CVS_PROXY_PORT
139     unset CVS_RCMD_PORT
140     unset CVS_RSH
141     unset CVS_SERVER
142     unset CVS_SERVER_SLEEP
143     CVS_SIGN_COMMITS=
144     export CVS_SIGN_COMMITS
145     unset CVS_SSH
146     unset CVS_VERIFY_CHECKOUTS
147     unset CVS_VERIFY_TEMPLATE
148     unset CVSIGNORE
149     unset CVSREAD
150     unset CVSREADONLYFS
151     unset CVSUMASK
152     unset CVSWRAPPERS
153
154     # Need to pass -d "$CVSROOT", because there may be a CVS directory in the
155     # current directory.
156     cvs -d "$CVSROOT" init || func_fatal_error "cvs init failed"
157     gzip -d -c < "$fromfile" | (cd "$cvs_dir" && tar xf -)
158
159     # A witness that contains all versions.
160     # Can be e.g. ABOUT-NLS, intl/ChangeLog, m4/gettext.m4.
161     witness='ABOUT-NLS'
162
163     # Get list of tags or versions.
164     sed_extract_tags_from_log1='/^symbolic names:/{
165 :a
166 /^      /p
167 n
168 ba
169 }
170 /^keyword substitution:/q'
171     sed_extract_tags_from_log2='s/^     \([^:]*\):.*/\1/'
172     rlog_harmless_warning_regex="warning: Unknown phrases like \`commitid \.\.\.;' are present\."
173     tags=`
174       ( LC_ALL=C rlog "$cvs_dir"/archive/$witness,v 2>&1 1>&3 \
175         | grep -v "$rlog_harmless_warning_regex" 1>&2 ) 3>&1 \
176       | sed -n -e "$sed_extract_tags_from_log1" \
177       | sed -e "$sed_extract_tags_from_log2"`
178     test -n "$tags" || func_fatal_error "no release tags found"
179     sed_tag_to_version='s/_/./g'
180     for tag in $tags; do
181       if test $tag != release; then
182         version=`echo "$tag" | sed -e "$sed_tag_to_version"`
183         (cd "$work_dir"
184          cvs -d "$CVSROOT" checkout -r"$tag" archive > /dev/null 2> cvs.err \
185            || { cat cvs.err 1>&2; exit 1; }
186          cat cvs.err | grep -v '^cvs checkout: Updating'
187          find archive -name CVS -type d -print | xargs rm -rf
188          rm -rf CVS
189         ) || func_fatal_error "cvs checkout failed"
190         mv "$work_dir"/archive "$unpacked/$version"
191       fi
192     done
193     rm -rf "$cvs_dir"
194     rm -rf "$work_dir"
195     ;;
196
197   git)
198     # Set variables
199     # - work_dir        directory containing the temporary checkout
200     work_dir=tmpwrk$$
201     mkdir "$work_dir" || {
202       if test -d "$work_dir"; then
203         func_fatal_error "directory $work_dir already exists"
204       else
205         func_fatal_error "cannot create directory $work_dir"
206       fi
207     }
208
209     mkdir "$work_dir/master" || func_fatal_error "mkdir failed"
210     gzip -d -c < "$fromfile" | (cd "$work_dir/master" && tar xf -)
211     cd "$work_dir"
212     tags=`cd master && git tag`
213     test -n "$tags" || func_fatal_error "no release tags found"
214     for tag in $tags; do
215       if test $tag != empty; then
216         version=$tag
217         (cd master && git checkout -q $tag) \
218           || func_fatal_error "git checkout failed"
219         rm -f master/.gitignore
220         mv master/.git .git
221         mkdir "$unpacked/$version" || func_fatal_error "mkdir failed"
222         (cd master && tar cf - .) | (cd "$unpacked/$version" && tar xf -) \
223           || func_fatal_error "file copy failed"
224         mv .git master/.git
225       fi
226     done
227     cd ..
228     rm -rf "$work_dir"
229     ;;
230 esac
231
232 # Find a good 'tar' program.
233 existing_file=gettext-0.10.35/ABOUT-NLS
234 TAR=tar
235 TAR_OPTIONS=
236 for prog in tar gnutar gtar; do
237   if (cd "$unpacked" && $prog cf - --owner=root --group=root "$existing_file") >/dev/null 2>&1; then
238     TAR=$prog
239     TAR_OPTIONS='--owner=root --group=root'
240     break
241   fi
242 done
243
244 # Create the target archive.
245 case "$to" in
246   dir*)
247     (cd "$unpacked" && $TAR cf - $TAR_OPTIONS *) \
248       | { case "$to" in
249             dir) cat > "$tofile" ;;
250             dirgz) gzip -c -9 > "$tofile" ;;
251             dirbz2) bzip2 -c -9 > "$tofile" ;;
252             dirxz) xz -c -5 > "$tofile" ;;
253           esac
254         } \
255       || func_fatal_error "archive creation failed"
256     ;;
257
258   cvs)
259     # Set variables
260     # - cvs_dir         directory containing the temporary repository
261     cvs_dir=autopoint-files
262     # Use an umask of 077, to avoid attacks that work by overwriting files in
263     # the "$CVSROOT"/CVSROOT directory.
264     (umask 077 && mkdir "$cvs_dir") || {
265       if test -d "$cvs_dir"; then
266         func_fatal_error "directory $cvs_dir already exists"
267       else
268         func_fatal_error "cannot create directory $cvs_dir"
269       fi
270     }
271     CVSROOT=`pwd`/"$cvs_dir"
272     unset CVS_CLIENT_LOG
273     unset CVS_CLIENT_PORT
274     unset CVS_IGNORE_REMOTE_ROOT
275     unset CVS_PASSFILE
276     unset CVS_PASSWORD
277     unset CVS_RCMD_PORT
278     unset CVS_RSH
279     unset CVS_SERVER
280     unset CVS_SERVER_SLEEP
281     unset CVSIGNORE
282     unset CVSREAD
283     unset CVSUMASK
284     unset CVSWRAPPERS
285
286     # Set a nonstandard variable, for a good-looking cvs history.
287     cvsuser=bruno
288     gcc -shared -fPIC -O cvsuser.c -o cvsuser.so
289     cvsuser_hack=`pwd`/cvsuser.so
290
291     # Need to pass -d "$CVSROOT", because there may be a CVS directory in the
292     # current directory.
293     cvs -d "$CVSROOT" init || func_fatal_error "cvs init failed"
294
295     # Add the contents of the unpacked directory to the repository.
296     (cd "$unpacked"
297      for version in *; do
298        cvsver=`echo "$version" | sed -e 's/\./_/g'`
299        (cd $version
300         CVSUSER=$cvsuser LD_PRELOAD=$cvsuser_hack \
301         cvs -d "$CVSROOT" import -m "Import $version" archive release "$cvsver" > /dev/null
302        ) || func_fatal_error "cvs import failed"
303        # In order to avoid keyword substitution, we have to use "cvs admin"
304        # in a temporary checkout.
305        mkdir tmpcheckout || func_fatal_error "mkdir failed"
306        (cd tmpcheckout \
307         && cvs -d "$CVSROOT" checkout -r"$cvsver" archive > /dev/null \
308         && cvs -d "$CVSROOT" admin -ko `find . -type f -print | sed -e 's,^\./,,' | grep -v '^CVS/' | grep -v '/CVS/'` > /dev/null
309        ) 2> cvs.err || { cat cvs.err 1>&2; func_fatal_error "cvs checkout or admin failed"; }
310        rm -rf tmpcheckout
311      done
312     )
313     (cd "$cvs_dir" && $TAR cf - $TAR_OPTIONS archive) \
314       | gzip -c -9 > "$tofile" \
315       || func_fatal_error "archive creation failed"
316     rm -rf "$cvs_dir" cvsuser.so
317     ;;
318
319   git)
320     git_dir=`pwd`/tmpgit$$
321     mkdir "$git_dir" || func_fatal_error "mkdir failed"
322     unset GIT_CONFIG
323     (cd "$git_dir" && {
324       git init -q
325       git config user.name 'GNU Gettext Build'
326       git config user.email 'bug-gnu-gettext@gnu.org'
327       touch .gitignore
328       git add .
329       git commit --author="Bruno Haible <bruno@clisp.org>" \
330                  --message="Empty directory" 2>&1 >/dev/null
331       git tag empty
332     }) || func_fatal_error "git init failed"
333     sed_remove_leading_dot='s,^\./,,'
334     sed_remove_git_infrastructure='/^\.git/d'
335     (cd "$unpacked"
336      for version in *; do
337        (cd $version && tar cf - .) | \
338        (cd "$git_dir" && {
339          prev_files=`find . -type f | sed -e "$sed_remove_leading_dot" -e "$sed_remove_git_infrastructure"`
340          if test -n "$prev_files"; then
341            git rm -q $prev_files
342          fi
343          tar xf -
344          git add .
345          git commit --author="Bruno Haible <bruno@clisp.org>" \
346                     --message="Import $version" 2>&1 >/dev/null
347          git tag "$version"
348        }) || func_fatal_error "file copy into git repository failed"
349      done
350     )
351     (cd "$git_dir" && git reset -q --hard empty && git repack -a -d -q) \
352       || func_fatal_error "git reset or repack failed"
353     (cd "$git_dir" && $TAR cf - $TAR_OPTIONS .git) \
354       | gzip -c -9 > "$tofile" \
355       || func_fatal_error "archive creation failed"
356     rm -rf "$git_dir"
357     ;;
358 esac
359
360 rm -rf "$unpacked"