TheRealAIGuy commited on
Commit
2e1a5b4
·
verified ·
1 Parent(s): b13b432

Improved sync.sh for smooth syncing

Browse files
Files changed (1) hide show
  1. sync.sh +206 -26
sync.sh CHANGED
@@ -1,35 +1,215 @@
1
  #!/bin/bash
2
- # Bidirectional sync between Hugging Face Space and GitHub
3
- set -euo pipefail
4
-
5
- echo "Fetching updates from both remotes..."
6
- git fetch --prune origin
7
- git fetch --prune github
8
-
9
- # 1. Detect divergent histories FIRST
10
- if ! git merge-base --is-ancestor origin/main github/main && \
11
- ! git merge-base --is-ancestor github/main origin/main; then
12
- echo "❌ WARNING: Divergent histories detected! Both remotes have different new commits."
13
- echo "Manual merge required."
14
- echo "HF main: $(git rev-parse origin/main)"
15
- echo "GitHub main: $(git rev-parse github/main)"
16
- exit 1
 
 
 
 
 
 
 
 
 
 
17
  fi
18
 
19
- # 2. Count commits to determine exactly who is ahead
20
- HF_AHEAD=$(git rev-list --count github/main..origin/main)
21
- GH_AHEAD=$(git rev-list --count origin/main..github/main)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- if [ "$HF_AHEAD" -gt 0 ]; then
24
- echo "HF has $HF_AHEAD new commit(s). Pushing HF → GitHub..."
25
- git push github "refs/remotes/origin/main:refs/heads/main"
 
26
 
27
- elif [ "$GH_AHEAD" -gt 0 ]; then
28
- echo "GitHub has $GH_AHEAD new commit(s). Pushing GitHub → HF..."
29
- git push origin "refs/remotes/github/main:refs/heads/main"
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  else
32
- echo " No changes detected. Both remotes are perfectly in sync."
 
33
  fi
34
 
35
- echo "Synchronization complete."
 
1
  #!/bin/bash
2
+
3
+ # ==============================================================================
4
+ # PAYGORN GIT COMMAND CENTER
5
+ # ==============================================================================
6
+
7
+ # Colors for UI
8
+ GREEN='\033[0;32m'
9
+ BLUE='\033[0;34m'
10
+ YELLOW='\033[1;33m'
11
+ RED='\033[0;31m'
12
+ NC='\033[0m' # No Color
13
+
14
+ # Default Configurations
15
+ DEFAULT_GH_URL="github.com/SamaKool/Paygorn.git"
16
+ DEFAULT_HF_URL="huggingface.co/spaces/TwoBraincells/Elite-Trade-Sentry.git"
17
+ TOKEN_FILE=".git_tokens"
18
+
19
+ # Ensure token file is gitignored
20
+ if ! grep -q "^${TOKEN_FILE}$" .gitignore 2>/dev/null; then
21
+ echo "${TOKEN_FILE}" >> .gitignore
22
+ fi
23
+
24
+ # Load saved tokens
25
+ if [ -f "$TOKEN_FILE" ]; then
26
+ source "$TOKEN_FILE"
27
  fi
28
 
29
+ # ==============================================================================
30
+ # HELPER FUNCTIONS
31
+ # ==============================================================================
32
+
33
+ get_token() {
34
+ local provider=$1
35
+ local var_name="${provider}_TOKEN"
36
+ local token="${!var_name}"
37
+
38
+ # Loop until the user actually provides a non-empty string
39
+ while [ -z "$token" ]; do
40
+ echo -e "${YELLOW}No token found for ${provider}.${NC}"
41
+ read -s -p "Please paste your ${provider} token: " token
42
+ echo ""
43
+
44
+ if [ -n "$token" ]; then
45
+ # Save for future
46
+ echo "${var_name}=\"${token}\"" >> "$TOKEN_FILE"
47
+ export ${var_name}="${token}"
48
+ else
49
+ echo -e "${RED}Token cannot be blank. Try again.${NC}"
50
+ fi
51
+ done
52
+ }
53
+
54
+ configure_remote() {
55
+ local remote_name=$1
56
+ local base_url=$2
57
+ local token=$3
58
+
59
+ # Inject token into URL for password-less auth
60
+ local auth_url="https://oauth2:${token}@${base_url}"
61
+
62
+ # Check if remote exists, modify if it does, add if it doesn't
63
+ if git remote | grep -q "^${remote_name}$"; then
64
+ git remote set-url "$remote_name" "$auth_url"
65
+ else
66
+ git remote add "$remote_name" "$auth_url"
67
+ fi
68
+ }
69
+
70
+ check_uncommitted() {
71
+ if [[ -n $(git status -s) ]]; then
72
+ echo -e "${YELLOW}Wait! You have uncommitted local changes.${NC}"
73
+ read -p "Do you want me to automatically commit them for you? (y/n): " auto_commit
74
+ if [[ "${auto_commit,,}" == "y" ]]; then
75
+ git add .
76
+ git commit -m "chore: auto-sync update $(date +'%Y-%m-%d %H:%M:%S')"
77
+ echo -e "${GREEN}Changes committed successfully.${NC}"
78
+ else
79
+ echo -e "${RED}Warning: Continuing without committing your latest local changes!${NC}"
80
+ fi
81
+ fi
82
+ }
83
+
84
+ compare_branches() {
85
+ local remote_name=$1
86
+ echo -e "${BLUE}Fetching latest data from ${remote_name}...${NC}"
87
+ git fetch "$remote_name" main 2>/dev/null
88
+
89
+ local divergence=$(git rev-list --left-right --count HEAD...${remote_name}/main 2>/dev/null)
90
+ local ahead=$(echo $divergence | awk '{print $1}')
91
+ local behind=$(echo $divergence | awk '{print $2}')
92
+
93
+ echo -e "Status: You are ${GREEN}${ahead} commits ahead${NC}, and ${RED}${behind} commits behind${NC} ${remote_name}/main."
94
+ }
95
 
96
+ execute_push() {
97
+ local remote_name=$1
98
+ check_uncommitted
99
+ compare_branches "$remote_name"
100
 
101
+ echo -e "${BLUE}Initiating push to ${remote_name}...${NC}"
102
+ # Push active local branch (HEAD) to remote main
103
+ if git push "$remote_name" HEAD:main; then
104
+ echo -e "${GREEN}Push successful!${NC}"
105
+ else
106
+ echo -e "${RED}Push rejected by remote.${NC}"
107
+ read -p "Do you want to FORCE push and overwrite the remote? (y/n): " force
108
+ if [[ "${force,,}" == "y" ]]; then
109
+ echo -e "${YELLOW}Force pushing...${NC}"
110
+ git push -f "$remote_name" HEAD:main
111
+ echo -e "${GREEN}Force push complete.${NC}"
112
+ else
113
+ echo "Operation aborted."
114
+ fi
115
+ fi
116
+ }
117
 
118
+ execute_pull() {
119
+ local remote_name=$1
120
+ echo -e "${BLUE}Pulling from ${remote_name}...${NC}"
121
+
122
+ if git pull "$remote_name" main --rebase; then
123
+ echo -e "${GREEN}Pull successful! Your local repo is up to date.${NC}"
124
+ else
125
+ echo -e "${RED}Pull failed due to conflicts.${NC}"
126
+ echo "Please resolve conflicts manually, or run 'git rebase --abort'."
127
+ fi
128
+ }
129
+
130
+ # ==============================================================================
131
+ # MAIN FLOW
132
+ # ==============================================================================
133
+
134
+ echo -e "${BLUE}=======================================${NC}"
135
+ echo -e "${GREEN} PAYGORN SYNC COMMAND CENTER ${NC}"
136
+ echo -e "${BLUE}=======================================${NC}"
137
+
138
+ # STEP 1: Action Selection
139
+ echo "What do you want to do?"
140
+ echo " A) Push"
141
+ echo " B) Pull"
142
+ echo " C) Sync (Zero-Touch Cloud Sync)"
143
+ read -p "Select (A/B/C): " action
144
+
145
+ # STEP 2: Target Selection
146
+ if [[ "${action,,}" == "a" || "${action,,}" == "b" ]]; then
147
+ echo ""
148
+ echo "Target provider?"
149
+ echo " A) GitHub (SamaKool/Paygorn)"
150
+ echo " B) HuggingFace (TwoBraincells/Elite-Trade-Sentry)"
151
+ read -p "Select (A/B): " target
152
+
153
+ if [[ "${target,,}" == "a" ]]; then
154
+ get_token "GITHUB"
155
+ configure_remote "github" "$DEFAULT_GH_URL" "$GITHUB_TOKEN"
156
+ if [[ "${action,,}" == "a" ]]; then execute_push "github"; else execute_pull "github"; fi
157
+ elif [[ "${target,,}" == "b" ]]; then
158
+ get_token "HF"
159
+ configure_remote "hf" "$DEFAULT_HF_URL" "$HF_TOKEN"
160
+ if [[ "${action,,}" == "a" ]]; then execute_push "hf"; else execute_pull "hf"; fi
161
+ else
162
+ echo -e "${RED}Invalid target selected. Exiting.${NC}"
163
+ exit 1
164
+ fi
165
+
166
+ # SYNC LOGIC (Automated Zero-Touch)
167
+ elif [[ "${action,,}" == "c" ]]; then
168
+ echo -e "\n${BLUE}Initiating Zero-Touch Synchronization...${NC}"
169
+
170
+ # Check if user forgot to commit locally before syncing clouds
171
+ check_uncommitted
172
+
173
+ get_token "GITHUB"
174
+ get_token "HF"
175
+ configure_remote "github" "$DEFAULT_GH_URL" "$GITHUB_TOKEN"
176
+ configure_remote "hf" "$DEFAULT_HF_URL" "$HF_TOKEN"
177
+
178
+ echo "Fetching metadata from both remotes..."
179
+ git fetch --prune github 2>/dev/null
180
+ git fetch --prune hf 2>/dev/null
181
+
182
+ # 1. The Disaster Guard (Divergent History Check)
183
+ if ! git merge-base --is-ancestor hf/main github/main >/dev/null 2>&1 && \
184
+ ! git merge-base --is-ancestor github/main hf/main >/dev/null 2>&1; then
185
+ echo -e "${RED}❌ WARNING: Divergent histories detected!${NC}"
186
+ echo "Both remotes have different, conflicting new commits."
187
+ echo "Manual merge required to prevent data loss."
188
+ exit 1
189
+ fi
190
+
191
+ # 2. The Zero-Touch Direction Detector
192
+ HF_AHEAD=$(git rev-list --count github/main..hf/main 2>/dev/null || echo 0)
193
+ GH_AHEAD=$(git rev-list --count hf/main..github/main 2>/dev/null || echo 0)
194
+
195
+ if [ "$HF_AHEAD" -gt 0 ]; then
196
+ echo -e "${YELLOW}HuggingFace is ahead by $HF_AHEAD commit(s). Pushing HF → GitHub...${NC}"
197
+ git push github "refs/remotes/hf/main:refs/heads/main"
198
+ echo -e "${GREEN}Sync complete.${NC}"
199
+
200
+ elif [ "$GH_AHEAD" -gt 0 ]; then
201
+ echo -e "${YELLOW}GitHub is ahead by $GH_AHEAD commit(s). Pushing GitHub → HuggingFace...${NC}"
202
+ git push hf "refs/remotes/github/main:refs/heads/main"
203
+ echo -e "${GREEN}Sync complete.${NC}"
204
+
205
+ else
206
+ echo -e "${GREEN}✅ No changes detected. Both remotes are perfectly in sync.${NC}"
207
+ fi
208
+
209
+ # INVALID ACTION
210
  else
211
+ echo -e "${RED}Invalid action selected. Run script again.${NC}"
212
+ exit 1
213
  fi
214
 
215
+ echo -e "${GREEN}All tasks completed. Over and out.${NC}"