blob: 72e2dfcf11cfa1d4fc08f4e1e1a8c279b3077406 [file] [log] [blame]
[email protected]a72bd942010-03-26 19:45:591#!/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
8import os
9import re
10import subprocess
11import sys
12
13
14def show_dir(full_name, relative_name, color):
15 """Display active work in a single git repo."""
16
17 def show_name():
18 """Display the directory name."""
19
20 if color:
21 sys.stdout.write('========= %s[44m%s[37m%s%s[0m ========\n' %
22 (chr(27), chr(27), relative_name, chr(27)))
23 else:
24 sys.stdout.write('========= %s ========\n' % relative_name)
25
26 lines_printed = 0
27
28 cmd = ['git', 'branch', '-v']
29 if color:
30 cmd.append('--color')
31
32 branch = subprocess.Popen(cmd,
33 cwd=full_name,
34 stdout=subprocess.PIPE).communicate()[0].rstrip()
35
36 if len(branch.splitlines()) > 1:
37 if lines_printed == 0:
38 show_name()
39 lines_printed += 1
40 print branch
41
42 status = subprocess.Popen(['git', 'status'],
43 cwd=full_name,
44 stdout=subprocess.PIPE).communicate()[0].rstrip()
45
46 if len(status.splitlines()) > 2:
47 if lines_printed == 0:
48 show_name()
49 if lines_printed == 1:
50 print '---------------'
51 print status
52
53
54def find_file(filename):
55 """Search upwards from the current directory to find a file."""
56 path = filename
57 while os.getcwd().split('/'):
58 if os.path.isfile(path):
59 return path
60 path = os.path.join('../', path)
61
62
63def main():
64 """Take no arguments."""
65
66 color = False
67
68 if os.isatty(1):
69 color = True
70
71 base = os.path.basename(os.getcwd())
72 config_file = '.gclient_entries'
73 config_path = find_file(config_file)
74
75 if not config_path:
76 print "Can't find", config_file
77 sys.exit(1)
78
79 env = {}
80 execfile(config_path, env)
81
82 # which are the git repos?
83 raw = [k for k, v in env['entries'].items() if not re.search('svn', v)]
84 raw.sort()
85
86 # We want to use the full path for testing, but we want to use the relative
87 # path for display.
88 root = os.path.dirname(config_path)
89 fulldirs = map(lambda(p): os.path.normpath(os.path.join(root, p)), raw)
90 reldirs = map(lambda(p): re.sub('^' + base, '.', p), raw)
91
92 for full_path, relative_path in zip(fulldirs, reldirs):
93 show_dir(full_path, relative_path, color)
94
95if __name__ == '__main__':
96 main()