From 5cb2c1d8194bb540c7668f3a7fc0e60f12eb06d4 Mon Sep 17 00:00:00 2001 From: Mark Ridgwell Date: Sat, 11 Jul 2026 13:44:49 +0100 Subject: [PATCH] fix: warn instead of dying on per-repo errors in git/fetch and git/switchtomain A failure in a single repo (unreachable origin, failed checkout, etc.) previously called die() and aborted the whole loop via the piped subshell, leaving every later repo unprocessed. Now such failures are warned about and that repo is skipped so the rest still get fetched and/or switched. --- CHANGELOG.md | 1 + git/fetch | 30 +++++++++++++++++++----------- git/switchtomain | 25 +++++++++++++++++++------ 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c89b137..3cdea947 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ Please ADD ALL Changes to the UNRELEASED SECTION and not a specific release - check: use mapfile array for file collection to handle filenames containing spaces correctly - check: guard against empty file list before checking — prevents false-positive success when no scripts are found - git/ignore-changelog: skip push hooks when pushing to target repositories +- git/fetch and git/switchtomain now warn and skip a repo on error instead of aborting the whole run, so remaining repos still get processed. ### Changed - Replace raw echo with standard output helpers (die/info/success) in github/cancel-workflows - Replace raw echo with standard output helpers (die/info/success) in git/update-repos-personal diff --git a/git/fetch b/git/fetch index e5b4b557..169f5715 100755 --- a/git/fetch +++ b/git/fetch @@ -9,6 +9,10 @@ success() { printf '\n\033[32m✓\033[0m %s\n' "$*" } +warn() { + printf '\n\033[33m⚠\033[0m %s\n' "$*" >&2 +} + info() { printf '\n\033[32m→\033[0m %s\n' "$*" } @@ -49,18 +53,18 @@ do else git -C "$CURRENT_DIR" config --local --unset core.hookspath 2>/dev/null || true fi - git -C "$CURRENT_DIR" remote get-url origin 2>&1 || die "Could not determine origin for ${CURRENT_DIR}" - git -C "$CURRENT_DIR" fetch --prune --prune-tags 2>&1 || die "Could not fetch ${CURRENT_DIR}" - git -C "$CURRENT_DIR" remote update origin --prune 2>&1 || die "Could not update remote refs for ${CURRENT_DIR}" + git -C "$CURRENT_DIR" remote get-url origin 2>&1 || { warn "Could not determine origin for ${CURRENT_DIR}"; continue; } + git -C "$CURRENT_DIR" fetch --prune --prune-tags 2>&1 || { warn "Could not fetch ${CURRENT_DIR}"; continue; } + git -C "$CURRENT_DIR" remote update origin --prune 2>&1 || { warn "Could not update remote refs for ${CURRENT_DIR}"; continue; } - REPO_STATUS=$(git -C "$CURRENT_DIR" status --porcelain 2>&1) || die "Could not read status for ${CURRENT_DIR}" + REPO_STATUS=$(git -C "$CURRENT_DIR" status --porcelain 2>&1) || { warn "Could not read status for ${CURRENT_DIR}"; continue; } if [ -z "$REPO_STATUS" ] then if git -C "$CURRENT_DIR" rev-parse --verify '@{upstream}' > /dev/null 2>&1 then info "No pending changes in working folder. Rebasing..." - git -C "$CURRENT_DIR" rebase 2>&1 || die "Could not rebase ${CURRENT_DIR}" + git -C "$CURRENT_DIR" rebase 2>&1 || warn "Could not rebase ${CURRENT_DIR}" git -C "$CURRENT_DIR" rebase --abort > /dev/null 2>&1 else info "No valid upstream for current branch. Skipping rebase." @@ -71,7 +75,7 @@ do if [ "$SWITCH_TO_MAIN" = "1" ] then - CURRENT_BRANCH=$(git -C "$CURRENT_DIR" rev-parse --abbrev-ref HEAD 2>&1) || die "Could not determine current branch for ${CURRENT_DIR}" + CURRENT_BRANCH=$(git -C "$CURRENT_DIR" rev-parse --abbrev-ref HEAD 2>&1) || { warn "Could not determine current branch for ${CURRENT_DIR}"; continue; } DEFAULT_BRANCH=$(git -C "$CURRENT_DIR" symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's|refs/remotes/origin/||') if [ -z "$DEFAULT_BRANCH" ] @@ -84,13 +88,17 @@ do if [ -z "$REPO_STATUS" ] then info "Switching to ${DEFAULT_BRANCH} branch..." - git -C "$CURRENT_DIR" checkout "$DEFAULT_BRANCH" 2>&1 || die "Could not switch to ${DEFAULT_BRANCH} in ${CURRENT_DIR}" - if git -C "$CURRENT_DIR" rev-parse --verify '@{upstream}' > /dev/null 2>&1 + if git -C "$CURRENT_DIR" checkout "$DEFAULT_BRANCH" 2>&1 then - git -C "$CURRENT_DIR" rebase 2>&1 || die "Could not rebase ${DEFAULT_BRANCH} in ${CURRENT_DIR}" - git -C "$CURRENT_DIR" rebase --abort > /dev/null 2>&1 + if git -C "$CURRENT_DIR" rev-parse --verify '@{upstream}' > /dev/null 2>&1 + then + git -C "$CURRENT_DIR" rebase 2>&1 || warn "Could not rebase ${DEFAULT_BRANCH} in ${CURRENT_DIR}" + git -C "$CURRENT_DIR" rebase --abort > /dev/null 2>&1 + else + info "No valid upstream for ${DEFAULT_BRANCH}. Skipping rebase." + fi else - info "No valid upstream for ${DEFAULT_BRANCH}. Skipping rebase." + warn "Could not switch to ${DEFAULT_BRANCH} in ${CURRENT_DIR}" fi else info "Pending changes in working folder. Skipping switch to ${DEFAULT_BRANCH}." diff --git a/git/switchtomain b/git/switchtomain index d1a30c9c..60b4549a 100755 --- a/git/switchtomain +++ b/git/switchtomain @@ -9,6 +9,10 @@ success() { printf '\n\033[32m✓\033[0m %s\n' "$*" } +warn() { + printf '\n\033[33m⚠\033[0m %s\n' "$*" >&2 +} + info() { printf '\n\033[32m→\033[0m %s\n' "$*" } @@ -18,14 +22,23 @@ do info "$line" CURRENT_DIR=$line - DEFAULT_BRANCH=$(git -C "$CURRENT_DIR" remote show origin | sed -n '/HEAD branch/s/.*: //p') info "Retrieving $CURRENT_DIR..." - git -C "$CURRENT_DIR" remote get-url origin 2>&1 - git -C "$CURRENT_DIR" fetch origin 2>&1 + git -C "$CURRENT_DIR" remote get-url origin 2>&1 || { warn "Could not determine origin for ${CURRENT_DIR}"; continue; } + git -C "$CURRENT_DIR" fetch origin 2>&1 || { warn "Could not fetch ${CURRENT_DIR}"; continue; } + + DEFAULT_BRANCH=$(git -C "$CURRENT_DIR" remote show origin | sed -n '/HEAD branch/s/.*: //p') - [ -z "$DEFAULT_BRANCH" ] && info "No default branch found" - [ -n "$DEFAULT_BRANCH" ] && git -C "$CURRENT_DIR" checkout "$DEFAULT_BRANCH" 2>&1 - [ -n "$DEFAULT_BRANCH" ] && git -C "$CURRENT_DIR" fetch origin 2>&1 + if [ -z "$DEFAULT_BRANCH" ] + then + info "No default branch found" + else + if git -C "$CURRENT_DIR" checkout "$DEFAULT_BRANCH" 2>&1 + then + git -C "$CURRENT_DIR" fetch origin 2>&1 || warn "Could not fetch ${DEFAULT_BRANCH} in ${CURRENT_DIR}" + else + warn "Could not switch to ${DEFAULT_BRANCH} in ${CURRENT_DIR}" + fi + fi success "$CURRENT_DIR" done