Skip to content

Instantly share code, notes, and snippets.

@lgelo
Last active August 29, 2015 14:01
Show Gist options
  • Save lgelo/c78c6f47b76cd98a0114 to your computer and use it in GitHub Desktop.
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
#!/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