Upgrading git-archive-all.sh
[fnpdjango.git] / bin / git-archive-all.sh
1 #!/bin/bash -
2 #
3 # File:        git-archive-all.sh
4 #
5 # Description: A utility script that builds an archive file(s) of all
6 #              git repositories and submodules in the current path.
7 #              Useful for creating a single tarfile of a git super-
8 #              project that contains other submodules.
9 #
10 # Examples:    Use git-archive-all.sh to create archive distributions
11 #              from git repositories. To use, simply do:
12 #
13 #                  cd $GIT_DIR; git-archive-all.sh
14 #
15 #              where $GIT_DIR is the root of your git superproject.
16 #
17 # License:     GPL3
18 #
19 ###############################################################################
20 #
21 # This program is free software; you can redistribute it and/or modify
22 # it under the terms of the GNU General Public License as published by
23 # the Free Software Foundation; either version 2 of the License, or
24 # (at your option) any later version.
25 #
26 # This program is distributed in the hope that it will be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 # GNU General Public License for more details.
30 #
31 # You should have received a copy of the GNU General Public License
32 # along with this program; if not, write to the Free Software
33 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
34 #
35 ###############################################################################
36
37 # DEBUGGING
38 set -e
39 set -C # noclobber
40
41 # TRAP SIGNALS
42 trap 'cleanup' QUIT EXIT
43
44 # For security reasons, explicitly set the internal field separator
45 # to newline, space, tab
46 OLD_IFS=$IFS
47 IFS='
48         '
49
50 function cleanup () {
51     rm -f $TMPFILE
52     rm -f $TOARCHIVE
53     IFS="$OLD_IFS"
54 }
55
56 function usage () {
57     echo "Usage is as follows:"
58     echo
59     echo "$PROGRAM <--version>"
60     echo "    Prints the program version number on a line by itself and exits."
61     echo
62     echo "$PROGRAM <--usage|--help|-?>"
63     echo "    Prints this usage output and exits."
64     echo
65     echo "$PROGRAM [--format <fmt>] [--prefix <path>] [--verbose|-v] [--separate|-s] [output_file]"
66     echo "    Creates an archive for the entire git superproject, and its submodules"
67     echo "    using the passed parameters, described below."
68     echo
69     echo "    If '--format' is specified, the archive is created with the named"
70     echo "    git archiver backend. Obviously, this must be a backend that git archive"
71     echo "    understands. The format defaults to 'tar' if not specified."
72     echo
73     echo "    If '--prefix' is specified, the archive's superproject and all submodules"
74     echo "    are created with the <path> prefix named. The default is to not use one."
75     echo
76     echo "    If '--separate' or '-s' is specified, individual archives will be created"
77     echo "    for each of the superproject itself and its submodules. The default is to"
78     echo "    concatenate individual archives into one larger archive."
79     echo
80     echo "    If 'output_file' is specified, the resulting archive is created as the"
81     echo "    file named. This parameter is essentially a path that must be writeable."
82     echo "    When combined with '--separate' ('-s') this path must refer to a directory."
83     echo "    Without this parameter or when combined with '--separate' the resulting"
84     echo "    archive(s) are named with a dot-separated path of the archived directory and"
85     echo "    a file extension equal to their format (e.g., 'superdir.submodule1dir.tar')."
86     echo
87     echo "    If '--verbose' or '-v' is specified, progress will be printed."
88 }
89
90 function version () {
91     echo "$PROGRAM version $VERSION"
92 }
93
94 # Internal variables and initializations.
95 readonly PROGRAM=`basename "$0"`
96 readonly VERSION=0.2
97
98 OLD_PWD="`pwd`"
99 TMPDIR=${TMPDIR:-/tmp}
100 TMPFILE=`mktemp "$TMPDIR/$PROGRAM.XXXXXX"` # Create a place to store our work's progress
101 TOARCHIVE=`mktemp "$TMPDIR/$PROGRAM.toarchive.XXXXXX"`
102 OUT_FILE=$OLD_PWD # assume "this directory" without a name change by default
103 SEPARATE=0
104 VERBOSE=0
105
106 FORMAT=tar
107 PREFIX=
108 TREEISH=HEAD
109
110 # RETURN VALUES/EXIT STATUS CODES
111 readonly E_BAD_OPTION=254
112 readonly E_UNKNOWN=255
113
114 # Process command-line arguments.
115 while test $# -gt 0; do
116     case $1 in
117         --format )
118             shift
119             FORMAT="$1"
120             shift
121             ;;
122
123         --prefix )
124             shift
125             PREFIX="$1"
126             shift
127             ;;
128
129         --separate | -s )
130             shift
131             SEPARATE=1
132             ;;
133
134         --version )
135             version
136             exit
137             ;;
138
139         --verbose | -v )
140             shift
141             VERBOSE=1
142             ;;
143
144         -? | --usage | --help )
145             usage
146             exit
147             ;;
148
149         -* )
150             echo "Unrecognized option: $1" >&2
151             usage
152             exit $E_BAD_OPTION
153             ;;
154
155         * )
156             break
157             ;;
158     esac
159 done
160
161 if [ ! -z "$1" ]; then
162     OUT_FILE="$1"
163     shift
164 fi
165
166 # Validate parameters; error early, error often.
167 if [ $SEPARATE -eq 1 -a ! -d $OUT_FILE ]; then
168     echo "When creating multiple archives, your destination must be a directory."
169     echo "If it's not, you risk being surprised when your files are overwritten."
170     exit
171 elif [ `git config -l | grep -q '^core\.bare=false'; echo $?` -ne 0 ]; then
172     echo "$PROGRAM must be run from a git working copy (i.e., not a bare repository)."
173     exit
174 fi
175
176 # Create the superproject's git-archive
177 if [ $VERBOSE -eq 1 ]; then
178     echo -n "creating superproject archive..."
179 fi
180 git archive --format=$FORMAT --prefix="$PREFIX" $TREEISH > $TMPDIR/$(basename $(pwd)).$FORMAT
181 if [ $VERBOSE -eq 1 ]; then
182     echo "done"
183 fi
184 echo $TMPDIR/$(basename $(pwd)).$FORMAT >| $TMPFILE # clobber on purpose
185 superfile=`head -n 1 $TMPFILE`
186
187 if [ $VERBOSE -eq 1 ]; then
188     echo -n "looking for subprojects..."
189 fi
190 # find all '.git' dirs, these show us the remaining to-be-archived dirs
191 # we only want directories that are below the current directory
192 find . -mindepth 2 -name '.git' -type d -print | sed -e 's/^\.\///' -e 's/\.git$//' >> $TOARCHIVE
193 # as of version 1.7.8, git places the submodule .git directories under the superprojects .git dir
194 # the submodules get a .git file that points to their .git dir. we need to find all of these too
195 find . -mindepth 2 -name '.git' -type f -print | xargs grep -l "gitdir" | sed -e 's/^\.\///' -e 's/\.git$//' >> $TOARCHIVE
196 if [ $VERBOSE -eq 1 ]; then
197     echo "done"
198     echo "  found:"
199     cat $TOARCHIVE | while read arch
200     do
201       echo "    $arch"
202     done
203 fi
204
205 if [ $VERBOSE -eq 1 ]; then
206     echo -n "archiving submodules..."
207 fi
208 while read path; do
209     TREEISH=$(git submodule | grep "^ .*${path%/} " | cut -d ' ' -f 2) # git submodule does not list trailing slashes in $path
210     cd "$path"
211     git archive --format=$FORMAT --prefix="${PREFIX}$path" ${TREEISH:-HEAD} > "$TMPDIR"/"$(echo "$path" | sed -e 's/\//./g')"$FORMAT
212     if [ $FORMAT == 'zip' ]; then
213         # delete the empty directory entry; zipped submodules won't unzip if we don't do this
214         zip -d "$(tail -n 1 $TMPFILE)" "${PREFIX}${path%/}" >/dev/null # remove trailing '/'
215     fi
216     echo "$TMPDIR"/"$(echo "$path" | sed -e 's/\//./g')"$FORMAT >> $TMPFILE
217     cd "$OLD_PWD"
218 done < $TOARCHIVE
219 if [ $VERBOSE -eq 1 ]; then
220     echo "done"
221 fi
222
223 if [ $VERBOSE -eq 1 ]; then
224     echo -n "concatenating archives into single archive..."
225 fi
226 # Concatenate archives into a super-archive.
227 if [ $SEPARATE -eq 0 ]; then
228     if [ $FORMAT == 'tar' ]; then
229         sed -e '1d' $TMPFILE | while read file; do
230             tar --concatenate -f "$superfile" "$file" && rm -f "$file"
231         done
232     elif [ $FORMAT == 'zip' ]; then
233         sed -e '1d' $TMPFILE | while read file; do
234             # zip incorrectly stores the full path, so cd and then grow
235             cd `dirname "$file"`
236             zip -g "$superfile" `basename "$file"` && rm -f "$file"
237         done
238         cd "$OLD_PWD"
239     fi
240
241     echo "$superfile" >| $TMPFILE # clobber on purpose
242 fi
243 if [ $VERBOSE -eq 1 ]; then
244     echo "done"
245 fi
246
247 if [ $VERBOSE -eq 1 ]; then
248     echo -n "moving archive to $OUT_FILE..."
249 fi
250 while read file; do
251     mv "$file" "$OUT_FILE"
252 done < $TMPFILE
253 if [ $VERBOSE -eq 1 ]; then
254     echo "done"
255 fi