blob: 00172f0e6c68fecc5ab0c8aa3e6c82a449aec43b [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
Mike Frysingerae6cb082019-08-27 01:10:59 -040052 def ValidateOptions(self, opt, args):
53 if opt.fullpath and opt.name_only:
54 self.OptionParser.error('cannot combine -f and -n')
55
Doug Andersonfce89f22011-03-14 16:07:42 -070056 def Execute(self, opt, args):
57 """List all projects and the associated directories.
58
59 This may be possible to do with 'repo forall', but repo newbies have
60 trouble figuring that out. The idea here is that it should be more
61 discoverable.
62
63 Args:
Olof Johanssond75c6692012-10-09 08:25:55 +020064 opt: The options.
Doug Andersonfce89f22011-03-14 16:07:42 -070065 args: Positional args. Can be a list of projects to list, or empty.
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))