[英]Change git commit message for commits older than particular date
I need to update commit messages for commits, that were made before certain date. 我需要更新在特定日期之前进行的提交的提交消息。 My idea for the code: 我对代码的想法:
git filter-branch -f --msg-filter '
if [ "$GIT_AUTHOR_DATE" < "2012-11-01T00:00:00" ];
then
cat && echo "#imported"
else
cat &
fi'
HEAD
Can the 'if' statement compare dates with greater than / less than at all? 'if'语句是否可以将日期大于/小于的日期进行比较?
The test
program 1 usually can compare strings like this, but it does lexicographic comparisons. test
程序 1通常可以像这样比较字符串,但是它可以进行字典比较。 The ability to do such comparisons is an extension , but is available on Linux , Mac OS X , and most if not all BSDs . 进行这种比较的功能是一种扩展 ,但在Linux , Mac OS X和大多数(如果不是全部) BSD上可用。
There are a number of issues with doing this: 这样做有很多问题:
The <
or >
must be escaped from the shell. <
或>
必须从外壳中转义。 [ "abc" < "def" ]
runs /bin/[
with arguments "abc"
and ]
, with standard input being redirected from file def
, assuming file def
exists (otherwise you get an error). [ "abc" < "def" ]
运行/bin/[
与参数"abc"
和]
,与标准的输入从文件重定向def
,假设文件def
存在(否则你得到一个错误)。
As already noted, the comparison is lexicographic. 如前所述,比较是按字典顺序进行的。 This means that to compare dates, they must be in a form in which such a comparison produces the correct result. 这意味着要比较日期,日期必须采用这种形式才能产生正确的结果。 Using ISO 8601 format will actually work, since the fields are fixed size and are in big-endian order. 实际上,使用ISO 8601格式是可行的,因为这些字段是固定大小的,并且按big-endian顺序排列。 However, filter-branch
does not export the date in ISO 8601 form. 但是, filter-branch
不会以ISO 8601格式导出日期。 What you get instead looks like 1460884741 -0700
, for instance. 例如,您得到的结果看起来像是1460884741 -0700
。 The first number is a time stamp in seconds since 1970, and the second is a time zone offset (which you should largely ignore and simply work directly in UTC). 第一个数字是自1970年以来以秒为单位的时间戳,第二个是时区偏移量(您应该很大程度上忽略它,而直接在UTC中直接工作)。
Besides these problems, your message-filter needs to avoid backgrounding the cat
command. 除了这些问题之外,您的消息过滤器还需要避免使cat
命令后台运行。 Hence, what you want is: 因此,您想要的是:
First, convert 2012-11-01T00:00:00 into a UTC time stamp. 首先,将2012-11-01T00:00:00转换为UTC时间戳。 This is a bit tricky. 这有点棘手。 We can use python and its time.mktime
but this insists on doing local time correction, so we must un-do the local time correction, which depends in part on whether DST is in force. 我们可以使用python及其time.mktime
但是它坚持要进行本地时间校正,因此我们必须取消本地时间校正,这部分取决于DST是否有效。 My local time zone offset is -08:00 except during DST (such as now) when it is -07:00. 我的本地时区偏移为-08:00,但DST(例如现在)为-07:00时除外。 This may thus be off by an hour (it was in my first paste, and I didn't even notice; edited now to fix): 因此,这可能会减少一个小时(这是在我的第一个粘贴中,我什至没有注意到;现在进行编辑以进行修复):
$ python -c 'import time; print(int(time.mktime((2012,11,1,-8,0,0,0,0,0))))' 1351728000
To verify that this is the correct time stamp: 要验证这是正确的时间戳记:
$ env TZ=UTC date -r 1351728000 Thu Nov 1 00:00:00 UTC 2012
Then, use this value in your --msg-filter
with a numeric comparison on the first (non-offset) field in $GIT_AUTHOR_DATE
or $GIT_COMMITTER_DATE
as desired: 然后,在您的--msg-filter
使用此值, $GIT_COMMITTER_DATE
根据需要在$GIT_AUTHOR_DATE
或$GIT_COMMITTER_DATE
的第一个(非偏移量)字段上进行数字比较:
... --msg-filter 'cat; set $GIT_AUTHOR_DATE; \\ test $1 -lt 1351728000 && echo "#imported"' ...
All the other usual caveats about using git filter-branch
apply, of course. 当然,所有其他有关使用git filter-branch
警告也适用。
1 /bin/[
is a link to /bin/test
. 1 /bin/[
是/bin/test
的链接。 When test
is invoked as [
it looks for a closing ]
but is otherwise the same program. 当test
以[
查找结束符]
形式调用时,否则为同一程序。 It is also built in to many shells for speed, but still implements the same semantics. 为了提高速度,它也内置在许多shell中,但仍实现相同的语义。 2 2
2 Or at least, is supposed to do so. 2或者至少应该这样做。
Thanks to ideas from torek, I updated my script: 感谢torek的想法,我更新了脚本:
git filter-branch -f --msg-filter '
if [ "$GIT_AUTHOR_DATE" \< "@1351728000" ];
then
cat && echo "#imported"
else
cat &
fi' HEAD
Notable changes: 显着变化:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.