简体   繁体   English

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

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

I have a script that shows the last few log changes, but if a fetch hasn't been ran in a day or two, I want the script to run it manually. 我有一个脚本显示最后几个日志更改,但如果一天或两天没有运行提取,我希望脚本手动运行它。

Here's sample bash code of how it would work: 以下是它如何工作的示例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

There is no way to know when the last git fetch was run , as git does not store that information. 无法知道何时运行最后一个git fetch ,因为git不存储该信息。 If you want to know, you will have to write a time stamp somewhere every time you run it. 如果你想知道,每次运行它时都必须在某个地方写一个时间戳。

However, you can find out when git fetch last actually fetched something . 但是,你可以找出git fetch最后实际获取的东西 Every time git fetch fetches new commits, it updates the reflog. 每次git fetch获取新提交时,它都会更新reflog。 To see it, use eg 要查看它,请使用eg

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

This will show the last change to origin/master , with the date. 这将显示带有日期的origin/master的最后一次更改。 Caveat: This will also show when you modified it (by pushing). 警告:当修改它时(通过推动)也会显示。

Finally, it is probably easier to just fetch whenever it's convenient, without all this checking. 最后,在没有所有这些检查的情况下,只要方便就可以更容易地获取。 git fetch runs very quickly if there's nothing to fetch. 如果没有东西可以获取, git fetch运行得非常快。

Here's what I ended up doing, thanks to @sleske 这是我最终做的,感谢@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