diff --git a/auto_git_update.sh b/auto_git_update.sh index 5657d5f..5aaec6a 100755 --- a/auto_git_update.sh +++ b/auto_git_update.sh @@ -245,6 +245,7 @@ update_repo() { local repo_dir="$1" local repo_number="$2" + # Change to the repository directory. cd "$repo_dir" || { log_msg "Repo #$repo_number: Unable to cd to $repo_dir"; return 1; } log_msg "Processing repository #$repo_number at $repo_dir" @@ -258,7 +259,7 @@ update_repo() { # If the repository has no commits, create an initial empty commit. if ! git rev-parse HEAD &>/dev/null; then log_msg "Repo #$repo_number: No commits found. Creating an initial empty commit." - if ! git commit --allow-empty -m "Initial commit" ; then + if ! git commit --allow-empty -m "Initial commit"; then log_msg "Repo #$repo_number: Failed to create initial commit." echo "Repo #$repo_number: No commit" >> "$SMS_REPORT" return 1 @@ -275,6 +276,7 @@ update_repo() { repo_name=$(basename "$repo_dir") log_msg "Repo #$repo_number: Attempting to create GitHub remote for $repo_name." create_github_repo "$repo_name" "$repo_dir" "$repo_number" + # Recheck the remote after attempting creation. if check_github_remote; then remote_available=true log_msg "Repo #$repo_number: GitHub remote now exists." @@ -301,6 +303,8 @@ update_repo() { # Stage all changes. log_msg "Repo #$repo_number: Staging all changes." git add -A + + # Commit changes; override PGP signing for automation. if git -c commit.gpgSign=false commit -m "Automated update"; then log_msg "Repo #$repo_number: Local commit succeeded." else @@ -309,7 +313,7 @@ update_repo() { return 0 fi - # Pull with rebase if a remote exists and WiFi is available. + # Pull with rebase if remote exists and WiFi is available. if [ "$remote_available" = true ] && check_wifi; then log_msg "Repo #$repo_number: Attempting pull --rebase on branch 'main'." if timeout 60s git pull --rebase --no-edit origin main; then @@ -328,22 +332,30 @@ update_repo() { # Push if remote exists and WiFi is available. if [ "$remote_available" = true ] && check_wifi; then log_msg "Repo #$repo_number: Attempting push on branch 'main'." + # Try the initial push. if timeout 60s git push origin main; then log_msg "Repo #$repo_number: Push succeeded." echo "Repo #$repo_number: P" >> "$SMS_REPORT" echo "P" else - log_msg "Repo #$repo_number: Push failed. Attempting fallback remote add and push." - # Fallback: add remote if missing and try push again. + # Capture error output. + local push_output push_status + push_output=$(timeout 60s git push origin main 2>&1) || push_status=$? + log_msg "Repo #$repo_number: Push failed with error: $push_output" + echo "Repo #$repo_number: PF" >> "$SMS_REPORT" + # Fallback: Force-set the remote URL using your GitHub username. local repo_name repo_name=$(basename "$repo_dir") - git remote add origin "git@github.com:kleinpanic/$repo_name.git" 2>/dev/null || true - if timeout 60s git push origin main; then + log_msg "Repo #$repo_number: Attempting fallback: setting remote URL to git@github.com:kleinpanic/$repo_name.git" + git remote set-url origin "git@github.com:kleinpanic/$repo_name.git" 2>&1 || true + local fallback_output fallback_status + fallback_output=$(timeout 60s git push origin main 2>&1) || fallback_status=$? + if [ "${fallback_status:-0}" -eq 0 ]; then log_msg "Repo #$repo_number: Fallback push succeeded." echo "Repo #$repo_number: P" >> "$SMS_REPORT" echo "P" else - log_msg "Repo #$repo_number: Fallback push failed." + log_msg "Repo #$repo_number: Fallback push failed with error: $fallback_output" echo "Repo #$repo_number: PF" >> "$SMS_REPORT" echo "PF" return 1