blob: 9d35426864d91f6532635c422ac309fb9e8be037 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
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 Owenscecd1d82012-11-01 22:59:27 -070016from __future__ import print_function
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import sys
18
19from color import Coloring
20from command import InteractiveCommand
21from git_command import GitCommand
22
23class _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
30class Stage(InteractiveCommand):
31 common = True
32 helpSummary = "Stage file(s) for commit"
33 helpUsage = """
34%prog -i [<project>...]
35"""
36 helpDescription = """
37The '%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 Pursehouseb5267f92013-05-06 07:52:52 +090052 all_projects = [p for p in self.GetProjects(args) if p.IsDirty()]
David Pursehouse8a68ff92012-09-24 12:15:13 +090053 if not all_projects:
Sarah Owenscecd1d82012-11-01 22:59:27 -070054 print('no projects have uncommitted modifications', file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070055 return
56
57 out = _ProjectList(self.manifest.manifestProject.config)
58 while True:
Shawn O. Pearcedeec0532009-04-18 11:22:13 -070059 out.header(' %s', 'project')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070060 out.nl()
61
Sarah Owensa6053d52012-11-01 13:36:50 -070062 for i in range(len(all_projects)):
David Pursehouse3bcd3052017-07-10 22:42:22 +090063 project = all_projects[i]
64 out.write('%3d: %s', i + 1, project.relpath + '/')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070065 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 Pursehouse8a68ff92012-09-24 12:15:13 +090097 if 0 < a_index and a_index <= len(all_projects):
98 _AddI(all_projects[a_index - 1])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070099 continue
100
David Pursehouseb5267f92013-05-06 07:52:52 +0900101 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 Projectcf31fe92008-10-21 07:00:00 -0700104 continue
Sarah Owenscecd1d82012-11-01 22:59:27 -0700105 print('Bye.')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106
107def _AddI(project):
108 p = GitCommand(project, ['add', '--interactive'], bare=False)
109 p.Wait()