温馨提示×

Rust代码在Debian如何进行版本控制

小樊
46
2025-11-17 09:18:21
栏目: 编程语言

在 Debian 上对 Rust 代码进行版本控制

一 环境准备

  • 安装 Git:sudo apt update && sudo apt install -y git
  • 安装 Rust 工具链(rustup):curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh;安装后执行 source “$HOME/.cargo/env”
  • 验证:rustc --version、cargo --version
  • 建议同时安装代码质量工具(可选):rustup component add rustfmt clippy,用于统一格式与静态检查

二 初始化 Git 仓库与基本流程

  • 初始化仓库:git init
  • 配置身份(全局):git config --global user.name “Your Name”;git config --global user.email “you@example.com”
  • 忽略构建产物:创建 .gitignore,加入 target/、**/*.rs.bk、.idea/、.vscode/ 等
  • 首次提交:git add .;git commit -m “init project”
  • 远程协作:git remote add origin ;git push -u origin main

三 分支策略与版本打标

  • 分支策略:小型项目可用 GitHub Flow(main 受保护,功能在 feature/* 分支开发,经 PR 合并);中大型项目可用 Git Flow(main/develop/feature/release/hotfix)
  • 版本打标:遵循 语义化版本(SemVer)。例如发布 v1.2.3:
    • 更新版本:cargo set-version 1.2.3(或手动编辑 Cargo.toml 的 version 字段)
    • 提交并打标签:git add Cargo.toml;git commit -m “chore: release v1.2.3”;git tag -a v1.2.3 -m “Release v1.2.3”
    • 推送:git push && git push --tags
  • 提示:库的发布到 crates.io 时使用 cargo publish(遵循 SemVer);应用发布建议同时打 Git 标签以便回溯

四 与 Debian 打包的版本对齐

  • 若需要将 Rust 应用打包为 .deb 并在 Debian 系环境分发,可使用 cargo-deb
    • 安装:cargo install cargo-deb
    • 构建:cargo deb(产物位于 target/debian/*.deb)
    • 安装测试:sudo dpkg -i target/debian/*.deb
    • 版本对齐建议:让 Debian 包的版本与 Git 标签一致(例如 v1.2.3),便于追踪与回滚

五 质量保障与持续集成

  • 本地质量门禁:cargo fmt --check(或 cargo fmt);cargo clippy;cargo test;性能回归可用 cargo bench(可选)
  • CI 建议(GitHub Actions/GitLab CI 示例要点):
    • 在 CI 中安装 rustup 与工具链,执行 cargo fmt、cargo clippy、cargo test
    • 构建并(可选)运行 cargo deb,执行安装/卸载 smoke test
    • 成功构建后自动打标签并推送(仅限受保护分支的发布流程)

0