From 4ef4fbaa24617f770aa167ee0d05ab289f5384a5 Mon Sep 17 00:00:00 2001
From: Andres Montano <andres.montano@tecnalia.com>
Date: Fri, 3 Jan 2025 12:54:05 +0100
Subject: [PATCH] Fix script now uses find properly

---
 ...dev_remotes.bash => set_dotdev_remotes.sh} | 44 +++++++++++++++----
 1 file changed, 35 insertions(+), 9 deletions(-)
 rename scripts/{set_dotdev_remotes.bash => set_dotdev_remotes.sh} (55%)
 mode change 100644 => 100755

diff --git a/scripts/set_dotdev_remotes.bash b/scripts/set_dotdev_remotes.sh
old mode 100644
new mode 100755
similarity index 55%
rename from scripts/set_dotdev_remotes.bash
rename to scripts/set_dotdev_remotes.sh
index 9235f01..9c3c9e2
--- a/scripts/set_dotdev_remotes.bash
+++ b/scripts/set_dotdev_remotes.sh
@@ -1,30 +1,46 @@
 #!/usr/bin/env bash
 #
 # Updates Git remote URLs by replacing 'tecnalia.com' with 'tecnalia.dev'
-# in all Git repositories under the current directory
+# recursively in all Git repositories under the current directory
 #
 
-set -euo pipefail  # Makes the script safer
+set -e pipefail  # Keep error handling but remove -u flag
 
 # Color definitions for better readability
 readonly GREEN='\033[0;32m'
 readonly YELLOW='\033[1;33m'
+readonly BLUE='\033[0;34m'
 readonly NC='\033[0m'  # No Color
 
 usage() {
-    echo "Usage: $(basename "$0") [-d|--dry-run]"
-    echo "  -d, --dry-run    Show changes without applying them"
+    echo "Usage: $(basename "$0") [-d|--dry-run] [-m|--max-depth N]"
+    echo "Options:"
+    echo "  -d, --dry-run     Show changes without applying them"
+    echo "  -m, --max-depth N Maximum depth for recursive search (default: unlimited)"
+    echo "                    Use 1 for current directory only"
+    echo "  -h, --help        Show this help message"
     exit 1
 }
 
 # Improved argument processing
 dry_run=0
+max_depth=""
+
 while [[ $# -gt 0 ]]; do
     case $1 in
         -d|--dry-run)
             dry_run=1
             shift
             ;;
+        -m|--max-depth)
+            if [[ -n "${2:-}" && $2 =~ ^[0-9]+$ ]]; then
+                max_depth="-maxdepth $2"
+                shift 2
+            else
+                echo "Error: --max-depth requires a number"
+                usage
+            fi
+            ;;
         -h|--help)
             usage
             ;;
@@ -40,7 +56,11 @@ process_repo() {
     local repo_path=$1
     local remote=$2
     local remote_url
-    remote_url=$(git -C "${repo_path}" remote get-url "${remote}")
+
+    if ! remote_url=$(git -C "${repo_path}" remote get-url "${remote}" 2>/dev/null); then
+        echo "  Warning: Could not get URL for remote ${remote}" >&2
+        return 1
+    fi
 
     if [[ ${remote_url} == *"tecnalia.com"* ]]; then
         local new_url=${remote_url//tecnalia.com/tecnalia.dev}
@@ -64,24 +84,30 @@ process_repo() {
 main() {
     local count=0
     local errors=0
+    local repos_found=0
+
+    echo -e "${BLUE}Starting recursive search for Git repositories...${NC}"
 
     # Use process substitution for better handling of spaces in names
-    while IFS= read -r -d '' git_folder; do
+    while IFS= read -r -d '' git_folder || [ -n "$git_folder" ]; do
+        ((repos_found++))
         repo_path=$(dirname "$(readlink -f "${git_folder}")")
+        echo -e "${BLUE}Processing repository: ${repo_path}${NC}"
 
-        while IFS= read -r remote; do
+        while IFS= read -r remote || [ -n "$remote" ]; do
             if process_repo "${repo_path}" "${remote}"; then
                 ((count++))
             else
                 ((errors++))
             fi
-        done < <(git -C "${repo_path}" remote)
+        done < <(git -C "${repo_path}" remote 2>/dev/null || echo "")
 
-    done < <(find . -name '.git' -type d -print0)
+    done < <(find . ${max_depth:-} -name '.git' -type d -print0 2>/dev/null || echo "")
 
     # Final summary
     echo
     echo "Execution summary:"
+    echo "  Git repositories found: $repos_found"
     echo "  Repositories processed: $count"
     [[ $errors -gt 0 ]] && echo "  Errors found: $errors"
     [[ $dry_run -eq 1 ]] && echo "  (Dry-run mode: no changes were made)"
-- 
GitLab