From ddd32822b142ac95650e38e5f615d4e874898c1f Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 14 Nov 2023 20:14:53 +0000 Subject: [PATCH] feat(scripts): add git-bare-clone --- bin/git-bare-clone | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 bin/git-bare-clone diff --git a/bin/git-bare-clone b/bin/git-bare-clone new file mode 100755 index 0000000..4db9eab --- /dev/null +++ b/bin/git-bare-clone @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# Creates a new bare clone of a repository with the bare files within a `.bare` +# directory. It also sets the origin URL so push and pull work as expected. + +if [[ "$1" == "" ]]; then + echo "Usage: ${0##*/} []" + exit 2 +fi + +set -euo pipefail + +repository_url="${1}" +directory="${2:-}" +location=".bare" + +# If no destination directory is specified, get it from the repository URL - the same as "git clone". +if [ -z "${directory}" ]; then + directory="$(basename -s .git "${repository_url}")" +fi + +# Create the parent directory if needed. +mkdir -pv "${directory}" +cd "${directory}" + +git clone --bare "${repository_url}" "${location}" + +# Adjust origin fetch locations. +cd "${location}" +git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" + +# Set .git file contents. +cd .. +echo "gitdir: ./${location}" > .git