繁体   English   中英

git:检查最后一次获取的运行时间

[英]git: check when the last fetch was run

我有一个脚本显示最后几个日志更改,但如果一天或两天没有运行提取,我希望脚本手动运行它。

以下是它如何工作的示例bash代码:

#!/bin/bash

# this is what I need:
last_fetch_date=`git fetch --show-last-fetch-date`

# do the math to see how long ago
timestamp=`date -d "$last_fetch_date" +%s`
now=`date +%s`
diff=`echo $now - $timestamp | bc -l`

# two days
if [ `echo $diff' >= 2*24*60*60' | bc -l` == "1" ]; then
    git fetch --tags
fi

无法知道何时运行最后一个git fetch ,因为git不存储该信息。 如果你想知道,每次运行它时都必须在某个地方写一个时间戳。

但是,你可以找出git fetch最后实际获取的东西 每次git fetch获取新提交时,它都会更新reflog。 要查看它,请使用eg

git reflog show --date=iso -n 1 origin/master

这将显示带有日期的origin/master的最后一次更改。 警告:当修改它时(通过推动)也会显示。

最后,在没有所有这些检查的情况下,只要方便就可以更容易地获取。 如果没有东西可以获取, git fetch运行得非常快。

这是我最终做的,感谢@sleske

#!/bin/bash

# do the math to see how long ago was the last fetch
 now=`date +%s`
 last_fetch=`git reflog show --date=raw -n 1 origin/master | grep -oE '{[0-9]+' | sed 's%{%%'`
 diff=`echo $now - $last_fetch | bc -l`
if [ `echo $diff' >= 2*24*60*60' | bc -l` == "1" ]; then
    git fetch -q
    git fetch --tags -q
fi

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM