diff --git a/sos-git.sh b/sos-git.sh new file mode 100755 index 0000000..8e5d30b --- /dev/null +++ b/sos-git.sh @@ -0,0 +1,539 @@ +#!/bin/bash +function loadDefaultConfiguration() { + export "SOSGIT_CONFIGVERSION=1" + export "SOSGIT_SETUP_USER=false" + export "SOSGIT_SETUP_SYSTEM=false" + export "SOSGIT_SHELL=/bin/sh" + export "SOSGIT_MAKE=/bin/make" + export "SOSGIT_EDITOR=/bin/nano" + export "SOSGIT_CURL=/bin/curl" + export "SOSGIT_GIT=/bin/git" + export "SOSGIT_LS=/bin/ls" + export "SOSGIT_TITLE_SUPPRESS=false" + export "SOSGIT_TITLE_TEXT=false" + export "SOSGIT_TMP=/dev/shm" + export "SOSGIT_REPOS=$HOME/.sostools/git/repos" + export "SOSGIT_UPDATEURL=https://staropensource.ddns.net/git/staropensource/sostools/raw/branch/develop/sos-git.sh" + export "SOSGIT_REQUESTSTRING=https://staropensource.ddns.net/git/%s/%s" + export "SOSGIT_REQUESTSTRING_REMOTE=https://%s:%s@staropensource.ddns.net/git/%s/%s" +} +function loadSystemConfigurationMigrationError() { + echo "Your system configuration files are too old, but can't be reset." + echo "Please update all configuration files to the new version," + echo "if you still want to use them." + exit "249" +} +function loadSystemConfiguration() { + if [ -f "/etc/sostools/git/sos-git.conf" ]; then + export "SOSGIT_EXITSTRING=$SOSGIT_CONFIGVERSION" + source "/etc/sostools/git/sos-git.conf" + if [ ! "$SOSGIT_CONFIGVERSION" == "$SOSGIT_EXITSTRING" ]; then + if [ -f "/etc/sostools/git/sos-git.conf" ] || [ -d "/etc/sostools/git/sos-git.conf" ]; then + rm -rf "/etc/sostools/git/sos-git.conf" + if [ ! "$?" == "0" ]; then loadSystemConfigurationMigrationError; fi + fi + mv "/etc/sostools/git/sos-git.conf" "/etc/sostools/git/sos-git.conf.bak" + if [ ! "$?" == "0" ]; then loadSystemConfigurationMigrationError; fi + if [ -f "/etc/sostools/git/sos-git.conf.d" ] || [ -d "/etc/sostools/git/sos-git.conf.d" ]; then + rm -rf "/etc/sostools/git/sos-git.conf" + if [ ! "$?" == "0" ]; then loadSystemConfigurationMigrationError; fi + fi + mv "/etc/sostools/git/sos-git.conf.d" "/etc/sostools/git/sos-git.conf.d.bak" + if [ ! "$?" == "0" ]; then loadSystemConfigurationMigrationError; fi + doSystemSetup "force" &> /dev/null + echo "Your system configuration files are too old and have been reset." + echo "A backup is named \"sos-git.conf.bak\" and \"sos-git.conf.d.bak\"." + echo "Please update all configuration files to the new version," + echo "if you still want to use them." + exit "250" + fi + fi + if [ -d "/etc/sostools/git/sos-git.conf.d/" ]; then + for file in $(/bin/ls -Aw 1 "/etc/sostools/git/sos-git.conf.d/");do + source "/etc/sostools/git/sos-git.conf.d/${file}" + done + fi +} +function loadUserConfiguration() { + if [ -f "$HOME/.sostools/git/sos-git.conf" ]; then + export "SOSGIT_EXITSTRING=$SOSGIT_CONFIGVERSION" + source "$HOME/.sostools/git/sos-git.conf" + if [ ! "$SOSGIT_CONFIGVERSION" == "$SOSGIT_EXITSTRING" ]; then + mv "$HOME/.sostools/git/sos-git.conf" "$HOME/.sostools/git/sos-git.conf.bak" + mv "$HOME/.sostools/git/sos-git.conf.d" "$HOME/.sostools/git/sos-git.conf.d.bak" + doUserSetup "force" &> /dev/null + echo "Your user configuration files are too old and have been reset." + echo "A backup is named \"sos-git.conf.bak\" and \"sos-git.conf.d.bak\"." + echo "Please update all configuration files to the new version," + echo "if you still want to use them." + exit "251" + fi + fi + if [ -d "$HOME/.sostools/git/sos-git.conf.d/" ]; then + for file in $(/bin/ls -Aw 1 "$HOME/.sostools/git/sos-git.conf.d/");do + source "$HOME/.sostools/git/sos-git.conf.d/${file}" + done + fi +} +function getDefaultConfiguration() { + # SOSGIT_CONFIGVERSION=1 + # # sos-git configuration\n + # # this is the default configuration, you\n + # # may make changes in sos-git.conf.d\n + # # since they may get overriten in a future\n + # # update.\n + # \n + # # Commands\n + # ## Shell\n + # ## DEFAULT: /bin/sh\n + # ## You may want to set this to /bin/bash\n + # #SOSGIT_SHELL=/bin/sh\n + # #\n + # ## Make\n + # ## DEFAULT: /bin/make\n + # #SOSGIT_MAKE=/bin/make\n + # #\n + # ## Editor\n + # ## DEFAULT: /bin/nano\n + # ## You may want to set this to /bin/vi\n + # ## or /bin/vim or any other editor.\n + # #SOSGIT_EDITOR=/bin/nano\n + # #\n + # ## cURL\n + # ## DEFAULT: /bin/curl\n + # #SOSGIT_CURL=/bin/curl\n + # #\n + # ## git\n + # ## DEFAULT: /bin/git\n + # #SOSGIT_GIT=/bin/git\n + # #\n + # ## ls\n + # ## DEFAULT: /bin/ls\n + # ## The ls command must be from GNU\n + # ## coreutils, otherwise it may not\n + # ## work correctly with sos-git.\n + # #SOSGIT_LS=/bin/ls\n + # \n + # # Title\n + # ## Suppress\n + # ## DEFAULT: false\n + # ## This hides the branding at\n + # ## startup completely.\n + # #SOSGIT_TITLE_SUPPRESS=false\n + # #\n + # ## Text\n + # ## DEFAULT: false\n + # ## This prints the branding\n + # ## at startup in normal text.\n + # #SOSGIT_TITLE_TEXT=false\n + # \n + # # Temp\n + # # DEFAULT: /dev/shm\n + # # The temporary directory.\n + # # NO TRAILING SLASH AT THE END!\n + # #SOSGIT_TMP=/dev/shm\n + # \n + # # Repository save directory\n + # # DEFAULT: \$HOME/.sostools/git/repos\n + # # The directory where your downloaded\n + # # repositories are stored.\n + # # NO TRAILING SLASH AT THE END!\n + # # NO ~, USE \$HOME INSTEAD!\n + # #SOSGIT_REPOS=\$HOME/.sostools/git/repos\n + # \n + # # Internet\n + # ## Update URL\n + # ## DEFAULT: https://staropensource.ddns.net\n + # ## /git/staropensource/sostools\n + # ## /raw/branch/master/sos-git.sh + # ## The update URL, from where sos-git gets\n + # ## updates. The save location will be\n + # ## determined from the command line arguments.\n + # #SOSGIT_UPDATEURL=https://staropensource.ddns.net/git/staropensource/sostools/raw/branch/develop/sos-git.sh\n + # #\n + # ## Request string\n + # ## DEFAULT: https://staropensource.ddns.net\n + # ## /git/%s/%s\n + # ## String is used for making requests and\n + # ## downloading repositories. First %s is user,\n + # ## second %s is the repository. + # ## NO TRAILING SLASH AT THE END!\n + # #SOSGIT_REQUESTSTRING=https://staropensource.ddns.net/git/%s/%s\n + # #\n + # ## Remote request string\n + # ## DEFAULT: https://%s:%s@staropensource.ddns.net/%s/%s + # ## String is used for remoteauth, that updates\n + # ## the remote string in git. First %s is username,\n + # ## second %s is password, third %s is user and\n + # ## fourth %s is the repository.\n + # ## NO TRAILING SLASH AT THE END!\n + # #SOSGIT_REQUESTSTRING_REMOTE=https://%s:%s@staropensource.ddns.net/%s/%s + export "SOSGIT_EXITSTRING=SOSGIT_CONFIGVERSION=1\n# sos-git configuration\n# this is the default configuration, you\n# may make changes in sos-git.conf.d\n# since they may get overriten in a future\n# update.\n\n# Commands\n## Shell\n## DEFAULT: /bin/sh\n## You may want to set this to /bin/bash\n#SOSGIT_SHELL=/bin/sh\n#\n## Make\n## DEFAULT: /bin/make\n#SOSGIT_MAKE=/bin/make\n#\n## Editor\n## DEFAULT: /bin/nano\n## You may want to set this to /bin/vi\n## or /bin/vim or any other editor.\n#SOSGIT_EDITOR=/bin/nano\n#\n## cURL\n## DEFAULT: /bin/curl\n#SOSGIT_CURL=/bin/curl\n#\n## git\n## DEFAULT: /bin/git\n#SOSGIT_GIT=/bin/git\n#\n## ls\n## DEFAULT: /bin/ls\n## The ls command must be from GNU\n## coreutils, otherwise it may not\n## work correctly with sos-git.\n#SOSGIT_LS=/bin/ls\n\n# Title\n## Suppress\n## DEFAULT: false\n## This hides the branding at\n## startup completely.\n#SOSGIT_TITLE_SUPPRESS=false\n#\n## Text\n## DEFAULT: false\n## This prints the branding\n## at startup in normal text.\n#SOSGIT_TITLE_TEXT=false\n\n# Temp\n# DEFAULT: /dev/shm\n# The temporary directory.\n# NO TRAILING SLASH AT THE END!\n#SOSGIT_TMP=/dev/shm\n\n# Repository save directory\n# DEFAULT: \$HOME/.sostools/git/repos\n# The directory where your downloaded\n# repositories are stored.\n# NO TRAILING SLASH AT THE END!\n# NO ~, USE \$HOME INSTEAD!\n#SOSGIT_REPOS=\$HOME/.sostools/git/repos\n\n# Internet\n## Update URL\n## DEFAULT: https://staropensource.ddns.net\n## /git/staropensource/sostools\n## /raw/branch/master/sos-git.sh\n## The update URL, from where sos-git gets\n## updates. The save location will be\n## determined from the command line arguments.\n#SOSGIT_UPDATEURL=https://staropensource.ddns.net/git/staropensource/sostools/raw/branch/master/sos-git.sh\n#\n## Request string\n## DEFAULT: https://staropensource.ddns.net\n## /git/%s/%s\n## String is used for making requests and\n## downloading repositories. First %s is user,\n## second %s is the repository.\n## NO TRAILING SLASH AT THE END!\n#SOSGIT_REQUESTSTRING=https://staropensource.ddns.net/git/%s/%s\n#\n## Remote request string\n## DEFAULT: https://%s:%s@staropensource.ddns.net/%s/%s## String is used for remoteauth, that updates\n## the remote string in git. First %s is username,\n## second %s is password, third %s is user and\n## fourth %s is the repository.\n## NO TRAILING SLASH AT THE END!\n#SOSGIT_REQUESTSTRING_REMOTE=https://%s:%s@staropensource.ddns.net/%s/%s\n" + return "0" +} +function doUserSetup() { + if [ "$1" == "force" ] || [ ! "$SOSGIT_SETUP_USER" == "true" ]; then + echo "Starting user setup" + mkdir "$HOME/.sostools/git/sos-git.conf.d" -p + getDefaultConfiguration + echo -e "$SOSGIT_EXITSTRING" &> $HOME/.sostools/git/sos-git.conf + echo -e "# DO NOT DELETE OR MODIFY THIS FILE\nSOSGIT_SETUP_USER=true" &> $HOME/.sostools/git/sos-git.conf.d/setup.conf + echo "Applying new changes" + loadUserConfiguration + fi +} +function doSystemSetup() { + if [ "$1" == "force" ] || [ ! "$SOSGIT_SETUP_SYSTEM" == "true" ] && [ "$(whoami)" == "root" ]; then + echo "Starting system setup" + mkdir "/etc/sostools/git/sos-git.conf.d" -p + getDefaultConfiguration + echo -e "$SOSGIT_EXITSTRING" &> /etc/sostools/git/sos-git.conf + echo -e "# DO NOT DELETE OR MODIFY THIS FILE\nSOSGIT_SETUP_SYSTEM=true" &> /etc/sostools/git/sos-git.conf.d/setup.conf + echo "Applying new changes" + loadSystemConfiguration + fi +} +function doRepositorySetup() { + if [ -d "$SOSGIT_REPOS" ]; then + return "0" + elif [ -f "$SOSGIT_REPOS" ]; then + echo "Moving \"$SOSGIT_REPOS\" to \"$SOSGIT_REPOS.bak\"" + mv "$SOSGIT_REPOS" "$SOSGIT_REPOS.bak" + echo "Creating repository save directory" + mkdir -p "$SOSGIT_REPOS" + else + echo "Creating repository save directory" + mkdir -p "$SOSGIT_REPOS" + fi +} +loadDefaultConfiguration +loadSystemConfiguration +loadUserConfiguration +doSystemSetup +doUserSetup +doRepositorySetup +function translateRepo() { + export "SOSGIT_EXITSTRING=${SOSGIT_REPOS}/$1" +} +function existRepo() { + translateRepo "$1" + if [ -d "$SOSGIT_EXITSTRING" ]; then + return "0" + else + return "1" + fi +} +function noRepoFound() { + echo "ERROR: Repository \"$1\" does not exist." + exit 9 +} +function formatRequestString() { + # dont noodle me that I shouldve used -v var. I tried, it didnt work. yes i did typos to fuck you off. + printf "$SOSGIT_REQUESTSTRING" "$1" "$2" &> ${SOSGIT_TMP}/formatrequeststring + export "SOSGIT_EXITSTRING=$(cat ${SOSGIT_TMP}/formatrequeststring)" + rm -rf "${SOSGIT_TMP}/formatrequeststring" + return +} +function formatRemoteRequestString() { + # same here as in formatRequestString() + printf "$SOSGIT_REQUESTSTRING_REMOTE" "$1" "$2" "$3" "$4" &> ${SOSGIT_TMP}/formatremoterequeststring + export "SOSGIT_EXITSTRING=$(cat ${SOSGIT_TMP}/formatremoterequeststring)" + rm -rf "${SOSGIT_TMP}/formatremoterequeststring" + return +} +function existRepoOnline() { + formatRequestString "$1" "$2" + $SOSGIT_CURL "-sSLqq" "${SOSGIT_EXITSTRING}/.git/HEAD" &> /dev/null + if [ "$?" == "0" ]; then + return "0" + else + return "1" + fi +} +function hasMakefile() { + translateRepo "$1" + cd "$SOSGIT_EXITSTRING" + if [ -f "Makefile" ]; then + return "0" + else + return "1" + fi +} +function noMakefileFound() { + echo "ERROR: Repository \"$1\" is not compatible with make." + exit 7 +} +function argumentMissing() { + echo "ERROR: Missing arguments." + exit 6 +} +function existBranch() { + translateRepo "$1" + cd "$SOSGIT_EXITSTRING" + git branch | grep "$2" &> /dev/null + if [ "$?" == "0" ]; then + return "0" + else + return "1" + fi +} +function noBranchFound() { + echo "ERROR: Branch \"$2\" in repository \"$1\" does not exist." +} +if [ ! "$SOSGIT_TITLE_SUPPRESS" == "true" ]; then + if [ ! "$SOSGIT_TITLE_TEXT" == "true" ]; then + # _ _ + # ___ ___ ___ __ _(_) |_ + #/ __|/ _ \/ __|_____ / _` | | __| + #\__ \ (_) \__ \_____| (_| | | |_ + #|___/\___/|___/ \__, |_|\__| + # |___/ + echo " _ _" + echo " ___ ___ ___ __ _(_) |_" + echo "/ __|/ _ \\/ __|_____ / _\` | | __|" + echo "\\__ \\ (_) \\__ \\_____| (_| | | |_" + echo "|___/\\___/|___/ \\__, |_|\\__|" + echo " |___/" + else + echo "sos-git" + fi +fi +if [ "$1" == "" ]; then + echo "Use \"sos-git help\" for all commands." + echo "If you want more information, use \"sos-git helpextend\"." + exit 1 +elif [ "$1" == "help" ]; then + echo " [optional]" + echo "sos-git branch " + echo " build " + echo " shell " + echo " clean " + echo " editconf " + echo " getrepo " + echo " help" + echo " helpextend" + echo " install " + echo " list" + echo " pull " + echo " push " + echo " remoteauth [user]" + echo " remrepo " + echo " selfupdate" + echo " upstream " + exit 0 +elif [ "$1" == "helpextend" ]; then + echo " [optional]" + echo "command | args | description" + echo "branch | | list all branches" + echo "build | | build repository" + echo "shell | | spawn shell in repository" + echo "clean | | clean built executables" + echo "editconf | | edit the configuration" + echo "getrepo | | clone a repository" + echo "help | | lists all commands" + echo "helpextend | | lists all commands with extended information" + echo "install | | install built executables" + echo "list | | lists all cloned repositories" + echo "pull | | pull new updates" + echo "push | | push new changes" + echo "remoteauth | [user] | update remote to include login information" + echo "remrepo | | remove a cloned repository" + echo "selfupdate | | update sos-git" + echo "upstream | | push new changes and set branch upstream" +elif [ "$1" == "build" ]; then + if [ "$2" == "" ]; then + argumentMissing + fi + if existRepo "$2"; then + if hasMakefile "$2"; then + translateRepo "$2" + cd "$SOSGIT_EXITSTRING" + echo "Building repository \"$2\"" + $SOSGIT_MAKE -j$(nproc) build + exit "$?" + else + noMakefileFound "$2" + fi + else + noRepoFound "$2" + fi +elif [ "$1" == "shell" ]; then + if [ "$2" == "" ]; then + argumentMissing + fi + if existRepo "$2"; then + translateRepo "$2" + cd "$SOSGIT_EXITSTRING" + $SOSGIT_SHELL + exit "$?" + else + noRepoFound "$2" + fi +elif [ "$1" == "clean" ]; then + if [ "$2" == "" ]; then + argumentMissing + fi + if existRepo "$2"; then + if hasMakefile "$2"; then + translateRepo "$2" + cd "$SOSGIT_EXITSTRING" + echo "Cleaning repository \"$2\"" + $SOSGIT_MAKE -j$(nproc) clean + exit "$?" + else + noMakefileFound "$2" + fi + else + noRepoFound "$2" + fi +elif [ "$1" == "editconf" ]; then + if [ "$2" == "" ]; then + argumentMissing + fi + if [ "$2" == "true" ]; then + if [ "$(whoami)" == "root" ]; then + collectFiles "/etc/sostools/git/sos-git.conf.d/" + $SOSGIT_EDITOR "/etc/sostools/git/sos-git.conf" $SOSGIT_EXITSTRING + else + echo "ERROR: You aren't root." + exit "2" + fi + elif [ "$2" == "false" ]; then + collectFiles "$HOME/.sostools/git/sos-git.conf" + $SOSGIT_EDITOR "$HOME/.sostools/git/sos-git.conf" $SOSGIT_EXITSTRING + else + echo "ERROR: Invalid boolean." + fi +elif [ "$1" == "getrepo" ]; then + if [ "$2" == "" ] || [ "$3" == "" ]; then + argumentMissing + fi + if existRepoOnline "$2" "$3"; then + translateRepo "" + cd "$SOSGIT_EXITSTRING" + formatRequestString "$2" "$3" + echo "Cloning repository" + $SOSGIT_GIT clone "$SOSGIT_EXITSTRING" "$3" + cd "$3" + echo "Cloning submodules" + $SOSGIT_GIT submodule update --init --force --recursive + else + noRepoFound "$3" + fi +elif [ "$1" == "install" ]; then + if [ "$2" == "" ]; then + argumentMissing + fi + if existRepo "$2"; then + if hasMakefile "$2"; then + translateRepo "$2" + cd "$SOSGIT_EXITSTRING" + echo "Installing repository \"$2\"" + $SOSGIT_MAKE -j$(nproc) install + exit "$?" + else + noMakefileFound "$2" + fi + else + noRepoFound "$2" + fi +elif [ "$1" == "pull" ]; then + if [ "$2" == "" ]; then + argumentMissing + fi + if existRepo "$2"; then + translateRepo "$2" + cd "$SOSGIT_EXITSTRING" + echo "Pulling new changes" + $SOSGIT_GIT pull + exit "$?" + else + noRepoFound "$2" + fi +elif [ "$1" == "push" ]; then + if [ "$2" == "" ]; then + argumentMissing + fi + if existRepo "$2"; then + translateRepo "$2" + cd "$SOSGIT_EXITSTRING" + echo "Pushing new changes" + $SOSGIT_GIT push + exit "$?" + else + noRepoFound "$2" + fi +elif [ "$1" == "remoteauth" ]; then + if [ "$2" == "" ] || [ "$3" == "" ]; then + argumentMissing + fi + if existRepo "$3"; then + translateRepo "$3" + cd "$SOSGIT_EXITSTRING" + echo "You are now required to enter your StarOpenGit credentials. If you are using 2FA, use a Application Token as a password." + if [ "$4" == "" ]; then + echo -n "Enter username: " + read -r "SOSGIT_USERNAME" + else + export "SOSGIT_USERNAME=$4" + fi + echo -n "Enter password: " + read -r -s "SOSGIT_PASSWD" + echo "********" + git remote remove origin + formatRemoteRequestString "$SOSGIT_USERNAME" "$SOSGIT_PASSWD" "$2" "$3" + git remote add origin "$SOSGIT_EXITSTRING" + echo -e "\nClearing variables containing password" + export "SOSGIT_PASSWD=" + export "SOSGIT_EXITSTRING=" + echo "Updated remote for repository \"$3\"" + else + noRepoFound "$3" + fi +elif [ "$1" == "selfupdate" ]; then + echo "Updating sos-git to \"$0\"" + $SOSGIT_CURL "-sSLqq" "$SOSGIT_UPDATEURL" -O "$0" +elif [ "$1" == "list" ]; then + translateRepo "" + $SOSGIT_LS -A -h "$SOSGIT_EXITSTRING" +elif [ "$1" == "remrepo" ]; then + if [ "$2" == "" ]; then + argumentMissing + fi + if existRepo "$2"; then + translateRepo "$2" + echo "Removing repository \"$2\"" + rm -rf "$SOSGIT_EXITSTRING" + else + noRepoFound "$2" + fi +elif [ "$1" == "upstream" ]; then + if [ "$2" == "" ] || [ "$3" == "" ]; then + argumentMissing + fi + if existRepo "$2"; then + translateRepo "$2" + cd "$SOSGIT_EXITSTRING" + if existBranch "$2" "$3"; then + echo "Pushing new changes and making branch \"$3\" upstream." + git push "--set-upstream" "origin" "$3" + else + noBranchFound "$2" "$3" + fi + else + noRepoFound "$2" + fi +elif [ "$1" == "branch" ]; then + if [ "$2" == "" ]; then + argumentMissing + fi + if existRepo "$2"; then + translateRepo "$2" + cd "$SOSGIT_EXITSTRING" + git branch + else + noRepoFound "$2" + fi +else + argumentMissing +fi