fix: exclude file movements for last updated time (#1023)

* Fix last updated time misleading, only show when file content change
or otherwise when it is first created

Fix #1015

* Fix prettier

* - Simplify regex
- Uses shelljs instead of cross-spawn
- Make logic clearer

* Add test when repositories is moved

* Use shell.exec mock

I initially try to mock the whole shelljs. But it returns error
shell.exec is not a function when i try to provide the mockResolvedValue
I think it is because of the inner code of shelljs who run a forEach to
require each of its method which make it a promise. I tried moving the
jest.mock inside beforeAll and also adding babel-dynamic-import but it did
not solve the problem. In the end, I decided to just mock shelljs.exec since
it is the only function used anyway
This commit is contained in:
Fienny Angelina 2018-10-15 05:09:39 +08:00 committed by Yangshun Tay
parent bc7be697ad
commit a39677c202
2 changed files with 63 additions and 4 deletions

View file

@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const spawn = require('cross-spawn');
const shell = require('shelljs');
const TRUNCATE_MARKER = /<!--\s*truncate\s*-->/;
@ -38,10 +38,34 @@ function idx(target, keyPaths) {
);
}
function isNormalInteger(str) {
return /^\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 silentState = shell.config.silent; // save old silent state
shell.config.silent = true;
const result = shell
.exec(`git log --follow --summary --format=%ct ${filepath}`)
.stdout.trim();
shell.config.silent = silentState;
// Format the log results to be ['1234567', 'rename ...', '1234566', 'move ...', '1234565', '1234564']
const records = result
.toString('utf-8')
.replace(/\n\s*\n/g, '\n')
.split('\n')
.filter(String);
const timeSpan = records.find((item, index, arr) => {
const isTimestamp = isNormalInteger(item);
const isLastTwoItem = index + 2 >= arr.length;
const nextItemIsTimestamp = isNormalInteger(arr[index + 1]);
return isTimestamp && (isLastTwoItem || nextItemIsTimestamp);
});
if (timeSpan) {
const date = new Date(parseInt(timeSpan, 10) * 1000);
return date.toLocaleString();