stage.py 3.0 KB

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