-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull-from-github.bat
More file actions
141 lines (123 loc) · 4.29 KB
/
Copy pathpull-from-github.bat
File metadata and controls
141 lines (123 loc) · 4.29 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
@echo off
setlocal EnableExtensions EnableDelayedExpansion
title Quillite Markdown - Get Latest Code
cd /d "%~dp0"
set "REPO_URL=https://github.com/liuhang798/quillite-markdown.git"
set "BRANCH=main"
set "MAX_RETRIES=3"
set "SYSTEM_PROXY="
set "STATUS_FILE=%TEMP%\md-reader-pull-%RANDOM%-%RANDOM%.tmp"
echo.
echo ========================================
echo Quillite Markdown - Get Latest Code
echo ========================================
echo Project: %CD%
echo Remote : %REPO_URL%
echo Branch : %BRANCH%
echo.
where git >nul 2>&1
if errorlevel 1 (
echo [ERROR] Git was not found.
echo Install Git for Windows from:
echo https://git-scm.com/download/win
goto :failed
)
if not exist ".git" (
echo [ERROR] This folder is not a Git repository.
echo Run this file from the Quillite Markdown project folder.
goto :failed
)
echo [1/6] Checking the local worktree...
git status --porcelain --untracked-files=all >"%STATUS_FILE%"
if errorlevel 1 (
echo [ERROR] Git could not inspect the local worktree.
goto :failed
)
for %%S in ("%STATUS_FILE%") do if %%~zS GTR 0 (
echo [STOP] Local changes were found. Nothing was downloaded.
echo.
type "%STATUS_FILE%"
echo.
echo Commit, push, stash, or remove these changes before trying again.
goto :failed
)
del /q "%STATUS_FILE%" >nul 2>&1
echo [2/6] Configuring the GitHub remote...
git remote get-url origin >nul 2>&1
if errorlevel 1 (
git remote add origin "%REPO_URL%"
) else (
git remote set-url origin "%REPO_URL%"
)
if errorlevel 1 (
echo [ERROR] Git could not configure the origin remote.
goto :failed
)
for /f "usebackq delims=" %%P in (`powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$p=Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'; if($p.ProxyEnable -eq 1 -and $p.ProxyServer){$v=[string]$p.ProxyServer; if($v -match '(?:^|;)https=([^;]+)'){$v=$Matches[1]} elseif($v -match '(?:^|;)http=([^;]+)'){$v=$Matches[1]}; if($v -notmatch '^[a-z]+://'){$v='http://'+$v}; $v}"`) do set "SYSTEM_PROXY=%%P"
if defined SYSTEM_PROXY (
echo [3/6] Windows system proxy detected for this update.
) else (
echo [3/6] Using a direct network connection.
)
echo [4/6] Fetching the latest GitHub history...
call :fetch_with_retry
if errorlevel 1 goto :network_failed
git show-ref --verify --quiet "refs/remotes/origin/%BRANCH%"
if errorlevel 1 (
echo [ERROR] origin/%BRANCH% was not found after fetching.
goto :failed
)
echo [5/6] Switching to the local %BRANCH% branch...
git checkout "%BRANCH%" >nul 2>&1
if errorlevel 1 (
git checkout -b "%BRANCH%" --track "origin/%BRANCH%"
if errorlevel 1 (
echo [ERROR] Git could not switch to the %BRANCH% branch.
goto :failed
)
)
echo [6/6] Applying the update with fast-forward only...
git merge --ff-only "origin/%BRANCH%"
if errorlevel 1 (
echo.
echo [STOP] Local and GitHub history have diverged.
echo No files were overwritten and no automatic merge was created.
echo Push or reconcile the local commits before trying again.
goto :failed
)
echo.
echo Current version:
git log -1 --oneline
echo.
echo ========================================
echo Latest code downloaded successfully.
echo ========================================
if not defined PULL_SCRIPT_TEST ping 127.0.0.1 -n 4 >nul
exit /b 0
:fetch_with_retry
set /a "ATTEMPT=1"
:fetch_retry
echo Network attempt !ATTEMPT!/%MAX_RETRIES%...
if defined SYSTEM_PROXY (
git -c http.version=HTTP/1.1 -c "http.proxy=!SYSTEM_PROXY!" fetch --no-tags --prune origin "%BRANCH%"
) else (
git -c http.version=HTTP/1.1 fetch --no-tags --prune origin "%BRANCH%"
)
if not errorlevel 1 exit /b 0
if !ATTEMPT! GEQ %MAX_RETRIES% exit /b 1
set /a "ATTEMPT+=1"
echo Connection interrupted. Retrying in 5 seconds...
if not defined PULL_SCRIPT_TEST timeout /t 5 /nobreak >nul
goto :fetch_retry
:network_failed
echo.
echo [ERROR] GitHub download failed after %MAX_RETRIES% attempts.
echo Open https://github.com in a browser to check the connection,
echo then disable an unstable VPN/proxy if necessary and try again.
goto :failed
:failed
if exist "%STATUS_FILE%" del /q "%STATUS_FILE%" >nul 2>&1
echo.
echo The update did not complete. Your local files were not overwritten.
if not defined PULL_SCRIPT_TEST ping 127.0.0.1 -n 11 >nul
exit /b 1