Skip to content

Commit

Permalink
Fix last updated time misleading, only show when file content change
Browse files Browse the repository at this point in the history
or otherwise when it is first created

Fix #1015
  • Loading branch information
fiennyangeln committed Oct 8, 2018
1 parent 2e96569 commit 26957af
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions v1/lib/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,35 @@ function idx(target, keyPaths) {
);
}

function isNormalInteger(str) {
return /^\+?(0|[1-9]\d*)$/.test(str);
}

function getGitLastUpdated(filepath) {
const timeSpan = spawn
.sync('git', ['log', '-1', '--format=%ct', filepath])
.stdout.toString('utf-8');
// To differentiate between content change and file renaming / moving, use --summary
// To follow the file history until before it is moved (when we create new version), use
// --follow
const result = spawn.sync('git', [
'log',
'--follow',
'--summary',
'--format=%ct',
filepath,
]);

// Format the log results to be ['1234567', 'rename ...', '1234566', 'move ...', '1234565', '1234564']
const records = result.stdout
.toString('utf-8')
.replace(/\n\s*\n/g, '\n')
.split('\n')
.filter(String);

const timeSpan = records.find((item, index, arr) =>
// The correct timeSpan will be a number which is not followed by summary meaning
// the next element is also a number OR it is the last 2 element (since the
// last element will always be the summary -- 'create mode ... ')
isNormalInteger(item) && (index + 2 === arr.length || isNormalInteger(arr[index + 1]))
);
if (timeSpan) {
const date = new Date(parseInt(timeSpan, 10) * 1000);
return date.toLocaleString();
Expand Down

0 comments on commit 26957af

Please sign in to comment.