2022-09-16 09:58:36 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*
|
2024-01-22 18:29:54 +00:00
|
|
|
* @flow strict-local
|
2022-09-16 09:58:36 +00:00
|
|
|
* @format
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs');
|
2023-11-06 20:59:38 +00:00
|
|
|
const path = require('path');
|
|
|
|
const {cp, echo, exec, exit} = require('shelljs');
|
2022-09-16 09:58:36 +00:00
|
|
|
|
2024-01-22 18:29:54 +00:00
|
|
|
/*::
|
|
|
|
type Commit = string
|
|
|
|
*/
|
|
|
|
|
2022-09-16 09:58:36 +00:00
|
|
|
function isGitRepo() {
|
|
|
|
try {
|
|
|
|
return (
|
|
|
|
exec('git rev-parse --is-inside-work-tree', {
|
|
|
|
silent: true,
|
|
|
|
}).stdout.trim() === 'true'
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
echo(
|
|
|
|
`It wasn't possible to check if we are in a git repository. Details: ${error}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-22 18:29:54 +00:00
|
|
|
function exitIfNotOnGit /*::<T>*/(
|
|
|
|
command /*: () => T */,
|
|
|
|
errorMessage /*: string */,
|
|
|
|
gracefulExit /*: boolean */ = false,
|
2024-01-31 11:36:02 +00:00
|
|
|
// $FlowFixMe[incompatible-return] Asserts return value
|
|
|
|
) /*: T */ {
|
2022-09-16 09:58:36 +00:00
|
|
|
if (isGitRepo()) {
|
|
|
|
return command();
|
|
|
|
} else {
|
|
|
|
echo(errorMessage);
|
|
|
|
exit(gracefulExit ? 0 : 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 18:29:54 +00:00
|
|
|
function isTaggedLatest(commitSha /*: Commit */) /*: boolean */ {
|
2022-09-16 09:58:36 +00:00
|
|
|
return (
|
|
|
|
exec(`git rev-list -1 latest | grep ${commitSha}`, {
|
|
|
|
silent: true,
|
|
|
|
}).stdout.trim() === commitSha
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-22 18:29:54 +00:00
|
|
|
function getBranchName() /*: string */ {
|
2022-09-16 09:58:36 +00:00
|
|
|
return exec('git rev-parse --abbrev-ref HEAD', {
|
|
|
|
silent: true,
|
|
|
|
}).stdout.trim();
|
|
|
|
}
|
|
|
|
|
2024-01-22 18:29:54 +00:00
|
|
|
function getCurrentCommit() /*: Commit */ {
|
2022-09-16 09:58:36 +00:00
|
|
|
return isGitRepo()
|
|
|
|
? exec('git rev-parse HEAD', {
|
|
|
|
silent: true,
|
|
|
|
}).stdout.trim()
|
|
|
|
: 'TEMP';
|
|
|
|
}
|
|
|
|
|
2024-01-22 18:29:54 +00:00
|
|
|
function saveFiles(filePaths /*: Array<string> */, tmpFolder /*: string */) {
|
2022-09-16 09:58:36 +00:00
|
|
|
for (const filePath of filePaths) {
|
|
|
|
const dirName = path.dirname(filePath);
|
|
|
|
if (dirName !== '.') {
|
2022-09-22 14:34:50 +00:00
|
|
|
const destFolder = `${tmpFolder}/${dirName}`;
|
2024-09-17 14:03:44 +00:00
|
|
|
fs.mkdirSync(destFolder, {recursive: true});
|
2022-09-16 09:58:36 +00:00
|
|
|
}
|
2022-09-22 14:34:50 +00:00
|
|
|
cp(filePath, `${tmpFolder}/${filePath}`);
|
2022-09-16 09:58:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 18:29:54 +00:00
|
|
|
function revertFiles(filePaths /*: Array<string> */, tmpFolder /*: string */) {
|
2022-09-16 09:58:36 +00:00
|
|
|
for (const filePath of filePaths) {
|
2022-09-22 14:34:50 +00:00
|
|
|
const absoluteTmpPath = `${tmpFolder}/${filePath}`;
|
2022-09-16 09:58:36 +00:00
|
|
|
if (fs.existsSync(absoluteTmpPath)) {
|
|
|
|
cp(absoluteTmpPath, filePath);
|
|
|
|
} else {
|
|
|
|
echo(
|
|
|
|
`It was not possible to revert ${filePath} since ${absoluteTmpPath} does not exist.`,
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-03 00:40:52 +00:00
|
|
|
// git restore for local path
|
2024-01-22 18:29:54 +00:00
|
|
|
function restore(repoPath /*: string */) {
|
2023-06-03 00:40:52 +00:00
|
|
|
const result = exec('git restore .', {
|
|
|
|
cwd: repoPath,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.code !== 0) {
|
|
|
|
throw new Error(result.stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-16 09:58:36 +00:00
|
|
|
module.exports = {
|
|
|
|
exitIfNotOnGit,
|
|
|
|
getCurrentCommit,
|
|
|
|
getBranchName,
|
|
|
|
isTaggedLatest,
|
|
|
|
revertFiles,
|
|
|
|
saveFiles,
|
2023-06-03 00:40:52 +00:00
|
|
|
restore,
|
2022-09-16 09:58:36 +00:00
|
|
|
};
|