From 32baf81b56191533ac3b8f708e0eaf0d7f2112d7 Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Sun, 8 Dec 2019 14:39:03 -0600 Subject: [PATCH] Support specifying a command after `yadm enter` --- test/test_enter.py | 18 ++++++++++++++++-- yadm | 12 +++++++++--- yadm.1 | 12 ++++++++---- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/test/test_enter.py b/test/test_enter.py index 469e3ee..106eb5c 100644 --- a/test/test_enter.py +++ b/test/test_enter.py @@ -58,8 +58,9 @@ def test_enter(runner, yadm_y, paths, shell, success): 'csh', 'zsh', ]) +@pytest.mark.parametrize('cmd', [False, True], ids=['no-cmd', 'cmd']) @pytest.mark.usefixtures('ds1_copy') -def test_enter_shell_ops(runner, yadm_y, paths, shell, opts, path): +def test_enter_shell_ops(runner, yadm_y, paths, shell, opts, path, cmd): """Enter tests for specific shell options""" # Create custom shell to detect options passed @@ -67,11 +68,24 @@ def test_enter_shell_ops(runner, yadm_y, paths, shell, opts, path): custom_shell.write('#!/bin/sh\necho OPTS=$*\necho PROMPT=$PROMPT') custom_shell.chmod(0o775) + test_cmd = ['test1', 'test2', 'test3'] + + enter_cmd = ['enter'] + if cmd: + enter_cmd += test_cmd + env = os.environ.copy() env['SHELL'] = custom_shell - run = runner(command=yadm_y('enter'), env=env) + run = runner(command=yadm_y(*enter_cmd), env=env) assert run.success assert run.err == '' assert f'OPTS={opts}' in run.out assert f'PROMPT=yadm shell ({paths.repo}) {path} >' in run.out + if cmd: + assert '-c ' + ' '.join(test_cmd) in run.out + assert 'Entering yadm repo' not in run.out + assert 'Leaving yadm repo' not in run.out + else: + assert 'Entering yadm repo' in run.out + assert 'Leaving yadm repo' in run.out diff --git a/yadm b/yadm index ec3a3d8..b5bd5cb 100755 --- a/yadm +++ b/yadm @@ -900,6 +900,7 @@ function encrypt() { } function enter() { + command="$*" require_shell require_repo @@ -913,12 +914,17 @@ function enter() { shell_path="%~" fi - echo "Entering yadm repo" + shell_cmd=() + if [ -n "$command" ]; then + shell_cmd=('-c' "$*") + fi + + [ "${#shell_cmd[@]}" -eq 0 ] && echo "Entering yadm repo" yadm_prompt="yadm shell ($YADM_REPO) $shell_path > " - PROMPT="$yadm_prompt" PS1="$yadm_prompt" "$SHELL" $shell_opts + PROMPT="$yadm_prompt" PS1="$yadm_prompt" "$SHELL" $shell_opts "${shell_cmd[@]}" - echo "Leaving yadm repo" + [ "${#shell_cmd[@]}" -eq 0 ] && echo "Leaving yadm repo" } function git_command() { diff --git a/yadm.1 b/yadm.1 index ef24448..e2c3fc0 100644 --- a/yadm.1 +++ b/yadm.1 @@ -47,7 +47,7 @@ list .BR yadm " encrypt -.BR yadm " enter +.BR yadm " enter [ command ] .BR yadm " decrypt .RB [ -l ] @@ -187,10 +187,14 @@ See the ENCRYPTION section for more details. Run a sub-shell with all Git variables set. Exit the sub-shell the same way you leave your normal shell (usually with the "exit" command). This sub-shell can be used to easily interact with your yadm repository using "git" commands. This -could be useful if you are using a tool which uses Git directly. +could be useful if you are using a tool which uses Git directly, such as tig, +vim-fugitive, git-cola, etc. -For example, Emacs Tramp and Magit can manage files by using this -configuration: +Optionally, you can provide a command after "enter", and instead of invoking +your shell, that command will be run with all of the Git variables exposed to +the command's environment. + +Emacs Tramp and Magit can manage files by using this configuration: .RS (add-to-list 'tramp-methods