title: How to get subprocess stdout while running git command?
body: <p>I have a program written in python and used git command in it..
  For some reason I don't want to use git-python or others instead of subprocess.
  But I'm currently stuck in getting <code>git clone</code> output.</p>
  
  <p>I've tried some code snippet. Some works fine with commands like <code>ping 8.8.8.8</code>, but not the <code>git clone</code>.</p>
  
  <p>for example</p>
  
  <p>using thread</p>
  
  <pre><code>def log_worker(stdout):
      while True:
          last = non_block_read(stdout).strip() 
          if last != "":
              print(last)
  
  
  def non_block_read(output):
      fd = output.fileno()
      fl = fcntl.fcntl(fd, fcntl.F_GETFL)
      fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
      try:
          return output.read()
      except:
          return ''
  
  def test():
      mysql_process = subprocess.Popen(
          "ping google.com",
          shell=True,
          stdout=subprocess.PIPE,
          stderr=subprocess.PIPE)
  
      thread = Thread(target=log_worker, args=[mysql_process.stdout])
      thread.daemon = True
      thread.start()
  
      mysql_process.wait()
      thread.join(timeout=1)
  
  test()
  </code></pre>
  
  <p>or </p>
  
  <pre><code>newlines = ['\n', '\r\n', '\r']
  def unbuffered(proc, stream='stdout'):
      stream = getattr(proc, stream)
      with contextlib.closing(stream):
          while True:
              print('tt')
              out = []
              last = stream.read(1)
              # Don't loop forever
              if last == '' and proc.poll() is not None:
                  break
              print('last', last)
              while last not in newlines:
                  print("loop")
                  # Don't loop forever
                  if last == '' and proc.poll() is not None:
                      break
                  out.append(last)
                  last = stream.read(1)
              out = ''.join(out)
              yield out
  
  def example():
      cmd = ['ls', '-l', '/']
      proc = subprocess.Popen(
          cmd,
          stdout=subprocess.PIPE,
          stderr=subprocess.STDOUT,
          # Make all end-of-lines '\n'
          universal_newlines=True,
          shell = True
      )
      for line in unbuffered(proc):
          print('new line')
          print line
  
  example()
  </code></pre>
  
  <p>and most common one</p>
  
  <pre><code>for line in iter(proc.stdout.readline, ''):
      sys.stdout.write('{:.2f}  {}\n'.format(
          time.time() - start,
          line.rstrip()
      ))
      sys.stdout.flush()
  </code></pre>
  
  <p>all of them works fine with <code>ping google.com</code>, but not <code>git clone</code>.
  Is there any way to solve this?
  Thanks in advance!</p>
  
  <p>UPDATE1:
  In face, I'm just want to get the finished percent of <code>git clone</code>. Log or any log files are not needed.</p>