summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xmain.py27
-rwxr-xr-xrepo43
2 files changed, 69 insertions, 1 deletions
diff --git a/main.py b/main.py
index 2422e8be..06cd1108 100755
--- a/main.py
+++ b/main.py
@@ -26,6 +26,7 @@ import getpass
26import netrc 26import netrc
27import optparse 27import optparse
28import os 28import os
29import shlex
29import sys 30import sys
30import textwrap 31import textwrap
31import time 32import time
@@ -48,7 +49,7 @@ from color import SetDefaultColoring
48import event_log 49import event_log
49from repo_trace import SetTrace 50from repo_trace import SetTrace
50from git_command import user_agent 51from git_command import user_agent
51from git_config import init_ssh, close_ssh 52from git_config import init_ssh, close_ssh, RepoConfig
52from command import InteractiveCommand 53from command import InteractiveCommand
53from command import MirrorSafeCommand 54from command import MirrorSafeCommand
54from command import GitcAvailableCommand, GitcClientCommand 55from 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
diff --git a/repo b/repo
index 77e80284..77a3f8d3 100755
--- a/repo
+++ b/repo
@@ -13,6 +13,7 @@ from __future__ import print_function
13import datetime 13import datetime
14import os 14import os
15import platform 15import platform
16import shlex
16import subprocess 17import subprocess
17import sys 18import sys
18 19
@@ -693,6 +694,24 @@ def _SetConfig(cwd, name, value):
693 run_git('config', name, value, cwd=cwd) 694 run_git('config', name, value, cwd=cwd)
694 695
695 696
697def _GetRepoConfig(name):
698 """Read a repo configuration option."""
699 config = os.path.join(home_dot_repo, 'config')
700 if not os.path.exists(config):
701 return None
702
703 cmd = ['config', '--file', config, '--get', name]
704 ret = run_git(*cmd, check=False)
705 if ret.returncode == 0:
706 return ret.stdout
707 elif ret.returncode == 1:
708 return None
709 else:
710 print('repo: error: git %s failed:\n%s' % (' '.join(cmd), ret.stderr),
711 file=sys.stderr)
712 raise RunError()
713
714
696def _InitHttp(): 715def _InitHttp():
697 handlers = [] 716 handlers = []
698 717
@@ -876,6 +895,25 @@ class _Options(object):
876 version = False 895 version = False
877 896
878 897
898def _ExpandAlias(name):
899 """Look up user registered aliases."""
900 # We don't resolve aliases for existing subcommands. This matches git.
901 if name in {'gitc-init', 'help', 'init'}:
902 return name, []
903
904 alias = _GetRepoConfig('alias.%s' % (name,))
905 if alias is None:
906 return name, []
907
908 args = alias.strip().split(' ', 1)
909 name = args[0]
910 if len(args) == 2:
911 args = shlex.split(args[1])
912 else:
913 args = []
914 return name, args
915
916
879def _ParseArguments(args): 917def _ParseArguments(args):
880 cmd = None 918 cmd = None
881 opt = _Options() 919 opt = _Options()
@@ -1004,6 +1042,11 @@ def main(orig_args):
1004 file=sys.stderr) 1042 file=sys.stderr)
1005 sys.exit(1) 1043 sys.exit(1)
1006 if not repo_main: 1044 if not repo_main:
1045 # Only expand aliases here since we'll be parsing the CLI ourselves.
1046 # If we had repo_main, alias expansion would happen in main.py.
1047 cmd, alias_args = _ExpandAlias(cmd)
1048 args = alias_args + args
1049
1007 if opt.help: 1050 if opt.help:
1008 _Usage() 1051 _Usage()
1009 if cmd == 'help': 1052 if cmd == 'help':