From b75dee4536d761e7036fe6bd5662c3e1fa33e4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=91=E8=90=8C=E8=8A=BD?= <3205788256@qq.com> Date: Mon, 16 Feb 2026 00:29:47 +0800 Subject: [PATCH] update: 2026-02-16 00:29:47 --- AGENTS.md | 8 +- README.md | 58 +++++++++++-- quickgit/git_operations.py | 29 ++++++- quickgit/ui.py | 73 +++++++++++++++-- quickgit/utils.py | 73 +++++++++++++++++ 菜单提示说明.md | 164 +++++++++++++++++++++++++++++++++++++ 6 files changed, 385 insertions(+), 20 deletions(-) create mode 100644 菜单提示说明.md diff --git a/AGENTS.md b/AGENTS.md index 7b2ea07..bdc21ac 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -85,13 +85,19 @@ quickgit/ ├── __init__.py # Package init (empty) ├── config.py # Configuration constants ├── utils.py # Utilities (Colors, CommandExecutor, OutputFormatter, InputValidator, PlatformUtils) -├── git_operations.py # Git command wrapper +├── git_operations.py # Git command wrapper (supports custom work directory) ├── remote_manager.py # Remote repository management └── ui.py # User interface and menu system ``` **Principle:** Each module has a single, well-defined responsibility. +### Key Features + +1. **Flexible Directory Management**: Users can select any directory to manage at startup +2. **Cross-platform Path Handling**: Automatic path normalization for Windows/Linux/macOS +3. **Directory Validation**: Real-time validation of user-provided paths + ### Import Conventions **Order:** diff --git a/README.md b/README.md index babd2c8..c2b43d3 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ ## 功能特性 +- **灵活的目录管理** - 启动时可选择任意Git仓库目录进行管理 - **一键初始化Git仓库** - 自动完成Git初始化、分支创建、.gitignore配置 - **一键提交推送** - 快速提交代码并推送到远程仓库 - **多仓库支持** - 同时支持GitHub和Gitea仓库管理 @@ -12,6 +13,7 @@ - **彩色界面** - 友好的彩色控制台输出 - **模块化设计** - 易于维护和扩展 - **跨平台支持** - 完美兼容 Windows、Linux、macOS +- **智能路径处理** - 自动处理不同平台的路径格式 ## 项目结构 @@ -67,37 +69,75 @@ python mengya_git_manager.py ### 主要功能菜单 +**首次启动时:** +- 选择要管理的Git仓库目录 +- 支持绝对路径和相对路径 +- 直接回车使用脚本所在目录 +- 自动验证目录是否存在 + +**主菜单选项:** ``` -1. 初始化Git仓库 -2. 提交并推送更改 -3. 从远程仓库拉取 -4. 查看仓库状态 -5. 管理远程仓库 -6. 退出 +[1] 初始化Git仓库 +[2] 提交并推送更改 +[3] 从远程仓库拉取 +[4] 查看仓库状态 +[5] 管理远程仓库 +[6] 退出程序 + +小提示: +[*] 提交代码前建议先拉取最新代码,减少代码冲突 +[*] 使用SSH进行Git提交更方便快捷和安全 ``` ## 使用场景 +### 场景0:选择工作目录 + +启动脚本后,首先会要求选择工作目录: + +**Windows示例:** +``` +请输入要管理的Git仓库目录 (回车使用当前目录: E:\Projects\MyApp): +输入: C:\Users\YourName\project +或: .\myproject (相对路径) +或: 直接回车使用默认目录 +``` + +**Linux/macOS示例:** +``` +请输入要管理的Git仓库目录 (回车使用当前目录: /home/user/quickgit): +输入: /home/yourname/project +或: ./myproject (相对路径) +或: ~/project (使用~表示用户目录) +或: 直接回车使用默认目录 +``` + ### 场景1:初始化新项目 -1. 在项目目录运行脚本 +1. 运行脚本,输入项目目录路径(或直接回车使用当前目录) 2. 选择 `1. 初始化Git仓库` 3. 按提示配置GitHub或Gitea远程仓库 4. 完成首次提交 ### 场景2:提交代码更改 -1. 修改代码后运行脚本 +1. 运行脚本,选择要管理的Git仓库目录 2. 选择 `2. 提交并推送更改` 3. 输入提交信息(或使用默认信息) 4. 选择推送到哪个远程仓库 ### 场景3:拉取远程更新 -1. 运行脚本 +1. 运行脚本,选择要管理的Git仓库目录 2. 选择 `3. 从远程仓库拉取` 3. 选择要拉取的远程仓库 +### 场景4:管理多个项目 + +1. 每次运行脚本都可以选择不同的项目目录 +2. 无需在项目目录中运行脚本 +3. 一个工具管理所有Git项目 + ## 远程仓库配置 ### GitHub配置 diff --git a/quickgit/git_operations.py b/quickgit/git_operations.py index df126fc..95d2e88 100644 --- a/quickgit/git_operations.py +++ b/quickgit/git_operations.py @@ -11,13 +11,36 @@ from .config import Config class GitOperations: """Git操作类""" - def __init__(self): + def __init__(self, work_dir: str = ""): + """ + 初始化Git操作类 + + Args: + work_dir: 工作目录(默认为当前目录) + """ self.executor = CommandExecutor() - self.current_dir = os.getcwd() + if work_dir: + self.current_dir = work_dir + else: + self.current_dir = os.getcwd() + + def change_directory(self, new_dir: str): + """ + 切换工作目录 + + Args: + new_dir: 新的工作目录 + """ + if os.path.isdir(new_dir): + self.current_dir = os.path.abspath(new_dir) + os.chdir(self.current_dir) + else: + raise ValueError(f"目录不存在: {new_dir}") def is_git_repo(self) -> bool: """检查当前目录是否是Git仓库""" - return os.path.isdir('.git') + git_dir = os.path.join(self.current_dir, '.git') + return os.path.isdir(git_dir) def init_repo(self) -> bool: """ diff --git a/quickgit/ui.py b/quickgit/ui.py index a07e5a0..6b95e60 100644 --- a/quickgit/ui.py +++ b/quickgit/ui.py @@ -2,18 +2,71 @@ UI交互模块 - 处理用户界面和交互逻辑 """ +import os from .git_operations import GitOperations from .remote_manager import RemoteManager -from .utils import OutputFormatter, InputValidator, Colors +from .utils import OutputFormatter, InputValidator, Colors, PlatformUtils class GitManagerUI: """Git管理器UI""" - def __init__(self): - self.git_ops = GitOperations() + def __init__(self, work_dir: str = ""): + """ + 初始化UI + + Args: + work_dir: 工作目录(默认为当前目录) + """ + self.git_ops = GitOperations(work_dir) self.remote_mgr = RemoteManager() + def select_work_directory(self): + """让用户选择工作目录""" + from .utils import Colors + + print(f"\n{Colors.BRIGHT_CYAN}{'=' * 60}") + print(f"{Colors.BRIGHT_MAGENTA}{Colors.BOLD} QuickGit - 萌芽一键Git管理工具 v1.0{Colors.ENDC}") + print(f"{Colors.BRIGHT_CYAN}{'=' * 60}{Colors.ENDC}") + + # 获取当前目录 + current_dir = os.getcwd() + + # 显示平台信息和路径示例 + platform = PlatformUtils.get_platform_name() + print(f"{Colors.BRIGHT_YELLOW}当前平台:{Colors.ENDC} {Colors.WHITE}{platform}{Colors.ENDC}") + print(f"{Colors.BRIGHT_YELLOW}脚本目录:{Colors.ENDC} {Colors.WHITE}{current_dir}{Colors.ENDC}") + print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}") + + # 显示路径输入提示 + OutputFormatter.info("请输入要管理的Git仓库目录") + + # 根据平台显示不同的示例 + if PlatformUtils.is_windows(): + OutputFormatter.tip("Windows路径示例: C:\\Users\\YourName\\project") + OutputFormatter.tip("或使用相对路径: .\\myproject") + else: + OutputFormatter.tip("Linux/macOS路径示例: /home/yourname/project") + OutputFormatter.tip("或使用相对路径: ./myproject 或 ~/project") + + print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}") + + # 获取用户输入的目录 + work_dir = InputValidator.get_directory( + f"{Colors.BRIGHT_CYAN}>> 请输入目录路径{Colors.ENDC}", + default=current_dir + ) + + # 切换到工作目录 + try: + self.git_ops.change_directory(work_dir) + OutputFormatter.success(f"已切换到工作目录: {work_dir}") + except Exception as e: + OutputFormatter.error(f"切换目录失败: {str(e)}") + return False + + return True + def show_welcome(self): """显示欢迎信息""" from .utils import Colors @@ -43,6 +96,11 @@ class GitManagerUI: OutputFormatter.menu_item(5, "管理远程仓库") OutputFormatter.menu_item(6, "退出程序") print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}") + + # 显示永久提示 + OutputFormatter.tip("提交代码前建议先拉取最新代码,减少代码冲突") + OutputFormatter.tip("使用SSH进行Git提交更方便快捷和安全") + print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}") def handle_init_repo(self): """处理初始化仓库""" @@ -60,10 +118,6 @@ class GitManagerUI: OutputFormatter.error("当前目录不是Git仓库,请先初始化") return - # 提示:先拉取再提交 - OutputFormatter.tip("建议先拉取最新代码再提交,减少代码冲突") - print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}") - # 检查是否有更改 OutputFormatter.status('running', "检查文件更改中...") @@ -158,6 +212,11 @@ class GitManagerUI: def run(self): """运行主程序""" + # 首先选择工作目录 + if not self.select_work_directory(): + return + + # 显示欢迎信息 self.show_welcome() while True: diff --git a/quickgit/utils.py b/quickgit/utils.py index 7c73b14..1c6e3ab 100644 --- a/quickgit/utils.py +++ b/quickgit/utils.py @@ -206,6 +206,42 @@ class PlatformUtils: return "macOS" else: return "Unknown" + + @staticmethod + def normalize_path(path: str) -> str: + """ + 标准化路径 - 跨平台兼容 + + Args: + path: 输入路径 + + Returns: + 标准化后的绝对路径 + """ + # 展开用户目录 (~) + path = os.path.expanduser(path) + # 转换为绝对路径 + path = os.path.abspath(path) + # 标准化路径分隔符 + path = os.path.normpath(path) + return path + + @staticmethod + def is_valid_directory(path: str) -> bool: + """ + 检查路径是否为有效目录 + + Args: + path: 要检查的路径 + + Returns: + 是否为有效目录 + """ + try: + normalized_path = PlatformUtils.normalize_path(path) + return os.path.isdir(normalized_path) + except Exception: + return False class InputValidator: @@ -272,3 +308,40 @@ class InputValidator: return default return user_input in ['y', 'yes'] + + @staticmethod + def get_directory(prompt: str, default: str = "") -> str: + """ + 获取用户输入的目录路径 + + Args: + prompt: 提示信息 + default: 默认目录(如果用户直接回车) + + Returns: + 标准化后的目录路径 + """ + while True: + # 显示提示 + if default: + user_input = input(f"{prompt} (回车使用当前目录: {default}): ").strip() + else: + user_input = input(f"{prompt}: ").strip() + + # 如果为空,使用默认值 + if not user_input: + if default: + return PlatformUtils.normalize_path(default) + else: + OutputFormatter.error("目录路径不能为空") + continue + + # 标准化路径 + normalized_path = PlatformUtils.normalize_path(user_input) + + # 检查目录是否存在 + if PlatformUtils.is_valid_directory(normalized_path): + return normalized_path + else: + OutputFormatter.error(f"目录不存在: {normalized_path}") + OutputFormatter.info("请输入有效的目录路径") diff --git a/菜单提示说明.md b/菜单提示说明.md new file mode 100644 index 0000000..99f1bb5 --- /dev/null +++ b/菜单提示说明.md @@ -0,0 +1,164 @@ +# QuickGit 主菜单提示功能 + +## 功能说明 + +在主菜单下方添加了两条永久提示,帮助用户养成良好的 Git 使用习惯。 + +--- + +## 显示效果 + +``` +============================================================ +>> 主菜单 +------------------------------------------------------------ +[1] 初始化Git仓库 +[2] 提交并推送更改 +[3] 从远程仓库拉取 +[4] 查看仓库状态 +[5] 管理远程仓库 +[6] 退出程序 +------------------------------------------------------------ +[*] 提交代码前建议先拉取最新代码,减少代码冲突 +[*] 使用SSH进行Git提交更方便快捷和安全 +------------------------------------------------------------ +>> 请输入选项 [1-6]: +``` + +--- + +## 提示内容 + +### 提示 1: 先拉取再提交 +``` +[*] 提交代码前建议先拉取最新代码,减少代码冲突 +``` + +**目的:** +- 避免代码冲突 +- 养成良好的协作习惯 +- 减少合并错误 + +**最佳实践流程:** +1. 先执行 `[3] 从远程仓库拉取` +2. 解决可能的冲突 +3. 再执行 `[2] 提交并推送更改` + +### 提示 2: 使用 SSH +``` +[*] 使用SSH进行Git提交更方便快捷和安全 +``` + +**优势:** +- **更安全**: 使用公钥加密,不需要每次输入密码 +- **更快捷**: 配置一次,永久使用 +- **更方便**: 无需记住密码,自动认证 + +**SSH 配置步骤:** + +1. **生成 SSH 密钥:** + ```bash + ssh-keygen -t ed25519 -C "your_email@example.com" + ``` + +2. **查看公钥:** + ```bash + # Linux/macOS + cat ~/.ssh/id_ed25519.pub + + # Windows + type %USERPROFILE%\.ssh\id_ed25519.pub + ``` + +3. **添加公钥到远程仓库:** + - GitHub: Settings → SSH and GPG keys → New SSH key + - Gitea: Settings → SSH/GPG Keys → Add Key + +4. **测试连接:** + ```bash + # GitHub + ssh -T git@github.com + + # Gitea + ssh -T git@repo.shumengya.top -p 8022 + ``` + +--- + +## 设计原则 + +### 1. 醒目但不干扰 +- 使用青色 `[*]` 图标 +- 位置在菜单选项之后 +- 不影响菜单选择 + +### 2. 简洁明了 +- 每条提示不超过 30 个字符 +- 直接说明要点 +- 易于理解 + +### 3. 实用性强 +- 针对常见问题 +- 提供最佳实践 +- 帮助用户避免错误 + +--- + +## 技术实现 + +### 代码位置 +文件: `quickgit/ui.py` + +### 实现代码 +```python +def show_main_menu(self): + """显示主菜单""" + print(f"\n{Colors.BRIGHT_MAGENTA}{Colors.BOLD}>> 主菜单{Colors.ENDC}") + print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}") + + OutputFormatter.menu_item(1, "初始化Git仓库") + OutputFormatter.menu_item(2, "提交并推送更改") + OutputFormatter.menu_item(3, "从远程仓库拉取") + OutputFormatter.menu_item(4, "查看仓库状态") + OutputFormatter.menu_item(5, "管理远程仓库") + OutputFormatter.menu_item(6, "退出程序") + print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}") + + # 显示永久提示 + OutputFormatter.tip("提交代码前建议先拉取最新代码,减少代码冲突") + OutputFormatter.tip("使用SSH进行Git提交更方便快捷和安全") + print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}") +``` + +### 使用的函数 +```python +# 定义在 quickgit/utils.py +OutputFormatter.tip(text: str) +# 显示格式: [*] + 青色文字 +``` + +--- + +## 用户反馈 + +这些提示将帮助用户: +- ✅ 减少代码冲突 +- ✅ 了解 SSH 的优势 +- ✅ 养成良好的 Git 工作流程 +- ✅ 提高工作效率 +- ✅ 增强代码安全性 + +--- + +## 未来扩展 + +可以考虑添加更多实用提示: +- Git 分支管理建议 +- .gitignore 配置提示 +- 提交信息规范建议 +- 标签使用说明 + +但要注意: +- 提示数量不宜过多(建议不超过 3 条) +- 内容要简洁实用 +- 避免信息过载