2011-02-23 43 views
6

EDIT概要:GIT中不允许日期之前在其 “内部日期格式”(从epoch秒)给出的1973年3月3日9点46分四十秒(划时代+ 100000000s)。这是允许“20110224”作为“2011-02-24”的简称。 - 这不是问题:不是,但它也没有记录。 - 解决方法:不能依赖git内部日期。 - 感谢:霍布斯

大家好,

我有一些问题,用git过滤分支,我已经找到了到git的承诺树。考虑一下这个脚本:

#!/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的内部格式”日 - 请查看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_AUTHOR_DATE?如果这意义重大,我的git --version给出git version 1.7.1

回答

19

在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的日期和字符串不分析任何其他日期格式,GIT_AUTHOR_DATE将被视为无效并完全忽略(显然,默默地)。

你的方法应该只要你坚持做工精细,虽然以合成是1973年之后发生的提交,否则,使用其他日期格式之一:)

+0

优秀赶上! – Arrowmaster 2011-02-23 17:46:07

+0

哇,我希望我可以多次赞扬这个。你想怎样才能检查这个? – 2011-02-23 20:56:33

+4

@Adrian我没有看到文档中的任何内容,并且我已经检出了git源代码...所以我开始进行grepping。有时候RTFS会比任何事情都更快地陷入问题的核心:) – hobbs 2011-02-23 21:29:43