wfrichar@chromium.org | a72bd94 | 2010-03-26 19:45:59 +0000 | [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 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
piman@chromium.org | f43d019 | 2010-04-15 02:36:04 +0000 | [diff] [blame] | 10 | import gclient_utils |
wfrichar@chromium.org | a72bd94 | 2010-03-26 19:45:59 +0000 | [diff] [blame] | 11 | import os |
| 12 | import re |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | |
| 17 | def show_dir(full_name, relative_name, color): |
| 18 | """Display active work in a single git repo.""" |
| 19 | |
| 20 | def show_name(): |
| 21 | """Display the directory name.""" |
| 22 | |
| 23 | if color: |
| 24 | sys.stdout.write('========= %s[44m%s[37m%s%s[0m ========\n' % |
| 25 | (chr(27), chr(27), relative_name, chr(27))) |
| 26 | else: |
| 27 | sys.stdout.write('========= %s ========\n' % relative_name) |
| 28 | |
| 29 | lines_printed = 0 |
| 30 | |
| 31 | cmd = ['git', 'branch', '-v'] |
| 32 | if color: |
| 33 | cmd.append('--color') |
| 34 | |
| 35 | branch = subprocess.Popen(cmd, |
| 36 | cwd=full_name, |
| 37 | stdout=subprocess.PIPE).communicate()[0].rstrip() |
| 38 | |
| 39 | if len(branch.splitlines()) > 1: |
| 40 | if lines_printed == 0: |
| 41 | show_name() |
| 42 | lines_printed += 1 |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 43 | print(branch) |
wfrichar@chromium.org | a72bd94 | 2010-03-26 19:45:59 +0000 | [diff] [blame] | 44 | |
| 45 | status = subprocess.Popen(['git', 'status'], |
| 46 | cwd=full_name, |
| 47 | stdout=subprocess.PIPE).communicate()[0].rstrip() |
| 48 | |
| 49 | if len(status.splitlines()) > 2: |
| 50 | if lines_printed == 0: |
| 51 | show_name() |
| 52 | if lines_printed == 1: |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 53 | print('---------------') |
| 54 | print(status) |
wfrichar@chromium.org | a72bd94 | 2010-03-26 19:45:59 +0000 | [diff] [blame] | 55 | |
| 56 | |
wfrichar@chromium.org | a72bd94 | 2010-03-26 19:45:59 +0000 | [diff] [blame] | 57 | def main(): |
| 58 | """Take no arguments.""" |
| 59 | |
| 60 | color = False |
| 61 | |
| 62 | if os.isatty(1): |
| 63 | color = True |
| 64 | |
| 65 | base = os.path.basename(os.getcwd()) |
piman@chromium.org | f43d019 | 2010-04-15 02:36:04 +0000 | [diff] [blame] | 66 | root, entries = gclient_utils.GetGClientRootAndEntries() |
wfrichar@chromium.org | a72bd94 | 2010-03-26 19:45:59 +0000 | [diff] [blame] | 67 | |
piman@chromium.org | f43d019 | 2010-04-15 02:36:04 +0000 | [diff] [blame] | 68 | # which entries map to a git repos? |
sbc@chromium.org | c965a48 | 2012-09-12 21:02:02 +0000 | [diff] [blame] | 69 | raw = [k for k, v in entries.items() if v and not re.search('svn', v)] |
wfrichar@chromium.org | a72bd94 | 2010-03-26 19:45:59 +0000 | [diff] [blame] | 70 | raw.sort() |
| 71 | |
| 72 | # We want to use the full path for testing, but we want to use the relative |
| 73 | # path for display. |
wfrichar@chromium.org | a72bd94 | 2010-03-26 19:45:59 +0000 | [diff] [blame] | 74 | fulldirs = map(lambda(p): os.path.normpath(os.path.join(root, p)), raw) |
| 75 | reldirs = map(lambda(p): re.sub('^' + base, '.', p), raw) |
| 76 | |
| 77 | for full_path, relative_path in zip(fulldirs, reldirs): |
| 78 | show_dir(full_path, relative_path, color) |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | main() |