wtf 2.1 KB
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/env python
# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Display active git branches and code changes in a chromiumos workspace."""

8 9
from __future__ import print_function

10
import gclient_utils
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import os
import re
import subprocess
import sys


def show_dir(full_name, relative_name, color):
  """Display active work in a single git repo."""

  def show_name():
    """Display the directory name."""

    if color:
      sys.stdout.write('========= %s[44m%s[37m%s%s[0m ========\n' %
                       (chr(27), chr(27), relative_name, chr(27)))
    else:
      sys.stdout.write('========= %s ========\n' % relative_name)

  lines_printed = 0

  cmd = ['git', 'branch', '-v']
  if color:
    cmd.append('--color')

  branch = subprocess.Popen(cmd,
                            cwd=full_name,
                            stdout=subprocess.PIPE).communicate()[0].rstrip()

  if len(branch.splitlines()) > 1:
    if lines_printed == 0:
      show_name()
    lines_printed += 1
43
    print(branch)
44 45 46 47 48 49 50 51 52

  status = subprocess.Popen(['git', 'status'],
                            cwd=full_name,
                            stdout=subprocess.PIPE).communicate()[0].rstrip()

  if len(status.splitlines()) > 2:
    if lines_printed == 0:
      show_name()
    if lines_printed == 1:
53 54
      print('---------------')
    print(status)
55 56 57 58 59 60 61 62 63 64 65


def main():
  """Take no arguments."""

  color = False

  if os.isatty(1):
    color = True

  base = os.path.basename(os.getcwd())
66
  root, entries = gclient_utils.GetGClientRootAndEntries()
67

68
  # which entries map to a git repos?
69
  raw = [k for k, v in entries.items() if v and not re.search('svn', v)]
70 71 72 73 74 75 76 77 78 79 80 81
  raw.sort()

  # We want to use the full path for testing, but we want to use the relative
  # path for display.
  fulldirs = map(lambda(p): os.path.normpath(os.path.join(root, p)), raw)
  reldirs = map(lambda(p): re.sub('^' + base, '.', p), raw)

  for full_path, relative_path in zip(fulldirs, reldirs):
    show_dir(full_path, relative_path, color)

if __name__ == '__main__':
  main()