1+ import os
2+ import re
3+ import subprocess
4+
5+ def run_git_command (command ):
6+ result = subprocess .getoutput (command )
7+ print (f"Git Command: { command } " )
8+ print (f"Git Output: { result } " )
9+ return result
10+
11+ def parse_merge_commit (line ):
12+ # Parse merge commit messages like:
13+ # "355dc82 Merge pull request #71 from RooVetGit/better-error-handling"
14+ pattern = r"([a-f0-9]+)\s+Merge pull request #(\d+) from (.+)"
15+ match = re .match (pattern , line )
16+ if match :
17+ sha , pr_number , branch = match .groups ()
18+ return {
19+ 'sha' : sha ,
20+ 'pr_number' : pr_number ,
21+ 'branch' : branch
22+ }
23+ return None
24+
25+ def get_version_refs ():
26+ # Get the merge commits with full message
27+ command = 'git log --merges --pretty=oneline -n 3'
28+ result = run_git_command (command )
29+
30+ if result :
31+ commits = result .split ('\n ' )
32+ if len (commits ) >= 3 :
33+ # Parse HEAD~1 (PR to generate notes for)
34+ head_info = parse_merge_commit (commits [1 ])
35+ # Parse HEAD~2 (previous PR to compare against)
36+ base_info = parse_merge_commit (commits [2 ])
37+
38+ if head_info and base_info :
39+ # Set output for GitHub Actions
40+ with open (os .environ ['GITHUB_OUTPUT' ], 'a' ) as gha_outputs :
41+ gha_outputs .write (f"head_ref={ head_info ['sha' ]} \n " )
42+ gha_outputs .write (f"base_ref={ base_info ['sha' ]} " )
43+
44+ print (f"Head ref (PR #{ head_info ['pr_number' ]} ): { head_info ['sha' ]} " )
45+ print (f"Base ref (PR #{ base_info ['pr_number' ]} ): { base_info ['sha' ]} " )
46+ return head_info , base_info
47+
48+ print ("Could not find or parse sufficient merge history" )
49+ return None , None
50+
51+ if __name__ == "__main__" :
52+ head_info , base_info = get_version_refs ()
0 commit comments