linux: shared memory interface - link with librt
[supercollider.git] / package / git-archive-all.sh
blob7eb95513bcf9c7317a849cbae4d4400a676daa93
1 #!/bin/bash -
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.
17 # DEBUGGING
18 set -e
19 set -C # noclobber
21 # TRAP SIGNALS
22 trap 'cleanup' QUIT EXIT
24 # For security reasons, explicitly set the internal field separator
25 # to newline, space, tab
26 OLD_IFS=$IFS
27 IFS='
30 function cleanup () {
31 rm -f $TMPFILE
32 rm -f $TOARCHIVE
33 IFS="$OLD_IFS"
36 function usage () {
37 echo "Usage is as follows:"
38 echo
39 echo "$PROGRAM <--version>"
40 echo " Prints the program version number on a line by itself and exits."
41 echo
42 echo "$PROGRAM <--usage|--help|-?>"
43 echo " Prints this usage output and exits."
44 echo
45 echo "$PROGRAM [--format <fmt>] [--prefix <path>] [--separate|-s] [output_file]"
46 echo " Creates an archive for the entire git superproject, and its submodules"
47 echo " using the passed parameters, described below."
48 echo
49 echo " If '--format' is specified, the archive is created with the named"
50 echo " git archiver backend. Obviously, this must be a backend that git-archive"
51 echo " understands. The format defaults to 'tar' if not specified."
52 echo
53 echo " If '--prefix' is specified, the archive's superproject and all submodules"
54 echo " are created with the <path> prefix named. The default is to not use one."
55 echo
56 echo " If '--separate' or '-s' is specified, individual archives will be created"
57 echo " for each of the superproject itself and its submodules. The default is to"
58 echo " concatenate individual archives into one larger archive."
59 echo
60 echo " If 'output_file' is specified, the resulting archive is created as the"
61 echo " file named. This parameter is essentially a path that must be writeable."
62 echo " When combined with '--separate' ('-s') this path must refer to a directory."
63 echo " Without this parameter or when combined with '--separate' the resulting"
64 echo " archive(s) are named with a dot-separated path of the archived directory and"
65 echo " a file extension equal to their format (e.g., 'superdir.submodule1dir.tar')."
68 function version () {
69 echo "$PROGRAM version $VERSION"
72 # Internal variables and initializations.
73 readonly PROGRAM=`basename "$0"`
74 readonly VERSION=0.2
76 OLD_PWD="`pwd`"
77 TMPDIR=${TMPDIR:-/tmp}
78 TMPFILE=`mktemp "$TMPDIR/$PROGRAM.XXXXXX"` # Create a place to store our work's progress
79 TOARCHIVE=`mktemp "$TMPDIR/$PROGRAM.toarchive.XXXXXX"`
80 OUT_FILE=$OLD_PWD # assume "this directory" without a name change by default
81 SEPARATE=0
83 FORMAT=tar
84 PREFIX=
85 TREEISH=HEAD
87 # RETURN VALUES/EXIT STATUS CODES
88 readonly E_BAD_OPTION=254
89 readonly E_UNKNOWN=255
91 # Process command-line arguments.
92 while test $# -gt 0; do
93 case $1 in
94 --format )
95 shift
96 FORMAT="$1"
97 shift
100 --prefix )
101 shift
102 PREFIX="$1"
103 shift
106 --separate | -s )
107 shift
108 SEPARATE=1
111 --version )
112 version
113 exit
116 -? | --usage | --help )
117 usage
118 exit
121 -* )
122 echo "Unrecognized option: $1" >&2
123 usage
124 exit $E_BAD_OPTION
128 break
130 esac
131 done
133 if [ ! -z "$1" ]; then
134 OUT_FILE="$1"
135 shift
138 # Validate parameters; error early, error often.
139 if [ $SEPARATE -eq 1 -a ! -d $OUT_FILE ]; then
140 echo "When creating multiple archives, your destination must be a directory."
141 echo "If it's not, you risk being surprised when your files are overwritten."
142 exit
143 elif [ `git config -l | grep -q '^core\.bare=false'; echo $?` -ne 0 ]; then
144 echo "$PROGRAM must be run from a git working copy (i.e., not a bare repository)."
145 exit
148 # Create the superproject's git-archive
149 git archive --format=$FORMAT --prefix="$PREFIX" $TREEISH > $TMPDIR/$(basename $(pwd)).$FORMAT
150 echo $TMPDIR/$(basename $(pwd)).$FORMAT >| $TMPFILE # clobber on purpose
151 superfile=`head -n 1 $TMPFILE`
153 # find all '.git' dirs, these show us the remaining to-be-archived dirs
154 find . -name '.git' -type d -print | sed -e 's/^\.\///' -e 's/\.git$//' | grep -v '^$' >> $TOARCHIVE
156 while read path; do
157 TREEISH=$(git submodule | grep "^ .*${path%/} " | cut -d ' ' -f 2) # git-submodule does not list trailing slashes in $path
158 cd "$path"
159 git archive --format=$FORMAT --prefix="${PREFIX}$path" ${TREEISH:-HEAD} > "$TMPDIR"/"$(echo "$path" | sed -e 's/\//./g')"$FORMAT
160 if [ $FORMAT == 'zip' ]; then
161 # delete the empty directory entry; zipped submodules won't unzip if we don't do this
162 zip -d "$(tail -n 1 $TMPFILE)" "${PREFIX}${path%/}" >/dev/null # remove trailing '/'
164 echo "$TMPDIR"/"$(echo "$path" | sed -e 's/\//./g')"$FORMAT >> $TMPFILE
165 cd "$OLD_PWD"
166 done < $TOARCHIVE
168 # Concatenate archives into a super-archive.
169 if [ $SEPARATE -eq 0 ]; then
170 if [ $FORMAT == 'tar' ]; then
171 sed -e '1d' $TMPFILE | while read file; do
172 tar --concatenate -f "$superfile" "$file" && rm -f "$file"
173 done
174 elif [ $FORMAT == 'zip' ]; then
175 sed -e '1d' $TMPFILE | while read file; do
176 # zip incorrectly stores the full path, so cd and then grow
177 cd `dirname "$file"`
178 zip -g "$superfile" `basename "$file"` && rm -f "$file"
179 done
180 cd "$OLD_PWD"
183 echo "$superfile" >| $TMPFILE # clobber on purpose
186 while read file; do
187 mv "$file" "$OUT_FILE"
188 done < $TMPFILE