stage.py 2.9 KB

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