7

编辑摘要:Git 不允许在 1973/03/03 09:46:40 (epoch+100000000s) 之前以其“内部日期格式”(自纪元以来的秒数)给出日期。这是为了允许“20110224”作为“2011-02-24”的缩写形式。--这不是错误:不是真的,但也没有记录。--解决方法:当你不能依赖 git 内部日期时,不要依赖它。--感谢:霍布斯

大家好,

我有一些关于 git filter-branch 的问题,我已经追踪到 git commit-tree。考虑这个脚本:

#!/bin/bash
# please run these commands in an empty directory
# (should not destroy an existing repo, though. I think it would only
# a few dangling objects)

set -e -o pipefail

git init
tree=$(git write-tree)
commit=$(echo "my first commit -- the tree is empty" |
     env GIT_AUTHOR_DATE="0 +0000" git commit-tree $tree)

echo "This is commit $commit:"
git cat-file commit $commit

请注意,env GIT_AUTHOR_DATE="0 +0000"使用“Git 内部格式”将日期设置为 1970-01-01。

但是这个脚本的输出(原始提交)是

tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
author Jane Doe <jane> 1298477214 +0100
committer Jane Doe <jane> 1298477214 +0100

my first commit -- the tree is empty

现在为什么 git 忽略 $GIT_AUTHOR_DATE?如果这很重要,我git --version会给git version 1.7.1.

4

1 回答 1

22

在 git 日期解析器代码中找到:

/*
 * Seconds since 1970? We trigger on that for any numbers with
 * more than 8 digits. This is because we don't want to rule out
 * numbers like 20070606 as a YYYYMMDD date.
 */
if (num >= 100000000 && nodate(tm)) {

由于该代码明确拒绝将小数字视为可能的 unix-dates,并且该字符串不会像任何其他日期格式一样解析,GIT_AUTHOR_DATE因此被视为无效并完全忽略(显然是默默地)。

只要您坚持合成 1973 年之后发生的提交,您的方法应该可以正常工作。否则,请使用其他日期格式之一:)

于 2011-02-23T16:26:14.237 回答