stage.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #
  2. # Copyright (C) 2008 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import sys
  16. from color import Coloring
  17. from command import InteractiveCommand
  18. from git_command import GitCommand
  19. class _ProjectList(Coloring):
  20. def __init__(self, gc):
  21. Coloring.__init__(self, gc, 'interactive')
  22. self.prompt = self.printer('prompt', fg='blue', attr='bold')
  23. self.header = self.printer('header', attr='bold')
  24. self.help = self.printer('help', fg='red', attr='bold')
  25. class Stage(InteractiveCommand):
  26. common = True
  27. helpSummary = "Stage file(s) for commit"
  28. helpUsage = """
  29. %prog -i [<project>...]
  30. """
  31. helpDescription = """
  32. The '%prog' command stages files to prepare the next commit.
  33. """
  34. def _Options(self, p):
  35. p.add_option('-i', '--interactive',
  36. dest='interactive', action='store_true',
  37. help='use interactive staging')
  38. def Execute(self, opt, args):
  39. if opt.interactive:
  40. self._Interactive(opt, args)
  41. else:
  42. self.Usage()
  43. def _Interactive(self, opt, args):
  44. all = filter(lambda x: x.IsDirty(), self.GetProjects(args))
  45. if not all:
  46. print >>sys.stderr,'no projects have uncommitted modifications'
  47. return
  48. out = _ProjectList(self.manifest.manifestProject.config)
  49. while True:
  50. out.header(' %-20s %s', 'project', 'path')
  51. out.nl()
  52. for i in xrange(0, len(all)):
  53. p = all[i]
  54. out.write('%3d: %-20s %s', i + 1, p.name, p.relpath + '/')
  55. out.nl()
  56. out.nl()
  57. out.write('%3d: (', 0)
  58. out.prompt('q')
  59. out.write('uit)')
  60. out.nl()
  61. out.prompt('project> ')
  62. try:
  63. a = sys.stdin.readline()
  64. except KeyboardInterrupt:
  65. out.nl()
  66. break
  67. if a == '':
  68. out.nl()
  69. break
  70. a = a.strip()
  71. if a.lower() in ('q', 'quit', 'exit'):
  72. break
  73. if not a:
  74. continue
  75. try:
  76. a_index = int(a)
  77. except ValueError:
  78. a_index = None
  79. if a_index is not None:
  80. if a_index == 0:
  81. break
  82. if 0 < a_index and a_index <= len(all):
  83. _AddI(all[a_index - 1])
  84. continue
  85. p = filter(lambda x: x.name == a or x.relpath == a, all)
  86. if len(p) == 1:
  87. _AddI(p[0])
  88. continue
  89. print 'Bye.'
  90. def _AddI(project):
  91. p = GitCommand(project, ['add', '--interactive'], bare=False)
  92. p.Wait()