Skip to content

Commit 00006b4

Browse files
authored
Update main.yml
1 parent 5b21381 commit 00006b4

1 file changed

Lines changed: 40 additions & 32 deletions

File tree

.github/workflows/main.yml

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ on:
66
repo_url:
77
description: 'Git 仓库 URL'
88
required: true
9+
branch:
10+
description: '仓库分支 (默认使用默认分支)'
11+
required: false
12+
default: ''
913
start_commit:
1014
description: '起始提交'
1115
required: true
@@ -28,56 +32,59 @@ jobs:
2832
- name: 检出当前仓库
2933
uses: actions/checkout@v4
3034

31-
- name: 生成补丁文件
32-
id: generate
35+
- name: 准备环境
3336
run: |
34-
#!/bin/bash
35-
set -euo pipefail
36-
37-
# 修正输入参数访问方式
38-
REPO_URL="${{ github.event.inputs.repo_url }}"
39-
START_COMMIT="${{ github.event.inputs.start_commit }}"
40-
END_COMMIT="${{ github.event.inputs.end_commit }}"
41-
PATCH_TYPE="${{ github.event.inputs.patch_type }}"
42-
OUTPUT_DIR="./patches"
37+
mkdir -p source_repo
38+
echo "准备完成"
4339
44-
# 创建临时目录
45-
TEMP_DIR=$(mktemp -d)
46-
echo "临时工作目录: $TEMP_DIR"
47-
48-
# 克隆仓库(单分支完整历史)
49-
echo "正在克隆仓库(单分支完整历史)..."
50-
git clone --single-branch "$REPO_URL" "$TEMP_DIR"
51-
cd "$TEMP_DIR"
40+
- name: 克隆源仓库
41+
run: |
42+
cd source_repo
43+
# 使用轻量克隆
44+
if [ -n "${{ github.event.inputs.branch }}" ]; then
45+
echo "正在克隆 ${{ github.event.inputs.repo_url }} 的分支: ${{ github.event.inputs.branch }}"
46+
git clone --depth 1 --single-branch --branch "${{ github.event.inputs.branch }}" "${{ github.event.inputs.repo_url }}" .
47+
else
48+
echo "正在克隆 ${{ github.event.inputs.repo_url }} 的默认分支"
49+
git clone --depth 1 --single-branch "${{ github.event.inputs.repo_url }}" .
50+
fi
5251
52+
- name: 生成补丁文件
53+
id: generate
54+
run: |
55+
cd source_repo
56+
5357
# 获取实际提交哈希
5458
echo "正在解析提交哈希..."
55-
START_HASH=$(git rev-parse "$START_COMMIT")
56-
END_HASH=$(git rev-parse "$END_COMMIT")
57-
59+
START_HASH=$(git rev-parse "${{ github.event.inputs.start_commit }}")
60+
END_HASH=$(git rev-parse "${{ github.event.inputs.end_commit }}")
61+
5862
# 验证提交是否存在
5963
if ! git rev-parse --verify "$START_HASH" >/dev/null 2>&1; then
60-
echo "::error::起始提交 '$START_COMMIT' 不存在"
64+
echo "::error::起始提交 '${{ github.event.inputs.start_commit }}' 不存在"
6165
exit 1
6266
fi
6367
6468
if ! git rev-parse --verify "$END_HASH" >/dev/null 2>&1; then
65-
echo "::error::结束提交 '$END_COMMIT' 不存在"
69+
echo "::error::结束提交 '${{ github.event.inputs.end_commit }}' 不存在"
6670
exit 1
6771
fi
6872
6973
# 创建输出目录
74+
OUTPUT_DIR="../patches"
7075
mkdir -p "$OUTPUT_DIR"
7176
PATCH_COUNT=0
7277
7378
# 根据选择的类型生成补丁
74-
if [ "$PATCH_TYPE" = "单个" ]; then
79+
if [ "${{ github.event.inputs.patch_type }}" = "单个" ]; then
7580
echo "正在生成单个补丁文件..."
7681
git diff "$START_HASH" "$END_HASH" > "$OUTPUT_DIR/combined.patch"
7782
PATCH_COUNT=1
7883
echo "已生成单个补丁文件: combined.patch"
7984
else
8085
echo "正在生成多个补丁文件..."
86+
# 获取完整历史以生成多个补丁
87+
git fetch --unshallow
8188
git format-patch "$START_HASH..$END_HASH" -o "$OUTPUT_DIR" --quiet
8289
PATCH_COUNT=$(ls -1 "$OUTPUT_DIR"/*.patch 2>/dev/null | wc -l)
8390
echo "已生成 $PATCH_COUNT 个补丁文件"
@@ -86,7 +93,7 @@ jobs:
8693
# 设置输出变量
8794
echo "patch_dir=$OUTPUT_DIR" >> $GITHUB_OUTPUT
8895
echo "patch_count=$PATCH_COUNT" >> $GITHUB_OUTPUT
89-
echo "patch_type=$PATCH_TYPE" >> $GITHUB_OUTPUT
96+
echo "patch_type=${{ github.event.inputs.patch_type }}" >> $GITHUB_OUTPUT
9097
9198
- name: 创建时间戳命名的 ZIP 文件
9299
id: create_zip
@@ -95,20 +102,21 @@ jobs:
95102
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
96103
ZIP_NAME="patch_$TIMESTAMP.zip"
97104
98-
# 进入补丁目录
99-
cd ${{ steps.generate.outputs.patch_dir }}
100-
101105
# 创建 ZIP 文件
106+
cd ${{ steps.generate.outputs.patch_dir }}
102107
zip -r "../$ZIP_NAME" .
103-
104-
# 返回上级目录
105108
cd ..
106109
107110
# 设置输出变量
108111
echo "zip_name=$ZIP_NAME" >> $GITHUB_OUTPUT
109112
echo "zip_path=$(pwd)/$ZIP_NAME" >> $GITHUB_OUTPUT
110113
echo "已创建 ZIP 文件: $ZIP_NAME"
111114
115+
- name: 清理源仓库
116+
run: |
117+
rm -rf source_repo
118+
echo "已清理源仓库"
119+
112120
- name: 上传 ZIP 文件到工作流产物
113121
uses: actions/upload-artifact@v4
114122
with:
@@ -118,7 +126,7 @@ jobs:
118126

119127
- name: 显示结果
120128
run: |
121-
if [ "${{ steps.generate.outputs.patch_type }}" = "单个" ]; then
129+
if [ "${{ github.event.inputs.patch_type }}" = "单个" ]; then
122130
echo "成功生成单个补丁文件: combined.patch"
123131
else
124132
echo "成功生成 ${{ steps.generate.outputs.patch_count }} 个补丁文件"

0 commit comments

Comments
 (0)