-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush.bat
More file actions
303 lines (256 loc) · 9.49 KB
/
push.bat
File metadata and controls
303 lines (256 loc) · 9.49 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
@echo off
setlocal enabledelayedexpansion
REM ============================================================================
REM Bit HCI Push Script v0.1.0
REM Purpose: Ensure local and remote repositories are fully synchronized
REM Features: Auto-commit, pull, push, and verification
REM Usage: push.bat [branch] [commit_message]
REM ============================================================================
REM Use UTF-8 code page for proper Chinese character display
chcp 65001 >NUL 2>&1
echo.
echo ========================================
echo Bit HCI Push Script v0.1.0
echo ========================================
echo.
REM Parse arguments
set BRANCH=%1
set COMMIT_MSG=%2
if "%BRANCH%"=="" set BRANCH=main
REM Move to repository root (parent of script directory)
cd /d "%~dp0.."
REM Check git availability
git --version >NUL 2>&1 || (
echo [ERROR] Git is not installed or not in PATH.
echo [INFO] Please install Git and try again.
exit /b 1
)
echo [INFO] Using branch: %BRANCH%
git rev-parse --is-inside-work-tree >NUL 2>&1 || (
echo [ERROR] Not inside a Git repository.
echo [INFO] Please ensure you are in the project directory.
exit /b 1
)
REM Ensure we are on the target branch (create if missing)
for /f %%b in ('git rev-parse --abbrev-ref HEAD') do set CURR=%%b
if /I not "%CURR%"=="%BRANCH%" (
echo [INFO] Switching to %BRANCH%...
git checkout %BRANCH% 2>NUL || git checkout -b %BRANCH%
if errorlevel 1 (
echo [ERROR] Failed to switch/create branch %BRANCH%.
exit /b 1
)
)
echo [INFO] Current repository status:
echo.
git status -sb
echo.
REM ============================================================================
REM Step 1: Fetch and check remote status
REM ============================================================================
echo [INFO] Step 1: Fetching remote changes...
git fetch origin %BRANCH% 2>NUL
REM Check if remote branch exists
git rev-parse origin/%BRANCH% >NUL 2>&1
if errorlevel 1 (
echo [WARN] Remote branch origin/%BRANCH% does not exist yet.
echo [INFO] Will create it on first push.
set REMOTE_EXISTS=0
) else (
set REMOTE_EXISTS=1
)
if %REMOTE_EXISTS%==1 (
REM Check for commits behind remote
for /f %%i in ('git rev-list HEAD..origin/%BRANCH% --count 2^>NUL') do set BEHIND=%%i
if not defined BEHIND set BEHIND=0
REM Check for commits ahead of remote
for /f %%i in ('git rev-list origin/%BRANCH%..HEAD --count 2^>NUL') do set AHEAD=%%i
if not defined AHEAD set AHEAD=0
echo [INFO] Local status: %AHEAD% commit^(s^) ahead, %BEHIND% commit^(s^) behind remote.
) else (
set BEHIND=0
set AHEAD=0
)
REM ============================================================================
REM Step 2: Pull remote changes if behind
REM ============================================================================
if %BEHIND% gtr 0 (
echo.
echo [INFO] Step 2: Pulling remote changes ^(%BEHIND% new commit^(s^)^)...
git pull --rebase origin %BRANCH%
if errorlevel 1 (
echo.
echo [ERROR] Pull with rebase failed.
echo [INFO] This usually means there are conflicts.
echo [INFO] Please resolve conflicts manually and run:
echo [INFO] git rebase --continue
echo [INFO] Then run this script again.
echo [INFO] Or abort the rebase:
echo [INFO] git rebase --abort
exit /b 1
)
echo [OK] Pulled successfully.
) else (
echo.
echo [INFO] Step 2: Local is up to date with remote.
)
REM ============================================================================
REM Step 3: Handle local changes
REM ============================================================================
echo.
echo [INFO] Step 3: Checking for local changes...
REM Check if there are any local file changes
git diff --quiet && git diff --cached --quiet
set HAS_LOCAL_CHANGES=%errorlevel%
if %HAS_LOCAL_CHANGES%==0 (
echo [INFO] No local file changes detected.
) else (
echo [INFO] Local file changes detected.
REM Sync docs/progress/PROGRESS.md into README.md between markers
echo [INFO] Syncing PROGRESS.md to README.md...
powershell -NoProfile -Command "^$p = Get-Content -Raw 'docs/progress/PROGRESS.md'; ^$r = Get-Content -Raw 'README.md'; ^$n = [regex]::Replace(^$r, '(?s)(<!-- PROGRESS:BEGIN -->).*?(<!-- PROGRESS:END -->)', '$1' + [Environment]::NewLine + ^$p.Trim() + [Environment]::NewLine + '$2'); Set-Content -Encoding UTF8 'README.md' ^$n" >NUL 2>&1
if not errorlevel 1 (
echo [OK] PROGRESS.md synced successfully.
) else (
echo [WARN] Failed to sync PROGRESS.md, continuing anyway...
)
REM Show files to be staged
echo.
echo [INFO] Files to be staged:
git status --short
echo.
REM Stage changes
echo [INFO] Staging all changes...
git add -A
REM Count staged files
set CHANGED_FILES=0
for /f %%i in ('git diff --cached --numstat ^| find /c /v ""') do set CHANGED_FILES=%%i
REM Commit if there are staged changes
git diff --cached --quiet
if errorlevel 1 (
echo [INFO] Found !CHANGED_FILES! changed file^(s^).
REM Generate commit message
if "!COMMIT_MSG!"=="" (
for /f "usebackq delims=" %%i in (`powershell -NoProfile -Command "$d=Get-Date; $d.ToString('yyyy-MM-dd_HH-mm-ss')"`) do set NOW=%%i
set "MSG=chore(repo): sync on !NOW!"
) else (
set "MSG=!COMMIT_MSG!"
)
echo [INFO] Commit message: !MSG!
git commit -m "!MSG!"
if not errorlevel 1 (
echo [OK] Changes committed successfully.
) else (
echo [ERROR] Commit failed.
exit /b 1
)
) else (
echo [INFO] No changes to commit.
)
)
REM ============================================================================
REM Step 4: Check if there are unpushed commits
REM ============================================================================
echo.
echo [INFO] Step 4: Checking for unpushed commits...
if %REMOTE_EXISTS%==1 (
for /f %%i in ('git rev-list origin/%BRANCH%..HEAD --count 2^>NUL') do set UNPUSHED=%%i
) else (
for /f %%i in ('git rev-list HEAD --count 2^>NUL') do set UNPUSHED=%%i
)
if not defined UNPUSHED set UNPUSHED=0
if %UNPUSHED%==0 (
echo [INFO] No unpushed commits.
echo.
echo ========================================
echo [OK] Everything is up to date!
echo ========================================
echo [INFO] Local and remote are in sync.
exit /b 0
)
echo [INFO] Found %UNPUSHED% unpushed commit^(s^).
REM ============================================================================
REM Step 5: Push to remote
REM ============================================================================
echo.
echo [INFO] Step 5: Pushing to remote...
REM Ensure remote 'origin' exists
git remote get-url origin >NUL 2>&1 || (
echo [ERROR] Remote 'origin' not set.
echo [INFO] Please add a remote: git remote add origin ^<url^>
exit /b 1
)
REM Display remote URL
for /f "delims=" %%i in ('git remote get-url origin') do set REMOTE_URL=%%i
echo [INFO] Remote URL: !REMOTE_URL!
REM Detect if upstream is missing
set NEED_UPSTREAM=0
git rev-parse --abbrev-ref --symbolic-full-name @{u} >NUL 2>&1 || set NEED_UPSTREAM=1
echo [INFO] Pushing %UNPUSHED% commit^(s^) to origin/%BRANCH%...
if !NEED_UPSTREAM!==1 (
echo [INFO] Setting upstream branch...
git push -u origin %BRANCH%
if errorlevel 1 goto push_failed
) else (
git push origin %BRANCH%
if errorlevel 1 goto push_failed
)
echo [OK] Pushed successfully.
goto push_success
:push_failed
echo.
echo [ERROR] Push failed.
echo [INFO] Common reasons:
echo [INFO] - Network connection issues
echo [INFO] - Authentication failed
echo [INFO] - Remote repository issues
echo [INFO] - Remote has changes (run script again to pull first)
echo [INFO] Please check and try again.
exit /b 1
:push_success
REM ============================================================================
REM Step 6: Final verification
REM ============================================================================
echo.
echo [INFO] Step 6: Verifying synchronization...
REM Fetch again to ensure we have latest state
git fetch origin %BRANCH% >NUL 2>&1
REM Check final status
for /f %%i in ('git rev-list HEAD..origin/%BRANCH% --count 2^>NUL') do set FINAL_BEHIND=%%i
for /f %%i in ('git rev-list origin/%BRANCH%..HEAD --count 2^>NUL') do set FINAL_AHEAD=%%i
if not defined FINAL_BEHIND set FINAL_BEHIND=0
if not defined FINAL_AHEAD set FINAL_AHEAD=0
if %FINAL_BEHIND%==0 (
if %FINAL_AHEAD%==0 (
echo [OK] Verification passed: Local and remote are in sync!
) else (
echo [WARN] Verification: %FINAL_AHEAD% commit^(s^) ahead ^(this is unexpected^).
)
) else (
echo [ERROR] Verification failed: %FINAL_BEHIND% commit^(s^) behind remote.
echo [INFO] Remote may have received new commits during push.
echo [INFO] Please run this script again to sync.
exit /b 1
)
echo.
echo ========================================
echo [OK] Sync completed successfully!
echo ========================================
echo.
echo [INFO] Summary:
if defined CHANGED_FILES echo - Files changed: !CHANGED_FILES!
echo - Commits pushed: %UNPUSHED%
echo - Branch: %BRANCH%
echo - Remote: !REMOTE_URL!
echo - Status: Local and remote are in sync
echo.
REM Show last commit info
echo [INFO] Last commit:
git log -1 --oneline --decorate
echo.
REM Show remote tracking status
echo [INFO] Branch tracking:
git branch -vv | findstr /C:"%BRANCH%"
echo.
endlocal
exit /b 0