wtf 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. """Display active git branches and code changes in a chromiumos workspace."""
  6. from __future__ import print_function
  7. import gclient_utils
  8. import os
  9. import re
  10. import subprocess
  11. import sys
  12. def show_dir(full_name, relative_name, color):
  13. """Display active work in a single git repo."""
  14. def show_name():
  15. """Display the directory name."""
  16. if color:
  17. sys.stdout.write('========= %s[44m%s[37m%s%s[0m ========\n' %
  18. (chr(27), chr(27), relative_name, chr(27)))
  19. else:
  20. sys.stdout.write('========= %s ========\n' % relative_name)
  21. lines_printed = 0
  22. cmd = ['git', 'branch', '-v']
  23. if color:
  24. cmd.append('--color')
  25. branch = subprocess.Popen(cmd,
  26. cwd=full_name,
  27. stdout=subprocess.PIPE).communicate()[0].rstrip()
  28. if len(branch.splitlines()) > 1:
  29. if lines_printed == 0:
  30. show_name()
  31. lines_printed += 1
  32. print(branch)
  33. status = subprocess.Popen(['git', 'status'],
  34. cwd=full_name,
  35. stdout=subprocess.PIPE).communicate()[0].rstrip()
  36. if len(status.splitlines()) > 2:
  37. if lines_printed == 0:
  38. show_name()
  39. if lines_printed == 1:
  40. print('---------------')
  41. print(status)
  42. def main():
  43. """Take no arguments."""
  44. color = False
  45. if os.isatty(1):
  46. color = True
  47. base = os.path.basename(os.getcwd())
  48. root, entries = gclient_utils.GetGClientRootAndEntries()
  49. # which entries map to a git repos?
  50. raw = [k for k, v in entries.items() if v and not re.search('svn', v)]
  51. raw.sort()
  52. # We want to use the full path for testing, but we want to use the relative
  53. # path for display.
  54. fulldirs = map(lambda(p): os.path.normpath(os.path.join(root, p)), raw)
  55. reldirs = map(lambda(p): re.sub('^' + base, '.', p), raw)
  56. for full_path, relative_path in zip(fulldirs, reldirs):
  57. show_dir(full_path, relative_path, color)
  58. if __name__ == '__main__':
  59. main()