Skip to content

Commit

Permalink
Merge pull request #12628 from keymanapp/fix/developer/cherry-pick/12…
Browse files Browse the repository at this point in the history
…626-handle-merge-commits-when-checking-git-log

fix(developer): handle merge commits when checking git log date 🍒 🏠
  • Loading branch information
mcdurdin authored Nov 6, 2024
2 parents cab7cb5 + 3da2c28 commit f183880
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions developer/src/kmc/src/util/getLastGitCommitDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,53 @@ export const expectedGitDateFormat = /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d\d\
*/
export function getLastGitCommitDate(path: string): string {
// TZ=UTC0 git log -1 --no-merges --date=format:%Y-%m-%dT%H:%M:%SZ --format=%ad
let result = null;
let result = callGitLog([
'log', // git log
'-1', // one commit only
'--no-merges', // we're only interested in 'real' commits
'--format=%at', // emit only the commit date as a UNIX timestamp
'--',
path
]);

try {
result = execFileSync('git', [
if(typeof result != 'string') {
return null;
}

if(result == '') {
// #12626: no history was found, but not an error. We may have only a merge
// commit in history for that file, allow merges in the log
result = callGitLog([
'log', // git log
'-1', // one commit only
'--no-merges', // we're only interested in 'real' commits
'--format=%at', // emit only the commit date as a UNIX timestamp
'--',
path
], {
]);

if(typeof result != 'string') {
return null;
}
}

const sec = Number.parseInt(result);
if(Number.isNaN(sec)) {
// We received invalid data, perhaps a git error string so just ignore
return null;
}

// We receive a timestamp in seconds but we need milliseconds
return new Date(sec * 1000).toISOString();
}

function callGitLog(params: string[]) {
try {
const result = execFileSync('git', params, {
encoding: 'utf-8', // force a string result rather than Buffer
windowsHide: true, // on windows, we may need this to suppress a console window popup
stdio: ['pipe', 'pipe', 'pipe'] // all output via pipe, so we don't get git errors on console
});
return result.trim();
} catch (e) {
// If git is not available, or the file is not in-repo, then it is probably
// fine to just silently return null, as the only machines where this is
Expand All @@ -34,9 +66,4 @@ export function getLastGitCommitDate(path: string): string {
// seems low.
return null;
}

// We receive a timestamp in seconds but we need milliseconds
const msec = Number.parseInt(result.trim()) * 1000;

return new Date(msec).toISOString();
}

0 comments on commit f183880

Please sign in to comment.