Script สำหรับวนลูปทุกโฟลเดอร์ แล้วทำการ เปลี่ยน GIT URL ให้เป็นแบบที่ฝัง personal access tokens ใหม่ (glpat-...) เข้าไปให้โดยอัตโนมัติ และฉลาดพอที่จะ ดึงชื่อโปรเจกต์เดิม (เช่น phunsanit/snippets) , host (github, gitlab) , username (pitt.p) มาใช้ต่อ ทำให้คุณไม่ต้องมากรอกชื่อโปรเจกต์ใหม่ทีละอัน ที่ละ repository
- ไปที่ root ของ Multi-repo เช่น
cd /Users/Shared/Gits/phunsanit - สร้างไฟล์
touch git-update-personal-access-tokens-all.sh - set permission
sudo chmod +x git-update-personal-access-tokens-all.sh - ใส่เนื้อหา
git-update-personal-access-tokens-all.sh
JavaScript
SQL
#!/bin/bash
# Clear screen
clear
echo "======================================================="
echo " GIT TOKEN CHECKER & UPDATER"
echo "======================================================="
# Define your new personal access token
NEW_TOKEN=glpat-xxxxxxxxxxxxx
# Loop through all subdirectories
for dir in */; do
# Change directory to the subdirectory
cd "$dir"
# Get the current remote URL
CURRENT_URL=$(git remote -v | grep fetch | awk '{print $2}')
# Extract the host, username, and project name from the current URL
HOST=$(echo "$CURRENT_URL" | awk -F[/:] '{print $3}')
USERNAME=$(echo "$CURRENT_URL" | awk -F[/:] '{print $4}')
PROJECT=$(echo "$CURRENT_URL" | awk -F[/:] '{print $5}')
# Construct the new URL with the new token
NEW_URL="https://$NEW_TOKEN@$HOST/$USERNAME/$PROJECT.git"
# Set the new remote URL
git remote set-url origin "$NEW_URL"
# Change back to the parent directory
cd ..
done
echo "======================================================="
echo "All repositories have been updated with the new personal access token."
echo "======================================================="
บทสรุป
บทความนี้ได้แนะนำวิธีการอัปเดต personal access tokens ในทุก subfolder ของ repository ด้วยการใช้ script bash ซึ่งจะวนลูปทุกโฟลเดอร์ ดึงข้อมูลจาก URL ปัจจุบัน และแทนที่ด้วย token ใหม่ ทำให้คุณสามารถอัปเดต token ได้อย่างอัตโนมัติและสะดวกสบาย ไม่ต้องกรอกข้อมูลใหม่ทีละ repository ทีละอัน