Files
SMATEC-FRONTEND/update-iconfont.sh

119 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
set -e
echo "=== Update Iconfont - Git Push Script with Merge Main ==="
# Hàm kiểm tra lỗi và thoát
handle_error() {
echo "❌ Lỗi: $1"
# Nếu có stash, hiển thị thông báo để người dùng biết cách xử lý
if [ "$stashed" = true ]; then
echo "⚠️ Có stash được lưu, hãy kiểm tra bằng 'git stash list' và xử lý thủ công nếu cần."
fi
exit 1
}
# Kiểm tra xem có trong Git repository không
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
handle_error "Thư mục hiện tại không phải là Git repository!"
fi
# Các file cần commit
FILE1="src/components/IconFont/index.tsx"
FILE2="src/app.tsx"
# Kiểm tra file tồn tại
if [ ! -f "$FILE1" ]; then
handle_error "Không tìm thấy file $FILE1"
fi
if [ ! -f "$FILE2" ]; then
handle_error "Không tìm thấy file $FILE2"
fi
# Lấy nhánh hiện tại
current_branch=$(git branch --show-current)
if [ -z "$current_branch" ]; then
handle_error "Không thể xác định nhánh hiện tại!"
fi
echo "👉 Bạn đang ở nhánh: $current_branch"
# Hỏi nhánh chính, mặc định là 'master' nếu người dùng không nhập
read -p "Nhập tên nhánh chính (mặc định: master): " target_branch
target_branch=${target_branch:-master}
# Kiểm tra xem nhánh chính có tồn tại trên remote không
if ! git ls-remote --heads origin "$target_branch" >/dev/null 2>&1; then
handle_error "Nhánh $target_branch không tồn tại trên remote!"
fi
# Hiển thị thay đổi của 2 file
echo ""
echo "📋 Các thay đổi trong 2 file iconfont:"
echo "---"
git --no-pager diff "$FILE1" "$FILE2" 2>/dev/null || echo "(Không có thay đổi hoặc file chưa được track)"
echo "---"
# Kiểm tra có thay đổi không
if git diff --quiet "$FILE1" "$FILE2" 2>/dev/null && git diff --cached --quiet "$FILE1" "$FILE2" 2>/dev/null; then
echo "⚠️ Không có thay đổi nào trong 2 file iconfont."
exit 0
fi
# --- Bước 1: Stash nếu có thay đổi chưa commit ---
stashed=false
if [[ -n $(git status --porcelain) ]]; then
echo "💾 Có thay đổi chưa commit -> stash lại..."
git stash push -m "auto-stash-before-iconfont-$(date +%s)" || handle_error "Lỗi khi stash code!"
stashed=true
fi
# --- Bước 2: Đồng bộ code từ nhánh chính về nhánh hiện tại ---
echo "🔄 Đồng bộ code từ $target_branch về $current_branch..."
git fetch origin "$target_branch" || handle_error "Lỗi khi fetch $target_branch!"
git merge origin/"$target_branch" --no-edit || {
handle_error "Merge từ $target_branch về $current_branch bị conflict, hãy xử lý thủ công rồi chạy lại."
}
# --- Bước 3: Nếu có stash thì pop lại ---
if [ "$stashed" = true ]; then
echo "📥 Pop lại code đã stash..."
git stash pop || handle_error "Stash pop bị conflict, hãy xử lý thủ công bằng 'git stash list' và 'git stash apply'!"
fi
# --- Bước 4: Add và Commit 2 file iconfont ---
echo "📥 Đang add 2 file iconfont..."
git add "$FILE1" "$FILE2" || handle_error "Lỗi khi add files!"
read -p "Nhập commit message (mặc định: 'chore: update iconfont url'): " commit_message
commit_message=${commit_message:-"chore: update iconfont url"}
git commit -m "$commit_message" || handle_error "Lỗi khi commit!"
# --- Bước 5: Push nhánh hiện tại ---
echo "🚀 Đang push nhánh $current_branch lên remote..."
git push origin "$current_branch" || handle_error "Push nhánh $current_branch thất bại!"
# --- Bước 6: Checkout sang nhánh chính ---
echo "🔄 Chuyển sang nhánh $target_branch..."
git checkout "$target_branch" || handle_error "Checkout sang $target_branch thất bại!"
# --- Bước 7: Pull nhánh chính ---
echo "🔄 Pull code mới nhất từ remote $target_branch..."
git pull origin "$target_branch" --no-rebase || handle_error "Pull $target_branch thất bại!"
# --- Bước 8: Merge nhánh hiện tại vào nhánh chính ---
echo "🔀 Merge $current_branch vào $target_branch..."
git merge "$current_branch" --no-edit || {
handle_error "Merge từ $current_branch vào $target_branch bị conflict, hãy xử lý thủ công rồi chạy lại."
}
# --- Bước 9: Push nhánh chính ---
git push origin "$target_branch" || handle_error "Push $target_branch thất bại!"
# --- Quay lại nhánh hiện tại ---
echo "🔄 Quay lại nhánh $current_branch..."
git checkout "$current_branch" || handle_error "Checkout về $current_branch thất bại!"
echo ""
echo "✅ Hoàn tất! Đã commit 2 file iconfont và merge vào $target_branch."