Files
scripts/ginit
2025-12-18 09:09:39 +08:00

94 lines
2.9 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ---------------------------------------------------------------------------
# ginit Initialise a new Git repository and add a remote origin
#
# Usage:
# ./ginit [group] [project]
#
# group (optional) Git organisation / group name. If omitted the script
# tries to infer it from the current directory.
# project (optional) Repository name. If omitted, the script derives it
# from the current directory name (stripping any leading prefix
# before a dot, e.g. “foo.bar” → “bar”).
#
# The script will:
# 1. Load the base Git URL from $HOME/.config/ginit-bash-url (prompting
# the user the first time and persisting the value).
# 2. Verify a README.md exists in the current directory.
# 3. Build the remote URL: $GIT_BASEURL/$group/$project.git
# 4. Prompt before running: git init && git remote add origin <repo>
#
# ---------------------------------------------------------------------------
# Resolve GIT_BASEURL always from the config file, never from the environment
# ---------------------------------------------------------------------------
# Path to the persisted config file
GIT_BASEURL_CFG="$HOME/.config/ginit-bash-url"
# If the config file exists, read the URL from it
if [ -f "$GIT_BASEURL_CFG" ]; then
GIT_BASEURL=$(cat "$GIT_BASEURL_CFG")
fi
# If we still dont have a value, ask the user and persist it
if [ -z "$GIT_BASEURL" ]; then
read -p "Enter the Git base URL (e.g. https://your.git-domain.com): " GIT_BASEURL
# Ensure the config directory exists
mkdir -p "$(dirname "$GIT_BASEURL_CFG")"
echo "$GIT_BASEURL" > "$GIT_BASEURL_CFG"
echo "Saved Git base URL to $GIT_BASEURL_CFG"
fi
# ---------------------------------------------------------------------------
# Validate that we now have a value
# ---------------------------------------------------------------------------
if [ -z "$GIT_BASEURL" ]; then
printf "Required git-base-url not set.\n"
exit 1
fi
# clear
if [ -d .git ]; then
echo 'GIT repo is already initialized!'
exit
fi
if [ ! -f README.md ]; then
echo 'README.md is required.'
exit
fi
# ---------------------------------------------------------------------------
# Existing argument handling (unchanged)
# ---------------------------------------------------------------------------
if [ $1 ]; then
group=$1
if [ $2 ]; then
project=$2
else
project=${PWD##*/}
project=${project/*\./}
project=${PWD##*/}
group=${project/\.*/}
group=${group/app/webapp}
project=${project/*\./}
fi
fi
# Rock
repo="$GIT_BASEURL/$group/$project.git"
# Roll
echo "Git Repo Init:"
echo "--------------"
echo "Working dir: $PWD"
echo "Repository : $repo"
echo ""
echo "Press enter to run \$ git init && git remote add origin [Repo]"
echo "or press CtrlC to cancel."
# Go go go
read confirm2
git init && git remote add origin $repo