blob: aeb4951336d0a025e222845b0880524a4dd987be [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
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
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import sys
19
20from color import Coloring
21from command import InteractiveCommand
22from git_command import GitCommand
23
24class _ProjectList(Coloring):
25 def __init__(self, gc):
26 Coloring.__init__(self, gc, 'interactive')
27 self.prompt = self.printer('prompt', fg='blue', attr='bold')
28 self.header = self.printer('header', attr='bold')
29 self.help = self.printer('help', fg='red', attr='bold')
30
31class Stage(InteractiveCommand):
32 common = True
33 helpSummary = "Stage file(s) for commit"
34 helpUsage = """
35%prog -i [<project>...]
36"""
37 helpDescription = """
38The '%prog' command stages files to prepare the next commit.
39"""
40
41 def _Options(self, p):
42 p.add_option('-i', '--interactive',
43 dest='interactive', action='store_true',
44 help='use interactive staging')
45
46 def Execute(self, opt, args):
47 if opt.interactive:
48 self._Interactive(opt, args)
49 else:
50 self.Usage()
51
52 def _Interactive(self, opt, args):
David Pursehouseb5267f92013-05-06 07:52:52 +090053 all_projects = [p for p in self.GetProjects(args) if p.IsDirty()]
David Pursehouse8a68ff92012-09-24 12:15:13 +090054 if not all_projects:
Sarah Owenscecd1d82012-11-01 22:59:27 -070055 print('no projects have uncommitted modifications', file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070056 return
57
58 out = _ProjectList(self.manifest.manifestProject.config)
59 while True:
Shawn O. Pearcedeec0532009-04-18 11:22:13 -070060 out.header(' %s', 'project')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070061 out.nl()
62
Sarah Owensa6053d52012-11-01 13:36:50 -070063 for i in range(len(all_projects)):
David Pursehouse3bcd3052017-07-10 22:42:22 +090064 project = all_projects[i]
65 out.write('%3d: %s', i + 1, project.relpath + '/')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066 out.nl()
67 out.nl()
68
69 out.write('%3d: (', 0)
70 out.prompt('q')
71 out.write('uit)')
72 out.nl()
73
74 out.prompt('project> ')
75 try:
76 a = sys.stdin.readline()
77 except KeyboardInterrupt:
78 out.nl()
79 break
80 if a == '':
81 out.nl()
82 break
83
84 a = a.strip()
85 if a.lower() in ('q', 'quit', 'exit'):
86 break
87 if not a:
88 continue
89
90 try:
91 a_index = int(a)
92 except ValueError:
93 a_index = None
94
95 if a_index is not None:
96 if a_index == 0:
97 break
David Pursehouse8a68ff92012-09-24 12:15:13 +090098 if 0 < a_index and a_index <= len(all_projects):
99 _AddI(all_projects[a_index - 1])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700100 continue
101
David Pursehouseb5267f92013-05-06 07:52:52 +0900102 projects = [p for p in all_projects if a in [p.name, p.relpath]]
103 if len(projects) == 1:
104 _AddI(projects[0])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105 continue
Sarah Owenscecd1d82012-11-01 22:59:27 -0700106 print('Bye.')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700107
108def _AddI(project):
109 p = GitCommand(project, ['add', '--interactive'], bare=False)
110 p.Wait()