[email protected] | a72bd94 | 2010-03-26 19:45:59 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Display active git branches and code changes in a chromiumos workspace.""" |
| 7 | |
[email protected] | f43d019 | 2010-04-15 02:36:04 | [diff] [blame] | 8 | import gclient_utils |
[email protected] | a72bd94 | 2010-03-26 19:45:59 | [diff] [blame] | 9 | import os |
| 10 | import re |
| 11 | import subprocess |
| 12 | import sys |
| 13 | |
| 14 | |
| 15 | def show_dir(full_name, relative_name, color): |
| 16 | """Display active work in a single git repo.""" |
| 17 | |
| 18 | def show_name(): |
| 19 | """Display the directory name.""" |
| 20 | |
| 21 | if color: |
| 22 | sys.stdout.write('========= %s[44m%s[37m%s%s[0m ========\n' % |
| 23 | (chr(27), chr(27), relative_name, chr(27))) |
| 24 | else: |
| 25 | sys.stdout.write('========= %s ========\n' % relative_name) |
| 26 | |
| 27 | lines_printed = 0 |
| 28 | |
| 29 | cmd = ['git', 'branch', '-v'] |
| 30 | if color: |
| 31 | cmd.append('--color') |
| 32 | |
| 33 | branch = subprocess.Popen(cmd, |
| 34 | cwd=full_name, |
| 35 | stdout=subprocess.PIPE).communicate()[0].rstrip() |
| 36 | |
| 37 | if len(branch.splitlines()) > 1: |
| 38 | if lines_printed == 0: |
| 39 | show_name() |
| 40 | lines_printed += 1 |
| 41 | print branch |
| 42 | |
| 43 | status = subprocess.Popen(['git', 'status'], |
| 44 | cwd=full_name, |
| 45 | stdout=subprocess.PIPE).communicate()[0].rstrip() |
| 46 | |
| 47 | if len(status.splitlines()) > 2: |
| 48 | if lines_printed == 0: |
| 49 | show_name() |
| 50 | if lines_printed == 1: |
| 51 | print '---------------' |
| 52 | print status |
| 53 | |
| 54 | |
[email protected] | a72bd94 | 2010-03-26 19:45:59 | [diff] [blame] | 55 | def main(): |
| 56 | """Take no arguments.""" |
| 57 | |
| 58 | color = False |
| 59 | |
| 60 | if os.isatty(1): |
| 61 | color = True |
| 62 | |
| 63 | base = os.path.basename(os.getcwd()) |
[email protected] | f43d019 | 2010-04-15 02:36:04 | [diff] [blame] | 64 | root, entries = gclient_utils.GetGClientRootAndEntries() |
[email protected] | a72bd94 | 2010-03-26 19:45:59 | [diff] [blame] | 65 | |
[email protected] | f43d019 | 2010-04-15 02:36:04 | [diff] [blame] | 66 | # which entries map to a git repos? |
[email protected] | c965a48 | 2012-09-12 21:02:02 | [diff] [blame] | 67 | raw = [k for k, v in entries.items() if v and not re.search('svn', v)] |
[email protected] | a72bd94 | 2010-03-26 19:45:59 | [diff] [blame] | 68 | raw.sort() |
| 69 | |
| 70 | # We want to use the full path for testing, but we want to use the relative |
| 71 | # path for display. |
[email protected] | a72bd94 | 2010-03-26 19:45:59 | [diff] [blame] | 72 | fulldirs = map(lambda(p): os.path.normpath(os.path.join(root, p)), raw) |
| 73 | reldirs = map(lambda(p): re.sub('^' + base, '.', p), raw) |
| 74 | |
| 75 | for full_path, relative_path in zip(fulldirs, reldirs): |
| 76 | show_dir(full_path, relative_path, color) |
| 77 | |
| 78 | if __name__ == '__main__': |
| 79 | main() |