Skip to content

Instantly share code, notes, and snippets.

@arbakker
Created June 8, 2020 12:00
Show Gist options
  • Save arbakker/ad9b6570c999cd9f4375a0d0fe8da0bf to your computer and use it in GitHub Desktop.
Save arbakker/ad9b6570c999cd9f4375a0d0fe8da0bf to your computer and use it in GitHub Desktop.
Bash script for downloading directories from GIT repositories, without cloning the repository.
#!/usr/bin/env bash
#
# Bash script for downloading directories from GIT repositories, without cloning the repository.
# Requires the SVN client to be installed and available in PATH.
#
# Author: [email protected]
# Date: 05/06/2020
PROGRAM_NAME=$(basename $0)
URL="$1"
DEST_DIR="$2"
function usage {
echo "usage: $PROGRAM_NAME <URL> [OUTPUT_DIR]"
echo ""
echo "downloads single directory from GIT repository"
echo "requires svn client to be installed"
exit 1
}
if [[ -z "$URL" ]]; then
usage
fi
set -eu
# check if valid git url by checking for /tree/ in path
if [[ ! $URL =~ ^.*?\/tree\/.*?$ ]]; then
echo "NOT VALID GIT URL"
fi
# check if url is on master branch
# if master rename /tree/master/ > /trunk/
# else rename /tree/$branch-name/ > /branches/$branch-name/
if [[ $URL =~ ^.*?\/tree\/master\/.*?$ ]]; then
SVN_URL=$(sed -E "s|\/tree\/master\/|\/trunk\/|g" <<< "$URL")
else
SVN_URL=$(sed -E "s|\/tree\/(.*?)\/|\/branches\/\1\/|g" <<< "$URL")
fi
if [[ -z "$DEST_DIR" ]]; then
svn checkout "$SVN_URL"
DEST_DIR=$(dirname $SVN_URL)
else
svn checkout "$SVN_URL" "$DEST_DIR"
fi
# rm .svn directory
rm -rf "$DEST_DIR/.svn"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment