Skip to content

Commit

Permalink
use repository's configured default branch instead of assuming master
Browse files Browse the repository at this point in the history
GitHub allows repositories to have a configured default branch other
than 'master'. When reporting that a local copy of a repo may be
dirty, use the default branch reported by the API, instead of assuming
a static value of 'master'.

Signed-off-by: Doug Hellmann <dhellmann@redhat.com>
  • Loading branch information
dhellmann committed May 25, 2021
1 parent b2604c3 commit 48a45a9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Changelog
1.10.1 (unreleased)
-------------------

- Nothing changed yet.
- When determining if a repository is dirty, use the repository's
configured default branch from GitHub instead of assuming that the
default is "master".


1.10.0 (2021-04-10)
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ What it does:
- unknown files in the tree (in --verbose mode only)
- staged but not committed changes
- uncommitted (and unstaged changes)
- non-master branch checked out
- committed changes that haven't been pushed to master
- non-default branch checked out
- committed changes that haven't been pushed to default branch
- remote URL pointing to an unexpected location (in --verbose mode only)

You can ask it to not change any files on disk and just look for pending
Expand Down
11 changes: 7 additions & 4 deletions ghcloneall.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,12 @@ def __exit__(self, exc_type, exc_value, exc_tb):


class Repo(object):
def __init__(self, name, clone_url, alt_urls=()):
def __init__(self, name, clone_url, alt_urls=(), default_branch='master'):
self.name = name
self.clone_url = clone_url
self.urls = {clone_url}
self.urls.update(alt_urls)
self.default_branch = default_branch

def __repr__(self):
return 'Repo({!r}, {!r}, {{{}}})'.format(
Expand All @@ -374,7 +375,8 @@ def from_repo(cls, repo):
# use repo['git_url'] for anonymous checkouts, but they're slower
# (at least as long as you use SSH connection multiplexing)
clone_url = repo['ssh_url']
return cls(repo['name'], clone_url, (repo['clone_url'],))
return cls(repo['name'], clone_url, (repo['clone_url'],),
repo['default_branch'])

@classmethod
def from_gist(cls, gist):
Expand Down Expand Up @@ -618,8 +620,9 @@ def verify(self, repo, dir):
self.progress_item.update(' (local commits)')
self.dirty = True
branch = self.get_current_branch(dir)
if branch != 'master':
self.progress_item.update(' (not on master)')
if branch != repo.default_branch:
self.progress_item.update(' (not on {})'.format(
repo.default_branch))
if self.options.verbose >= 2:
self.progress_item.extra_info('branch: {}'.format(branch))
self.dirty = True
Expand Down
46 changes: 46 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ def repo(name, **kwargs):
'disabled': False,
'clone_url': 'https://github.com/test_user/%s' % name,
'ssh_url': 'git@github.com:test_user/%s.git' % name,
'default_branch': 'master',
}
repo.update(kwargs)
return repo
Expand Down Expand Up @@ -928,6 +929,26 @@ def test_RepoTask_run_updates(monkeypatch, ):
assert wrangler.n_dirty == 0


def test_RepoTask_run_updates_main(monkeypatch, ):
monkeypatch.setattr(os.path, 'exists', lambda dir: True)
buf = StringIO()
progress = ghcloneall.Progress(stream=buf)
wrangler = ghcloneall.RepoWrangler(progress=progress)
task = wrangler.repo_task(Repo('xyzzy', default_branch='main'))
responses = ['aaaaa', 'bbbbb']
task.get_current_commit = lambda dir: responses.pop(0)
task.get_current_branch = lambda dir: 'main'
task.run()
assert show_ansi_result(buf.getvalue()) == (
'+ xyzzy (updated)\n'
"[####################] 1/0"
)
assert wrangler.n_repos == 1
assert wrangler.n_new == 0
assert wrangler.n_updated == 1
assert wrangler.n_dirty == 0


def raise_exception(*args):
raise Exception("oh no")

Expand Down Expand Up @@ -1010,6 +1031,31 @@ def test_RepoTask_verify():
assert task.dirty


def test_RepoTask_verify_main():
buf = StringIO()
progress = ghcloneall.Progress(stream=buf)
wrangler = ghcloneall.RepoWrangler(progress=progress, verbose=2)
task = wrangler.repo_task(Repo('xyzzy', default_branch='main'))
task.get_current_branch = lambda dir: 'boo'
task.get_remote_url = lambda dir: 'root@github.com:test_user/xyzzy'
task.has_local_changes = lambda dir: True
task.has_staged_changes = lambda dir: True
task.has_local_commits = lambda dir: True
task.verify(task.repo, 'xyzzy')
# NB: we can see that the output doesn't work right when the terminal
# width is 80 instead of 100, but I'm not up to fixing it today
assert show_ansi_result(buf.getvalue(), width=100) == (
'+ xyzzy (local changes) (staged changes) (local commits)'
' (not on main) (wrong remote url)\n'
' branch: boo\n'
' remote: root@github.com:test_user/xyzzy.git\n'
' expected: git@github.com:test_user/xyzzy.git\n'
' alternatively: https://github.com/test_user/xyzzy\n'
"[####################] 1/0"
)
assert task.dirty


def test_RepoTask_verify_unknown_files():
buf = StringIO()
progress = ghcloneall.Progress(stream=buf)
Expand Down

0 comments on commit 48a45a9

Please sign in to comment.