main
js 64 lines 1.57 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7 'use strict';
8
9 const execFileSync = require('child_process').execFileSync;
10
11 const exec = (command, args) => {
12 console.log('> ' + [command].concat(args).join(' '));
13 const options = {
14 cwd: process.cwd(),
15 env: process.env,
16 stdio: 'pipe',
17 encoding: 'utf-8',
18 };
19 return execFileSync(command, args, options);
20 };
21
22 const isGit = () => {
23 try {
24 const wt = execGitCmd(['rev-parse', '--is-inside-work-tree']);
25 return wt.length > 0 && wt[0] === 'true';
26 } catch (_e) {
27 return false;
28 }
29 };
30
31 const isSl = () => {
32 try {
33 execSlCmd(['whereami']);
34 return true;
35 } catch (_e) {
36 return false;
37 }
38 };
39
40 const execGitCmd = args => exec('git', args).trim().toString().split('\n');
41 const execSlCmd = args => exec('sl', args).trim().toString().split('\n');
42
43 const listChangedFiles = () => {
44 if (isGit()) {
45 const mergeBase = execGitCmd(['merge-base', 'HEAD', 'main']);
46 return new Set([
47 ...execGitCmd([
48 'diff',
49 '--name-only',
50 '--diff-filter=ACMRTUB',
51 mergeBase,
52 ]),
53 ...execGitCmd(['ls-files', '--others', '--exclude-standard']),
54 ]);
55 } else if (isSl()) {
56 const mergeBase = execSlCmd(['log', '-r', 'last(public() & ::.)'])[0]
57 .trim()
58 .split(/\s+/)[1];
59 return new Set(execSlCmd(['status', '--no-status', '--rev', mergeBase]));
60 }
61 throw new Error('Not a git or sl repo');
62 };
63
64 module.exports = listChangedFiles;