Spaces:
Sleeping
Sleeping
Keldos
commited on
Commit
•
37526ea
1
Parent(s):
a35d67d
fix: 修复后台更新逻辑,避免stash产生错误等
Browse files- modules/repo.py +40 -16
- modules/utils.py +2 -2
modules/repo.py
CHANGED
@@ -51,14 +51,14 @@ def run(command, desc=None, errdesc=None, custom_env=None, live: bool = default_
|
|
51 |
return (result.stdout or "")
|
52 |
|
53 |
|
54 |
-
def run_pip(command, desc=None, live=default_command_live):
|
55 |
# if args.skip_install:
|
56 |
# return
|
57 |
|
58 |
index_url_line = f' --index-url {index_url}' if index_url != '' else ''
|
59 |
return run(
|
60 |
f'"{python}" -m pip {command} --prefer-binary{index_url_line}',
|
61 |
-
desc=f"Installing {desc}...",
|
62 |
errdesc=f"Couldn't install {desc}",
|
63 |
live=live
|
64 |
)
|
@@ -158,6 +158,12 @@ def get_tag_commit_hash(tag):
|
|
158 |
commit_hash = "<none>"
|
159 |
return commit_hash
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
def background_update():
|
162 |
# {git} fetch --all && ({git} pull https://github.com/GaiZhenbiao/ChuanhuChatGPT.git main -f || ({git} stash && {git} pull https://github.com/GaiZhenbiao/ChuanhuChatGPT.git main -f && {git} stash pop)) && {pip} install -r requirements.txt")
|
163 |
try:
|
@@ -165,47 +171,65 @@ def background_update():
|
|
165 |
latest_release_tag = latest_release["tag"]
|
166 |
latest_release_hash = get_tag_commit_hash(latest_release_tag)
|
167 |
need_pip = latest_release["need_pip"]
|
|
|
168 |
|
|
|
169 |
current_branch = get_current_branch()
|
170 |
-
updater_branch = f'tmp_{
|
|
|
171 |
track_repo = "https://github.com/GaiZhenbiao/ChuanhuChatGPT.git"
|
172 |
try:
|
173 |
try:
|
174 |
-
run(f"{git} fetch {track_repo}", desc="Fetching from github...", live=False)
|
175 |
except Exception:
|
176 |
-
logging.error(f"Update failed in fetching")
|
177 |
return "failed"
|
178 |
|
179 |
-
run(f'{git} stash
|
180 |
-
|
|
|
|
|
181 |
run(f"{git} checkout -b {updater_branch}", live=False)
|
182 |
run(f"{git} reset --hard FETCH_HEAD", live=False)
|
183 |
-
run(f"{git} reset --hard {latest_release_hash}", desc=f'Checking out {latest_release_tag}...')
|
184 |
run(f"{git} checkout {current_branch}", live=False)
|
185 |
|
186 |
try:
|
187 |
-
run(f"{git} merge {updater_branch} -q", desc="Trying to apply latest update...")
|
188 |
except Exception:
|
189 |
logging.error(f"Update failed in merging")
|
190 |
try:
|
191 |
-
run(f"{git} merge --abort", desc="
|
192 |
-
run(f"{git} reset --hard {
|
193 |
-
run(f"{git} stash pop", live=False)
|
194 |
run(f"{git} branch -D -f {updater_branch}", live=False)
|
|
|
|
|
195 |
logging.error(f"Update failed, but your file was safely reset to the state before the update.")
|
196 |
return "failed"
|
197 |
except Exception as e:
|
198 |
-
logging.error(f"!!!Update failed in resetting, try to reset your files manually.")
|
199 |
return "failed"
|
200 |
-
|
201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
run(f"{git} branch -D -f {updater_branch}", live=False)
|
|
|
203 |
except Exception as e:
|
204 |
logging.error(f"Update failed: {e}")
|
205 |
return "failed"
|
206 |
if need_pip:
|
207 |
try:
|
208 |
-
run_pip(f"install -r requirements.txt", "requirements")
|
209 |
except Exception:
|
210 |
logging.error(f"Update failed in pip install")
|
211 |
return "failed"
|
|
|
51 |
return (result.stdout or "")
|
52 |
|
53 |
|
54 |
+
def run_pip(command, desc=None, pref=None, live=default_command_live):
|
55 |
# if args.skip_install:
|
56 |
# return
|
57 |
|
58 |
index_url_line = f' --index-url {index_url}' if index_url != '' else ''
|
59 |
return run(
|
60 |
f'"{python}" -m pip {command} --prefer-binary{index_url_line}',
|
61 |
+
desc=f"{pref} Installing {desc}...",
|
62 |
errdesc=f"Couldn't install {desc}",
|
63 |
live=live
|
64 |
)
|
|
|
158 |
commit_hash = "<none>"
|
159 |
return commit_hash
|
160 |
|
161 |
+
def repo_need_stash():
|
162 |
+
try:
|
163 |
+
return subprocess.check_output([git, "diff-index", "--quiet", "HEAD", "--"], shell=False, encoding='utf8').strip() != ""
|
164 |
+
except Exception:
|
165 |
+
return True
|
166 |
+
|
167 |
def background_update():
|
168 |
# {git} fetch --all && ({git} pull https://github.com/GaiZhenbiao/ChuanhuChatGPT.git main -f || ({git} stash && {git} pull https://github.com/GaiZhenbiao/ChuanhuChatGPT.git main -f && {git} stash pop)) && {pip} install -r requirements.txt")
|
169 |
try:
|
|
|
171 |
latest_release_tag = latest_release["tag"]
|
172 |
latest_release_hash = get_tag_commit_hash(latest_release_tag)
|
173 |
need_pip = latest_release["need_pip"]
|
174 |
+
need_stash = repo_need_stash()
|
175 |
|
176 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
177 |
current_branch = get_current_branch()
|
178 |
+
updater_branch = f'tmp_{timestamp}'
|
179 |
+
backup_branch = f'backup_{timestamp}'
|
180 |
track_repo = "https://github.com/GaiZhenbiao/ChuanhuChatGPT.git"
|
181 |
try:
|
182 |
try:
|
183 |
+
run(f"{git} fetch {track_repo}", desc="[Updater] Fetching from github...", live=False)
|
184 |
except Exception:
|
185 |
+
logging.error(f"Update failed in fetching, check your network connection")
|
186 |
return "failed"
|
187 |
|
188 |
+
run(f'{git} stash push --include-untracked -m "updater-{timestamp}"',
|
189 |
+
desc=f'[Updater] Restoring you local changes on stash updater-{timestamp}', live=False) if need_stash else None
|
190 |
+
|
191 |
+
run(f"{git} checkout -b {backup_branch}", live=False)
|
192 |
run(f"{git} checkout -b {updater_branch}", live=False)
|
193 |
run(f"{git} reset --hard FETCH_HEAD", live=False)
|
194 |
+
run(f"{git} reset --hard {latest_release_hash}", desc=f'[Updater] Checking out {latest_release_tag}...', live=False)
|
195 |
run(f"{git} checkout {current_branch}", live=False)
|
196 |
|
197 |
try:
|
198 |
+
run(f"{git} merge --no-edit {updater_branch} -q", desc=f"[Updater] Trying to apply latest update on version {latest_release_tag}...")
|
199 |
except Exception:
|
200 |
logging.error(f"Update failed in merging")
|
201 |
try:
|
202 |
+
run(f"{git} merge --abort", desc="[Updater] Conflict detected, canceling update...")
|
203 |
+
run(f"{git} reset --hard {backup_branch}", live=False)
|
|
|
204 |
run(f"{git} branch -D -f {updater_branch}", live=False)
|
205 |
+
run(f"{git} branch -D -f {backup_branch}", live=False)
|
206 |
+
run(f"{git} stash pop", live=False) if need_stash else None
|
207 |
logging.error(f"Update failed, but your file was safely reset to the state before the update.")
|
208 |
return "failed"
|
209 |
except Exception as e:
|
210 |
+
logging.error(f"!!!Update failed in resetting, try to reset your files manually. {e}")
|
211 |
return "failed"
|
212 |
+
|
213 |
+
if need_stash:
|
214 |
+
try:
|
215 |
+
run(f"{git} stash apply", desc="[Updater] Trying to restore your local modifications...", live=False)
|
216 |
+
except Exception:
|
217 |
+
run(f"{git} reset --hard {backup_branch}", desc="[Updater] Conflict detected, canceling update...", live=False)
|
218 |
+
run(f"{git} branch -D -f {updater_branch}", live=False)
|
219 |
+
run(f"{git} branch -D -f {backup_branch}", live=False)
|
220 |
+
run(f"{git} stash pop", live=False)
|
221 |
+
logging.error(f"Update failed in applying your local changes, but your file was safely reset to the state before the update.")
|
222 |
+
return "failed"
|
223 |
+
run(f"{git} stash drop", live=False)
|
224 |
+
|
225 |
run(f"{git} branch -D -f {updater_branch}", live=False)
|
226 |
+
run(f"{git} branch -D -f {backup_branch}", live=False)
|
227 |
except Exception as e:
|
228 |
logging.error(f"Update failed: {e}")
|
229 |
return "failed"
|
230 |
if need_pip:
|
231 |
try:
|
232 |
+
run_pip(f"install -r requirements.txt", pref="[Updater]", desc="requirements", live=False)
|
233 |
except Exception:
|
234 |
logging.error(f"Update failed in pip install")
|
235 |
return "failed"
|
modules/utils.py
CHANGED
@@ -542,10 +542,10 @@ def transfer_input(inputs):
|
|
542 |
def update_chuanhu():
|
543 |
from .repo import background_update
|
544 |
|
545 |
-
print("Trying to update...")
|
546 |
update_status = background_update()
|
547 |
if update_status == "success":
|
548 |
-
|
549 |
status = '<span id="update-status" class="hideK">success</span>'
|
550 |
return gr.Markdown.update(value=i18n("更新成功,请重启本程序")+status)
|
551 |
else:
|
|
|
542 |
def update_chuanhu():
|
543 |
from .repo import background_update
|
544 |
|
545 |
+
print("[Updater] Trying to update...")
|
546 |
update_status = background_update()
|
547 |
if update_status == "success":
|
548 |
+
logging.info("Successfully updated, restart needed")
|
549 |
status = '<span id="update-status" class="hideK">success</span>'
|
550 |
return gr.Markdown.update(value=i18n("更新成功,请重启本程序")+status)
|
551 |
else:
|