Skip to content

Instantly share code, notes, and snippets.

@evansd
Created September 28, 2020 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evansd/5193be7fd5621566858226a6cf44dc2d to your computer and use it in GitHub Desktop.
Save evansd/5193be7fd5621566858226a6cf44dc2d to your computer and use it in GitHub Desktop.
#!/bin/bash
set -eo pipefail
this_dir="$( unset CDPATH && cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
: ${GIT_DIR="$this_dir/shared-git-repo"}
remote="$1"
sha="$2"
local_path="$3"
if [[ -z "$remote" || -z "$sha" || -z "$local_path" ]]; then
echo 'Usage: fetch-commit REMOTE SHA LOCAL_PATH' >&2
echo >&2
echo 'Fetch commit referenced by SHA from REMOTE and check it out in LOCAL_PATH' >&2
exit 1
fi
export GIT_DIR
mkdir -p "$GIT_DIR"
git init --bare --quiet
if ! git cat-file -e "$sha^{commit}" 2>/dev/null; then
echo "Fetching commit $sha from $remote" >&2
git fetch --depth 1 --force "$remote" "$sha"
else
echo "Commit $sha already downloaded" >&2
fi
mkdir -p "$local_path"
git --work-tree="$local_path" checkout --quiet --force "$sha"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment