#!/bin/bash # Enhanced sync script for collaborative work with both Hugging Face and Azure DevOps # Usage: ./sync_repos_enhanced.sh [commit_message] [--force-azure] set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Parse arguments COMMIT_MSG=${1:-"Auto-sync: $(date '+%Y-%m-%d %H:%M:%S')"} FORCE_AZURE=false if [[ "$2" == "--force-azure" ]]; then FORCE_AZURE=true fi echo -e "${GREEN}🔄 Starting enhanced repository sync...${NC}" echo -e "${BLUE}📝 Commit message: ${COMMIT_MSG}${NC}" echo -e "${BLUE}🔧 Force Azure push: ${FORCE_AZURE}${NC}" # Check if we're in a git repository if ! git rev-parse --git-dir > /dev/null 2>&1; then echo -e "${RED}❌ Error: Not in a git repository${NC}" exit 1 fi # Function to check for conflicts check_conflicts() { if git ls-files -u | grep -q .; then echo -e "${RED}❌ Merge conflicts detected! Please resolve them manually.${NC}" echo -e "${YELLOW} Run: git status to see conflicted files${NC}" echo -e "${YELLOW} After resolving: git add . && git commit${NC}" exit 1 fi } # Function to fetch and merge from remote fetch_and_merge() { local remote_name=$1 local branch_name=${2:-main} echo -e "${GREEN}📥 Fetching from ${remote_name}...${NC}" git fetch $remote_name # Check if there are new commits on the remote local local_commit=$(git rev-parse HEAD) local remote_commit=$(git rev-parse $remote_name/$branch_name) if [ "$local_commit" != "$remote_commit" ]; then echo -e "${YELLOW}⚠️ New commits detected on ${remote_name}. Attempting merge...${NC}" # Try to merge if git merge $remote_name/$branch_name --no-edit; then echo -e "${GREEN}✅ Successfully merged changes from ${remote_name}${NC}" else echo -e "${RED}❌ Merge failed. Please resolve conflicts manually.${NC}" echo -e "${YELLOW} Run: git status to see conflicted files${NC}" echo -e "${YELLOW} After resolving: git add . && git commit${NC}" exit 1 fi else echo -e "${GREEN}✅ ${remote_name} is up to date${NC}" fi } # Check for uncommitted changes if ! git diff-index --quiet HEAD --; then echo -e "${YELLOW}⚠️ You have uncommitted changes. Please commit them first.${NC}" echo -e "${YELLOW} Run: git add . && git commit -m \"your message\"${NC}" exit 1 fi # Fetch and merge from both remotes before pushing echo -e "${GREEN}🔄 Syncing with remote repositories...${NC}" fetch_and_merge "origin" "main" fetch_and_merge "azure" "main" # Check for conflicts after merges check_conflicts # Add all changes and commit if there are any if ! git diff-index --quiet HEAD --; then echo -e "${GREEN}📦 Adding and committing changes...${NC}" git add . git commit -m "$COMMIT_MSG" fi # Push to Hugging Face (origin) echo -e "${GREEN}🚀 Pushing to Hugging Face (origin)...${NC}" if git push origin main; then echo -e "${GREEN}✅ Successfully pushed to Hugging Face${NC}" else echo -e "${RED}❌ Failed to push to Hugging Face${NC}" exit 1 fi # Push to Azure DevOps (azure) echo -e "${GREEN}🚀 Pushing to Azure DevOps (azure)...${NC}" if [ "$FORCE_AZURE" = true ]; then echo -e "${YELLOW}⚠️ Force pushing to Azure DevOps...${NC}" if git push azure main --force; then echo -e "${GREEN}✅ Successfully force pushed to Azure DevOps${NC}" else echo -e "${RED}❌ Failed to force push to Azure DevOps${NC}" exit 1 fi else if git push azure main; then echo -e "${GREEN}✅ Successfully pushed to Azure DevOps${NC}" else echo -e "${YELLOW}⚠️ Regular push failed. You can:${NC}" echo -e "${YELLOW} 1. Run: ./sync_repos_enhanced.sh \"$COMMIT_MSG\" --force-azure${NC}" echo -e "${YELLOW} 2. Or manually resolve conflicts and push${NC}" echo -e "${YELLOW} 3. Or fetch and merge from Azure first: git fetch azure && git merge azure/main${NC}" exit 1 fi fi echo -e "${GREEN}🎉 Enhanced repository sync completed successfully!${NC}" echo -e "${GREEN}📊 Remotes configured:${NC}" git remote -v echo -e "${BLUE}💡 Tips for collaborative work:${NC}" echo -e "${BLUE} • Always run this script before starting new work${NC}" echo -e "${BLUE} • Use --force-azure only when you're sure you want to overwrite Azure changes${NC}" echo -e "${BLUE} • Communicate with your team about which repository to use as primary${NC}"