Created
August 5, 2018 08:08
-
-
Save brandonkal/843d07a453f578bbd8e6e7dad3cc7cf5 to your computer and use it in GitHub Desktop.
Useful Git Scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Adds desired submodule to project at location and performs a shallow clone, | |
# Then it performs a sparse checkout with the files specified | |
set -e | |
usage() { | |
echo | |
echo "usage: submodule-add https://gitremote.git vendor/mod" | |
echo "Optionally you can pass desired files and folders to checkout as additional arguments..." | |
echo "e.g. submodule-add https://remoterepo.git submodule importantdir docker-compose.yml" | |
echo | |
exit | |
} | |
if [[ -z "$2" ]]; then | |
usage | |
fi | |
validurl='(https?|ssh)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' | |
if ! [[ "$1" =~ $validurl ]]; then | |
echo "Invalid URL Specified!" | |
usage | |
fi | |
URL="$1" | |
FOLDER="$2" | |
if [[ -z "$3" ]]; then | |
echo "Which directories and files would you like to checkout (seperated by space)?" | |
echo "Leave blank to for a full shallow checkout" | |
read -a CHECKOUT | |
else | |
CHECKOUT=( "${@:3}" ) | |
fi | |
git submodule add --depth 2 "$URL" "$FOLDER" | |
cd "$FOLDER" | |
if [ -z "${CHECKOUT[0]}" ]; then | |
echo "No parameters specified. Performing a full checkout." | |
exit | |
else | |
git config core.sparsecheckout true | |
S=$(git rev-parse --git-dir)/info/sparse-checkout | |
echo /README.md > $S | |
for i in "${CHECKOUT[@]}" | |
do | |
echo $i >> $S | |
done | |
fi | |
# Now Checkout the Submodule | |
git checkout | |
git gc --prune=all | |
cd .. | |
cd "$(git rev-parse --git-dir)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment