blob: 72868192ef7ba68e45c7b70c3c0cf2ffacb58982 [file] [log] [blame]
Anton Staaf60f4fd72011-02-01 13:20:54 -08001#!/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
8import os
9import re
10import subprocess
11
12def RunCommand(path, command):
13 """Run a command in a given directory, return stdout."""
14
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#
23def FindFileUpwards(filename, path):
24 """Search upwards from the a directory to find a file."""
25
26 path = os.path.realpath(path)
27 while True:
28 file_path = os.path.join(path, filename)
29 if os.path.exists(file_path):
30 return file_path
31 (new_path, _) = os.path.split(path)
32 if new_path == path:
33 return None
34 path = new_path
35
36
37def ShowName(relative_name, color):
38 """Display the directory name."""
39
40 if color:
41 print('\033[44m\033[37m%s\033[0m' % relative_name)
42 else:
43 print relative_name
44
45
Anton Staaf1037c442011-02-08 14:39:53 -080046def GetBranches(full_name, relative_name, color):
47 """Return a list of branch descriptions."""
Anton Staaf60f4fd72011-02-01 13:20:54 -080048
Anton Staaf60f4fd72011-02-01 13:20:54 -080049 command = ['git', 'branch', '-vv']
50
51 if color:
52 command.append('--color')
53
Anton Staaf1037c442011-02-08 14:39:53 -080054 branches = RunCommand(full_name, command).splitlines()
Anton Staaf60f4fd72011-02-01 13:20:54 -080055
Anton Staaf1037c442011-02-08 14:39:53 -080056 if re.search(r"\(no branch\)", branches[0]):
57 return []
Anton Staaf60f4fd72011-02-01 13:20:54 -080058
Anton Staaf1037c442011-02-08 14:39:53 -080059 return branches
Anton Staaf60f4fd72011-02-01 13:20:54 -080060
Anton Staaf1037c442011-02-08 14:39:53 -080061def GetStatus(full_name, relative_name, color):
62 """Return a list of files that have modifications."""
Anton Staaf60f4fd72011-02-01 13:20:54 -080063
Anton Staaf1037c442011-02-08 14:39:53 -080064 command = ['git', 'status', '-s']
65
66 return RunCommand(full_name, command).splitlines()
67
68
69def ShowDir(full_name, relative_name, color):
70 """Display active work in a single git repository."""
71
72 branches = GetBranches(full_name, relative_name, color)
73 status = GetStatus(full_name, relative_name, color)
74
75 if branches or status:
76 ShowName(relative_name, color)
77
78 if branches: print '\n'.join(branches)
79 if status: print '\n'.join(status)
80
81 if branches or status:
Anton Staaf60f4fd72011-02-01 13:20:54 -080082 print ""
83
84
85def FindRoot():
86 """Returns the repo root."""
87
88 repo_file = '.repo'
89 repo_path = FindFileUpwards(repo_file, os.getcwd())
90
91 if repo_path is None:
92 raise Exception('Failed to find %s.' % repo_file)
93
94 return os.path.dirname(repo_path)
95
96
97def main():
98 """Take no arguments."""
99
100 color = os.isatty(1)
Anton Staaf60f4fd72011-02-01 13:20:54 -0800101 root = FindRoot()
102 repos = RunCommand(root, ['repo', 'forall', '-c', 'pwd']).splitlines()
103
104 # We want to use the full path for testing, but we want to use the relative
105 # path for display.
Anton Staaf82340d72011-02-08 14:39:19 -0800106 reldirs = [re.sub('^' + re.escape(root) + '/', '', p) for p in repos]
Anton Staaf60f4fd72011-02-01 13:20:54 -0800107
Anton Staaf82340d72011-02-08 14:39:19 -0800108 for full, relative in zip(repos, reldirs):
109 ShowDir(full, relative, color)
Anton Staaf60f4fd72011-02-01 13:20:54 -0800110
111
112if __name__ == '__main__':
113 main()