|
| 1 | +import 'package:changelog_cli/src/model/model.dart'; |
| 2 | +import 'package:mason_logger/mason_logger.dart'; |
| 3 | + |
| 4 | +class RevertDetector { |
| 5 | + /// Detects reverted commits in the changelog entries and marks them appropriately |
| 6 | + static List<ChangelogEntry> detectReverts( |
| 7 | + List<ChangelogEntry> entries, { |
| 8 | + Logger? logger, |
| 9 | + }) { |
| 10 | + final result = <ChangelogEntry>[]; |
| 11 | + final commitRefToEntry = <String, ChangelogEntry>{}; |
| 12 | + |
| 13 | + // Build a map of commit refs to entries for quick lookup |
| 14 | + for (final entry in entries) { |
| 15 | + commitRefToEntry[entry.ref] = entry; |
| 16 | + } |
| 17 | + |
| 18 | + logger?.detail('Detecting reverted commits in ${entries.length} entries'); |
| 19 | + |
| 20 | + for (final entry in entries) { |
| 21 | + final revertInfo = parseRevertCommit(entry.commit.message); |
| 22 | + |
| 23 | + if (revertInfo != null) { |
| 24 | + // This is a revert commit |
| 25 | + logger?.detail('Found revert commit ${entry.ref}: ${revertInfo.revertedCommitRef}'); |
| 26 | + |
| 27 | + // Find the original commit that was reverted |
| 28 | + final revertedEntry = commitRefToEntry[revertInfo.revertedCommitRef]; |
| 29 | + if (revertedEntry != null) { |
| 30 | + // Mark the original commit as reverted |
| 31 | + final updatedRevertedEntry = revertedEntry.copyWith( |
| 32 | + isReverted: true, |
| 33 | + revertedByRef: entry.ref, |
| 34 | + ); |
| 35 | + commitRefToEntry[revertedEntry.ref] = updatedRevertedEntry; |
| 36 | + logger?.detail('Marked commit ${revertedEntry.ref} as reverted by ${entry.ref}'); |
| 37 | + } else { |
| 38 | + logger?.detail('Could not find original commit ${revertInfo.revertedCommitRef} to mark as reverted'); |
| 39 | + } |
| 40 | + |
| 41 | + // Skip revert commits - they shouldn't appear in the changelog |
| 42 | + logger?.detail('Skipping revert commit ${entry.ref} from changelog'); |
| 43 | + } else { |
| 44 | + // Regular commit, add the potentially updated version from our map |
| 45 | + final updatedEntry = commitRefToEntry[entry.ref] ?? entry; |
| 46 | + result.add(updatedEntry); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + final revertedCount = result.where((e) => e.isReverted).length; |
| 51 | + logger?.detail('Found $revertedCount reverted commits'); |
| 52 | + |
| 53 | + return result; |
| 54 | + } |
| 55 | + |
| 56 | + /// Parses a commit message to extract revert information |
| 57 | + static RevertInfo? parseRevertCommit(String commitMessage) { |
| 58 | + // Common revert message patterns |
| 59 | + final revertPatterns = [ |
| 60 | + // Git's default revert format: "Revert "commit title"" |
| 61 | + RegExp(r'^Revert\s+".*"\s*\n\nThis reverts commit\s+([a-f0-9]{7,40})\.?', caseSensitive: false, multiLine: true), |
| 62 | + |
| 63 | + // Alternative format: "Revert commit abc1234" |
| 64 | + RegExp(r'^Revert\s+commit\s+([a-f0-9]{7,40})', caseSensitive: false, multiLine: true), |
| 65 | + |
| 66 | + // GitHub style: "Revert #PR-number" or similar patterns |
| 67 | + RegExp(r'^Revert\s+".*"\s*.*commit\s+([a-f0-9]{7,40})', caseSensitive: false, multiLine: true, dotAll: true), |
| 68 | + |
| 69 | + // Manual revert with "This reverts commit" somewhere in message |
| 70 | + RegExp(r'This reverts commit\s+([a-f0-9]{7,40})\.?', caseSensitive: false, multiLine: true), |
| 71 | + ]; |
| 72 | + |
| 73 | + for (final pattern in revertPatterns) { |
| 74 | + final match = pattern.firstMatch(commitMessage); |
| 75 | + if (match != null) { |
| 76 | + final revertedCommitRef = match.group(1); |
| 77 | + if (revertedCommitRef != null) { |
| 78 | + return RevertInfo(revertedCommitRef: revertedCommitRef); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return null; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Information about a revert commit |
| 88 | +class RevertInfo { |
| 89 | + const RevertInfo({ |
| 90 | + required this.revertedCommitRef, |
| 91 | + }); |
| 92 | + |
| 93 | + final String revertedCommitRef; |
| 94 | +} |
0 commit comments