blob: 961b1956e9e12c3e32f7f317a84fd2ead8187eae [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Doug Andersonfce89f22011-03-14 16:07:42 -07002#
3# Copyright (C) 2011 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
Chirayu Desai04d84a22013-03-17 18:46:23 +053018import sys
Olof Johanssond75c6692012-10-09 08:25:55 +020019
Doug Andersonfce89f22011-03-14 16:07:42 -070020from command import Command, MirrorSafeCommand
21
22class List(Command, MirrorSafeCommand):
23 common = True
24 helpSummary = "List projects and their associated directories"
25 helpUsage = """
Olof Johanssond75c6692012-10-09 08:25:55 +020026%prog [-f] [<project>...]
27%prog [-f] -r str1 [str2]..."
Doug Andersonfce89f22011-03-14 16:07:42 -070028"""
29 helpDescription = """
30List all projects; pass '.' to list the project for the cwd.
31
32This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
33"""
34
David Pursehouseda45e5d2013-05-15 17:34:45 +090035 def _Options(self, p):
Olof Johanssond75c6692012-10-09 08:25:55 +020036 p.add_option('-r', '--regex',
37 dest='regex', action='store_true',
38 help="Filter the project list based on regex or wildcard matching of strings")
Graham Christensen0369a062015-07-29 17:02:54 -050039 p.add_option('-g', '--groups',
40 dest='groups',
41 help="Filter the project list based on the groups the project is in")
Olof Johanssond75c6692012-10-09 08:25:55 +020042 p.add_option('-f', '--fullpath',
43 dest='fullpath', action='store_true',
44 help="Display the full work tree path instead of the relative path")
Chirayu Desai04d84a22013-03-17 18:46:23 +053045 p.add_option('-n', '--name-only',
46 dest='name_only', action='store_true',
47 help="Display only the name of the repository")
48 p.add_option('-p', '--path-only',
49 dest='path_only', action='store_true',
50 help="Display only the path of the repository")
Olof Johanssond75c6692012-10-09 08:25:55 +020051
Doug Andersonfce89f22011-03-14 16:07:42 -070052 def Execute(self, opt, args):
53 """List all projects and the associated directories.
54
55 This may be possible to do with 'repo forall', but repo newbies have
56 trouble figuring that out. The idea here is that it should be more
57 discoverable.
58
59 Args:
Olof Johanssond75c6692012-10-09 08:25:55 +020060 opt: The options.
Doug Andersonfce89f22011-03-14 16:07:42 -070061 args: Positional args. Can be a list of projects to list, or empty.
62 """
Chirayu Desai04d84a22013-03-17 18:46:23 +053063
64 if opt.fullpath and opt.name_only:
65 print('error: cannot combine -f and -n', file=sys.stderr)
66 sys.exit(1)
67
Olof Johanssond75c6692012-10-09 08:25:55 +020068 if not opt.regex:
Graham Christensen0369a062015-07-29 17:02:54 -050069 projects = self.GetProjects(args, groups=opt.groups)
Olof Johanssond75c6692012-10-09 08:25:55 +020070 else:
71 projects = self.FindProjects(args)
72
73 def _getpath(x):
74 if opt.fullpath:
75 return x.worktree
76 return x.relpath
Doug Andersonfce89f22011-03-14 16:07:42 -070077
78 lines = []
79 for project in projects:
Chirayu Desai04d84a22013-03-17 18:46:23 +053080 if opt.name_only and not opt.path_only:
81 lines.append("%s" % ( project.name))
82 elif opt.path_only and not opt.name_only:
83 lines.append("%s" % (_getpath(project)))
84 else:
85 lines.append("%s : %s" % (_getpath(project), project.name))
Doug Andersonfce89f22011-03-14 16:07:42 -070086
87 lines.sort()
Sarah Owenscecd1d82012-11-01 22:59:27 -070088 print('\n'.join(lines))