diff --git a/README.md b/README.md index 56741c6..78fd24e 100644 --- a/README.md +++ b/README.md @@ -48,12 +48,14 @@ create_ssh_config.sh [repo url full] ([jump proxy]) Create a new clone ```sh -new_clone.sh [repository] [branch] ([host]) ([remote name]) +new_clone.sh -r -b [-H ] [-f ] [-n ] ``` -The [host] is the SSH Host name entry, this will replace any "[host]:" in the [repository]. If the [host] is not set the host set in the [repository] will be used. If nothing found the script will abort +The [host (-H)] is the SSH Host name entry, this will replace any "[host]:" in the [repository]. If the [host (-H)] is not set the host set in the [repository] will be used. If nothing found the script will abort. Note that if [host (-H)] is not set the host name will be the Repository name which has to match the SSH config setting. -A [branch] name must be set all the time. +A [branch (-B)] name must be set all the time. + +An override folder naem can be set with `-f` An optional [remote name] can be set, if not set "origin" will be used. diff --git a/src/bin/new_clone.sh b/src/bin/new_clone.sh index 04d2e15..aacc40b 100755 --- a/src/bin/new_clone.sh +++ b/src/bin/new_clone.sh @@ -6,15 +6,99 @@ # COMMAND: new_clone.sh [] [] [] -REPOSITORY="$1"; -BRANCH="$2"; -REMOTE_HOST="$3"; -REPOSITORY_FOLDER="$4" -REMOTE_NAME="$5"; -if [ "${REPOSITORY}" == "--help" ]; then - echo "$0 [] [] []"; + +function error() { + if [ -t 1 ]; then echo "[MAK] ERROR: $*" >&2; fi; exit 0; +} + +usage() { + cat < -b | --branch [-H | --host ] [-f | --folder ] [-n | --remote ] + +New clone a git repository via ssh into the clone folder. + +Available options: + +-h, --help Print this help and exit +-r, --repository Repository path (e.g. user/repo.git) +-b, --branch Branch to clone (e.g. main) +-H, --host Override SSH host from ssh config (e.g. my-ssh-host) +-f, --folder Target folder name for the repository (e.g. repo-name) +-n, --remote Remote name (defaults to origin) + +EOF + exit +} + +# REPOSITORY="$1"; +# BRANCH="$2"; +# REMOTE_HOST="$3"; +# REPOSITORY_FOLDER="$4" +# REMOTE_NAME="$5"; +# if [ "${REPOSITORY}" == "--help" ]; then +# echo "$0 [] [] []"; +# exit; +# fi; +REPOSITORY=""; +BRANCH=""; +REMOTE_HOST=""; +REPOSITORY_FOLDER=""; +REMOTE_NAME="origin"; +while [ -n "${1-}" ]; do + case "${1}" in + -r | --repository) + REPOSITORY="${2-}"; + shift + ;; + -b | --branch) + BRANCH="${2-}"; + shift + ;; + -H | --host) + REMOTE_HOST="${2-}"; + shift + ;; + -f | --folder) + REPOSITORY_FOLDER="${2-}"; + shift + ;; + -n | --remote) + REMOTE_NAME="${2-}"; + shift + ;; + -h | --help) + usage + ;; + # invalid option + -?*) + error "[!] Unknown option: '$1'." + ;; + esac + shift; +done; + +# if no repository or banch name given, show error +error=0 +if [ -z "${REPOSITORY}" ]; then + echo "[!] Must set a repository full url, ssh only"; + error=1; +fi; +if [ -z "${BRANCH}" ]; then + echo "[!] Must set a branch name"; + error=1; +fi; +# further checks that repository folder if set can only by alphanumeric, -, _ or . +if [ -n "${REPOSITORY_FOLDER}" ]; then + if ! [[ "${REPOSITORY_FOLDER}" =~ ^[a-zA-Z0-9._-]+$ ]]; then + echo "[!] Repository folder name can only contain alphanumeric characters, dots, dashes or underscores"; + error=1; + fi; +fi; + +if [ $error -eq 1 ]; then exit; fi; + if [ -z "${REMOTE_NAME}" ]; then REMOTE_NAME="origin" fi;