Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(developer): handle merge commits when checking git log date #12627

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
Loading