嵌入式版本控制
概述
嵌入式开发中,版本控制不仅管理代码,还涉及固件版本、硬件版本、配置版本等多个维度。
Git基础
初始化和配置
| Bash |
|---|
| # 初始化仓库
git init
# 配置用户信息
git config user.name "Your Name"
git config user.email "your@email.com"
# 配置默认分支
git config init.defaultBranch main
|
基本操作
| Bash |
|---|
| # 查看状态
git status
# 添加文件
git add .
git add file.c
# 提交
git commit -m "Add new feature"
# 查看日志
git log --oneline
git log --graph --oneline --all
# 查看差异
git diff
git diff HEAD~1
|
分支操作
| Bash |
|---|
| # 创建分支
git branch feature/new-driver
# 切换分支
git checkout feature/new-driver
git switch feature/new-driver
# 创建并切换
git checkout -b feature/new-driver
# 合并分支
git checkout main
git merge feature/new-driver
# 删除分支
git branch -d feature/new-driver
# 查看分支
git branch -a
|
远程操作
| Bash |
|---|
| # 添加远程仓库
git remote add origin https://github.com/user/repo.git
# 推送
git push origin main
git push -u origin main # 设置上游
# 拉取
git pull origin main
git fetch origin
# 克隆
git clone https://github.com/user/repo.git
git clone --depth 1 https://github.com/user/repo.git # 浅克隆
|
Git LFS(大文件存储)
嵌入式开发中常有大的二进制文件(SDK、工具链、镜像等)。
| Bash |
|---|
| # 安装Git LFS
sudo apt install git-lfs
git lfs install
# 跟踪大文件
git lfs track "*.bin"
git lfs track "*.img"
git lfs track "sdk/*"
# 查看跟踪规则
git lfs track
# 查看LFS文件
git lfs ls-files
|
.gitattributes
| Text Only |
|---|
| *.bin filter=lfs diff=lfs merge=lfs -text
*.img filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
|
子模块(Submodule)
管理依赖的库或SDK。
| Bash |
|---|
| # 添加子模块
git submodule add https://github.com/user/library.git libs/library
# 初始化和更新
git submodule init
git submodule update
# 克隆时自动初始化
git clone --recursive https://github.com/user/repo.git
# 更新子模块
git submodule update --remote
# 删除子模块
git submodule deinit libs/library
git rm libs/library
|
固件版本管理
版本号规范
| Text Only |
|---|
| 主版本.次版本.修订版本.构建版本
例:1.2.3.456
主版本:重大变更、架构改变
次版本:新功能添加
修订版本:Bug修复
构建版本:编译次数/日期
|
版本头文件
| C |
|---|
| // version.h
#ifndef __VERSION_H__
#define __VERSION_H__
#define VERSION_MAJOR 1
#define VERSION_MINOR 2
#define VERSION_PATCH 3
#define VERSION_BUILD 456
#define VERSION_STRING "1.2.3.456"
#define VERSION_DATE "2024-01-15"
#define VERSION_TIME "14:30:00"
#define VERSION_GIT_HASH "abc1234"
void print_version(void);
#endif
|
自动生成版本信息
| Makefile |
|---|
| # Makefile
GIT_HASH := $(shell git rev-parse --short HEAD)
BUILD_DATE := $(shell date +%Y-%m-%d)
BUILD_TIME := $(shell date +%H:%M:%S)
CFLAGS += -DGIT_HASH=\"$(GIT_HASH)\"
CFLAGS += -DBUILD_DATE=\"$(BUILD_DATE)\"
CFLAGS += -DBUILD_TIME=\"$(BUILD_TIME)\"
|
| C |
|---|
| // version.c
#include "version.h"
#include <stdio.h>
void print_version(void)
{
printf("Firmware Version: %s\n", VERSION_STRING);
printf("Build Date: %s %s\n", BUILD_DATE, BUILD_TIME);
printf("Git Hash: %s\n", GIT_HASH);
}
|
Git Hooks
pre-commit钩子
| Bash |
|---|
| #!/bin/bash
# .git/hooks/pre-commit
# 检查代码格式
if ! git diff --cached --name-only | grep '\.c$' | xargs clang-format --dry-run --Werror; then
echo "Code format check failed!"
exit 1
fi
# 检查大文件
large_files=$(git diff --cached --name-only | xargs -I {} sh -c 'if [ -f "{}" ] && [ $(stat -f%z "{}") -gt 1048576 ]; then echo "{}"; fi')
if [ -n "$large_files" ]; then
echo "Large files detected (>1MB):"
echo "$large_files"
exit 1
fi
exit 0
|
pre-push钩子
| Bash |
|---|
| #!/bin/bash
# .git/hooks/pre-push
# 运行测试
make test
if [ $? -ne 0 ]; then
echo "Tests failed!"
exit 1
fi
exit 0
|
CI/CD集成
GitHub Actions示例
| YAML |
|---|
| # .github/workflows/build.yml
name: Build Firmware
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Install Toolchain
run: |
sudo apt install gcc-arm-none-eabi
- name: Build
run: |
make all
- name: Run Tests
run: |
make test
- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: firmware
path: output/firmware.bin
|
发布管理
创建发布标签
| Bash |
|---|
| # 创建标签
git tag -a v1.2.3 -m "Release version 1.2.3"
# 推送标签
git push origin v1.2.3
# 删除标签
git tag -d v1.2.3
git push origin --delete v1.2.3
# 查看标签
git tag -l
git show v1.2.3
|
发布分支策略
| Text Only |
|---|
| main (稳定版本)
│
├── develop (开发分支)
│ │
│ ├── feature/xxx (功能分支)
│ ├── feature/yyy
│ └── ...
│
├── release/1.2.0 (发布分支)
│ │
│ └── → main (合并后打tag)
│
└── hotfix/xxx (紧急修复)
|
常用Git别名
| Bash |
|---|
| git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --graph --oneline --all"
git config --global alias.df diff
git config --global alias.ds "diff --staged"
|
参考资料