From 25c132b9ff69bfee0b573f8f7226a7b002a0a658 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Wed, 6 May 2026 10:17:52 +0200 Subject: fix: harden error handling and fix silent failures - guard all cd calls with error messages - check commit and push exit codes; fail loudly on hook rejection - warn on missing system path entries (were silently skipped) - clear only when stdout is a terminal - simplify beginswith to single [[ ]] expression - remove redundant touch before lastupdate write - quote $NC in echo -e calls --- dot-backup.sh | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/dot-backup.sh b/dot-backup.sh index 7db9237..369ae3b 100755 --- a/dot-backup.sh +++ b/dot-backup.sh @@ -169,20 +169,10 @@ if [[ -f "$DOTFILES_LIST" ]]; then fi # helper function to check if a string ($2) begins with another string ($1) -beginswith() { - case $2 in - "$1"*) - true - ;; - *) - false - ;; - esac; -} +beginswith() { [[ "$2" == "$1"* ]]; } lastupdate() { D=$(date) - [ ! -f "${DEFAULT_OUTPUT_DIR}/lastupdate" ] && touch "${DEFAULT_OUTPUT_DIR}/lastupdate" echo "Last update: $D" > "${DEFAULT_OUTPUT_DIR}/lastupdate" } @@ -274,7 +264,7 @@ do_restore() { # ── MAIN ────────────────────────────────────────────────────────────────────── -if [[ "$QUIET" == false ]]; then clear; fi +if [[ "$QUIET" == false && -t 1 ]]; then clear; fi if [[ "$FIRST_RUN" == true ]]; then echo -e "${GREEN}First run — config directories created:${NC}" @@ -300,7 +290,7 @@ if [[ -d "$DEFAULT_OUTPUT_DIR" ]]; then echo -e "\t${BLUE}$DEFAULT_OUTPUT_DIR ${GREEN} ${NC}" [ -d "$DEFAULT_OUTPUT_DIR/home" ] || mkdir -p "$DEFAULT_OUTPUT_DIR/home" [ -d "$DEFAULT_OUTPUT_DIR/system" ] || mkdir -p "$DEFAULT_OUTPUT_DIR/system" - cd "$DEFAULT_OUTPUT_DIR" + cd "$DEFAULT_OUTPUT_DIR" || { echo -e "${RED}Cannot cd to $DEFAULT_OUTPUT_DIR${NC}"; exit 1; } if git rev-parse --is-inside-work-tree &>/dev/null; then echo -e "${GREEN}Git Repository already initialized.${NC}" else @@ -314,7 +304,7 @@ else echo -e "${YELLOW}creating our backup directories${NC}" mkdir -p "$DEFAULT_OUTPUT_DIR"/{home,system} echo -e "${YELLOW}Initializing Git Repository.${NC}" - cd "$DEFAULT_OUTPUT_DIR" + cd "$DEFAULT_OUTPUT_DIR" || { echo -e "${RED}Cannot cd to $DEFAULT_OUTPUT_DIR${NC}"; exit 1; } git init git add . fi @@ -322,7 +312,7 @@ fi # Ensure remote is registered if GIT_REMOTE is configured if [[ -n "$GIT_REMOTE" && "$DRY_RUN" == false ]]; then - cd "$DEFAULT_OUTPUT_DIR" + cd "$DEFAULT_OUTPUT_DIR" || { echo -e "${RED}Cannot cd to $DEFAULT_OUTPUT_DIR${NC}"; exit 1; } if git remote get-url origin &>/dev/null; then current=$(git remote get-url origin) if [[ "$current" != "$GIT_REMOTE" ]]; then @@ -339,9 +329,8 @@ fi if [[ "$DRY_RUN" == false ]]; then lastupdate fi -echo -e $NC +echo -e "$NC" -cd "$WORKDIR" # we iterate all dotfiles in the list for i in "${DOTFILES[@]}"; do # if it's a file in my home @@ -360,6 +349,8 @@ for i in "${DOTFILES[@]}"; do elif [[ -d "$i" ]]; then echo -e "${BLUE}$i${NC}" do_rsync "$i" "${DEFAULT_OUTPUT_DIR}/system/${i#/}" true + else + echo -e "\n${RED}NOT FOUND: ${i}${NC}\n" fi else echo -e "\n${RED}NOT FOUND: ${i}${NC}\n" @@ -372,13 +363,16 @@ if [[ "$DRY_RUN" == true ]]; then fi echo -e "\n${GREEN}Adding all copied files to git.${NC}" -cd "$DEFAULT_OUTPUT_DIR" +cd "$DEFAULT_OUTPUT_DIR" || { echo -e "${RED}Cannot cd to $DEFAULT_OUTPUT_DIR${NC}"; exit 1; } git add . if git diff --cached --quiet; then echo -e "${YELLOW}Nothing new to commit.${NC}" else - git commit -m "backup: $(date '+%Y-%m-%d %H:%M:%S')" + if ! git commit -m "backup: $(date '+%Y-%m-%d %H:%M:%S')"; then + echo -e "${RED}Commit failed (hook rejected?). Files staged but not committed.${NC}" + exit 1 + fi echo -e "${GREEN}Committed.${NC}" fi @@ -389,11 +383,14 @@ if [[ "$PUSH" == true ]]; then fi branch="${GIT_BRANCH:-$(git symbolic-ref --short HEAD)}" echo -e "${GREEN}Pushing to origin/$branch...${NC}" - git push origin "$branch" + if ! git push origin "$branch"; then + echo -e "${RED}Push failed.${NC}" + exit 1 + fi echo -e "${GREEN}Pushed.${NC}" fi -echo -e $NC +echo -e "$NC" cd "$WORKDIR" if [[ "$QUIET" == true ]]; then echo "Log written to: $LOG_FILE" >&3; fi exit 0 -- cgit v1.2.3