0

I'm trying to find what is the oldest git commit date since the git tag released. I found this other SO question Get all git commits since last tag and from there I tried this command (where <tag_name> is obviously my tag name).

I first wrote the question with the format %cI but it should be %aI for the author ref not the commiter ref (thanks to @torek for pointing this out)

git log -1 <tag_name>..HEAD --format="%aI"

but this returns the latest commit, not the older commit

ok then I tried to reverse the full list

git log <tag_name>..HEAD --format="%aI" --reverse

and I can see the oldest commits showing on the top but then if I add the max count of -1, well it doesn't work and the reason is because the reverse is only applied after retrieving n count, so I still get the newest instead of the oldest

I saw someone posting this command, but that doesn't work on windows terminal, so I cannot use this command since I'm looking for a universal command (it has to work on all OS)

git log --reverse --format=%aI | head -n 1

So what git command, that works on any OS, can I use to show me the oldest git commit date since my last release tag? By the way I ran an extra git command to get the last tag, if somehow we can it all in 1 git command instead of 2, that would be even more wonderful.

The reason I need to find the oldest commit date is that I want to use it Lerna-Lite for a new feature to show commit author login username in a changelog & github release, and that is only available by querying the GitHub Graphql API to return all commit details and I obviously don't want to query commits that were already released. So I need to be precise when querying from a given date so that it returns the exact amount of data needed and nothing more, hence the need to know the exact date timestamp of the oldest commit since the last release.

EDIT

To be clear, I'm really looking for the oldest commit date since last tag release and I'm not looking for the tag date. My scenario is that if I create a Pull Request while being in v1.0.0 tag but only merge it after the release of v1.1.0 then the commit becomes older than the tag date and so I'm not seeing the commit that was merged because it's older than the tag itself, however I do see it in the git log list.

Also I'm looking for a cross environment command that does not include any | or & because that unfortunately does not work on Windows because we cannot pipe commands on Windows :(

EDIT 2 (alternative solution for now)

So in Lerna-Lite, we use execa to query git in JS/TS, and with that I found an alternative (but is far from being performant). If I use the git log reverse (without the -1 count) then it returns a complete list of all commits which execa returns as a long string and from there I can simply do a string split of \n and get only the first split match which is what I want. The main downside, like I said, is that it's not performant at all and is probably equivalent to using the unix pipe command git log --reverse | head -n 1.

const lastTagName = 'v1.1.0';
const stdout = execa.sync('git', ['log', `${lastTagName}..HEAD`, '--format="%aI"', '--reverse']).stdout;
const [oldestCommitInfo] = stdout.split('\n');

This works and I'll use that for now but I still wish to find a better solution that is much more performant than this alternative and that would for all OS

ghiscoding
  • 12,308
  • 6
  • 69
  • 112
  • Commits contain two time-stamps: author date and committer date. Both can be spoofed (deliberately or, by someone having their computer clock set wrong, accidentally). Do you want those dates, or do you want some other mechanism for finding the "age" of a commit? Remember that commits relate to other commits via the *commit graph* (see, e.g., [Pretty git branch graphs](https://stackoverflow.com/q/1057564/1256452)). – torek Aug 05 '22 at 16:13
  • I simply want to find the oldest commit timestamp (the first person who opened the commit, so it's most probably the author not the committer) that was not yet published since the last release tag and I cannot use the tag date for the reason I explained in the question. – ghiscoding Aug 07 '22 at 15:20
  • So you want to use every commit in the range, look at *both* timestamps of each commit, and choose whichever of all of these timestamps is the lowest number (oldest) and use that particular commit? You will need to write a program to achieve this. – torek Aug 07 '22 at 15:58
  • I don't have to compare, just know the commit author timestamp is good enough for my use case. For now I'm using the alternative that I found and wrote in my last edit, it`s not the most efficient but it works and is cross platform compatible too. If there is a better way of doing this, then I hope someone can post a better answer but for now I'm ok with the alternative/hack – ghiscoding Aug 07 '22 at 20:49
  • `%cI` is fine, it's the committer timestamp (not "commit author timestamp", that's nonsense and highly misleading as the two timestamps are "committer timestamp" and "author timestamp" and "commit author timestamp" sounds like you mean `%a`) in strict ISO format, and what you have now is probably the best you can do. – torek Aug 07 '22 at 22:05
  • oh damn you're so right, sorry about that, the code should be `%aI` instead of `%cI`, I'll update the question – ghiscoding Aug 08 '22 at 00:13

0 Answers0