首页 > 解决方案 > 有谁知道为什么“之前”包括完成日期?

问题描述

有谁知道为什么before包括完成日期?

日期2021-07-01 14:13实际上是在之后2021-07-01

在此处输入图像描述

为什么结果是错误的?

git 版本是 2.32.0

标签: gitgit-log

解决方案


请注意,日期过滤器使用提交日期时间,而不是作者日期时间。

尽管没有记录,如jthill 的回答中所述并通过我的有限测试得到证实,但令人惊讶的是,当未指定时间时,所有日期过滤器似乎都是指客户端计算机上的当前时间!请注意,如果您想覆盖它,您可以指定一个时间:

git log --before=2021-07-02T23:59:59(一天结束)

或者

git log --before=2021-07-02T00:00:00(一天的开始)

下面是我的测试脚本。它会生成一个新的仓库,其中包含一个空提交,提交者日期为 2 天前减去 1 分钟。(所以从现在开始的 1 分钟提交将恰好是 2 天前。)然后脚本循环一分钟,每 10 秒打印一次当前时间,以及使用--before--after选项的日志。起初 --before 不显示任何内容,而 --after 显示提交,但一分钟后,一旦当前时间超过提交日期,日志就会翻转:

#!/bin/bash

# create a test repo
git init
echo ""

# create a date which is 2 days ago minus 1 minute
var1=$(date --date '-2879 min')
# create a date that is the first date in only YYYY-MM-DD format
var2=$(date -d "$var1" +%F)
# show the variables
echo "var1=$var1"
echo "var2=$var2"
echo ""

# create an empty commit with a committer date set to 2 days ago minus 1 minute
GIT_COMMITTER_DATE=$var1 git commit --allow-empty -m 'Create an empty commit'

# display the log; there should only be 1 commit
git log --pretty=fuller
echo ""

# Every 10 seconds for 70 seconds, display the log before 2 days ago without a time specified.
# As soon as the current time passes the time of the commiter_date on the commit, the log will show.
for (( x=1; x<=8; x++ ))
do  
    echo "Current time: $(date)"
    echo "Output of: 'git log --before=$var2':"
    git log --before=$var2
    echo ""
    echo "Output of: 'git log --after=$var2':"
    git log --after=$var2
    echo ""
    sleep 10
done

推荐阅读