如何使用 cygwin date 返回过去,返回具有静态秒值的日期

发布于 2024-12-09 05:22:02 字数 327 浏览 0 评论 0原文

我正在使用 git,尝试同步到去年每个月的第一次提交。我希望能够逐月回顾,在每个月的第一天,在每个月的第一秒。或者至少来自某个恒定的“秒”值。到目前为止,我有这个:

$(git rev-list --before "$(date -d "$(date +%Y-%m-01) -$i Months" +%Y-%m)- 01" -n 01 HEAD)

这显然不包括恒定的秒值。就目前情况而言,运行此脚本并在一小时后再次运行它会返回两个不同的 sha1,因为它距离我运行该脚本的确切时间要回溯 x 个月。我希望无论何时运行此脚本,返回的 sha1 都是相同的。这有道理吗?有什么想法吗?

I am using git, trying to sync to the first commits of each month for the last year. I want to be able to go back month by month, on the first day of the month, ON THE FIRST SECOND OF THE MONTH. or at least from some constant "seconds" value. So far I have this:

$(git rev-list --before "$(date -d "$(date +%Y-%m-01) -$i months" +%Y-%m)-01" -n 01 HEAD)

This obviously does not include a constant seconds value. As it stands, running this script and then running it again an hour later returns two different sha1's because it is going back x months FROM THE EXACT TIME at which I run the script. I want the returned sha1's to be the same no matter when I run this script. Does that make sense? Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩心态 2024-12-16 05:22:02

首先,您的命令中有一个拼写错误。它必须是 --before="$(...,您缺少 =

date 的行为不是问题这里 date +%Y-%m-01 将返回类似 2011-10-01 的字符串,不包含其他时间信息,因此第二个日期调用。将减少 $i 月数并且还将返回类似于 2011-09-01 的格式的字符串,除了该字符串之外,不会将任何其他信息作为参数 的值传递给 git-rev-list。 >--before=

使用 git-rev-list 时必须考虑一些事项:

  1. 使用 HEAD 时,您始终引用 <例如,当您当前分支时。检查您的 HEAD 将更改的获得的提交 ID 之一,您可能想使用 master 或任何其他分支名称作为参考。Git
  2. 没有提交的时间顺序。您可以在 9 月 1 日的提交之后按层次在 8 月 1 日创作提交。当使用 --before--after 参数时,这将导致输出混乱,因为它们依赖于提交者字段的时间戳。
  3. 提交者字段的时间戳也可能会产生误导。当您的分支没有线性历史记录时,很难判断某个提交在历史上的某个时刻是否已成为存储库中分支的一部分。作者可能在提交 X 个月后推送/合并了他的分支,因此其他人不可见。

考虑到上述所有内容,以下命令对我有用:

$(git rev-list --after="$(date -d "$(date +%Y-%m-01) -$i Months" + %Y-%m)-01 00:00:00" master | tail -n 1)

这将返回给定月份第一天之后第一次提交的 ID。 (此提交不一定是在该月内进行的,也许该月没有任何提交。)

First of all, you have a typo in your command. It has to be --before="$(..., you are missing the =.

The behaviour of date is not the problem here. date +%Y-%m-01 will return something like 2011-10-01 as a string, there's no additional time information included. So the second date call will decrease it by the $i number of months and will also return a sting of the format like 2011-09-01. Besides that string no additional information is passed to git-rev-list as value of argument --before=.

There are some things you have to consider using git-rev-list:

  1. When using HEAD you're always referring to the current branch. So when you for example checkout one of the obtained commit-IDs your HEAD will change. You maybe want to use master or any other branch name as reference instead.
  2. Git has no temporal order of commits. You can have a commit authored on 1st of August hierarchically after a commit dated 1st of September. This will result in confusing outputs when using --before or --after arguments since they rely on the timestamp of the committer field.
  3. The timestamp of the committer field may also be misleading. When your branch doesn't have a linear history, it's hard to tell whether a commit has been part of the branch in a repository at a certain point in history. The author may have pushed/merged his branch X months after doing a commit, so it have not been visible to others.

Considering all the said, the following command works for me:

$(git rev-list --after="$(date -d "$(date +%Y-%m-01) -$i months" +%Y-%m)-01 00:00:00" master | tail -n 1)

This will return the ID of the first commit after the first of the given month. (This commit was not necessarily made during that month, maybe there were no commits in that month anyway.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文