File size: 4,641 Bytes
491bebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/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}"