From 05cebffe16494367f38396a40a56db5866de96ab Mon Sep 17 00:00:00 2001 From: Mike Vitousek Date: Sun, 30 Jun 2024 14:22:22 -0700 Subject: [PATCH] [prettier][eslint] Support sapling in prettier changed files command Summary: The listChangesFiles module didn't work under sapling; this fixes ghstack-source-id: 9f685de5e7550075369723b845104ac0e676ce95 Pull Request resolved: https://github.com/facebook/react/pull/30149 --- scripts/shared/listChangedFiles.js | 42 ++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/scripts/shared/listChangedFiles.js b/scripts/shared/listChangedFiles.js index 2fd80d4b488c4..b6449e5cc6e4b 100644 --- a/scripts/shared/listChangedFiles.js +++ b/scripts/shared/listChangedFiles.js @@ -19,14 +19,46 @@ const exec = (command, args) => { return execFileSync(command, args, options); }; +const isGit = () => { + try { + const wt = execGitCmd(['rev-parse', '--is-inside-work-tree']); + return wt.length > 0 && wt[0] === 'true'; + } catch (_e) { + return false; + } +}; + +const isSl = () => { + try { + execSlCmd(['whereami']); + return true; + } catch (_e) { + return false; + } +}; + const execGitCmd = args => exec('git', args).trim().toString().split('\n'); +const execSlCmd = args => exec('sl', args).trim().toString().split('\n'); const listChangedFiles = () => { - const mergeBase = execGitCmd(['merge-base', 'HEAD', 'main']); - return new Set([ - ...execGitCmd(['diff', '--name-only', '--diff-filter=ACMRTUB', mergeBase]), - ...execGitCmd(['ls-files', '--others', '--exclude-standard']), - ]); + if (isGit()) { + const mergeBase = execGitCmd(['merge-base', 'HEAD', 'main']); + return new Set([ + ...execGitCmd([ + 'diff', + '--name-only', + '--diff-filter=ACMRTUB', + mergeBase, + ]), + ...execGitCmd(['ls-files', '--others', '--exclude-standard']), + ]); + } else if (isSl()) { + const mergeBase = execSlCmd(['log', '-r', 'last(public() & ::.)'])[0] + .trim() + .split(/\s+/)[1]; + return new Set(execSlCmd(['status', '--no-status', '--rev', mergeBase])); + } + throw new Error('Not a git or sl repo'); }; module.exports = listChangedFiles;