From 917d271445162c0cdfb84ec808bdb3eac134d7ac Mon Sep 17 00:00:00 2001 From: Sijis Aviles Date: Sun, 2 Feb 2020 01:15:56 -0600 Subject: [PATCH] fix: Get branch remote from config This prefers to get the upstream defined from the config but it will fallback to origin if none is found. --- dulwich/porcelain.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/dulwich/porcelain.py b/dulwich/porcelain.py index 7695d7bf5..d827d33a5 100644 --- a/dulwich/porcelain.py +++ b/dulwich/porcelain.py @@ -940,10 +940,10 @@ def pull(repo, remote_location=None, refspecs=None, # Open the repo with open_repo_closing(repo) as r: if remote_location is None: - # TODO(jelmer): Retrieve remote to use from config rather than - # using default. - section = (b'remote', b'origin') config = r.get_config() + remote_name = get_branch_remote(r.path) + section = (b'remote', remote_name) + if config.has_section(section): url = config.get(section, 'url') remote_location = url.decode() @@ -1242,6 +1242,26 @@ def active_branch(repo): return active_ref[len(LOCAL_BRANCH_PREFIX):] +def get_branch_remote(repo): + """Return the active branch's remote name, if any. + + Args: + repo: Repository to open + Returns: + remote name + Raises: + KeyError: if the repository does not have a working tree + """ + with open_repo_closing(repo) as r: + branch_name = active_branch(r.path) + config = r.get_config() + try: + remote_name = config.get((b'branch', branch_name), 'remote') + except KeyError: + remote_name = b'origin' + return remote_name + + def fetch(repo, remote_location, remote_name=b'origin', outstream=sys.stdout, errstream=default_bytes_err_stream, message=None, depth=None, prune=False, prune_tags=False, **kwargs):