Skip to content

Instantly share code, notes, and snippets.

@aisrael
Last active August 29, 2015 14:00
Show Gist options
  • Save aisrael/415b15ea9022dc5bd67f to your computer and use it in GitHub Desktop.
Save aisrael/415b15ea9022dc5bd67f to your computer and use it in GitHub Desktop.
Provides a "git export" command similar to "svn export" that exports the current index (including all submodules) to the target path.
#!/bin/bash
#
# Place this in your executable path (e.g. "~/bin/git-export")
# and you should now be able to go: git export [path]
PWD=$(pwd)
# Exit on error (e.g., "Not a git repository")
set -e
TOP_LEVEL=$(git rev-parse --show-toplevel)
set +e
REPO_NAME=$(basename "$TOP_LEVEL")
if [[ -z "$1" ]]; then
cat <<- USAGE
Usage:
git export [path]
Where:
[path] is the folder to export to (will export to "[path]/$REPO_NAME")
USAGE
exit 0
fi
# Strip trailing slash if present
RAW_TARGET="${1%/}"
if [[ ! -d "$RAW_TARGET" ]]; then
echo "\"$RAW_TARGET\" is not a directory!"
exit 1
fi
pushd "$RAW_TARGET" >> /dev/null
TARGET_FOLDER=$(pwd)
popd >> /dev/null
TARGET="$TARGET_FOLDER/$REPO_NAME"
if [[ -d "$TARGET" ]]; then
echo "\"$TARGET\" already exists!"
exit 1
fi
git checkout-index --prefix="$TARGET/" -a
git submodule foreach --quiet 'git checkout-index --prefix="'$TARGET'/$path/" -a'
echo Exported index to $TARGET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment