@@ -232,6 +232,12 @@ def parse_repo_url(url_string):
232232 default = None ,
233233 help = "Pull request number. If not provided, script attempts to find the latest open PR for the current git branch."
234234 )
235+ parser .add_argument (
236+ "--branch" ,
237+ type = str ,
238+ default = None ,
239+ help = "Branch name to find the latest open PR for. Mutually exclusive with --pull_number. If neither --pull_number nor --branch is provided, uses the current git branch."
240+ )
235241 parser .add_argument (
236242 "--url" ,
237243 type = str ,
@@ -355,26 +361,40 @@ def parse_repo_url(url_string):
355361 sys .exit (1 )
356362
357363 pull_request_number = args .pull_number
358- current_branch_for_pr_check = None # Store branch name if auto-detecting PR
364+ branch_to_find_pr_for = None
365+
366+ if args .pull_number and args .branch :
367+ sys .stderr .write (f"Error: --pull_number and --branch are mutually exclusive.{ error_suffix } \n " )
368+ sys .exit (1 )
369+
359370 if not pull_request_number :
360- sys .stderr .write ("Pull number not specified, attempting to find PR for current branch...\n " )
361- current_branch_for_pr_check = get_current_branch_name ()
362- if current_branch_for_pr_check :
363- sys .stderr .write (f"Current git branch is: { current_branch_for_pr_check } \n " )
364- pull_request_number = get_latest_pr_for_branch (args .token , OWNER , REPO , current_branch_for_pr_check )
371+ if args .branch :
372+ branch_to_find_pr_for = args .branch
373+ sys .stderr .write (f"Pull number not specified, attempting to find PR for branch: { branch_to_find_pr_for } ...\n " )
374+ else :
375+ sys .stderr .write ("Pull number and branch not specified, attempting to find PR for current git branch...\n " )
376+ branch_to_find_pr_for = get_current_branch_name ()
377+ if branch_to_find_pr_for :
378+ sys .stderr .write (f"Current git branch is: { branch_to_find_pr_for } \n " )
379+ else :
380+ sys .stderr .write (f"Error: Could not determine current git branch. Cannot find PR automatically.{ error_suffix } \n " )
381+ sys .exit (1 )
382+
383+ if branch_to_find_pr_for : # This will be true if args.branch was given, or if get_current_branch_name() succeeded
384+ pull_request_number = get_latest_pr_for_branch (args .token , OWNER , REPO , branch_to_find_pr_for )
365385 if pull_request_number :
366- sys .stderr .write (f"Found PR #{ pull_request_number } for branch { current_branch_for_pr_check } .\n " )
386+ sys .stderr .write (f"Found PR #{ pull_request_number } for branch { branch_to_find_pr_for } .\n " )
367387 else :
368- sys .stderr .write (f"No open PR found for branch { current_branch_for_pr_check } in { OWNER } /{ REPO } .\n " ) # Informational
369- else :
370- sys .stderr .write (f"Error: Could not determine current git branch. Cannot find PR automatically.{ error_suffix } \n " )
371- sys .exit (1 )
388+ sys .stderr .write (f"No open PR found for branch { branch_to_find_pr_for } in { OWNER } /{ REPO } .\n " )
389+ # If branch_to_find_pr_for is None here, it means get_current_branch_name() failed and we already exited.
372390
373391 if not pull_request_number : # Final check for PR number
374- error_message = "Error: Pull request number is required."
375- if not args .pull_number and current_branch_for_pr_check : # Auto-detect branch ok, but no PR found
376- error_message = f"Error: Pull request number not specified and no open PR found for branch { current_branch_for_pr_check } ."
377- # The case where current_branch_for_pr_check is None (git branch fail) is caught and exited above.
392+ error_message = "Error: Pull request number could not be determined."
393+ if args .branch : # Specific error if --branch was used
394+ error_message = f"Error: No open PR found for specified branch '{ args .branch } '."
395+ elif not args .pull_number and branch_to_find_pr_for : # Auto-detect current branch ok, but no PR found
396+ error_message = f"Error: Pull request number not specified and no open PR found for current branch '{ branch_to_find_pr_for } '."
397+ # The case where current_branch_for_pr_check (now branch_to_find_pr_for) is None (git branch fail) is caught and exited above.
378398 sys .stderr .write (f"{ error_message } { error_suffix } \n " )
379399 sys .exit (1 )
380400
0 commit comments