The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import sys |
| 18 | |
| 19 | from color import Coloring |
| 20 | from command import InteractiveCommand |
| 21 | from git_command import GitCommand |
| 22 | |
| 23 | class _ProjectList(Coloring): |
| 24 | def __init__(self, gc): |
| 25 | Coloring.__init__(self, gc, 'interactive') |
| 26 | self.prompt = self.printer('prompt', fg='blue', attr='bold') |
| 27 | self.header = self.printer('header', attr='bold') |
| 28 | self.help = self.printer('help', fg='red', attr='bold') |
| 29 | |
| 30 | class Stage(InteractiveCommand): |
| 31 | common = True |
| 32 | helpSummary = "Stage file(s) for commit" |
| 33 | helpUsage = """ |
| 34 | %prog -i [<project>...] |
| 35 | """ |
| 36 | helpDescription = """ |
| 37 | The '%prog' command stages files to prepare the next commit. |
| 38 | """ |
| 39 | |
| 40 | def _Options(self, p): |
| 41 | p.add_option('-i', '--interactive', |
| 42 | dest='interactive', action='store_true', |
| 43 | help='use interactive staging') |
| 44 | |
| 45 | def Execute(self, opt, args): |
| 46 | if opt.interactive: |
| 47 | self._Interactive(opt, args) |
| 48 | else: |
| 49 | self.Usage() |
| 50 | |
| 51 | def _Interactive(self, opt, args): |
David Pursehouse | b5267f9 | 2013-05-06 07:52:52 +0900 | [diff] [blame] | 52 | all_projects = [p for p in self.GetProjects(args) if p.IsDirty()] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 53 | if not all_projects: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 54 | print('no projects have uncommitted modifications', file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 55 | return |
| 56 | |
| 57 | out = _ProjectList(self.manifest.manifestProject.config) |
| 58 | while True: |
Shawn O. Pearce | deec053 | 2009-04-18 11:22:13 -0700 | [diff] [blame] | 59 | out.header(' %s', 'project') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | out.nl() |
| 61 | |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 62 | for i in range(len(all_projects)): |
David Pursehouse | 3bcd305 | 2017-07-10 22:42:22 +0900 | [diff] [blame] | 63 | project = all_projects[i] |
| 64 | out.write('%3d: %s', i + 1, project.relpath + '/') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 | out.nl() |
| 66 | out.nl() |
| 67 | |
| 68 | out.write('%3d: (', 0) |
| 69 | out.prompt('q') |
| 70 | out.write('uit)') |
| 71 | out.nl() |
| 72 | |
| 73 | out.prompt('project> ') |
| 74 | try: |
| 75 | a = sys.stdin.readline() |
| 76 | except KeyboardInterrupt: |
| 77 | out.nl() |
| 78 | break |
| 79 | if a == '': |
| 80 | out.nl() |
| 81 | break |
| 82 | |
| 83 | a = a.strip() |
| 84 | if a.lower() in ('q', 'quit', 'exit'): |
| 85 | break |
| 86 | if not a: |
| 87 | continue |
| 88 | |
| 89 | try: |
| 90 | a_index = int(a) |
| 91 | except ValueError: |
| 92 | a_index = None |
| 93 | |
| 94 | if a_index is not None: |
| 95 | if a_index == 0: |
| 96 | break |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 97 | if 0 < a_index and a_index <= len(all_projects): |
| 98 | _AddI(all_projects[a_index - 1]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 99 | continue |
| 100 | |
David Pursehouse | b5267f9 | 2013-05-06 07:52:52 +0900 | [diff] [blame] | 101 | projects = [p for p in all_projects if a in [p.name, p.relpath]] |
| 102 | if len(projects) == 1: |
| 103 | _AddI(projects[0]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 104 | continue |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 105 | print('Bye.') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 106 | |
| 107 | def _AddI(project): |
| 108 | p = GitCommand(project, ['add', '--interactive'], bare=False) |
| 109 | p.Wait() |