Skip to content

Commit e5037dd

Browse files
committed
added pattern to skip commits using comments on commit for cherry pick merge between branches
1 parent 62c3ffa commit e5037dd

File tree

1 file changed

+83
-6
lines changed

1 file changed

+83
-6
lines changed

.github/workflows/sync-version-branches.yml

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,28 @@ jobs:
127127
128128
# Track failed commits for reporting
129129
failed_commits_file="/tmp/failed_commits.txt"
130+
ignored_commits_file="/tmp/ignored_commits.txt"
130131
touch "$failed_commits_file"
132+
touch "$ignored_commits_file"
131133
successful_count=0
132134
failed_count=0
135+
ignored_count=0
133136
134137
# Cherry-pick each commit individually to avoid merge conflicts
135138
while read commit; do
136139
commit_msg=$(git log -1 --format="%s" "$commit")
140+
commit_full_msg=$(git log -1 --format="%B" "$commit")
141+
142+
# Check if commit should be ignored (check for ignore patterns in commit message)
143+
# Patterns: [skip-sync], [no-sync], [ignore-sync], [skip-branch-sync]
144+
if echo "$commit_msg" | grep -qiE '\[(skip-sync|no-sync|ignore-sync|skip-branch-sync)\]' || \
145+
echo "$commit_full_msg" | grep -qiE '\[(skip-sync|no-sync|ignore-sync|skip-branch-sync)\]'; then
146+
echo "⏭️ Skipping ignored commit $commit: $commit_msg"
147+
echo "$commit" >> "$ignored_commits_file"
148+
ignored_count=$((ignored_count + 1))
149+
continue
150+
fi
151+
137152
echo "🍒 Cherry-picking $commit: $commit_msg"
138153
139154
# Try cherry-pick with conflict resolution
@@ -171,22 +186,69 @@ jobs:
171186
172187
# If we have failed commits OR want to ensure all changes are included, do a merge
173188
# This ensures ALL changes from main are included, even if cherry-pick missed some
174-
echo "🔄 Ensuring all changes from main are included..."
189+
echo "🔄 Ensuring all changes from main are included (excluding ignored commits)..."
175190
176191
# First, check what files differ between our current state and main
177192
git add -A
178193
current_diff=$(git diff --cached --name-only)
179194
180-
# Now merge main to get ALL changes (this is a safety net)
181-
if git merge --no-commit --no-ff main -m "Merge main to include all missing commits" 2>&1; then
182-
echo "✅ Merge successful, all commits from main are now included"
195+
# If there are ignored commits, create a cleaned version of main without them
196+
merge_source="main"
197+
if [ -s "$ignored_commits_file" ] && [ "$ignored_count" -gt 0 ]; then
198+
echo "🧹 Creating cleaned branch without ignored commits..."
199+
cleaned_branch="main-cleaned-$(date +%s)"
200+
git checkout -b "$cleaned_branch" main
201+
202+
# Revert ignored commits in reverse order (newest first)
203+
# Read commits into array and reverse
204+
mapfile -t ignored_commits < "$ignored_commits_file"
205+
for ((idx=${#ignored_commits[@]}-1; idx>=0; idx--)); do
206+
commit="${ignored_commits[idx]}"
207+
echo " Reverting ignored commit: $commit"
208+
git revert --no-commit "$commit" 2>/dev/null || true
209+
done
210+
211+
# Commit the reverts if there are any changes
212+
if ! git diff --cached --quiet || ! git diff --quiet; then
213+
git commit -m "Revert ignored commits for branch sync" || true
214+
fi
215+
216+
# Go back to sync branch
217+
git checkout "$sync_branch"
218+
merge_source="$cleaned_branch"
219+
echo "✅ Using cleaned branch for merge (excludes $ignored_count ignored commits)"
220+
fi
221+
222+
# Now merge the source (main or cleaned branch) to get ALL changes (this is a safety net)
223+
if git merge --no-commit --no-ff "$merge_source" -m "Merge $merge_source to include all missing commits" 2>&1; then
224+
echo "✅ Merge successful, all non-ignored commits from main are now included"
183225
else
184226
# Merge has conflicts, resolve by keeping target branch files where specified
185227
echo "⚠️ Merge has conflicts, will resolve by keeping target branch files where needed"
186228
# Conflicts will be resolved in the file reset section below
187229
fi
188230
189-
echo "📊 Summary: $successful_count successful cherry-picks, $failed_count failed (included via merge)"
231+
# Clean up temporary cleaned branch if it was created
232+
if [ "$merge_source" != "main" ]; then
233+
git branch -D "$cleaned_branch" 2>/dev/null || true
234+
fi
235+
236+
echo "📊 Summary: $successful_count successful cherry-picks, $failed_count failed (included via merge), $ignored_count ignored"
237+
238+
# Show ignored commits if any and store for PR body
239+
ignored_commits_info=""
240+
if [ -s "$ignored_commits_file" ]; then
241+
echo "⏭️ Ignored commits (marked with [skip-sync], [no-sync], [ignore-sync], or [skip-branch-sync]):"
242+
while read commit; do
243+
commit_msg=$(git log -1 --format="%s" "$commit" 2>/dev/null || echo "unknown")
244+
echo " - $commit: $commit_msg"
245+
ignored_commits_info="${ignored_commits_info}- \`$commit\`: $commit_msg"$'\n'
246+
done < "$ignored_commits_file"
247+
# Store ignored commits info for PR body
248+
echo "$ignored_commits_info" > ./ignored_commits_info.txt
249+
else
250+
echo "" > ./ignored_commits_info.txt
251+
fi
190252
else
191253
# Even if no commits found via log, check if there are file differences
192254
echo "ℹ️ No commits found via log, checking for file differences..."
@@ -322,22 +384,37 @@ jobs:
322384
# Get the target .NET version number for display
323385
dotnet_version=$(echo "${{ matrix.target_branch }}" | sed 's/[^0-9]*//g')
324386
387+
# Read ignored commits info if it exists
388+
ignored_section=""
389+
if [ -f "./ignored_commits_info.txt" ] && [ -s "./ignored_commits_info.txt" ]; then
390+
ignored_commits_list=$(cat ./ignored_commits_info.txt)
391+
ignored_section=$(cat << EOF
392+
393+
### ⏭️ Ignored Commits:
394+
The following commits were skipped (marked with \`[skip-sync]\`, \`[no-sync]\`, \`[ignore-sync]\`, or \`[skip-branch-sync]\`):
395+
${ignored_commits_list}
396+
EOF
397+
)
398+
fi
399+
325400
# Create PR using GitHub CLI (gh)
326401
PR_BODY=$(cat << EOF
327402
## 🤖 Automated Branch Sync
328403
329404
This PR syncs recent changes from \`main\` branch to \`${{ matrix.target_branch }}\`.
330405
331406
### Changes Applied:
332-
- ✅ Applied recent commits from main
407+
- ✅ Applied recent commits from main (using cherry-pick)
333408
- ✅ Updated version numbers (10.x.x → ${dotnet_version}.x.x)
334409
- ✅ Updated target framework (net10.0 → net${dotnet_version}.0)
335410
- ✅ Preserved .csproj files from ${{ matrix.target_branch }} branch
411+
${ignored_section}
336412
337413
### Review Notes:
338414
- All .csproj files maintain ${{ matrix.target_branch }} configurations
339415
- Directory.Build.props has been updated for ${{ matrix.target_branch }} compatibility
340416
- Ready for testing on ${{ matrix.target_branch }} environment
417+
- Commits marked with ignore tags were excluded from this sync
341418
342419
---
343420
_Created automatically by branch sync workflow_

0 commit comments

Comments
 (0)