-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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 last updated time misleading, only show when file content change or otherwise when it first created #1023
Changes from 2 commits
26957af
5d1e552
100b117
e3f3ca3
b38dd21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,10 +38,37 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for only noticing this now, but we should add a try/catch around the code within There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually im hoping we remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @endiliey yeah i see some v2 code uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. V1 too. Check |
||
// 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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks right, but could you test on a file that only has one commit? Could you also test on files that have 0 commits, aka newly-created files that haven't been checked into Git yet. I suspect we this part might crash. We don't want the page to blow up for any newly-created pages else they wouldn't be able to preview it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We currently have test that check for file with only one commit, non existing and those with no git timestamp in here |
||
(item, index, arr) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rewrite this part as such to make the logic clearer: const timeSpan = records.find((item, index, arr) => {
const isTimestamp = isNormalInteger(item);
const isLastItem = index + 2 <= arr.length; // Note: Let's use <= instead of === to be safer.
const nextItemIsTimestamp = isNormalInteger(arr[index + 1]);
return isTimestamp && (isLastItem || nextItemIsTimestamp);
}); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if we use <= it will return true for all element except the last |
||
// 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(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use this regex instead:
^\d+$
, which is simpler. This regex means the line only contains integers. Check out https://regex101.com/r/mp3HYD/2/There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be noted that above (fienny's PR) allow an optional
+
at the beginning of stringYangshun's recommended regex doesnt allow that.
If the optional
+
is intended, use/^\+?\d+$/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if there is
normal
andabnormal
integer from the function naming.isInt
is fine o.oThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The numbers are timestamps, why do we need the +? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yangshun yup i dont think we need the +