"""
Add the Git segment to the Powerline-shell prompt
"""
import os
import re
import subprocess


def get_git_status():
    """
    Get the status of the Git repository
    """
    has_pending_commits = True
    has_untracked_files = False
    origin_position = ""
    output = subprocess.Popen(['git', 'status', '--ignore-submodules'],
                              env={"LANG": "C", "HOME": os.getenv("HOME")},
                              stdout=subprocess.PIPE).communicate()[0]
    try:
        lines = output.split('\n')
    except TypeError:  # Python 3
        lines = output.decode().split('\n')
    for line in lines:
        origin_status = re.findall(
            r"Your branch is (ahead|behind).*?(\d+) comm", line)
        diverged_status = re.findall(r"and have (\d+) and (\d+) different commits each", line)
        if origin_status:
            origin_position = " %d" % int(origin_status[0][1])
            if origin_status[0][0] == 'behind':
                origin_position += u'\u21E3'
            if origin_status[0][0] == 'ahead':
                origin_position += u'\u21E1'
        if diverged_status:
            origin_position = " %d%c %d%c" % (int(diverged_status[0][0]), u'\u21E1', int(diverged_status[0][1]), u'\u21E3')

        if line.find('nothing to commit') >= 0:
            has_pending_commits = False
        if line.find('Untracked files') >= 0:
            has_untracked_files = True
    return has_pending_commits, has_untracked_files, origin_position


def add_git_segment():
    """
    Add the Git segment to the Powerline-shell prompt

    This version of the function fails quickly
    """
    oldcwd = os.getcwd()
    # Fail quickly if .git is not present in cwd and parents
    found = False
    pathhead, pathtail = oldcwd, '.'
    while pathtail != '':
        if os.access(".git", os.R_OK):
            found = True
            break
        pathhead, pathtail = os.path.split(pathhead)

    # If we aren't anywhere inside a Git repository, bail
    if not found:
        return

    try:
        os.chdir(pathhead)
        # See http://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
        p = subprocess.Popen(['git', 'symbolic-ref', '-q', 'HEAD'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = p.communicate()

    if 'Not a git repo' in err:
        return

        if out:
            branch = out[len('refs/heads/'):].rstrip()
        else:
            branch = '(Detached)'

    has_pending_commits, has_untracked_files, origin_position = get_git_status()
    branch += origin_position
    if has_untracked_files:
        branch += u' \u271A'

        bg = Color.REPO_CLEAN_BG
        fg = Color.REPO_CLEAN_FG
        if has_pending_commits:
            bg = Color.REPO_DIRTY_BG
            fg = Color.REPO_DIRTY_FG

        powerline.append(' %s ' % branch, fg, bg)

    except OSError:
        pass
    except subprocess.CalledProcessError:
        pass
    finally:  # Execute unconditionally, regardless of what happened in the try/except blocks
        os.chdir(oldcwd)

@@ -1,72 +1,4 @@
-%22%22%22%0AAdd the Git segment to the Powerline-shell prompt%0A%22%22%22%0Aimport os%0A
 impo
@@ -22,17 +22,16 @@
 rocess%0A%0A
-%0A
 def get_
@@ -48,65 +48,8 @@
 ():%0A
-    %22%22%22%0A    Get the status of the Git repository%0A    %22%22%22%0A
     
@@ -204,34 +204,16 @@
 ules'%5D,%0A
-                  
         
@@ -265,38 +265,8 @@
 %22)%7D,
-%0A                             
  std
@@ -311,147 +311,38 @@
     
-try:%0A        lines = output.split('%5Cn')%0A    except TypeError:  # Python 3%0A        lines = output.decode().split('%5Cn')%0A    for line in lines
+for line in output.split('%5Cn')
 :%0A  
@@ -1233,561 +1233,8 @@
 ():%0A
-    %22%22%22%0A    Add the Git segment to the Powerline-shell prompt%0A%0A    This version of the function fails quickly%0A    %22%22%22%0A    oldcwd = os.getcwd()%0A    # Fail quickly if .git is not present in cwd and parents%0A    found = False%0A    pathhead, pathtail = oldcwd, '.'%0A    while pathtail != '':%0A        if os.access(%22.git%22, os.R_OK):%0A            found = True%0A            break%0A        pathhead, pathtail = os.path.split(pathhead)%0A%0A    # If we aren't anywhere inside a Git repository, bail%0A    if not found:%0A        return%0A%0A    try:%0A        os.chdir(pathhead)%0A    
     
@@ -1322,20 +1322,16 @@
 ly.html%0A
-    
     p = 
@@ -1434,20 +1434,16 @@
 s.PIPE)%0A
-    
     out,
@@ -1514,24 +1514,44 @@
 return%0A%0A    
+branch = u'%5Cue0a0 '%0A
     if out:%0A
@@ -1550,20 +1550,16 @@
 if out:%0A
-    
         
@@ -1565,16 +1565,17 @@
  branch 
++
 = out%5Ble
@@ -1602,20 +1602,16 @@
 strip()%0A
-    
     else
@@ -1612,20 +1612,16 @@
   else:%0A
-    
         
@@ -1627,16 +1627,17 @@
  branch 
++
 = '(Deta
@@ -1813,20 +1813,16 @@
 u271A'%0A%0A
-    
     bg =
@@ -1842,28 +1842,24 @@
 LEAN_BG%0A    
- 
-   
 fg = Color.R
@@ -1871,20 +1871,16 @@
 LEAN_FG%0A
-    
     if h
@@ -1907,20 +1907,16 @@
         
-    
 bg = Col
@@ -1940,20 +1940,16 @@
         
-  
-  
 fg = Col
@@ -1965,21 +1965,86 @@
 IRTY_FG%0A
-%0A
     
+    branch += u' %5Cu2718'%0A    else:%0A        branch += u' %5Cu2714'%0A%0A%0A
     powe
@@ -2082,20 +2082,43 @@
 g, bg)%0A%0A
-    
+try:%0A    add_git_segment()%0A
 except O
@@ -2129,29 +2129,21 @@
 or:%0A    
- 
-   
 pass%0A
-    
 except s
@@ -2172,20 +2172,16 @@
 sError:%0A
-    
     pass
@@ -2185,123 +2185,4 @@
 ass%0A
-    finally:  # Execute unconditionally, regardless of what happened in the try/except blocks%0A        os.chdir(oldcwd)%0A

Revert "Merge remote-tracking branch 'py3k/py3k_compat'"