-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize synchronous getFileCommitDate code
- Loading branch information
Showing
5 changed files
with
54 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ export class FileNotTrackedError extends Error {} | |
* @throws Also throws when `git log` exited with non-zero, or when it outputs | ||
* unexpected text. | ||
*/ | ||
export function getFileCommitDate( | ||
export async function getFileCommitDate( | ||
/** Absolute path to the file. */ | ||
file: string, | ||
args: { | ||
|
@@ -36,12 +36,12 @@ export function getFileCommitDate( | |
/** Use `includeAuthor: true` to get the author information as well. */ | ||
includeAuthor?: false; | ||
}, | ||
): { | ||
): Promise<{ | ||
/** Relevant commit date. */ | ||
date: Date; | ||
/** Timestamp in **seconds**, as returned from git. */ | ||
timestamp: number; | ||
}; | ||
}>; | ||
/** | ||
* Fetches the git history of a file and returns a relevant commit date. | ||
* It gets the commit date instead of author date so that amended commits | ||
|
@@ -52,7 +52,7 @@ export function getFileCommitDate( | |
* @throws Also throws when `git log` exited with non-zero, or when it outputs | ||
* unexpected text. | ||
*/ | ||
export function getFileCommitDate( | ||
export async function getFileCommitDate( | ||
/** Absolute path to the file. */ | ||
file: string, | ||
args: { | ||
|
@@ -63,15 +63,16 @@ export function getFileCommitDate( | |
age?: 'oldest' | 'newest'; | ||
includeAuthor: true; | ||
}, | ||
): { | ||
): Promise<{ | ||
/** Relevant commit date. */ | ||
date: Date; | ||
/** Timestamp in **seconds**, as returned from git. */ | ||
timestamp: number; | ||
/** The author's name, as returned from git. */ | ||
author: string; | ||
}; | ||
export function getFileCommitDate( | ||
}>; | ||
|
||
export async function getFileCommitDate( | ||
file: string, | ||
{ | ||
age = 'oldest', | ||
|
@@ -80,11 +81,11 @@ export function getFileCommitDate( | |
age?: 'oldest' | 'newest'; | ||
includeAuthor?: boolean; | ||
}, | ||
): { | ||
): Promise<{ | ||
date: Date; | ||
timestamp: number; | ||
author?: string; | ||
} { | ||
}> { | ||
if (!shell.which('git')) { | ||
throw new GitNotFoundError( | ||
`Failed to retrieve git history for "${file}" because git is not installed.`, | ||
|
@@ -105,11 +106,24 @@ export function getFileCommitDate( | |
.filter(Boolean) | ||
.join(' '); | ||
|
||
const result = shell.exec(`git log ${args} -- "${path.basename(file)}"`, { | ||
// Setting cwd is important, see: https://github.com/facebook/docusaurus/pull/5048 | ||
cwd: path.dirname(file), | ||
silent: true, | ||
const result = await new Promise<{ | ||
code: number; | ||
stdout: string; | ||
stderr: string; | ||
}>((resolve) => { | ||
shell.exec( | ||
`git log ${args} -- "${path.basename(file)}"`, | ||
Check warning Code scanning / CodeQL Unsafe shell command constructed from library input Medium
This string concatenation which depends on
library input Error loading related location Loading shell command Error loading related location Loading This string concatenation which depends on library input Error loading related location Loading shell command Error loading related location Loading This string concatenation which depends on library input Error loading related location Loading shell command Error loading related location Loading This string concatenation which depends on library input is later used in a shell command Error loading related location Loading This string concatenation which depends on library input is later used in a shell command Error loading related location Loading This string concatenation which depends on library input is later used in a shell command Error loading related location Loading |
||
{ | ||
// Setting cwd is important, see: https://github.com/facebook/docusaurus/pull/5048 | ||
cwd: path.dirname(file), | ||
silent: true, | ||
}, | ||
(code, stdout, stderr) => { | ||
resolve({code, stdout, stderr}); | ||
}, | ||
); | ||
}); | ||
|
||
if (result.code !== 0) { | ||
throw new Error( | ||
`Failed to retrieve the git history for file "${file}" with exit code ${result.code}: ${result.stderr}`, | ||
|