mirror of
https://github.com/nvbn/thefuck.git
synced 2025-04-19 01:00:42 +01:00
Start support for git aliases
This commit is contained in:
parent
78769e4fbc
commit
b3e09d68df
@ -84,7 +84,7 @@ class TestGetCommand(object):
|
|||||||
shell=True,
|
shell=True,
|
||||||
stdout=PIPE,
|
stdout=PIPE,
|
||||||
stderr=PIPE,
|
stderr=PIPE,
|
||||||
env={'LANG': 'C'})
|
env={'LANG': 'C', 'GIT_TRACE': 1})
|
||||||
|
|
||||||
@pytest.mark.parametrize('args, result', [
|
@pytest.mark.parametrize('args, result', [
|
||||||
(['thefuck', 'ls', '-la'], 'ls -la'),
|
(['thefuck', 'ls', '-la'], 'ls -la'),
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from mock import Mock
|
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 thefuck.types import Settings
|
||||||
from tests.utils import Command
|
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)
|
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():
|
def test_memoize():
|
||||||
fn = Mock(__name__='fn')
|
fn = Mock(__name__='fn')
|
||||||
memoized = memoize(fn)
|
memoized = memoize(fn)
|
||||||
|
@ -82,7 +82,7 @@ def get_command(settings, args):
|
|||||||
script = shells.from_shell(script)
|
script = shells.from_shell(script)
|
||||||
logs.debug('Call: {}'.format(script), settings)
|
logs.debug('Call: {}'.format(script), settings)
|
||||||
result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE,
|
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):
|
if wait_output(settings, result):
|
||||||
return types.Command(script, result.stdout.read().decode('utf-8'),
|
return types.Command(script, result.stdout.read().decode('utf-8'),
|
||||||
result.stderr.read().decode('utf-8'))
|
result.stderr.read().decode('utf-8'))
|
||||||
|
@ -2,6 +2,7 @@ from difflib import get_close_matches
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
|
import re
|
||||||
import six
|
import six
|
||||||
from .types import Command
|
from .types import Command
|
||||||
|
|
||||||
@ -73,6 +74,25 @@ def sudo_support(fn):
|
|||||||
return wrapper
|
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):
|
def memoize(fn):
|
||||||
"""Caches previous calls to the function."""
|
"""Caches previous calls to the function."""
|
||||||
memo = {}
|
memo = {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user