blob: 945c28d821dd78a85a9d9a044cdb8b566ae19195 [file] [log] [blame]
Doug Andersonfce89f22011-03-14 16:07:42 -07001#
2# Copyright (C) 2011 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
Chirayu Desai04d84a22013-03-17 18:46:23 +053017import sys
Olof Johanssond75c6692012-10-09 08:25:55 +020018
Doug Andersonfce89f22011-03-14 16:07:42 -070019from command import Command, MirrorSafeCommand
20
21class List(Command, MirrorSafeCommand):
22 common = True
23 helpSummary = "List projects and their associated directories"
24 helpUsage = """
Olof Johanssond75c6692012-10-09 08:25:55 +020025%prog [-f] [<project>...]
26%prog [-f] -r str1 [str2]..."
Doug Andersonfce89f22011-03-14 16:07:42 -070027"""
28 helpDescription = """
29List all projects; pass '.' to list the project for the cwd.
30
31This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
32"""
33
David Pursehouseda45e5d2013-05-15 17:34:45 +090034 def _Options(self, p):
Olof Johanssond75c6692012-10-09 08:25:55 +020035 p.add_option('-r', '--regex',
36 dest='regex', action='store_true',
37 help="Filter the project list based on regex or wildcard matching of strings")
38 p.add_option('-f', '--fullpath',
39 dest='fullpath', action='store_true',
40 help="Display the full work tree path instead of the relative path")
Chirayu Desai04d84a22013-03-17 18:46:23 +053041 p.add_option('-n', '--name-only',
42 dest='name_only', action='store_true',
43 help="Display only the name of the repository")
44 p.add_option('-p', '--path-only',
45 dest='path_only', action='store_true',
46 help="Display only the path of the repository")
Olof Johanssond75c6692012-10-09 08:25:55 +020047
Doug Andersonfce89f22011-03-14 16:07:42 -070048 def Execute(self, opt, args):
49 """List all projects and the associated directories.
50
51 This may be possible to do with 'repo forall', but repo newbies have
52 trouble figuring that out. The idea here is that it should be more
53 discoverable.
54
55 Args:
Olof Johanssond75c6692012-10-09 08:25:55 +020056 opt: The options.
Doug Andersonfce89f22011-03-14 16:07:42 -070057 args: Positional args. Can be a list of projects to list, or empty.
58 """
Chirayu Desai04d84a22013-03-17 18:46:23 +053059
60 if opt.fullpath and opt.name_only:
61 print('error: cannot combine -f and -n', file=sys.stderr)
62 sys.exit(1)
63
Olof Johanssond75c6692012-10-09 08:25:55 +020064 if not opt.regex:
65 projects = self.GetProjects(args)
66 else:
67 projects = self.FindProjects(args)
68
69 def _getpath(x):
70 if opt.fullpath:
71 return x.worktree
72 return x.relpath
Doug Andersonfce89f22011-03-14 16:07:42 -070073
74 lines = []
75 for project in projects:
Chirayu Desai04d84a22013-03-17 18:46:23 +053076 if opt.name_only and not opt.path_only:
77 lines.append("%s" % ( project.name))
78 elif opt.path_only and not opt.name_only:
79 lines.append("%s" % (_getpath(project)))
80 else:
81 lines.append("%s : %s" % (_getpath(project), project.name))
Doug Andersonfce89f22011-03-14 16:07:42 -070082
83 lines.sort()
Sarah Owenscecd1d82012-11-01 22:59:27 -070084 print('\n'.join(lines))