1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-05 18:31:10 +01:00

Start support for git aliases

This commit is contained in:
mcarton 2015-07-16 20:23:31 +02:00
parent 78769e4fbc
commit b3e09d68df
4 changed files with 32 additions and 3 deletions

View File

@ -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'),

View File

@ -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)

View File

@ -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'))

View File

@ -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 = {}