2

Because of reasons, the "version" in my application is a timestamp (generated with git show --format=%ct). So the bug report says that vApp-<timestamp> is broken.

How would I go about reversing the operation and getting the commit hash from the timestamp using git?

I can run git log --date=unix and then rely on less to search for the timestamp, but I was hoping there was something more "built-in" to git.

This is distinct from the alleged duplicate because I don't want "all commits for a specific day", I want a single commit given the timestamp of that commit.

josaphatv
  • 633
  • 4
  • 19
  • 3
    You should be defining an explicit tag for any commit that is deployed. – chepner Oct 25 '21 at 18:52
  • 2
    Or...why not just use the commit id for your application "version"? Then you don't need to convert anything. – larsks Oct 25 '21 at 18:57
  • 1
    This is not a duplicate. I don't want "all commits for a specific day", I want to find the commit with a given timestamp. @TTT has provided an answer here that I can't get from the other. – josaphatv Oct 26 '21 at 14:12
  • @chepner We *should* be doing something else. I don't know that "tag every commit that is deployed" works when effectively *every* commit is deployed. – josaphatv Oct 26 '21 at 14:15
  • @larsks That's probably where we should go. But because of more than a decade of built-up legacy, the version needs to be monotonically increasing otherwise a ton of stuff breaks. Fixing that just isn't very high on our priority list since this timestamp approach works relatively well. Especially if it's easy enough to get the commit from the timestamp. – josaphatv Oct 26 '21 at 14:21
  • 1
    @josaphatv I wasn't thinking in terms of CD, which does complicate matters. Arguably, you should probably include the commit hash in the version, not just (or instead of) the timestamp. – chepner Oct 26 '21 at 14:26
  • @chepner my thoughts exactly. Perhaps `vApp--` would suffice. – TTT Oct 26 '21 at 16:14

1 Answers1

5

Suppose your Unix timestamp is 1635020939, then this should do it:

git log --date=unix --before=1635020939 --after=1635020939 --pretty=format:"%h"

Note that Git only stores timestamps to 1-second resolution, so you are not guaranteed to get only 1 commit ID. If multiple commits have the identical committer date timestamp, they all will be returned.

TTT
  • 22,611
  • 8
  • 63
  • 69