@samitouri / QOS-React-2 / commits / 26fc16484e

Script for syncing changes between forks (#18550)

Adds command `yarn merge-fork`. ```sh yarn merge-fork --base-dir=packages/react-reconciler/src ReactFiberWorkLoop ``` This will take all the changes in `ReactFiberWorkLoop.new.js` and apply them to `ReactFiberWorkLoop.old.js`. You can merge multiple modules at a time: ```sh yarn merge-fork \ --base-dir=packages/react-reconciler/src \ ReactFiberWorkLoop \ ReactFiberBeginWork \ ReactFiberCompleteWork \ ReactFiberCommitWork ``` You can provide explicit "old" and "new" file names. This only works for one module at a time: ```sh yarn merge-fork \ --base-dir=packages/react-reconciler/src \ --old=ReactFiberExpirationTime.js \ --new=ReactFiberLane.js ``` The default is to merge changes from the new module to the old one. To merge changes in the opposite direction, use `--reverse`. ```sh yarn merge-fork \ --reverse \ --base-dir=packages/react-reconciler/src \ ReactFiberWorkLoop ``` By default, the changes are compared to HEAD. You can use `--base-ref` to compare to any rev. For example, while working on a PR, you might make multiple commits to the new fork before you're ready to backport them to the old one. In that case, you want to compare to the merge base of your PR branch: ```sh yarn merge-fork \ --base-ref=$(git merge-base HEAD origin/master) --base-dir=packages/react-reconciler/src \ ReactFiberWorkLoop ```

Andrew Clark committed Apr 9, 2020 at 11:37 UTC 26fc16484e0586b65c7f549934354ef08f8d403e
3 files changed +169 -1
package.json
+2 -1
@@ -124,6 +124,7 @@
124 "flow-ci": "node ./scripts/tasks/flow-ci.js",
125 "prettier": "node ./scripts/prettier/index.js write-changed",
126 "prettier-all": "node ./scripts/prettier/index.js write",
127 - "version-check": "node ./scripts/tasks/version-check.js"
127 + "version-check": "node ./scripts/tasks/version-check.js",
128 + "merge-fork": "node ./scripts/merge-fork/merge-fork.js"
129 }
130 }
scripts/merge-fork/README.md new
+58
@@ -0,0 +1,58 @@
1 +# merge-fork
2 +
3 +Script for syncing changes between forked modules.
4 +
5 +## Basic example
6 +
7 +```sh
8 +yarn merge-fork --base-dir=packages/react-reconciler/src ReactFiberWorkLoop
9 +```
10 +
11 +This will take all the changes in `ReactFiberWorkLoop.new.js` and apply them to `ReactFiberWorkLoop.old.js`.
12 +
13 +## Syncing multiple modules at once
14 +
15 +You can merge multiple modules at a time:
16 +
17 +```sh
18 +yarn merge-fork \
19 + --base-dir=packages/react-reconciler/src \
20 + ReactFiberWorkLoop \
21 + ReactFiberBeginWork \
22 + ReactFiberCompleteWork \
23 + ReactFiberCommitWork
24 +```
25 +
26 +## Syncing modules with different names
27 +
28 +You can provide explicit "old" and "new" file names. This only works for one module at a time:
29 +
30 +```sh
31 +yarn merge-fork \
32 + --base-dir=packages/react-reconciler/src \
33 + --old=ReactFiberExpirationTime.js \
34 + --new=ReactFiberLane.js
35 +```
36 +
37 +## Syncing modules in the opposite direction (old -> new)
38 +
39 +The default is to merge changes from the new module to the old one. To merge changes in the opposite direction, use `--reverse`.
40 +
41 +```sh
42 +yarn merge-fork \
43 + --reverse \
44 + --base-dir=packages/react-reconciler/src \
45 + ReactFiberWorkLoop
46 +```
47 +
48 +## Comparing changes to an older base rev
49 +
50 +By default, the changes are compared to HEAD. You can use `--base-ref` to compare to any rev. For example, while working on a PR, you might make multiple commits to the new fork before you're ready to backport them to the old one. In that case, you want to compare to the merge base of your PR branch:
51 +
52 +```sh
53 +yarn merge-fork \
54 + --base-ref=$(git merge-base HEAD origin/master)
55 + --base-dir=packages/react-reconciler/src \
56 + ReactFiberWorkLoop
57 +```
58 +
scripts/merge-fork/merge-fork.js new
+109
@@ -0,0 +1,109 @@
1 +'use strict';
2 +
3 +/* eslint-disable no-for-of-loops/no-for-of-loops */
4 +
5 +const {writeFileSync} = require('fs');
6 +const path = require('path');
7 +const {spawnSync} = require('child_process');
8 +const minimist = require('minimist');
9 +const tmp = require('tmp');
10 +
11 +const argv = minimist(process.argv.slice(2), {
12 + boolean: ['reverse'],
13 + default: {
14 + 'base-ref': 'HEAD',
15 + },
16 +});
17 +
18 +const baseRef = argv['base-ref'];
19 +const baseDir = argv['base-dir'];
20 +
21 +function resolvePath(file) {
22 + return baseDir !== undefined ? path.join(baseDir, file) : file;
23 +}
24 +
25 +function getTransforms() {
26 + const old = argv.old;
27 + const base = argv.base;
28 + const _new = argv.new;
29 + if (old !== undefined) {
30 + if (_new === undefined) {
31 + throw Error('Cannot provide --old without also providing --new');
32 + }
33 + const oldPath = resolvePath(old);
34 + const newPath = resolvePath(_new);
35 +
36 + let basePath;
37 + let fromPath;
38 + let toPath;
39 + if (argv.reverse) {
40 + fromPath = oldPath;
41 + toPath = newPath;
42 + basePath = base !== undefined ? resolvePath(basePath) : oldPath;
43 + } else {
44 + fromPath = newPath;
45 + toPath = oldPath;
46 + basePath = base !== undefined ? resolvePath(basePath) : newPath;
47 + }
48 +
49 + return [
50 + {
51 + base: basePath,
52 + from: fromPath,
53 + to: toPath,
54 + },
55 + ];
56 + } else if (_new !== undefined) {
57 + throw Error('Cannot provide --new without also providing --old');
58 + }
59 + return argv._.map(filename => {
60 + const oldPath = resolvePath(filename + '.old.js');
61 + const newPath = resolvePath(filename + '.new.js');
62 +
63 + let basePath;
64 + let fromPath;
65 + let toPath;
66 + if (argv.reverse) {
67 + fromPath = oldPath;
68 + toPath = newPath;
69 + basePath = base !== undefined ? resolvePath(basePath) : oldPath;
70 + } else {
71 + fromPath = newPath;
72 + toPath = oldPath;
73 + basePath = base !== undefined ? resolvePath(basePath) : newPath;
74 + }
75 + return {
76 + base: basePath,
77 + from: fromPath,
78 + to: toPath,
79 + };
80 + });
81 +}
82 +
83 +for (const {base: baseFilename, from, to} of getTransforms()) {
84 + // Write the base file contents to a temporary file
85 + const gitShowResult = spawnSync(
86 + 'git',
87 + ['show', `${baseRef}:${baseFilename}`],
88 + {
89 + stdio: 'pipe',
90 + }
91 + );
92 + if (gitShowResult.status !== 0) {
93 + console.error('' + gitShowResult.stderr);
94 + continue;
95 + }
96 +
97 + const baseFileContents = gitShowResult.stdout;
98 + const base = tmp.fileSync().name;
99 + writeFileSync(base, baseFileContents);
100 +
101 + // Run the merge with `git merge-file`
102 + const mergeFileResult = spawnSync('git', ['merge-file', to, base, from], {
103 + stdio: 'pipe',
104 + });
105 +
106 + if (mergeFileResult.status !== 0) {
107 + console.error('' + mergeFileResult.stderr);
108 + }
109 +}