美化控制台输出,修复添加文件失败问题,移除矩形边框改用简洁线条
This commit is contained in:
@@ -26,12 +26,15 @@ class GitOperations:
|
||||
Returns:
|
||||
是否成功
|
||||
"""
|
||||
from .utils import Colors
|
||||
|
||||
if self.is_git_repo():
|
||||
OutputFormatter.warning("当前目录已经是Git仓库")
|
||||
return True
|
||||
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
# 初始化Git仓库
|
||||
OutputFormatter.info("正在初始化Git仓库...")
|
||||
OutputFormatter.step(1, "初始化Git仓库...")
|
||||
success, _ = self.executor.run("git init", show_output=False)
|
||||
if not success:
|
||||
OutputFormatter.error("Git初始化失败")
|
||||
@@ -39,7 +42,7 @@ class GitOperations:
|
||||
OutputFormatter.success("Git仓库初始化成功")
|
||||
|
||||
# 创建main分支
|
||||
OutputFormatter.info("正在创建main分支...")
|
||||
OutputFormatter.step(2, "创建main分支...")
|
||||
success, _ = self.executor.run(f"git checkout -b {Config.DEFAULT_BRANCH}", show_output=False)
|
||||
if success:
|
||||
OutputFormatter.success("main分支创建成功")
|
||||
@@ -47,10 +50,11 @@ class GitOperations:
|
||||
OutputFormatter.warning("main分支创建失败,将使用默认分支")
|
||||
|
||||
# 创建.gitignore文件
|
||||
OutputFormatter.step(3, "创建.gitignore文件...")
|
||||
self._create_gitignore()
|
||||
|
||||
# 首次提交
|
||||
OutputFormatter.info("正在进行首次提交...")
|
||||
OutputFormatter.step(4, "进行首次提交...")
|
||||
self.executor.run("git add .", show_output=False)
|
||||
success, _ = self.executor.run('git commit -m "first commit"', show_output=False)
|
||||
if success:
|
||||
@@ -58,11 +62,11 @@ class GitOperations:
|
||||
else:
|
||||
OutputFormatter.warning("首次提交失败(可能没有文件可提交)")
|
||||
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
return True
|
||||
|
||||
def _create_gitignore(self):
|
||||
"""创建.gitignore文件"""
|
||||
OutputFormatter.info("正在创建.gitignore文件...")
|
||||
try:
|
||||
with open('.gitignore', 'w', encoding='utf-8') as f:
|
||||
f.write(Config.GITIGNORE_TEMPLATE)
|
||||
@@ -104,7 +108,7 @@ class GitOperations:
|
||||
OutputFormatter.error("添加文件失败")
|
||||
return success
|
||||
|
||||
def commit(self, message: str = None) -> bool:
|
||||
def commit(self, message: str) -> bool:
|
||||
"""
|
||||
提交更改
|
||||
|
||||
@@ -117,7 +121,7 @@ class GitOperations:
|
||||
if not message:
|
||||
message = f"update: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
||||
|
||||
OutputFormatter.info("正在提交更改...")
|
||||
OutputFormatter.status('running', f"提交更改: {message}")
|
||||
success, _ = self.executor.run(f'git commit -m "{message}"', show_output=True)
|
||||
if success:
|
||||
OutputFormatter.success("提交成功")
|
||||
@@ -137,7 +141,7 @@ class GitOperations:
|
||||
return branch.strip()
|
||||
return Config.DEFAULT_BRANCH
|
||||
|
||||
def push(self, remote: str, branch: str = None) -> bool:
|
||||
def push(self, remote: str, branch: str = "") -> bool:
|
||||
"""
|
||||
推送到远程仓库
|
||||
|
||||
@@ -151,7 +155,7 @@ class GitOperations:
|
||||
if not branch:
|
||||
branch = self.get_current_branch()
|
||||
|
||||
OutputFormatter.info(f"正在推送到 {remote}...")
|
||||
OutputFormatter.status('running', f"推送到 {remote}/{branch}...")
|
||||
|
||||
# 先尝试直接推送
|
||||
success, _ = self.executor.run(f"git push {remote} {branch}", show_output=True)
|
||||
@@ -168,7 +172,7 @@ class GitOperations:
|
||||
|
||||
return success
|
||||
|
||||
def pull(self, remote: str, branch: str = None) -> bool:
|
||||
def pull(self, remote: str, branch: str = "") -> bool:
|
||||
"""
|
||||
从远程仓库拉取
|
||||
|
||||
@@ -182,7 +186,7 @@ class GitOperations:
|
||||
if not branch:
|
||||
branch = self.get_current_branch()
|
||||
|
||||
OutputFormatter.info(f"正在从 {remote} 拉取 {branch} 分支...")
|
||||
OutputFormatter.status('running', f"从 {remote}/{branch} 拉取更新...")
|
||||
success, _ = self.executor.run(f"git pull {remote} {branch}", show_output=True)
|
||||
|
||||
if success:
|
||||
@@ -196,12 +200,15 @@ class GitOperations:
|
||||
"""显示仓库状态"""
|
||||
from .utils import Colors
|
||||
|
||||
print(f"{Colors.CYAN}当前目录:{Colors.ENDC} {self.current_dir}\n")
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
OutputFormatter.key_value(">> 当前目录", self.current_dir, Colors.BRIGHT_YELLOW)
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
|
||||
# Git状态
|
||||
OutputFormatter.info("Git状态:")
|
||||
print(f"{Colors.BRIGHT_BLUE}>> Git状态:{Colors.ENDC}")
|
||||
self.executor.run("git status", show_output=True)
|
||||
|
||||
# 最近提交
|
||||
print(f"\n{Colors.CYAN}最近3次提交:{Colors.ENDC}")
|
||||
print(f"\n{Colors.BRIGHT_MAGENTA}>> 最近3次提交:{Colors.ENDC}")
|
||||
self.executor.run("git log --oneline -3", show_output=True)
|
||||
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
|
||||
|
||||
Reference in New Issue
Block a user