blob: ca51c5f7ea7de832daec82e426190d0665e6d72e [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")
Graham Christensen0369a062015-07-29 17:02:54 -050038 p.add_option('-g', '--groups',
39 dest='groups',
40 help="Filter the project list based on the groups the project is in")
Olof Johanssond75c6692012-10-09 08:25:55 +020041 p.add_option('-f', '--fullpath',
42 dest='fullpath', action='store_true',
43 help="Display the full work tree path instead of the relative path")
Chirayu Desai04d84a22013-03-17 18:46:23 +053044 p.add_option('-n', '--name-only',
45 dest='name_only', action='store_true',
46 help="Display only the name of the repository")
47 p.add_option('-p', '--path-only',
48 dest='path_only', action='store_true',
49 help="Display only the path of the repository")
Olof Johanssond75c6692012-10-09 08:25:55 +020050
Doug Andersonfce89f22011-03-14 16:07:42 -070051 def Execute(self, opt, args):
52 """List all projects and the associated directories.
53
54 This may be possible to do with 'repo forall', but repo newbies have
55 trouble figuring that out. The idea here is that it should be more
56 discoverable.
57
58 Args:
Olof Johanssond75c6692012-10-09 08:25:55 +020059 opt: The options.
Doug Andersonfce89f22011-03-14 16:07:42 -070060 args: Positional args. Can be a list of projects to list, or empty.
61 """
Chirayu Desai04d84a22013-03-17 18:46:23 +053062
63 if opt.fullpath and opt.name_only:
64 print('error: cannot combine -f and -n', file=sys.stderr)
65 sys.exit(1)
66
Olof Johanssond75c6692012-10-09 08:25:55 +020067 if not opt.regex:
Graham Christensen0369a062015-07-29 17:02:54 -050068 projects = self.GetProjects(args, groups=opt.groups)
Olof Johanssond75c6692012-10-09 08:25:55 +020069 else:
70 projects = self.FindProjects(args)
71
72 def _getpath(x):
73 if opt.fullpath:
74 return x.worktree
75 return x.relpath
Doug Andersonfce89f22011-03-14 16:07:42 -070076
77 lines = []
78 for project in projects:
Chirayu Desai04d84a22013-03-17 18:46:23 +053079 if opt.name_only and not opt.path_only:
80 lines.append("%s" % ( project.name))
81 elif opt.path_only and not opt.name_only:
82 lines.append("%s" % (_getpath(project)))
83 else:
84 lines.append("%s : %s" % (_getpath(project), project.name))
Doug Andersonfce89f22011-03-14 16:07:42 -070085
86 lines.sort()
Sarah Owenscecd1d82012-11-01 22:59:27 -070087 print('\n'.join(lines))