Dulwich.io dulwich / 917d271
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. Sijis Aviles 2 months ago
1 changed file(s) with 23 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
939939 # Open the repo
940940 with open_repo_closing(repo) as r:
941941 if remote_location is None:
942 # TODO(jelmer): Retrieve remote to use from config rather than
943 # using default.
944 section = (b'remote', b'origin')
945942 config = r.get_config()
943 remote_name = get_branch_remote(r.path)
944 section = (b'remote', remote_name)
945
946946 if config.has_section(section):
947947 url = config.get(section, 'url')
948948 remote_location = url.decode()
12391239 if not active_ref.startswith(LOCAL_BRANCH_PREFIX):
12401240 raise ValueError(active_ref)
12411241 return active_ref[len(LOCAL_BRANCH_PREFIX):]
1242
1243
1244 def get_branch_remote(repo):
1245 """Return the active branch's remote name, if any.
1246
1247 Args:
1248 repo: Repository to open
1249 Returns:
1250 remote name
1251 Raises:
1252 KeyError: if the repository does not have a working tree
1253 """
1254 with open_repo_closing(repo) as r:
1255 branch_name = active_branch(r.path)
1256 config = r.get_config()
1257 try:
1258 remote_name = config.get((b'branch', branch_name), 'remote')
1259 except KeyError:
1260 remote_name = b'origin'
1261 return remote_name
12421262
12431263
12441264 def fetch(repo, remote_location, remote_name=b'origin', outstream=sys.stdout,