list: add a --relative-to option

The current list output only shows project paths relative to the
root of the repo client checkout.  It can be helpful to also get
a listing of paths based on other paths (e.g. the current working
directory), so add an option to repo list to support that.  We'll
leverage this in bash completion to support completing projects by
their local paths and not just remote names.

Bug: https://crbug.com/gerrit/14797
Change-Id: Ia2b35d18c890217768448118b003874a1016efd4
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/312904
Reviewed-by: Xin Li <delphij@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Mike Frysinger 2021-07-26 15:28:01 -04:00
parent b380322174
commit 5467185db0
3 changed files with 22 additions and 7 deletions

View File

@ -37,6 +37,7 @@ __complete_repo_list_branches() {
__complete_repo_list_projects() { __complete_repo_list_projects() {
local repo=${COMP_WORDS[0]} local repo=${COMP_WORDS[0]}
"${repo}" list -n 2>/dev/null "${repo}" list -n 2>/dev/null
"${repo}" list -p --relative-to=. 2>/dev/null
} }
# Complete the repo <command> argument. # Complete the repo <command> argument.

View File

@ -27,15 +27,19 @@ project is in
\fB\-a\fR, \fB\-\-all\fR \fB\-a\fR, \fB\-\-all\fR
show projects regardless of checkout state show projects regardless of checkout state
.TP .TP
\fB\-f\fR, \fB\-\-fullpath\fR
display the full work tree path instead of the
relative path
.TP
\fB\-n\fR, \fB\-\-name\-only\fR \fB\-n\fR, \fB\-\-name\-only\fR
display only the name of the repository display only the name of the repository
.TP .TP
\fB\-p\fR, \fB\-\-path\-only\fR \fB\-p\fR, \fB\-\-path\-only\fR
display only the path of the repository display only the path of the repository
.TP
\fB\-f\fR, \fB\-\-fullpath\fR
display the full work tree path instead of the
relative path
.TP
\fB\-\-relative\-to\fR=\fI\,PATH\/\fR
display paths relative to this one (default: top of
repo client checkout)
.SS Logging options: .SS Logging options:
.TP .TP
\fB\-v\fR, \fB\-\-verbose\fR \fB\-v\fR, \fB\-\-verbose\fR

View File

@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
from command import Command, MirrorSafeCommand from command import Command, MirrorSafeCommand
@ -43,20 +45,26 @@ This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
p.add_option('-a', '--all', p.add_option('-a', '--all',
action='store_true', action='store_true',
help='show projects regardless of checkout state') help='show projects regardless of checkout state')
p.add_option('-f', '--fullpath',
dest='fullpath', action='store_true',
help='display the full work tree path instead of the relative path')
p.add_option('-n', '--name-only', p.add_option('-n', '--name-only',
dest='name_only', action='store_true', dest='name_only', action='store_true',
help='display only the name of the repository') help='display only the name of the repository')
p.add_option('-p', '--path-only', p.add_option('-p', '--path-only',
dest='path_only', action='store_true', dest='path_only', action='store_true',
help='display only the path of the repository') help='display only the path of the repository')
p.add_option('-f', '--fullpath',
dest='fullpath', action='store_true',
help='display the full work tree path instead of the relative path')
p.add_option('--relative-to', metavar='PATH',
help='display paths relative to this one (default: top of repo client checkout)')
def ValidateOptions(self, opt, args): def ValidateOptions(self, opt, args):
if opt.fullpath and opt.name_only: if opt.fullpath and opt.name_only:
self.OptionParser.error('cannot combine -f and -n') self.OptionParser.error('cannot combine -f and -n')
# Resolve any symlinks so the output is stable.
if opt.relative_to:
opt.relative_to = os.path.realpath(opt.relative_to)
def Execute(self, opt, args): def Execute(self, opt, args):
"""List all projects and the associated directories. """List all projects and the associated directories.
@ -76,6 +84,8 @@ This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
def _getpath(x): def _getpath(x):
if opt.fullpath: if opt.fullpath:
return x.worktree return x.worktree
if opt.relative_to:
return os.path.relpath(x.worktree, opt.relative_to)
return x.relpath return x.relpath
lines = [] lines = []