"""Get the current upstream branch."""

import re
from subprocess import check_output


def upstream(include_remote=False):
    """Get the upstream branch of the current branch."""

    branch_info = check_output(['git', 'status', '--porcelain', '--branch']).splitlines()[0]
    regex = '^##\s[-_a-zA-Z0-9]+\.{3}([-_a-zA-Z0-9]+/([-_a-zA-Z0-9]+))(?:\s\[.*\])?$'
    match = re.match(regex, branch_info)

    upstream_info = ''
    if match is not None:
        upstream_info = match.group(1) if include_remote else match.group(2)

    return upstream_info

"""Get the current upstream branch."""

import re
from subprocess import check_output, PIPE, Popen

_MERGE_CONFIG = 'git config --local branch.{}.merge'
_REMOTE_CONFIG = 'git config --local branch.{}.remote'

def upstream(include_remote=False):
    """Get the upstream branch of the current branch."""

    branch_name = check_output('git rev-parse --abbrev-ref HEAD'.split()).splitlines()[0]

    # get remote branch name
    proc = Popen(_MERGE_CONFIG.format(branch_name).split(), stdout=PIPE)
    upstream_info = proc.communicate()[0].strip()
    upstream_info = upstream_info.rsplit('/', 1)[-1]

    # optionally, get remote name
    if upstream_info and include_remote:
        remote_name = check_output(_REMOTE_CONFIG.format(branch_name).split()).strip()
        upstream_info = remote_name + '/' + upstream_info

    return upstream_info

Refactor how remote info is retrieved
Refactor how remote info is retrieved

The old way of retrieving remote information was to parse `git status`.
This was brittle and lazy. Now the config values storing the remote
information are used.

  """Get the current upstream branch."""
  
  import re
- from subprocess import check_output
+ from subprocess import check_output, PIPE, Popen
  
+ _MERGE_CONFIG = 'git config --local branch.{}.merge'
+ _REMOTE_CONFIG = 'git config --local branch.{}.remote'
  
  def upstream(include_remote=False):
      """Get the upstream branch of the current branch."""
  
+     branch_name = check_output('git rev-parse --abbrev-ref HEAD'.split()).splitlines()[0]
-     branch_info = check_output(['git', 'status', '--porcelain', '--branch']).splitlines()[0]
-     regex = '^##\s[-_a-zA-Z0-9]+\.{3}([-_a-zA-Z0-9]+/([-_a-zA-Z0-9]+))(?:\s\[.*\])?$'
-     match = re.match(regex, branch_info)
  
-     upstream_info = ''
-     if match is not None:
-         upstream_info = match.group(1) if include_remote else match.group(2)
+     # get remote branch name
+     proc = Popen(_MERGE_CONFIG.format(branch_name).split(), stdout=PIPE)
+     upstream_info = proc.communicate()[0].strip()
+     upstream_info = upstream_info.rsplit('/', 1)[-1]
+ 
+     # optionally, get remote name
+     if upstream_info and include_remote:
+         remote_name = check_output(_REMOTE_CONFIG.format(branch_name).split()).strip()
+         upstream_info = remote_name + '/' + upstream_info
  
      return upstream_info
  
Refactor how remote info is retrieved
## Code Before:
"""Get the current upstream branch."""

import re
from subprocess import check_output


def upstream(include_remote=False):
    """Get the upstream branch of the current branch."""

    branch_info = check_output(['git', 'status', '--porcelain', '--branch']).splitlines()[0]
    regex = '^##\s[-_a-zA-Z0-9]+\.{3}([-_a-zA-Z0-9]+/([-_a-zA-Z0-9]+))(?:\s\[.*\])?$'
    match = re.match(regex, branch_info)

    upstream_info = ''
    if match is not None:
        upstream_info = match.group(1) if include_remote else match.group(2)

    return upstream_info

## Instruction:
Refactor how remote info is retrieved
## Code After:
"""Get the current upstream branch."""

import re
from subprocess import check_output, PIPE, Popen

_MERGE_CONFIG = 'git config --local branch.{}.merge'
_REMOTE_CONFIG = 'git config --local branch.{}.remote'

def upstream(include_remote=False):
    """Get the upstream branch of the current branch."""

    branch_name = check_output('git rev-parse --abbrev-ref HEAD'.split()).splitlines()[0]

    # get remote branch name
    proc = Popen(_MERGE_CONFIG.format(branch_name).split(), stdout=PIPE)
    upstream_info = proc.communicate()[0].strip()
    upstream_info = upstream_info.rsplit('/', 1)[-1]

    # optionally, get remote name
    if upstream_info and include_remote:
        remote_name = check_output(_REMOTE_CONFIG.format(branch_name).split()).strip()
        upstream_info = remote_name + '/' + upstream_info

    return upstream_info
