Skip to content

Commit

Permalink
Fix default branch extraction (#250)
Browse files Browse the repository at this point in the history
* Add regression test

* Use newer version of git from buster-backports

* Allow dashes and periods in branch names
  • Loading branch information
colinodell authored Jul 27, 2022
1 parent 68fa58d commit 9664ad8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Added optional `force` arg to `checkout_pull_branch`, allowing any uncommitted changes in an existing repository to be discarded. (#246)

### Fixed

* Fixed `get_default_branch` not returning the full branch name if it contains dashes or periods (#250)

## [0.15.0] - 2022-05-24

### Changed
Expand Down
5 changes: 4 additions & 1 deletion docker/devbox.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ ENV PIP_NO_CACHE_DIR "true"
RUN mkdir /app && chown ${UID}:${GID} /app

USER root
RUN apt-get install git-core && apt-get clean
RUN echo "deb http://deb.debian.org/debian buster-backports main" > /etc/apt/sources.list.d/buster-backports.list \
&& apt-get update \
&& apt-get install -y -t buster-backports git-core \
&& apt-get clean

USER ${_USER}

Expand Down
2 changes: 1 addition & 1 deletion pygitops/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def get_default_branch(repo: Repo) -> str:
:param repo: git.Repo instance.
:return: string representing name of default branch.
"""
git_ref_regex = r"refs\/remotes\/origin\/(\w*)"
git_ref_regex = r"refs\/remotes\/origin\/([\w*\-\.]+)"
symbolic_ref_head = "refs/remotes/origin/HEAD"

# local repo should be aware of branch objects prior to running the `set-head` command, where an unknown branch might be present
Expand Down
14 changes: 10 additions & 4 deletions tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,13 @@ def test_get_default_branch__match_index_error__raises_pygitops_error(mocker):
get_default_branch(repo_mock)


def test_get_default_branch__default_branch_returned(tmp_path):
@pytest.mark.parametrize(
"branch_name",
["main", "master", "2.x", "name-with-dashes", "name_with_underscores"],
)
def test_get_default_branch__default_branch_returned(tmp_path, branch_name):

repos = _initialize_multiple_empty_repos(tmp_path)
repos = _initialize_multiple_empty_repos(tmp_path, initial_branch=branch_name)
remote_repo = repos.remote_repo
local_repo = repos.local_repo

Expand Down Expand Up @@ -831,7 +835,9 @@ def _assert_branch_state_stage_commit_push_changes__file_removed(


# Helper functions for testing git operations
def _initialize_multiple_empty_repos(base_path) -> MultipleTestingRepos:
def _initialize_multiple_empty_repos(
base_path, initial_branch="main"
) -> MultipleTestingRepos:
"""
Helper function used to initialize and configure local, remote, and clone repos for integration testing.
"""
Expand All @@ -848,7 +854,7 @@ def _initialize_multiple_empty_repos(base_path) -> MultipleTestingRepos:
clone_path.mkdir()

# The remote is set up as a bare repository
remote_repo = Repo.init(remote_path)
remote_repo = Repo.init(remote_path, initial_branch=initial_branch)

# give the remote repo some initial commit history
new_file_path = remote_path / SOME_CONTENT_FILENAME
Expand Down

0 comments on commit 9664ad8

Please sign in to comment.