factory/update_changelog.sh

69 lines
1.9 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 设置默认版本号 (格式: 2.7.YYYYMMDDHH)
DEFAULT_VERSION="2.8.$(date '+%Y%m%d%H')"
# 获取参数 (起始tag)
TARGET_TAG="$1"
# 获取最后一个tag
LAST_TAG=$(git describe --abbrev=0 --tags 2>/dev/null)
# 确定版本范围
if [ -z "$TARGET_TAG" ]; then
if [ -z "$LAST_TAG" ]; then
echo "没有找到任何tag将从头开始生成"
RANGE=""
else
RANGE="$LAST_TAG..HEAD"
fi
else
RANGE="$TARGET_TAG..HEAD"
fi
# 初始化临时文件
TEMP_FILE=$(mktemp)
# 生成变更日志头
echo "## $DEFAULT_VERSION" > "$TEMP_FILE"
echo >> "$TEMP_FILE"
# 按类型分类提交记录
process_commits() {
local type="$1"
local header="$2"
local pattern="$3"
# 查找匹配类型的提交
git log $RANGE --no-merges --pretty=format:"%s | %an | %ad" --date=short | grep -E "$pattern" | while read -r line; do
COMMIT_MSG=$(echo "$line" | cut -d'|' -f1 | sed 's/^ *//;s/ *$//')
AUTHOR=$(echo "$line" | cut -d'|' -f2 | sed 's/^ *//;s/ *$//')
DATE=$(echo "$line" | cut -d'|' -f3 | sed 's/^ *//;s/ *$//')
# 如果是第一次找到该类型,先打印标题
if [ $printed_header -eq 0 ]; then
echo "- $header" >> "$TEMP_FILE"
printed_header=1
fi
# 打印提交信息(移除类型前缀)
CLEAN_MSG=$(echo "$COMMIT_MSG" | sed -E "s/^$type:\s*//i")
echo " - $CLEAN_MSG [$AUTHOR]" >> "$TEMP_FILE"
done
}
# 处理各类型提交(按优先级排序)
printed_header=0; process_commits "feat" "feat: 新增功能" "^[fF]eat"
printed_header=0; process_commits "fix" "fix: 问题修复" "^[fF]ix"
printed_header=0; process_commits "" "other: 其他变更" "^((?![fF]eat|[fF]ix).)*$"
# 合并到原文件
if [ -f changelog.md ]; then
cat "$TEMP_FILE" changelog.md > changelog.md.tmp && mv changelog.md.tmp changelog.md
rm "$TEMP_FILE"
else
mv "$TEMP_FILE" changelog.md
fi
echo "变更日志已更新到 changelog.md"
echo "当前版本号: $DEFAULT_VERSION (请手动修改)"