Skip to content

Commit

Permalink
refactor: removes list and simplifies git default branch
Browse files Browse the repository at this point in the history
Removes the list of possible default branches and put in an if else
statement. Other possible default branches must use the rev option.
  • Loading branch information
Felipe P. Silva committed Aug 22, 2021
1 parent 0fd1c7b commit 37db3ea
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions peru/resources/plugins/git/git_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,22 @@ def plugin_reup(url, reup):

def git_default_branch(url) -> str:
"""
With a list of common default branches names, it checks against
the available branch list if this possible default branch exists.
By default git used to take master as default branch, but newer versions
tend to use main, even trunk or development can be found.
This function checks if the default branch is master.
If it is not found, then it assumes it is main.
For other default branches, user should use the 'rev' option.
Args:
url (str): url from the target repository to be checked.
Returns:
str: returns a possible match for the git default branch.
"""
repo_path = clone_if_needed(url)
default_branches_list = ['master', 'main']
for branch in default_branches_list:
output = git('show-ref', '--verify', '--quiet', 'refs/heads/' + branch,
git_dir=repo_path, checked=False, capture_output=True)
if output.returncode == 0:
return branch
return 'master'
output = git('show-ref', '--verify', '--quiet', 'refs/heads/master',
git_dir=repo_path, checked=False, capture_output=True)
if output.returncode == 0:
return 'master'
else:
return 'main'


def main():
Expand Down

0 comments on commit 37db3ea

Please sign in to comment.