Last active
August 29, 2015 14:01
-
-
Save lgelo/c78c6f47b76cd98a0114 to your computer and use it in GitHub Desktop.
Export branch from git repository and preserve existing file listed in variable or .gitattributes
This file contains 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 | |
REPO="[email protected]:lgelo/demo.git" | |
BRANCH="master" | |
TARGET="/srv/app/demo" | |
PRESERVE=( settings.py ) | |
STAGING=$(mktemp -d) | |
CWD=${PWD} | |
git clone --depth 1 --branch ${BRANCH} ${REPO} ${STAGING} | |
if [ -f .gitattributes ]; then | |
mapfile -O ${#PRESERVE[@]} -t PRESERVE < <(grep export-ignore .gitattributes | awk '{print $1}') | |
fi | |
if [ ! -d "${TARGET}" ]; then | |
mkdir -p "${TARGET}" | |
fi | |
if [ -n "${PRESERVE}" ]; then | |
printf -v exclude "%s|" "${PRESERVE[@]}" | |
exclude=${exclude%?} | |
printf -v exclude "!(%s)" "${exclude}" | |
shopt -s extglob | |
rm -rf ${TARGET}/${exclude} | |
else | |
rm -rf ${TARGET}/* | |
fi | |
cd ${STAGING} | |
git archive ${BRANCH} ${STAGING} | tar -x -C ${TARGET} | |
cd ${CWD} | |
rm -rf ${STAGING} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment