Skip to content
Snippets Groups Projects
Commit 4ef4fbaa authored by Montaño Sarria, Andres Felipe's avatar Montaño Sarria, Andres Felipe
Browse files

Fix script now uses find properly

parent 9b18f5fa
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env bash #!/usr/bin/env bash
# #
# Updates Git remote URLs by replacing 'tecnalia.com' with 'tecnalia.dev' # 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 # Color definitions for better readability
readonly GREEN='\033[0;32m' readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m' readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color readonly NC='\033[0m' # No Color
usage() { usage() {
echo "Usage: $(basename "$0") [-d|--dry-run]" echo "Usage: $(basename "$0") [-d|--dry-run] [-m|--max-depth N]"
echo " -d, --dry-run Show changes without applying them" 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 exit 1
} }
# Improved argument processing # Improved argument processing
dry_run=0 dry_run=0
max_depth=""
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
-d|--dry-run) -d|--dry-run)
dry_run=1 dry_run=1
shift 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) -h|--help)
usage usage
;; ;;
...@@ -40,7 +56,11 @@ process_repo() { ...@@ -40,7 +56,11 @@ process_repo() {
local repo_path=$1 local repo_path=$1
local remote=$2 local remote=$2
local remote_url 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 if [[ ${remote_url} == *"tecnalia.com"* ]]; then
local new_url=${remote_url//tecnalia.com/tecnalia.dev} local new_url=${remote_url//tecnalia.com/tecnalia.dev}
...@@ -64,24 +84,30 @@ process_repo() { ...@@ -64,24 +84,30 @@ process_repo() {
main() { main() {
local count=0 local count=0
local errors=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 # 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}")") 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 if process_repo "${repo_path}" "${remote}"; then
((count++)) ((count++))
else else
((errors++)) ((errors++))
fi 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 # Final summary
echo echo
echo "Execution summary:" echo "Execution summary:"
echo " Git repositories found: $repos_found"
echo " Repositories processed: $count" echo " Repositories processed: $count"
[[ $errors -gt 0 ]] && echo " Errors found: $errors" [[ $errors -gt 0 ]] && echo " Errors found: $errors"
[[ $dry_run -eq 1 ]] && echo " (Dry-run mode: no changes were made)" [[ $dry_run -eq 1 ]] && echo " (Dry-run mode: no changes were made)"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment