diff options
author | Mike Frysinger <vapier@google.com> | 2020-02-18 21:37:00 -0500 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2020-02-20 00:53:46 +0000 |
commit | 949bc34267245d35b066ebbc9e5ae8be081db86f (patch) | |
tree | 5cf3d4e49fd971863b8084cd0f97e11d0478442a /main.py | |
parent | f841ca48c150e8a62728c5875fb01dcf7c5756a7 (diff) | |
download | git-repo-949bc34267245d35b066ebbc9e5ae8be081db86f.tar.gz |
main/repo: add support for subcommand aliases
This supports [alias] sections with repo subcommands just like git.
Change-Id: Ie9235b5d4449414e6a745814f0110bd6af74ea93
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/255833
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'main.py')
-rwxr-xr-x | main.py | 27 |
1 files changed, 26 insertions, 1 deletions
@@ -26,6 +26,7 @@ import getpass | |||
26 | import netrc | 26 | import netrc |
27 | import optparse | 27 | import optparse |
28 | import os | 28 | import os |
29 | import shlex | ||
29 | import sys | 30 | import sys |
30 | import textwrap | 31 | import textwrap |
31 | import time | 32 | import time |
@@ -48,7 +49,7 @@ from color import SetDefaultColoring | |||
48 | import event_log | 49 | import event_log |
49 | from repo_trace import SetTrace | 50 | from repo_trace import SetTrace |
50 | from git_command import user_agent | 51 | from git_command import user_agent |
51 | from git_config import init_ssh, close_ssh | 52 | from git_config import init_ssh, close_ssh, RepoConfig |
52 | from command import InteractiveCommand | 53 | from command import InteractiveCommand |
53 | from command import MirrorSafeCommand | 54 | from command import MirrorSafeCommand |
54 | from command import GitcAvailableCommand, GitcClientCommand | 55 | from command import GitcAvailableCommand, GitcClientCommand |
@@ -155,6 +156,9 @@ class _Repo(object): | |||
155 | argv = [] | 156 | argv = [] |
156 | gopts, _gargs = global_options.parse_args(glob) | 157 | gopts, _gargs = global_options.parse_args(glob) |
157 | 158 | ||
159 | name, alias_args = self._ExpandAlias(name) | ||
160 | argv = alias_args + argv | ||
161 | |||
158 | if gopts.help: | 162 | if gopts.help: |
159 | global_options.print_help() | 163 | global_options.print_help() |
160 | commands = ' '.join(sorted(self.commands)) | 164 | commands = ' '.join(sorted(self.commands)) |
@@ -165,6 +169,27 @@ class _Repo(object): | |||
165 | 169 | ||
166 | return (name, gopts, argv) | 170 | return (name, gopts, argv) |
167 | 171 | ||
172 | def _ExpandAlias(self, name): | ||
173 | """Look up user registered aliases.""" | ||
174 | # We don't resolve aliases for existing subcommands. This matches git. | ||
175 | if name in self.commands: | ||
176 | return name, [] | ||
177 | |||
178 | key = 'alias.%s' % (name,) | ||
179 | alias = RepoConfig.ForRepository(self.repodir).GetString(key) | ||
180 | if alias is None: | ||
181 | alias = RepoConfig.ForUser().GetString(key) | ||
182 | if alias is None: | ||
183 | return name, [] | ||
184 | |||
185 | args = alias.strip().split(' ', 1) | ||
186 | name = args[0] | ||
187 | if len(args) == 2: | ||
188 | args = shlex.split(args[1]) | ||
189 | else: | ||
190 | args = [] | ||
191 | return name, args | ||
192 | |||
168 | def _Run(self, name, gopts, argv): | 193 | def _Run(self, name, gopts, argv): |
169 | """Execute the requested subcommand.""" | 194 | """Execute the requested subcommand.""" |
170 | result = 0 | 195 | result = 0 |