git - 有没有办法查看 .git 文件夹,并在它发生变化时更新我的​​ git 日志?

标签 git bash unix zsh fswatch

我一直在尝试创建一个自动更新的 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/

相关文章:

git - 为什么 VS Code 1.82-1.84 stash 从差异中删除的行?

android - 如何最有效地将 git build 版本号与 eclipse 的 apk 版本联系起来?

git - 回滚到 Mercurial 中的旧版本(如 git reset)

正则表达式/linux 查找命令未按预期工作

bash - 收到 "ambiguous redirect"错误

c - 消息队列错误 : No message of desired type

bash - 如何通过 bashrc 检查操作系统是 Sun Os 还是 Unix 或 Solaris

unix - wget拒绝仍然下载文件

Git:在接收后移动文件夹

linux - 在打印特定菜单的 shell 脚本中需要帮助