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
`) in strict ISO format, and what you have now is probably the best you can do.
– torek Aug 07 '22 at 22:05