Created
May 14, 2020 06:48
-
-
Save angelikatyborska/358beb677cfb402d47955ea4cb69d74b to your computer and use it in GitHub Desktop.
post-checkout git hook to stash deps and _build
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/sh | |
# Set this to a list of space-separated paths to files/directories, | |
# which will be stashed away, when the current branch is changed | |
ARTIFACT_PATHS="deps _build" | |
# Set this to a directory where you would like the build artifacts of | |
# other branches to be stashed | |
ARTIFACT_STASH_PATH=".artifacts-stash" | |
PREVIOUS_HEAD="$1" | |
CURRENT_HEAD="$2" | |
IS_BRANCH_CHECKOUT="$3" | |
if [ "$IS_BRANCH_CHECKOUT" = "0" ] ; then | |
# This was a "file" checkout, not a branch switch. | |
exit 0 | |
fi | |
if [ "$PREVIOUS_HEAD" = "$CURRENT_HEAD" ]; then | |
# Branch wasn't actually changed. Nothing to do. | |
exit 0 | |
fi | |
PREVIOUS_HEAD_SHORT=$(echo $PREVIOUS_HEAD | head -c8) | |
CURRENT_HEAD_SHORT=$(echo $CURRENT_HEAD | head -c8) | |
PREVIOUS_STASH_PATH="$ARTIFACT_STASH_PATH/$PREVIOUS_HEAD" | |
CURRENT_STASH_PATH="$ARTIFACT_STASH_PATH/$CURRENT_HEAD" | |
mkdir -p "$PREVIOUS_STASH_PATH" | |
# move current artifacts to an artifact stash directory | |
for path in $ARTIFACT_PATHS; do | |
if [ -e "$path" ] ; then | |
if [ -e "$CURRENT_STASH_PATH/$path" ] ; then | |
echo "[artifact-cache] Stashing (mv) '$path' for $PREVIOUS_HEAD_SHORT..." | |
mv "$path" "$PREVIOUS_STASH_PATH" | |
else | |
echo "[artifact-cache] Stashing (cp -a) '$path' for $PREVIOUS_HEAD_SHORT..." | |
cp -a "$path" "$PREVIOUS_STASH_PATH" | |
fi | |
else | |
echo "[artifact-cache] No '$path' found to stash for $PREVIOUS_HEAD_SHORT" | |
fi | |
done | |
# restore an artifact stash directory if exists | |
if [ -d "$CURRENT_STASH_PATH" ]; then | |
for path in $ARTIFACT_PATHS; do | |
if [ -e "$CURRENT_STASH_PATH/$path" ] ; then | |
echo "[artifact-cache] Restoring '$path' for $CURRENT_HEAD_SHORT..." | |
mv "$CURRENT_STASH_PATH/$path" "$path" | |
else | |
echo "[artifact-cache] No '$path' found to restore for $CURRENT_HEAD_SHORT" | |
fi | |
done | |
rm -rf "$CURRENT_STASH_PATH" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment