diff --git a/tests/test_main.py b/tests/test_main.py index 7d07150d..8288d543 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -84,7 +84,7 @@ class TestGetCommand(object): shell=True, stdout=PIPE, stderr=PIPE, - env={'LANG': 'C'}) + env={'LANG': 'C', 'GIT_TRACE': 1}) @pytest.mark.parametrize('args, result', [ (['thefuck', 'ls', '-la'], 'ls -la'), diff --git a/tests/test_utils.py b/tests/test_utils.py index 0352fd7b..1acee00d 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,6 @@ import pytest from mock import Mock -from thefuck.utils import sudo_support, wrap_settings, memoize, get_closest +from thefuck.utils import git_support, sudo_support, wrap_settings, memoize, get_closest from thefuck.types import Settings from tests.utils import Command @@ -26,6 +26,15 @@ def test_sudo_support(return_value, command, called, result): fn.assert_called_once_with(Command(called), None) +@pytest.mark.parametrize('called, command, stderr', [ + ('git co', "git 'checkout'", "19:22:36.299340 git.c:282 trace: alias expansion: co => 'checkout'"), + ('git com file', "git 'commit' '--verbose' file", "19:23:25.470911 git.c:282 trace: alias expansion: com => 'commit' '--verbose'")]) +def test_git_support(called, command, stderr): + @git_support + def fn(command, settings): return command.script + assert fn(Command(script=called, stderr=stderr), None) == command + + def test_memoize(): fn = Mock(__name__='fn') memoized = memoize(fn) diff --git a/thefuck/main.py b/thefuck/main.py index 8d1a3dac..e58550df 100644 --- a/thefuck/main.py +++ b/thefuck/main.py @@ -82,7 +82,7 @@ def get_command(settings, args): script = shells.from_shell(script) logs.debug('Call: {}'.format(script), settings) result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE, - env=dict(os.environ, LANG='C')) + env=dict(os.environ, LANG='C', GIT_TRACE=1)) if wait_output(settings, result): return types.Command(script, result.stdout.read().decode('utf-8'), result.stderr.read().decode('utf-8')) diff --git a/thefuck/utils.py b/thefuck/utils.py index 2f58e527..7d7e670b 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -2,6 +2,7 @@ from difflib import get_close_matches from functools import wraps import os import pickle +import re import six from .types import Command @@ -73,6 +74,25 @@ def sudo_support(fn): return wrapper +def git_support(fn): + """Resolve git aliases.""" + @wraps(fn) + def wrapper(command, settings): + if (command.script.startswith('git') and + 'trace: alias expansion:' in command.stderr): + + search = re.search("trace: alias expansion: ([^ ]*) => ([^\n]*)", + command.stderr) + alias = search.group(1) + expansion = search.group(2) + new_script = command.script.replace(alias, expansion) + + command = Command._replace(command, script=new_script) + return fn(command, settings) + + return wrapper + + def memoize(fn): """Caches previous calls to the function.""" memo = {}