OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2011 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 | |
8 import os | |
9 import re | |
10 import subprocess | |
11 import sys | |
12 | |
13 def RunCommand(path, command): | |
14 """Run a command in a given directory, return stdout""" | |
Kenneth Waters
2011/02/01 00:56:04
Period after stdout. And a blank line after the d
robotboy
2011/02/01 20:44:55
Done.
| |
15 return subprocess.Popen(command, | |
16 cwd=path, | |
17 stdout=subprocess.PIPE).communicate()[0].rstrip() | |
18 | |
19 # | |
20 # Taken with slight modification from gclient_utils.py in the depot_tools | |
21 # project. | |
22 # | |
23 def FindFileUpwards(filename, path): | |
24 """Search upwards from the a directory to find a file.""" | |
Kenneth Waters
2011/02/01 00:56:04
Blank line after the docstring.
robotboy
2011/02/01 20:44:55
Done.
| |
25 path = os.path.realpath(path) | |
26 while True: | |
27 file_path = os.path.join(path, filename) | |
28 if os.path.exists(file_path): | |
29 return file_path | |
30 (new_path, _) = os.path.split(path) | |
31 if new_path == path: | |
32 return None | |
33 path = new_path | |
34 | |
35 | |
36 def ShowName(relative_name, color): | |
37 """Display the directory name.""" | |
38 | |
39 if color: | |
40 sys.stdout.write('%s[44m%s[37m%s%s[0m\n' % | |
Kenneth Waters
2011/02/01 00:56:04
'\033[44m\033[37m%s\033[0m\n'
Kenneth Waters
2011/02/01 00:56:04
Why not print?
robotboy
2011/02/01 20:44:55
Done.
robotboy
2011/02/01 20:44:55
Cut and paste-itis. Fixed, thanks.
| |
41 (chr(27), chr(27), relative_name, chr(27))) | |
42 else: | |
43 sys.stdout.write('%s\n' % relative_name) | |
44 | |
45 | |
46 def ShowDir(full_name, relative_name, color): | |
47 """Display active work in a single git repo.""" | |
48 | |
49 lines_printed = 0 | |
50 command = ['git', 'branch', '-vv'] | |
51 | |
52 if color: | |
53 command.append('--color') | |
54 | |
55 branch = RunCommand(full_name, command) | |
56 lines = branch.splitlines() | |
57 | |
58 if (len(lines) > 1 or | |
59 (len(lines) == 1 and not re.search("\(no branch\)", lines[0]))) : | |
Kenneth Waters
2011/02/01 00:56:04
"\(no branch\)" is almost certainly wrong. Did yo
robotboy
2011/02/01 20:44:55
Yes, thanks, done.
| |
60 if lines_printed == 0: | |
61 ShowName(relative_name, color) | |
62 lines_printed += 1 | |
63 print branch | |
64 | |
65 status = RunCommand(full_name, ['git', 'status', '-s']) | |
66 | |
67 if len(status.splitlines()) > 0: | |
Kenneth Waters
2011/02/01 00:56:04
if status.splitlines():
robotboy
2011/02/01 20:44:55
Done.
| |
68 if lines_printed == 0: | |
69 ShowName(relative_name, color) | |
70 if lines_printed == 1: | |
71 print '---------------' | |
72 lines_printed += 1 | |
73 print status | |
74 | |
75 if lines_printed > 0: | |
76 print "" | |
77 | |
78 | |
79 def FindRoot(): | |
80 """Returns the repo root.""" | |
Kenneth Waters
2011/02/01 00:56:04
Add blank line after docstring.
robotboy
2011/02/01 20:44:55
Done.
| |
81 repo_file = '.repo' | |
82 repo_path = FindFileUpwards(repo_file, os.getcwd()) | |
83 | |
84 if not repo_path: | |
Kenneth Waters
2011/02/01 00:56:04
if repo_path is None:
not "" -> True
robotboy
2011/02/01 20:44:55
Done.
| |
85 raise Exception('Failed to find %s.' % repo_file) | |
86 | |
87 return os.path.dirname(repo_path) | |
88 | |
89 | |
90 def main(): | |
91 """Take no arguments.""" | |
92 | |
93 color = os.isatty(1) | |
94 base = os.path.basename(os.getcwd()) | |
95 root = FindRoot() | |
96 repos = RunCommand(root, ['repo', 'forall', '-c', 'pwd']).splitlines() | |
97 | |
98 # We want to use the full path for testing, but we want to use the relative | |
99 # path for display. | |
100 fulldirs = map(lambda(p): os.path.normpath(os.path.join(root, p)), repos) | |
Kenneth Waters
2011/02/01 00:56:04
fulldirs = [os.path.normpath(os.path.join(root, p)
robotboy
2011/02/01 20:44:55
Done.
| |
101 reldirs = map(lambda(p): re.sub('^' + base, '.', p), repos) | |
Kenneth Waters
2011/02/01 00:56:04
reldirs = [re.sub('^' + re.escape(base), '.', p) f
robotboy
2011/02/01 20:44:55
Done.
| |
102 | |
103 for full_path, relative_path in zip(fulldirs, reldirs): | |
104 ShowDir(full_path, relative_path, color) | |
105 | |
106 | |
107 if __name__ == '__main__': | |
108 main() | |
OLD | NEW |