feat(script/git-bare-clone): add option parsing, usage and workspace handling
Replace the previous simple one-liner with a small POSIX shell wrapper that: - provides a usage() help text and -h flag - supports -d <bare-repo-dir> to set the bare repo directory (default: "./.git-repo") - validates required arguments and prints helpful errors with non-zero exit codes - creates the workspace directory if it doesn't exist - uses safer variable handling when constructing the git clone and .git file - switches to /bin/sh -e for stricter, more portable execution This makes the script more robust, user-friendly and configurable while preserving the original behavior of producing a gitdir file and configuring remote.origin.fetch. BREAKING CHANGE: default bare repository directory changed from "./.bare" to "./.git-repo". If you rely on the previous default path, pass -d to restore the old location.
This commit is contained in:
+42
-6
@@ -1,7 +1,43 @@
|
||||
#!/bin/bash
|
||||
WS_DIR=${!#}
|
||||
if [ -z "$WS_DIR" ]; then
|
||||
WS_DIR="."
|
||||
fi
|
||||
#!/bin/sh -e
|
||||
|
||||
git clone --bare ${*: -$#:$#-1} $WS_DIR/.bare && echo "gitdir: ./.bare" > $WS_DIR/.git && git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: git bare-clone [-d bare-repo-dir] [-h] <git-repo-url> [workspace]
|
||||
EOF
|
||||
}
|
||||
|
||||
BARE_REPO_DIR="./.git-repo"
|
||||
while getopts ":d:h" opt; do
|
||||
case "$opt" in
|
||||
d) BARE_REPO_DIR="$OPTARG" ;;
|
||||
h) usage; exit 0 ;;
|
||||
:) echo "Option -$OPTARG requires an argument." >&2; usage; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
shift $((OPTIND - 1))
|
||||
|
||||
if [ -z "$BARE_REPO_DIR" ]; then
|
||||
echo "Bare repository directory is not specified." >&2
|
||||
usage
|
||||
exit 2
|
||||
fi
|
||||
|
||||
GIT_REPO_URL="$1"
|
||||
WORKSPACE="$2"
|
||||
|
||||
if [ -z "$GIT_REPO_URL" ]; then
|
||||
echo "Git repository URL is required." >&2
|
||||
usage
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if [ -z "$WORKSPACE" ]; then
|
||||
WORKSPACE="."
|
||||
fi
|
||||
|
||||
if [ ! -d "$WORKSPACE" ]; then
|
||||
mkdir -p $WORKSPACE
|
||||
fi
|
||||
|
||||
git clone --bare ${GIT_REPO_URL} $WORKSPACE/$BARE_REPO_DIR && echo "gitdir: $BARE_REPO_DIR" > $WORKSPACE/.git && git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
|
||||
|
||||
Reference in New Issue
Block a user