Skip to content

Commit f3511ee

Browse files
committed
feat(main): 增加远程仓库管理功能并优化分支加载- 添加获取远程仓库地址和重置远程仓库地址的功能
-优化分支加载逻辑,同时获取本地和远程分支 - 增加强推确认框,提高操作安全性 - 修复部分功能的远程仓库连接问题
1 parent 2f7c684 commit f3511ee

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

main.py

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ class GitCommitEditor(QWidget):
226226

227227
def __init__(self):
228228
super().__init__()
229+
self.remote_url = None
229230
self.setWindowTitle("Git Commit Editor (全功能整合版)")
230231

231232
self.repo_path = QLineEdit()
@@ -279,36 +280,51 @@ def browse_repo(self):
279280
self.repo_path.setText(path)
280281
save_last_repo_path(path)
281282
self.load_branches()
283+
def get_remote_url(self):
284+
# 获取远程仓库地址
285+
output = run_git_command(["git", "remote", "get-url", "origin"], cwd=self.repo_path.text())
286+
self.remote_url = output.strip()
287+
def reset_remote_url(self):
288+
# 重置远程仓库地址
289+
if self.remote_url is None:
290+
return
291+
run_git_command(["git", "remote", "add", "origin", self.remote_url], cwd=self.repo_path.text())
282292

283293
def load_branches(self):
284294
repo = self.repo_path.text()
285295
if not os.path.isdir(repo):
286296
return
287-
def get_all_branched():
297+
298+
def get_branches():
288299
# 获取所有的远程分支
289-
output = run_git_command(["git", "branch","-r"], cwd=repo)
290-
branches = [line for line in output.split("\n") if line.strip() != '']
291-
branches =branches[1:]
292-
branches = [branch.split("/")[-1] for branch in branches]
293-
return branches
294-
def get_selected_branch():
295-
# 获取当前选择的分支
296-
output = run_git_command(["git", "branch"], cwd=repo)
297-
branches = [line for line in output.split("\n") if line.strip() != '']
298-
branches = [branch.split("*")[-1].strip() for branch in branches if branch.startswith("*")]
299-
return branches[0]
300-
301-
branches = get_all_branched()
300+
output = run_git_command(["git", "branch", "-a"], cwd=repo)
301+
all = [line.strip() for line in output.split("\n") if line.strip() != '']
302+
current = ""
303+
branches = set()
304+
for branch in all:
305+
if "HEAD" in branch:
306+
continue
307+
if branch.startswith("remotes/origin/"):
308+
branches.add(branch.split("/")[-1])
309+
continue
310+
if branch.startswith("*"):
311+
current = branch.split("*")[-1].strip()
312+
branches.add(current)
313+
else:
314+
branches.add(branch)
315+
# 远程分支列表
316+
return branches, current
317+
318+
branches, current = get_branches()
302319
if not len(branches):
303-
QMessageBox.critical(self, "失败", "无法获取分支列表")
304320
return
305-
current = get_selected_branch()
306321
self.branch_selector.clear()
307322
self.branch_selector.addItems(branches)
308323
# 设置当前分支
309324
if current != "":
310325
self.branch_selector.setCurrentText(current)
311326
self.load_commits()
327+
self.get_remote_url()
312328

313329
def load_commits(self):
314330
repo = self.repo_path.text()
@@ -379,21 +395,24 @@ def rewrite_commits_randomly(self):
379395
errors='replace', cwd=self.repo_path.text(), capture_output=True, text=True)
380396

381397
if result.returncode == 0:
398+
self.reset_remote_url()
382399
self.load_commits()
383400
QMessageBox.information(self, "成功", "提交修改完成(使用 filter-repo)")
384401
else:
385402
QMessageBox.critical(self, "失败", result.stderr)
403+
except Exception as e:
404+
logging.error("批量修改失败:", exc_info=e)
405+
QMessageBox.critical(self, "失败", str(e))
386406
finally:
387407
os.remove(callback_path)
388408

389409
def push_force(self):
410+
# 增减确认框 确认是否需要强推 这是一个危险操作
411+
if QMessageBox.question(self, "确认", "这个操作会导致原来的提交记录丢失,确定要强推吗?", QMessageBox.Yes | QMessageBox.No) == QMessageBox.No:
412+
return
390413
repo = self.repo_path.text()
391414
branch = self.branch_selector.currentText()
392-
remotes = run_git_command(["git", "branch", "-r"], cwd=repo).splitlines()
393-
if not any(f"origin/{branch}" in r for r in remotes):
394-
QMessageBox.warning(self, "警告", f"远程未检测到分支 {branch}")
395-
return
396-
result = subprocess.run(["git", "push", "origin", branch, "--force"], cwd=repo, capture_output=True, text=True)
415+
result = subprocess.run(["git", "push",'--set-upstream', "origin", branch, "--force"], cwd=repo, capture_output=True, text=True)
397416
if result.returncode == 0:
398417
QMessageBox.information(self, "成功", "强推完成")
399418
else:
@@ -439,15 +458,17 @@ def edit_commit(self, item):
439458
errors='replace', capture_output=True, text=True)
440459

441460
if result.returncode == 0:
461+
self.reset_remote_url()
442462
self.load_commits()
443463
QMessageBox.information(self, "成功", "提交修改完成(使用 filter-repo)")
444464
else:
445465
logging.error(f"edit commit failed: {selected_commit}", result.stderr)
446466
QMessageBox.critical(self, "失败", result.stderr)
447467
except Exception as e:
448468
logging.error(f"edit commit failed: {selected_commit}", exc_info=e)
449-
os.remove(script_path)
450469
QMessageBox.critical(self, "错误", str(e))
470+
finally:
471+
os.remove(script_path)
451472

452473
def get_commit_info(self, selected_commit):
453474
try:

0 commit comments

Comments
 (0)