2

这个问题中,我正在探索如何监视我的项目目录的更改,以便我可以保留一个显示始终最新的 git diff 的终端,这样我就可以跳过git diff一遍又一遍地输入 shell 并进一步简化我的工作流程。

假设我有一个足够智能的工具,可以准确地知道文件系统何时发生了变化,我怎么能破坏 git diff 的显示?最终的解决方案将能够找出当前显示滚动到的位置,并重新滚动到相同的位置(在合理的位置,因为一切都可能被改变,当然)。我确信有一种方法可以找出您在缓冲区中的位置less,然后将其与它的新实例一起传递。

但这里的问题是我如何以编程方式控制也是交互式的界面?这是可以做到的吗?我可以使用某些重型工具(例如 tmux)将输入发送到程序中吗?根据我定制 tmux 来做很多事情的经验,它确实能够从 shell 接收命令并执行 shell 命令,并且它确实有一个发送密钥的命令。

4

1 回答 1

2

这是什么?近距离投票?也许有人认为这是不可能的。;)

我向您展示了完整的解决方案(您可以在github上找到最新版本):

~/util/git-diff-puppet:

#!/bin/sh

# This script runs git diff through tmux at the current directory, so that you can
# interactively scroll it. Listens for changes to the filesystem at this directory
# and tmux is used to issue commands to reload the diff.
set -e
[[ -f .tmp_git_diff ]] && echo "found .tmp_git_diff, exiting" && exit -1
# save the current git diff string to use for comparison
git diff > .tmp_git_diff
function cleanup {
    kill $FSWATCHPID
    rm .tmp_git_diff
    tmux kill-session -t git-diff-puppet
}
trap cleanup EXIT
tmux new-session -d -s git-diff-puppet sh
tmux send-keys -t git-diff-puppet "git diff" enter
fswatch . ~/util/git-diff-puppet-onchange &
FSWATCHPID=$!
tmux attach -t git-diff-puppet
echo "tmux finished: puppet script exiting"

~/util/git-diff-puppet-onchange:

#!/bin/sh

# This script is not for invoking directly. It is for use in conjunction (as a "callback") with
# git-diff-puppet: this script will be looking for the .tmp_git_diff file
set -e

[[ ! -f .tmp_git_diff ]] && echo ".tmp_git_diff not found; i was probably invoked in error, aborting" && exit 1
# diffing the current diff with the saved diff to see if we should re-show the git diff in tmux
if ! git diff | diff - .tmp_git_diff > /dev/null; then
    tmux send-keys -t git-diff-puppet q "git diff" enter
    git diff > .tmp_git_diff
fi

这是光荣的。由于超快的fswatch程序,即时更新。

还不错的是,我解释了fswatch虚假射击(不幸的是)。如果 mygit diff未更改,则不会通过 tmux 重新运行,因此会保留滚动偏移量。

于 2013-04-07T23:06:33.100 回答