mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	bpo-44074: let patchcheck infer the base branch name (GH-25991)
(cherry picked from commit 21fbbb98ba)
Co-authored-by: Leonardo Lai <leonardo.lai@live.com>
			
			
This commit is contained in:
		
							parent
							
								
									4d532d3f8f
								
							
						
					
					
						commit
						fbd9b9939c
					
				
					 2 changed files with 27 additions and 4 deletions
				
			
		|  | @ -0,0 +1 @@ | ||||||
|  | Make patchcheck automatically detect the correct base branch name (previously it was hardcoded to 'master') | ||||||
|  | @ -50,7 +50,8 @@ def get_git_branch(): | ||||||
|     try: |     try: | ||||||
|         return subprocess.check_output(cmd, |         return subprocess.check_output(cmd, | ||||||
|                                        stderr=subprocess.DEVNULL, |                                        stderr=subprocess.DEVNULL, | ||||||
|                                        cwd=SRCDIR) |                                        cwd=SRCDIR, | ||||||
|  |                                        encoding='UTF-8') | ||||||
|     except subprocess.CalledProcessError: |     except subprocess.CalledProcessError: | ||||||
|         return None |         return None | ||||||
| 
 | 
 | ||||||
|  | @ -64,28 +65,49 @@ def get_git_upstream_remote(): | ||||||
|     try: |     try: | ||||||
|         subprocess.check_output(cmd, |         subprocess.check_output(cmd, | ||||||
|                                 stderr=subprocess.DEVNULL, |                                 stderr=subprocess.DEVNULL, | ||||||
|                                 cwd=SRCDIR) |                                 cwd=SRCDIR, | ||||||
|  |                                 encoding='UTF-8') | ||||||
|     except subprocess.CalledProcessError: |     except subprocess.CalledProcessError: | ||||||
|         return "origin" |         return "origin" | ||||||
|     return "upstream" |     return "upstream" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def get_git_remote_default_branch(remote_name): | ||||||
|  |     """Get the name of the default branch for the given remote | ||||||
|  | 
 | ||||||
|  |     It is typically called 'main', but may differ | ||||||
|  |     """ | ||||||
|  |     cmd = "git remote show {}".format(remote_name).split() | ||||||
|  |     try: | ||||||
|  |         remote_info = subprocess.check_output(cmd, | ||||||
|  |                                               stderr=subprocess.DEVNULL, | ||||||
|  |                                               cwd=SRCDIR, | ||||||
|  |                                               encoding='UTF-8') | ||||||
|  |     except subprocess.CalledProcessError: | ||||||
|  |         return None | ||||||
|  |     for line in remote_info.splitlines(): | ||||||
|  |         if "HEAD branch:" in line: | ||||||
|  |             base_branch = line.split(":")[1].strip() | ||||||
|  |             return base_branch | ||||||
|  |     return None | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| @status("Getting base branch for PR", | @status("Getting base branch for PR", | ||||||
|         info=lambda x: x if x is not None else "not a PR branch") |         info=lambda x: x if x is not None else "not a PR branch") | ||||||
| def get_base_branch(): | def get_base_branch(): | ||||||
|     if not os.path.exists(os.path.join(SRCDIR, '.git')): |     if not os.path.exists(os.path.join(SRCDIR, '.git')): | ||||||
|         # Not a git checkout, so there's no base branch |         # Not a git checkout, so there's no base branch | ||||||
|         return None |         return None | ||||||
|  |     upstream_remote = get_git_upstream_remote() | ||||||
|     version = sys.version_info |     version = sys.version_info | ||||||
|     if version.releaselevel == 'alpha': |     if version.releaselevel == 'alpha': | ||||||
|         base_branch = "master" |         base_branch = get_git_remote_default_branch(upstream_remote) | ||||||
|     else: |     else: | ||||||
|         base_branch = "{0.major}.{0.minor}".format(version) |         base_branch = "{0.major}.{0.minor}".format(version) | ||||||
|     this_branch = get_git_branch() |     this_branch = get_git_branch() | ||||||
|     if this_branch is None or this_branch == base_branch: |     if this_branch is None or this_branch == base_branch: | ||||||
|         # Not on a git PR branch, so there's no base branch |         # Not on a git PR branch, so there's no base branch | ||||||
|         return None |         return None | ||||||
|     upstream_remote = get_git_upstream_remote() |  | ||||||
|     return upstream_remote + "/" + base_branch |     return upstream_remote + "/" + base_branch | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Miss Islington (bot)
						Miss Islington (bot)