1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
#!/bin/bash
#!/bin/bash
### How does this script operate?
#
# Script's expectations:
# - A directory that contains IMPORT.defs, where the import definitions are stored
# - Access to the referenced git repository URL (via internet, or otherwise)
#
# Script's arguments:
# - Path to the directory containing the external library to import/update
#
# Steps that this script performs:
# - Reads the external library import definitions (from the IMPORT.defs file)
# - Clones the library's git repo to a temporary location
# - Executes the import routine
# - Applies patches, if any are found in the patches/ directory inside target directory
# - In the IMPORT.defs: updates the git reference that was used for the import
### Shell configuration and script bootstrap
#
set -e
set -u
. `dirname $0`/_bootstrap.sh
### Define the help method
#
_showHelp()
{
cat <<EOF
Purpose:
Import/update an external library (in lib/) directory
Supported CLI arguments:
-r Git reference to use for import. It can be anything that uniquely
identifies a commit in the external library's git repository.
By default, the last commit on the repository's default branch is
used.
(Incompatible with -c flag.)
-c Use the current git reference that was used for the last import.
Mainly useful for testing the import procedure.
(Incompatible with -r option.)
-k Keep for inspection the git clone directory that was created during
the import.
-h/--help Show this help.
Usage:
Import the latest version (potentially non-stable):
$0 lib/inih
Import a specific commit/tag:
$0 -r r42 lib/inih
Re-import the currently imported commit/tag:
$0 -c lib/inih
EOF
}
_showHelpAndExit()
{
_showHelp
exit
}
### Parse the CLI arguments
#
if [[ $@ =~ [-][-]help ]]; then
_showHelpAndExit
fi
ARG_GIT_REF=""
ARG_GIT_REF_USE_CURRENT="false"
ARG_KEEP_GIT_CLONE="false"
while getopts ":r:ckh" opt; do
case "$opt" in
r)
ARG_GIT_REF="$OPTARG"
;;
c)
ARG_GIT_REF_USE_CURRENT="true"
;;
k)
ARG_KEEP_GIT_CLONE="true"
;;
h)
_showHelpAndExit
;;
?)
_fatalError "Unsupported argument: '-$OPTARG'. Run '$0 -h' to list supported arguments." $LINENO
;;
*)
_fatalError "Internal error (opt=$opt)" $LINENO
;;
esac
done
if [[ "$ARG_GIT_REF" != "" ]] && [[ "$ARG_GIT_REF_USE_CURRENT" == "true" ]]; then
_fatalError "The -r and -c flags cannot be used at the same time. Run '$0 -h' for more information." $LINENO
fi
RES="${@:$OPTIND:1}"
if [ "$RES" == "" ]; then
_fatalError "Missing the import target. Usage example: $0 PATH/TO/LIB" $LINENO
fi
ARG_EXTLIB_DIR="$RES"
### Check the target directory
#
if [ ! -d $ARG_EXTLIB_DIR ]; then
_fatalError "Import target directory missing: $ARG_EXTLIB_DIR" $LINENO
fi
EXTLIB_DIR="$ARG_EXTLIB_DIR"
### Read the import definitions
#
EXTLIB_IMPORT_DEFS_FILE="$EXTLIB_DIR/IMPORT.defs"
if [ ! -f $EXTLIB_IMPORT_DEFS_FILE ]; then
_fatalError "Missing import definitions file: $EXTLIB_IMPORT_DEFS_FILE" $LINENO
fi
_echo "Reading the import definitions file: $EXTLIB_IMPORT_DEFS_FILE"
. $EXTLIB_IMPORT_DEFS_FILE
### Decide on the final ref to use
#
EXTLIB_GIT_REF_TO_USE=""
if [ "$ARG_GIT_REF" != "" ]; then
EXTLIB_GIT_REF_TO_USE="$ARG_GIT_REF"
_echo "Will use the following git reference for import: $EXTLIB_GIT_REF_TO_USE"
elif [ "$ARG_GIT_REF_USE_CURRENT" == "true" ]; then
EXTLIB_GIT_REF_TO_USE="$EXTLIB_GIT_REF"
_echo "Will use the currently-used git reference for import: $EXTLIB_GIT_REF_TO_USE"
else
_echo "No git reference for import was specified, will use the last commit on the default branch."
fi
### Clone the repo
#
EXTLIB_DIR_GIT_TMP="$EXTLIB_DIR/_import-git-repo"
rm -rf $EXTLIB_DIR_GIT_TMP
_echo "Cloning the git repository: $EXTLIB_GIT_REPO_URL"
git clone $EXTLIB_GIT_REPO_URL $EXTLIB_DIR_GIT_TMP
if [ "$EXTLIB_GIT_REF_TO_USE" != "" ]; then
_echo "Checking out git reference: $EXTLIB_GIT_REF_TO_USE"
(
cd $EXTLIB_DIR_GIT_TMP
git checkout --detach $EXTLIB_GIT_REF_TO_USE
)
fi
### Run the import
#
_echo "Running the import..."
set +x
_snoopy_extlib_import "$EXTLIB_DIR_GIT_TMP" "$EXTLIB_DIR"
_echo "Import complete."
### Apply the patches
#
EXTLIB_DIR_PATCHES="$EXTLIB_DIR/patches"
if [ -d $EXTLIB_DIR_PATCHES ]; then
_echo "Applying patches:"
(
cd $EXTLIB_DIR
for PATCHFILE in `find . -type f -regex '.+\.\(diff\|patch\)$' | sort`; do
_echo " Applying patch $PATCHFILE..."
patch -p0 < $PATCHFILE
_echo " Applying patch $PATCHFILE done."
done
)
_echo "All patches applied."
fi
### Update the git reference that was used for import
#
if [ "$EXTLIB_GIT_REF_TO_USE" != "$EXTLIB_GIT_REF" ]; then
EXTLIB_GIT_REF_TO_STORE=`cd $EXTLIB_DIR_GIT_TMP && git describe --all --long --always`
_echo "Storing git reference that was used for import: $EXTLIB_GIT_REF_TO_STORE"
_echo "Storing git reference that was used for import in: $EXTLIB_IMPORT_DEFS_FILE"
sed -i "s#^EXTLIB_GIT_REF=.*#EXTLIB_GIT_REF=\"$EXTLIB_GIT_REF_TO_STORE\"#" $EXTLIB_IMPORT_DEFS_FILE
fi
### Remove the tmp git dir
#
if [ "$ARG_KEEP_GIT_CLONE" == "true" ]; then
_echo "Keeping the temporary git clone around for your inspection, at: $EXTLIB_DIR_GIT_TMP"
else
rm -rf $EXTLIB_DIR_GIT_TMP
fi
### All done.
#
_echo "Import complete."
_echo "(Use 'git diff $EXTLIB_DIR' to inspect the changes.)"
|