Skip to content

Commit

Permalink
Merge pull request #12627 from keymanapp/fix/developer/12626-handle-m…
Browse files Browse the repository at this point in the history
…erge-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 7059773 + 7af4ae4 commit 121d0e3
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions developer/src/kmc/src/util/getLastGitCommitDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,54 @@ export const expectedGitDateFormat = /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d\d\
* @returns string, in RFC3339, 'YYYY-MM-DDThh:nn:ss.SSSZ'
*/
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;
// git log -1 --no-merges --format=%at
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 121d0e3

Please sign in to comment.