From b6c17772c2e7e7c77c2a26f734fd79f01dacb2cf Mon Sep 17 00:00:00 2001 From: Staubgeborener Date: Sun, 21 Jan 2024 10:17:12 +0100 Subject: [PATCH] check for new $branch_name and $repository in .env This means that the .git/config does not have to be changed manually every time, but the script now checks whether changes have been made and adjusts them automatically. Branches are now added automatically if they do not yet exist. Also beautify the code a bit: - add some comments - removed tput as this is not used i.e. in the mainsail ui and leads to confusing/unnecessary output --- script.sh | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/script.sh b/script.sh index f9455dd..0dc6bb3 100755 --- a/script.sh +++ b/script.sh @@ -11,7 +11,7 @@ backup_path="$HOME/$backup_folder" # Check for updates [ $(git -C "$parent_path" rev-parse HEAD) = $(git -C "$parent_path" ls-remote $(git -C "$parent_path" rev-parse --abbrev-ref @{u} | \ -sed 's/\// /g') | cut -f1) ] && echo -e "Klipper-backup is up to date\n" || echo -e "Klipper-backup is $(tput setaf 1)not$(tput sgr0) up to date, consider making a $(tput setaf 1)git pull$(tput sgr0) to update\n" +sed 's/\// /g') | cut -f1) ] && echo -e "Klipper-backup is up to date\n" || echo -e "NEW klipper-backup version available!\n" # Check if backup folder exists, create one if it does not if [ ! -d "$backup_path" ]; then @@ -68,10 +68,10 @@ if [ ! -d ".git" ]; then defaultBranch = "$branch_name"" >> .git/config #Add desired branch name to config before init git init -# Check if the current checked out branch matches the branch name given in .env if not then redefine branch_name to use the checked out branch and warn the user of the mismatch +# Check if the current checked out branch matches the branch name given in .env if not update to new branch elif [[ $(git symbolic-ref --short -q HEAD) != "$branch_name" ]]; then - branch_name=$(git symbolic-ref --short -q HEAD) - echo "$(tput setaf 1)The branch name defined in .env does not match the branch that is currently checked out, to remove this warning update branch_name in .env to $branch_name$(tput sgr0)" + echo "New branch in .env detected, rename $(git symbolic-ref --short -q HEAD) to $branch_name branch" + git branch -m "$branch_name" fi [[ "$commit_username" != "" ]] && git config user.name "$commit_username" || git config user.name "$(whoami)" @@ -82,6 +82,11 @@ if [ -z "$(git remote get-url origin 2>/dev/null)" ]; then git remote add origin https://"$github_token"@github.com/"$github_username"/"$github_repository".git fi +# Check if remote origin changed and update when it is +if [[ "$github_repository" != $(git remote get-url origin | sed 's/https:\/\/.*@github.com\///' | sed 's/\.git$//' | xargs basename) ]]; then + git remote set-url origin https://"$github_token"@github.com/"$github_username"/"$github_repository".git +fi + git config advice.skippedCherryPicks false git add . git commit -m "$commit_message" @@ -93,5 +98,4 @@ if [[ $(git rev-parse HEAD) != $(git ls-remote $(git rev-parse --abbrev-ref @{u} fi # Remove files except .git folder after backup so that any file deletions can be logged on next backup -find "$backup_path" -maxdepth 1 -mindepth 1 ! -name '.git' -exec rm -rf {} \; - +find "$backup_path" -maxdepth 1 -mindepth 1 ! -name '.git' -exec rm -rf {} \; \ No newline at end of file