Get the name of the default branch for the given remote It is typically called 'main', but may differ
(remote_name)
| 93 | |
| 94 | |
| 95 | def get_git_remote_default_branch(remote_name): |
| 96 | """Get the name of the default branch for the given remote |
| 97 | |
| 98 | It is typically called 'main', but may differ |
| 99 | """ |
| 100 | cmd = f"git remote show {remote_name}".split() |
| 101 | env = os.environ.copy() |
| 102 | env['LANG'] = 'C' |
| 103 | try: |
| 104 | remote_info = subprocess.check_output(cmd, |
| 105 | stderr=subprocess.DEVNULL, |
| 106 | cwd=SRCDIR, |
| 107 | encoding='UTF-8', |
| 108 | env=env) |
| 109 | except subprocess.CalledProcessError: |
| 110 | return None |
| 111 | for line in remote_info.splitlines(): |
| 112 | if "HEAD branch:" in line: |
| 113 | base_branch = line.split(":")[1].strip() |
| 114 | return base_branch |
| 115 | return None |
| 116 | |
| 117 | |
| 118 | @status("Getting base branch for PR", |
no test coverage detected
searching dependent graphs…