我一直在尝试创建一个自动更新的 git log --graph --no-pager
。我使用 fswatch
取得了一些进步,但我遇到了一个问题,即我的 git 历史实际上比我的屏幕可以显示的要大。我尝试添加 | head -n $((`tput lines` - 1))
但由于换行,输出仍然溢出。然后我尝试使用 tput rmam
来禁用换行,但现在我通常看不到 HEAD
指向哪里。
谁能帮帮我?理想情况下,我想删除 --no-pager
,但我不知道如何让它与 fswatch
一起工作。这是我目前所拥有的:
function gwatch() {
tput rmam
location=$(git rev-parse --show-toplevel)
clear;
git --no-pager graph --color | head -n $((`tput lines` - 1));
tput smam
fswatch -or $location/.git | while read MODFILE
do
tput rmam
clear;
git --no-pager graph --color | head -n $((`tput lines` - 1));
tput smam
done
}
最佳答案
我有一个不如你的优雅但简单的解决方案:使用 watch。
watch -n 1 "sh -c 'git log | head -n $(tput lines)'"
watch 将为您进行分页,但您将无法浏览日志。
但是如果你想要那样的话,你可以使用下面的代码:
#!/bin/bash
gitwatch()
{
while true; do
sleep 3
fswatch -xr $location/.git/objects | while read events; do
if echo $events | grep -q Created; then
pid=$(pgrep -t $(ps -o tty= -p $$) $PAGER)
kill $pid
break
fi
done
done
}
location=$(git rev-parse --show-toplevel)
gitwatch &
while true; do
git log
done
我睡了,因为我们在 git 中的一次修改中创建了多个文件。这段代码需要一个比监视所有 git 对象更好的解决方案来捕获 git 中的修改!
您可以使用 -o --event=Created
而不是 -x
然后 grep
with Created>。但是,这种方法的响应速度较慢,而且似乎并非始终有效。
关于git - 有没有办法查看 .git 文件夹,并在它发生变化时更新我的 git 日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48428557/