Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions git/sparse_checkout.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

REMOTE_URL=$1
SUBDIR=$2
BRANCH=${3:-"main"}

if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage [REMOTE-URL] [SUBDIRECTORY] [BRANCH]"
echo "BRANCH argument is optional: defaults to main"
exit 1
fi

# This specifies the directory name that is to be created
MAIN_DIR="$(echo "$REMOTE_URL" | sed -n 's|\(.*com\)/\([^/]*\)/\([^/]*\)|\3|p')"

# reference: https://stackoverflow.com/questions/4114887/is-it-possible-to-do-a-sparse-checkout-without-checking-out-the-whole-repository

test -d "$MAIN_DIR" || git clone --filter=blob:none --no-checkout --depth 1 --sparse "$REMOTE_URL"
cd "$MAIN_DIR"

git config core.sparseCheckout true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to make permanent changes to my config. Can we enable flag only on commands that need it.

if test -f .git/info/sparse-checkout; then
[ "$(grep -c "$SUBDIR" .git/info/sparse-checkout)" -eq 0 ] && git sparse-checkout add "$SUBDIR"
else
git sparse-checkout set "$SUBDIR"
fi
git checkout --force
git pull origin "$BRANCH" --depth=1 --rebase --force

cd ..