From dd1de371075427126234079ab08edc4ef148c66a Mon Sep 17 00:00:00 2001 From: Andres Montano <andres.montano@tecnalia.com> Date: Fri, 3 Jan 2025 12:09:54 +0100 Subject: [PATCH] Add script to replace tecnalia.com by tecnalia.dev at git repos --- scripts/set_dotdev_remotes.bash | 90 +++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 scripts/set_dotdev_remotes.bash diff --git a/scripts/set_dotdev_remotes.bash b/scripts/set_dotdev_remotes.bash new file mode 100644 index 0000000..9235f01 --- /dev/null +++ b/scripts/set_dotdev_remotes.bash @@ -0,0 +1,90 @@ +#!/usr/bin/env bash +# +# Updates Git remote URLs by replacing 'tecnalia.com' with 'tecnalia.dev' +# in all Git repositories under the current directory +# + +set -euo pipefail # Makes the script safer + +# Color definitions for better readability +readonly GREEN='\033[0;32m' +readonly YELLOW='\033[1;33m' +readonly NC='\033[0m' # No Color + +usage() { + echo "Usage: $(basename "$0") [-d|--dry-run]" + echo " -d, --dry-run Show changes without applying them" + exit 1 +} + +# Improved argument processing +dry_run=0 +while [[ $# -gt 0 ]]; do + case $1 in + -d|--dry-run) + dry_run=1 + shift + ;; + -h|--help) + usage + ;; + *) + echo "Error: Unknown option $1" + usage + ;; + esac +done + +# Function to process a repository +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} == *"tecnalia.com"* ]]; then + local new_url=${remote_url//tecnalia.com/tecnalia.dev} + echo -e "${YELLOW}Found deprecated remote URL${NC}" + echo " Repository: ${repo_path}" + echo " Remote: ${remote}" + echo " Current URL: ${remote_url}" + echo " New URL: ${new_url}" + + if [[ ${dry_run} -eq 0 ]]; then + if git -C "${repo_path}" remote set-url "${remote}" "${new_url}"; then + echo -e " ${GREEN}Replacement completed!${NC}" + else + echo " Error updating remote URL" >&2 + return 1 + fi + fi + fi +} + +main() { + local count=0 + local errors=0 + + # Use process substitution for better handling of spaces in names + while IFS= read -r -d '' git_folder; do + repo_path=$(dirname "$(readlink -f "${git_folder}")") + + while IFS= read -r remote; do + if process_repo "${repo_path}" "${remote}"; then + ((count++)) + else + ((errors++)) + fi + done < <(git -C "${repo_path}" remote) + + done < <(find . -name '.git' -type d -print0) + + # Final summary + echo + echo "Execution summary:" + echo " Repositories processed: $count" + [[ $errors -gt 0 ]] && echo " Errors found: $errors" + [[ $dry_run -eq 1 ]] && echo " (Dry-run mode: no changes were made)" +} + +main \ No newline at end of file -- GitLab