这是一个用C++实现的Git版本控制系统,参与CodeCrafters的"Build Your Own Git"挑战。
本项目实现了一个功能完整的Git版本控制系统,支持以下核心功能:
- ✅ 仓库初始化 (
git init) - ✅ 对象操作 (
git hash-object,git cat-file) - ✅ 树对象操作 (
git write-tree,git ls-tree) - ✅ 提交管理 (
git commit-tree) - ✅ 远程仓库克隆 (
git clone)
- 语言: C++17
- 构建系统: CMake
- 依赖管理: vcpkg
- 核心依赖: OpenSSL, ZLIB, libcurl
├── CMakeLists.txt # CMake构建配置
├── vcpkg.json # vcpkg依赖配置
├── your_program.sh # 本地运行脚本
├── include/ # 头文件目录
│ ├── commands.h # Git命令处理函数声明
│ ├── debug.h # 调试工具
│ └── git_clone.h # Git克隆功能声明
└── src/cpp/ # 源代码目录
├── main.cpp # 程序入口点和命令分发
├── commands.cpp # Git命令实现
└── git_clone.cpp # Git克隆功能实现
确保系统已安装以下工具:
- CMake 3.13+
- C++17兼容的编译器
- vcpkg (推荐用于依赖管理)
-
克隆仓库:
git clone <repository-url> cd codecrafters-git-cpp
-
构建项目:
./your_program.sh --build-only
或者手动构建:
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake cmake --build ./build
使用提供的脚本运行:
./your_program.sh <command> [options]或直接运行可执行文件:
./build/git <command> [options]./your_program.sh init创建.git目录结构,包括objects、refs目录和HEAD文件。
./your_program.sh hash-object -w <file>将文件内容存储为Git blob对象并返回SHA1哈希。
./your_program.sh cat-file -p <hash>显示指定哈希的Git对象内容。
./your_program.sh write-tree将当前目录结构创建为Git树对象。
./your_program.sh ls-tree --name-only <tree-hash>显示树对象中的文件和目录列表。
./your_program.sh commit-tree <tree-sha> -p <parent-sha> -m "提交信息"基于树对象创建提交对象。
./your_program.sh commit-tree <tree-sha> -m "初始提交"./your_program.sh clone <repository-url> <directory>克隆远程仓库到指定目录。
.git目录,请在其他目录中测试。
-
使用别名 (推荐):
alias mygit=/path/to/your/repo/your_program.sh mkdir -p /tmp/testing && cd /tmp/testing mygit init
-
直接运行:
mkdir -p /tmp/testing && cd /tmp/testing /path/to/your/repo/your_program.sh init
# 创建测试目录
mkdir -p /tmp/git-test && cd /tmp/git-test
# 初始化仓库
/path/to/codecrafters-git-cpp/your_program.sh init
# 创建测试文件
echo "Hello, Git!" > test.txt
# 创建blob对象
/path/to/codecrafters-git-cpp/your_program.sh hash-object -w test.txt
# 创建树对象
/path/to/codecrafters-git-cpp/your_program.sh write-tree
# 创建提交
/path/to/codecrafters-git-cpp/your_program.sh commit-tree <tree-sha> -m "初始提交"程序包含调试输出,可通过std::cerr查看日志信息。
- 使用C++17标准特性
- 遵循Git对象格式规范
- 确保内存安全和异常处理
- 保持代码风格一致
- 目前仅支持基本的Git命令
- 不支持分支操作
- 不支持合并功能
- 网络协议实现较为简化
本项目遵循MIT许可证。