3 # File: git-archive-all.sh
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.
10 # Examples: Use git-archive-all.sh to create archive distributions
11 # from git repositories. To use, simply do:
13 # cd $GIT_DIR; git-archive-all.sh
15 # where $GIT_DIR is the root of your git superproject.
19 ###############################################################################
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.
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.
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
35 ###############################################################################
42 trap 'cleanup' QUIT EXIT
44 # For security reasons, explicitly set the internal field separator
45 # to newline, space, tab
57 echo "Usage is as follows:"
59 echo "$PROGRAM <--version>"
60 echo " Prints the program version number on a line by itself and exits."
62 echo "$PROGRAM <--usage|--help|-?>"
63 echo " Prints this usage output and exits."
65 echo "$PROGRAM [--format <fmt>] [--prefix <path>] [--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."
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."
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."
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."
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')."
89 echo "$PROGRAM version $VERSION"
92 # Internal variables and initializations.
93 readonly PROGRAM=`basename "$0"`
97 TMPDIR=${TMPDIR:-/tmp}
98 TMPFILE=`mktemp "$TMPDIR/$PROGRAM.XXXXXX"` # Create a place to store our work's progress
99 TOARCHIVE=`mktemp "$TMPDIR/$PROGRAM.toarchive.XXXXXX"`
100 OUT_FILE=$OLD_PWD # assume "this directory" without a name change by default
107 # RETURN VALUES/EXIT STATUS CODES
108 readonly E_BAD_OPTION=254
109 readonly E_UNKNOWN=255
111 # Process command-line arguments.
112 while test $# -gt 0; do
136 -? | --usage | --help )
142 echo "Unrecognized option: $1" >&2
153 if [ ! -z "$1" ]; then
158 # Validate parameters; error early, error often.
159 if [ $SEPARATE -eq 1 -a ! -d $OUT_FILE ]; then
160 echo "When creating multiple archives, your destination must be a directory."
161 echo "If it's not, you risk being surprised when your files are overwritten."
163 elif [ `git config -l | grep -q '^core\.bare=false'; echo $?` -ne 0 ]; then
164 echo "$PROGRAM must be run from a git working copy (i.e., not a bare repository)."
168 # Create the superproject's git archive
169 git archive --format=$FORMAT --prefix="$PREFIX" $TREEISH > $TMPDIR/$(basename $(pwd)).$FORMAT
170 echo $TMPDIR/$(basename $(pwd)).$FORMAT >| $TMPFILE # clobber on purpose
171 superfile=`head -n 1 $TMPFILE`
173 # find all '.git' dirs, these show us the remaining to-be-archived dirs
174 find . -name '.git' -type d -print | sed -e 's/^\.\///' -e 's/\.git$//' | (grep -v '^$' || echo -n) >> $TOARCHIVE
177 TREEISH=$(git submodule | grep "^ .*${path%/} " | cut -d ' ' -f 2) # git submodule does not list trailing slashes in $path
179 git archive --format=$FORMAT --prefix="${PREFIX}$path" ${TREEISH:-HEAD} > "$TMPDIR"/"$(echo "$path" | sed -e 's/\//./g')"$FORMAT
180 if [ $FORMAT == 'zip' ]; then
181 # delete the empty directory entry; zipped submodules won't unzip if we don't do this
182 zip -d "$(tail -n 1 $TMPFILE)" "${PREFIX}${path%/}" >/dev/null # remove trailing '/'
184 echo "$TMPDIR"/"$(echo "$path" | sed -e 's/\//./g')"$FORMAT >> $TMPFILE
188 # Concatenate archives into a super-archive.
189 if [ $SEPARATE -eq 0 ]; then
190 if [ $FORMAT == 'tar' ]; then
191 sed -e '1d' $TMPFILE | while read file; do
192 tar --concatenate -f "$superfile" "$file" && rm -f "$file"
194 elif [ $FORMAT == 'zip' ]; then
195 sed -e '1d' $TMPFILE | while read file; do
196 # zip incorrectly stores the full path, so cd and then grow
198 zip -g "$superfile" `basename "$file"` && rm -f "$file"
203 echo "$superfile" >| $TMPFILE # clobber on purpose
207 mv "$file" "$OUT_FILE"