backport 7.4.0 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -13
- package/dist/backportRun.d.ts +8 -3
- package/dist/backportRun.js +54 -42
- package/dist/entrypoint.cli.js +1 -1
- package/dist/entrypoint.module.d.ts +8 -3
- package/dist/entrypoint.module.js +3 -11
- package/dist/entrypoint.module.private.test.js +47 -346
- package/dist/options/cliArgs.d.ts +9 -4
- package/dist/options/cliArgs.js +28 -15
- package/dist/options/cliArgs.test.js +3 -3
- package/dist/options/config/globalConfig.d.ts +2 -2
- package/dist/options/config/globalConfig.js +13 -10
- package/dist/options/config/readConfigFile.d.ts +2 -2
- package/dist/options/options.d.ts +3 -3
- package/dist/options/options.test.js +5 -1
- package/dist/runSequentially.d.ts +6 -2
- package/dist/runSequentially.js +23 -7
- package/dist/scripts/postinstall.js +1 -0
- package/dist/scripts/postinstall.test.js +5 -1
- package/dist/services/HandledError.d.ts +1 -1
- package/dist/services/HandledError.js +2 -2
- package/dist/services/child-process-promisified.d.ts +19 -3
- package/dist/services/child-process-promisified.js +46 -17
- package/dist/services/git.d.ts +6 -7
- package/dist/services/git.integration.test.js +381 -65
- package/dist/services/git.js +188 -122
- package/dist/services/git.private.test.d.ts +1 -0
- package/dist/services/git.private.test.js +618 -0
- package/dist/services/git.test.js +167 -124
- package/dist/services/github/v3/addAssigneesToPullRequest.js +1 -1
- package/dist/services/github/v3/addLabelsToPullRequest.js +1 -1
- package/dist/services/github/v3/addReviewersToPullRequest.js +2 -1
- package/dist/services/github/v3/createPullRequest.js +3 -1
- package/dist/services/github/v3/createStatusComment.js +14 -15
- package/dist/services/github/v3/createStatusComment.test.js +15 -5
- package/dist/services/github/v4/enablePullRequestAutoMerge.js +2 -4
- package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +1 -0
- package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +5 -1
- package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +1 -0
- package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +5 -1
- package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +1 -0
- package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +5 -1
- package/dist/services/github/v4/fetchCommits/fetchPullRequestsBySearchQuery.js +1 -0
- package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.private.test.js +5 -1
- package/dist/services/logger.js +5 -1
- package/dist/services/test.d.ts +1 -0
- package/dist/services/test.js +23 -0
- package/dist/test/e2e/cli/entrypoint.cli.private.test.js +194 -12
- package/dist/test/e2e/cli/repo-with-backportrc-removed.private.test.js +17 -16
- package/dist/test/e2e/cli/runBackportViaCli.d.ts +2 -1
- package/dist/test/e2e/cli/runBackportViaCli.js +10 -2
- package/dist/test/e2e/module/entrypoint.module.mutation.test.js +26 -19
- package/dist/test/handleUnbackportedPullRequests.private.test.js +10 -8
- package/dist/test/sandbox.js +5 -1
- package/dist/test/setupFiles/automatic-mocks.js +5 -1
- package/dist/ui/cherrypickAndCreateTargetPullRequest.js +1 -1
- package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +7 -159
- package/dist/ui/getCommitsWithoutBackports.test.js +5 -1
- package/dist/ui/getGitConfigAuthor.js +1 -1
- package/dist/ui/getTargetBranches.test.js +5 -1
- package/dist/ui/ora.d.ts +1 -8
- package/dist/ui/setupRepo.js +1 -1
- package/dist/ui/setupRepo.test.js +98 -61
- package/dist/utils/isErrnoError.d.ts +2 -0
- package/dist/utils/isErrnoError.js +7 -0
- package/dist/utils/packageVersion.d.ts +1 -1
- package/dist/utils/packageVersion.js +1 -1
- package/package.json +13 -13
- package/yarn.lock +414 -695
package/dist/services/git.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getLocalSourceRepoPath = exports.pushBackportBranch = exports.getRepoForkOwner = exports.deleteBackportBranch = exports.createBackportBranch = exports.getGitConfig = exports.getUnstagedFiles = exports.getConflictingFiles = exports.commitChanges = exports.cherrypick = exports.getCommitsInMergeCommit = exports.getIsMergeCommit = exports.fetchBranch = exports.addRemote = exports.deleteRemote = exports.getIsCommitInBranch = exports.getGitProjectRootPath = exports.getRepoInfoFromGitRemotes = exports.isLocalConfigFileModified = exports.isLocalConfigFileUntracked = exports.getLocalConfigFileCommitDate = exports.cloneRepo = exports.getRemoteUrl = void 0;
|
|
4
4
|
const path_1 = require("path");
|
|
5
5
|
const lodash_1 = require("lodash");
|
|
6
6
|
const ora_1 = require("../ui/ora");
|
|
@@ -16,37 +16,47 @@ function getRemoteUrl({ repoName, accessToken, gitHostname = 'github.com' }, rep
|
|
|
16
16
|
exports.getRemoteUrl = getRemoteUrl;
|
|
17
17
|
async function cloneRepo({ sourcePath, targetPath }, onProgress) {
|
|
18
18
|
return new Promise((resolve, reject) => {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
const subprocess = (0, child_process_promisified_1.spawnOriginal)('git', [
|
|
20
|
+
'clone',
|
|
21
|
+
sourcePath,
|
|
22
|
+
targetPath,
|
|
23
|
+
'--progress',
|
|
24
|
+
]);
|
|
22
25
|
const progress = {
|
|
23
26
|
fileUpdate: 0,
|
|
24
27
|
objectReceive: 0,
|
|
25
28
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
29
|
+
subprocess.on('error', (err) => reject(err));
|
|
30
|
+
subprocess.stderr.on('data', (data) => {
|
|
31
|
+
logger_1.logger.verbose(data);
|
|
32
|
+
const [, objectReceiveProgress] = data.toString().match(/^Receiving objects:\s+(\d+)%/) || [];
|
|
33
|
+
if (objectReceiveProgress) {
|
|
34
|
+
progress.objectReceive = parseInt(objectReceiveProgress, 10);
|
|
35
|
+
}
|
|
36
|
+
const [, fileUpdateProgress] = data.toString().match(/^Updating files:\s+(\d+)%/) || [];
|
|
37
|
+
if (fileUpdateProgress) {
|
|
38
|
+
progress.objectReceive = 100;
|
|
39
|
+
progress.fileUpdate = parseInt(fileUpdateProgress, 10);
|
|
40
|
+
}
|
|
41
|
+
const progressSum = Math.round(progress.fileUpdate * 0.1 + progress.objectReceive * 0.9);
|
|
42
|
+
if (progressSum > 0) {
|
|
43
|
+
onProgress(progressSum);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
subprocess.on('close', (code) => {
|
|
47
|
+
if (code === 0 || code === null) {
|
|
48
|
+
resolve();
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
reject(new Error(`Git clone failed with exit code: ${code}`));
|
|
52
|
+
}
|
|
53
|
+
});
|
|
44
54
|
});
|
|
45
55
|
}
|
|
46
56
|
exports.cloneRepo = cloneRepo;
|
|
47
57
|
async function getLocalConfigFileCommitDate({ cwd }) {
|
|
48
58
|
try {
|
|
49
|
-
const { stdout } = await (0, child_process_promisified_1.
|
|
59
|
+
const { stdout } = await (0, child_process_promisified_1.spawnPromise)('git', ['--no-pager', 'log', '-1', '--format=%cd', '.backportrc.json'], cwd);
|
|
50
60
|
const timestamp = Date.parse(stdout);
|
|
51
61
|
if (timestamp > 0) {
|
|
52
62
|
return timestamp;
|
|
@@ -60,7 +70,7 @@ exports.getLocalConfigFileCommitDate = getLocalConfigFileCommitDate;
|
|
|
60
70
|
async function isLocalConfigFileUntracked({ cwd }) {
|
|
61
71
|
try {
|
|
62
72
|
// list untracked files
|
|
63
|
-
const { stdout } = await (0, child_process_promisified_1.
|
|
73
|
+
const { stdout } = await (0, child_process_promisified_1.spawnPromise)('git', ['ls-files', '.backportrc.json', '--exclude-standard', '--others'], cwd);
|
|
64
74
|
return !!stdout;
|
|
65
75
|
}
|
|
66
76
|
catch (e) {
|
|
@@ -70,17 +80,17 @@ async function isLocalConfigFileUntracked({ cwd }) {
|
|
|
70
80
|
exports.isLocalConfigFileUntracked = isLocalConfigFileUntracked;
|
|
71
81
|
async function isLocalConfigFileModified({ cwd }) {
|
|
72
82
|
try {
|
|
73
|
-
const { stdout } = await (0, child_process_promisified_1.
|
|
83
|
+
const { stdout } = await (0, child_process_promisified_1.spawnPromise)('git', ['--no-pager', 'diff', 'HEAD', '--name-only', '.backportrc.json'], cwd);
|
|
74
84
|
return !!stdout;
|
|
75
85
|
}
|
|
76
86
|
catch (e) {
|
|
77
|
-
return;
|
|
87
|
+
return false;
|
|
78
88
|
}
|
|
79
89
|
}
|
|
80
90
|
exports.isLocalConfigFileModified = isLocalConfigFileModified;
|
|
81
91
|
async function getRepoInfoFromGitRemotes({ cwd }) {
|
|
82
92
|
try {
|
|
83
|
-
const { stdout } = await (0, child_process_promisified_1.
|
|
93
|
+
const { stdout } = await (0, child_process_promisified_1.spawnPromise)('git', ['remote', '--verbose'], cwd);
|
|
84
94
|
const remotes = stdout
|
|
85
95
|
.split('\n')
|
|
86
96
|
.map((line) => {
|
|
@@ -100,9 +110,8 @@ async function getRepoInfoFromGitRemotes({ cwd }) {
|
|
|
100
110
|
exports.getRepoInfoFromGitRemotes = getRepoInfoFromGitRemotes;
|
|
101
111
|
async function getGitProjectRootPath(dir) {
|
|
102
112
|
try {
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
});
|
|
113
|
+
const cwd = dir;
|
|
114
|
+
const { stdout } = await (0, child_process_promisified_1.spawnPromise)('git', ['rev-parse', '--show-toplevel'], cwd);
|
|
106
115
|
return stdout.trim();
|
|
107
116
|
}
|
|
108
117
|
catch (e) {
|
|
@@ -113,37 +122,45 @@ async function getGitProjectRootPath(dir) {
|
|
|
113
122
|
exports.getGitProjectRootPath = getGitProjectRootPath;
|
|
114
123
|
async function getIsCommitInBranch(options, commitSha) {
|
|
115
124
|
try {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
});
|
|
125
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
126
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['merge-base', '--is-ancestor', commitSha, 'HEAD'], cwd);
|
|
119
127
|
return true;
|
|
120
128
|
}
|
|
121
129
|
catch (e) {
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
130
|
+
const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
|
|
131
|
+
if (isSpawnError) {
|
|
132
|
+
const commitNotInBranch = e.context.code === 1 && e.context.stderr === '';
|
|
133
|
+
const commitNotExist = e.context.code === 128 &&
|
|
134
|
+
e.context.stderr.includes('Not a valid object name');
|
|
135
|
+
if (commitNotInBranch || commitNotExist) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
126
138
|
}
|
|
127
|
-
|
|
139
|
+
throw e;
|
|
128
140
|
}
|
|
129
141
|
}
|
|
130
142
|
exports.getIsCommitInBranch = getIsCommitInBranch;
|
|
131
143
|
async function deleteRemote(options, remoteName) {
|
|
132
144
|
try {
|
|
133
|
-
|
|
145
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
146
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['remote', 'rm', remoteName], cwd);
|
|
134
147
|
}
|
|
135
148
|
catch (e) {
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
149
|
+
const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
|
|
150
|
+
if (isSpawnError &&
|
|
151
|
+
e.context.code > 0 &&
|
|
152
|
+
e.context.stderr.includes('No such remote')) {
|
|
153
|
+
return;
|
|
140
154
|
}
|
|
155
|
+
// re-throw
|
|
156
|
+
throw e;
|
|
141
157
|
}
|
|
142
158
|
}
|
|
143
159
|
exports.deleteRemote = deleteRemote;
|
|
144
160
|
async function addRemote(options, remoteName) {
|
|
145
161
|
try {
|
|
146
|
-
|
|
162
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
163
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['remote', 'add', remoteName, getRemoteUrl(options, remoteName)], cwd);
|
|
147
164
|
}
|
|
148
165
|
catch (e) {
|
|
149
166
|
// note: swallowing error
|
|
@@ -152,107 +169,131 @@ async function addRemote(options, remoteName) {
|
|
|
152
169
|
}
|
|
153
170
|
exports.addRemote = addRemote;
|
|
154
171
|
async function fetchBranch(options, branch) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
});
|
|
172
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
173
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['fetch', options.repoOwner, `${branch}:${branch}`, '--force'], cwd);
|
|
158
174
|
}
|
|
159
175
|
exports.fetchBranch = fetchBranch;
|
|
160
176
|
async function getIsMergeCommit(options, sha) {
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
});
|
|
177
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
178
|
+
const res = await (0, child_process_promisified_1.spawnPromise)('git', ['rev-list', '-1', '--merges', `${sha}~1..${sha}`], cwd);
|
|
164
179
|
return res.stdout !== '';
|
|
165
180
|
}
|
|
166
181
|
exports.getIsMergeCommit = getIsMergeCommit;
|
|
167
182
|
async function getCommitsInMergeCommit(options, sha) {
|
|
168
183
|
try {
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
});
|
|
184
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
185
|
+
const res = await (0, child_process_promisified_1.spawnPromise)('git', ['--no-pager', 'log', `${sha}^1..${sha}^2`, '--pretty=format:%H'], cwd);
|
|
172
186
|
return res.stdout.split('\n');
|
|
173
187
|
}
|
|
174
188
|
catch (e) {
|
|
189
|
+
const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
|
|
175
190
|
// swallow error
|
|
176
|
-
if (e.code === 128) {
|
|
191
|
+
if (isSpawnError && e.context.code === 128) {
|
|
177
192
|
return [];
|
|
178
193
|
}
|
|
179
194
|
throw e;
|
|
180
195
|
}
|
|
181
196
|
}
|
|
182
197
|
exports.getCommitsInMergeCommit = getCommitsInMergeCommit;
|
|
183
|
-
async function
|
|
184
|
-
const mainlinArg = options.mainline != undefined ? ` --mainline ${options.mainline}` : '';
|
|
185
|
-
const cherrypickRefArg = options.cherrypickRef === false ? '' : ' -x';
|
|
186
|
-
let shaOrRange = sha;
|
|
198
|
+
async function getShaOrRange(options, sha) {
|
|
187
199
|
if (!options.mainline) {
|
|
188
200
|
try {
|
|
189
201
|
const isMergeCommit = await getIsMergeCommit(options, sha);
|
|
190
202
|
if (isMergeCommit) {
|
|
191
203
|
const shas = await getCommitsInMergeCommit(options, sha);
|
|
192
|
-
|
|
204
|
+
return `${(0, lodash_1.last)(shas)}^..${(0, lodash_1.first)(shas)}`;
|
|
193
205
|
}
|
|
194
206
|
}
|
|
195
207
|
catch (e) {
|
|
196
|
-
|
|
197
|
-
//
|
|
198
|
-
|
|
208
|
+
const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
|
|
209
|
+
// swallow error if it's a 128 exit code
|
|
210
|
+
// exit 128 happens when the cherrypicked commit is empty which is handled below
|
|
211
|
+
// (128 also applies to many other types of errors)
|
|
212
|
+
if (!isSpawnError || e.context.code !== 128) {
|
|
199
213
|
throw e;
|
|
200
214
|
}
|
|
201
215
|
}
|
|
202
216
|
}
|
|
203
|
-
|
|
217
|
+
return sha;
|
|
218
|
+
}
|
|
219
|
+
async function cherrypick({ options, sha, mergedTargetPullRequest, commitAuthor, }) {
|
|
220
|
+
const shaOrRange = await getShaOrRange(options, sha);
|
|
221
|
+
const cmdArgs = [
|
|
222
|
+
'-c',
|
|
223
|
+
`user.name="${commitAuthor.name}"`,
|
|
224
|
+
'-c',
|
|
225
|
+
`user.email="${commitAuthor.email}"`,
|
|
226
|
+
`cherry-pick`,
|
|
227
|
+
...(options.mainline != undefined
|
|
228
|
+
? ['--mainline', `${options.mainline}`]
|
|
229
|
+
: []),
|
|
230
|
+
...(options.cherrypickRef === false ? [] : ['-x']),
|
|
231
|
+
shaOrRange,
|
|
232
|
+
];
|
|
204
233
|
try {
|
|
205
|
-
|
|
234
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
235
|
+
await (0, child_process_promisified_1.spawnPromise)('git', cmdArgs, cwd);
|
|
206
236
|
return { conflictingFiles: [], unstagedFiles: [], needsResolving: false };
|
|
207
237
|
}
|
|
208
238
|
catch (e) {
|
|
209
|
-
|
|
210
|
-
if (
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
239
|
+
const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
|
|
240
|
+
if (isSpawnError) {
|
|
241
|
+
// missing `mainline` option
|
|
242
|
+
if (e.message.includes('is a merge but no -m option was given')) {
|
|
243
|
+
throw new HandledError_1.HandledError('Cherrypick failed because the selected commit was a merge commit. Please try again by specifying the parent with the `mainline` argument:\n\n> backport --mainline\n\nor:\n\n> backport --mainline <parent-number>\n\nOr refer to the git documentation for more information: https://git-scm.com/docs/git-cherry-pick#Documentation/git-cherry-pick.txt---mainlineparent-number');
|
|
244
|
+
}
|
|
245
|
+
// commit was already backported
|
|
246
|
+
if (e.message.includes('The previous cherry-pick is now empty')) {
|
|
247
|
+
const shortSha = (0, commitFormatters_1.getShortSha)(sha);
|
|
248
|
+
throw new HandledError_1.HandledError(`Cherrypick failed because the selected commit (${shortSha}) is empty. ${mergedTargetPullRequest?.url
|
|
249
|
+
? `It looks like the commit was already backported in ${mergedTargetPullRequest.url}`
|
|
250
|
+
: 'Did you already backport this commit? '}`);
|
|
251
|
+
}
|
|
252
|
+
if (e.message.includes(`bad object ${sha}`)) {
|
|
253
|
+
throw new HandledError_1.HandledError(`Cherrypick failed because commit "${sha}" was not found`);
|
|
254
|
+
}
|
|
255
|
+
const isCherryPickError = e.context.cmdArgs.includes('cherry-pick') && e.context.code > 0;
|
|
256
|
+
if (isCherryPickError) {
|
|
257
|
+
const [conflictingFiles, unstagedFiles] = await Promise.all([
|
|
258
|
+
getConflictingFiles(options),
|
|
259
|
+
getUnstagedFiles(options),
|
|
260
|
+
]);
|
|
261
|
+
if (!(0, lodash_1.isEmpty)(conflictingFiles) || !(0, lodash_1.isEmpty)(unstagedFiles))
|
|
262
|
+
return { conflictingFiles, unstagedFiles, needsResolving: true };
|
|
263
|
+
}
|
|
231
264
|
}
|
|
232
|
-
// re-throw error if
|
|
265
|
+
// re-throw error if it didn't match the handled cases above
|
|
233
266
|
throw e;
|
|
234
267
|
}
|
|
235
268
|
}
|
|
236
269
|
exports.cherrypick = cherrypick;
|
|
237
270
|
async function commitChanges(commit, options) {
|
|
238
|
-
const noVerifyFlag = options.noVerify ?
|
|
271
|
+
const noVerifyFlag = options.noVerify ? ['--no-verify'] : [];
|
|
272
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
239
273
|
try {
|
|
240
|
-
await (0, child_process_promisified_1.
|
|
241
|
-
|
|
242
|
-
|
|
274
|
+
await (0, child_process_promisified_1.spawnPromise)('git', [
|
|
275
|
+
'commit',
|
|
276
|
+
'--no-edit',
|
|
277
|
+
...noVerifyFlag, // bypass pre-commit and commit-msg hooks
|
|
278
|
+
], cwd);
|
|
243
279
|
}
|
|
244
280
|
catch (e) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
281
|
+
const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
|
|
282
|
+
if (isSpawnError) {
|
|
283
|
+
if (e.context.stdout.includes('nothing to commit')) {
|
|
284
|
+
logger_1.logger.info(`Could not run "git commit". Probably because the changes were manually committed`, e);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
// manually set the commit message if the inferred commit message is empty
|
|
288
|
+
// this can happen if the user runs `git reset HEAD` and thereby aborts the cherrypick process
|
|
289
|
+
if (e.context.stderr.includes('Aborting commit due to empty commit message')) {
|
|
290
|
+
await (0, child_process_promisified_1.spawnPromise)('git', [
|
|
291
|
+
'commit',
|
|
292
|
+
`--message=${commit.sourceCommit.message}`,
|
|
293
|
+
...noVerifyFlag, // bypass pre-commit and commit-msg hooks
|
|
294
|
+
], cwd);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
256
297
|
}
|
|
257
298
|
// rethrow error if it can't be handled
|
|
258
299
|
throw e;
|
|
@@ -262,13 +303,15 @@ exports.commitChanges = commitChanges;
|
|
|
262
303
|
async function getConflictingFiles(options) {
|
|
263
304
|
const repoPath = (0, env_1.getRepoPath)(options);
|
|
264
305
|
try {
|
|
265
|
-
|
|
306
|
+
const cwd = repoPath;
|
|
307
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['--no-pager', 'diff', '--check'], cwd);
|
|
266
308
|
return [];
|
|
267
309
|
}
|
|
268
310
|
catch (e) {
|
|
269
|
-
const
|
|
311
|
+
const isSpawnError = e instanceof child_process_promisified_1.SpawnError;
|
|
312
|
+
const isConflictError = isSpawnError && e.context.code === 2;
|
|
270
313
|
if (isConflictError) {
|
|
271
|
-
const files = e.stdout
|
|
314
|
+
const files = e.context.stdout
|
|
272
315
|
.split('\n')
|
|
273
316
|
.filter((line) => !!line.trim() && !line.startsWith('+') && !line.startsWith('-'))
|
|
274
317
|
.map((line) => {
|
|
@@ -292,9 +335,8 @@ exports.getConflictingFiles = getConflictingFiles;
|
|
|
292
335
|
// retrieve the list of files that could not be cleanly merged
|
|
293
336
|
async function getUnstagedFiles(options) {
|
|
294
337
|
const repoPath = (0, env_1.getRepoPath)(options);
|
|
295
|
-
const
|
|
296
|
-
|
|
297
|
-
});
|
|
338
|
+
const cwd = repoPath;
|
|
339
|
+
const res = await (0, child_process_promisified_1.spawnPromise)('git', ['--no-pager', 'diff', '--name-only'], cwd);
|
|
298
340
|
const files = res.stdout
|
|
299
341
|
.split('\n')
|
|
300
342
|
.filter((file) => !!file)
|
|
@@ -304,8 +346,9 @@ async function getUnstagedFiles(options) {
|
|
|
304
346
|
exports.getUnstagedFiles = getUnstagedFiles;
|
|
305
347
|
async function getGitConfig({ dir, key, }) {
|
|
306
348
|
try {
|
|
307
|
-
const
|
|
308
|
-
|
|
349
|
+
const cwd = dir;
|
|
350
|
+
const res = await (0, child_process_promisified_1.spawnPromise)('git', ['config', key], cwd);
|
|
351
|
+
return res.stdout.trim();
|
|
309
352
|
}
|
|
310
353
|
catch (e) {
|
|
311
354
|
return;
|
|
@@ -318,16 +361,30 @@ exports.getGitConfig = getGitConfig;
|
|
|
318
361
|
async function createBackportBranch({ options, targetBranch, backportBranch, }) {
|
|
319
362
|
const spinner = (0, ora_1.ora)(options.ci, 'Pulling latest changes').start();
|
|
320
363
|
try {
|
|
321
|
-
const
|
|
364
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
365
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['reset', '--hard'], cwd);
|
|
366
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['clean', '-d', '--force'], cwd);
|
|
367
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['fetch', options.repoOwner, targetBranch], cwd);
|
|
368
|
+
await (0, child_process_promisified_1.spawnPromise)('git', [
|
|
369
|
+
'checkout',
|
|
370
|
+
'-B',
|
|
371
|
+
backportBranch,
|
|
372
|
+
`${options.repoOwner}/${targetBranch}`,
|
|
373
|
+
'--no-track',
|
|
374
|
+
], cwd);
|
|
322
375
|
spinner.succeed();
|
|
323
|
-
return res;
|
|
324
376
|
}
|
|
325
377
|
catch (e) {
|
|
326
378
|
spinner.fail();
|
|
327
|
-
|
|
328
|
-
e.stderr
|
|
329
|
-
|
|
330
|
-
|
|
379
|
+
if (e instanceof child_process_promisified_1.SpawnError) {
|
|
380
|
+
const isBranchInvalid = e.context.stderr.toLowerCase().includes(`couldn't find remote ref`) ||
|
|
381
|
+
e.context.stderr.toLowerCase().includes(`invalid refspec`) ||
|
|
382
|
+
e.context.stderr
|
|
383
|
+
.toLowerCase()
|
|
384
|
+
.includes(`is not a commit and a branch '${backportBranch}' cannot be created from it`);
|
|
385
|
+
if (isBranchInvalid) {
|
|
386
|
+
throw new HandledError_1.HandledError(`The branch "${targetBranch}" is invalid or doesn't exist`);
|
|
387
|
+
}
|
|
331
388
|
}
|
|
332
389
|
throw e;
|
|
333
390
|
}
|
|
@@ -335,7 +392,10 @@ async function createBackportBranch({ options, targetBranch, backportBranch, })
|
|
|
335
392
|
exports.createBackportBranch = createBackportBranch;
|
|
336
393
|
async function deleteBackportBranch({ options, backportBranch, }) {
|
|
337
394
|
const spinner = (0, ora_1.ora)(options.ci).start();
|
|
338
|
-
|
|
395
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
396
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['reset', '--hard'], cwd);
|
|
397
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['checkout', options.sourceBranch], cwd);
|
|
398
|
+
await (0, child_process_promisified_1.spawnPromise)('git', ['branch', '--delete', '--force', backportBranch], cwd);
|
|
339
399
|
spinner.stop();
|
|
340
400
|
}
|
|
341
401
|
exports.deleteBackportBranch = deleteBackportBranch;
|
|
@@ -350,24 +410,30 @@ async function pushBackportBranch({ options, backportBranch, }) {
|
|
|
350
410
|
const repoForkOwner = getRepoForkOwner(options);
|
|
351
411
|
const spinner = (0, ora_1.ora)(options.ci, `Pushing branch "${repoForkOwner}:${backportBranch}"`).start();
|
|
352
412
|
try {
|
|
353
|
-
const
|
|
413
|
+
const cwd = (0, env_1.getRepoPath)(options);
|
|
414
|
+
const res = await (0, child_process_promisified_1.spawnPromise)('git', ['push', repoForkOwner, `${backportBranch}:${backportBranch}`, '--force'], cwd);
|
|
354
415
|
spinner.succeed();
|
|
355
416
|
return res;
|
|
356
417
|
}
|
|
357
418
|
catch (e) {
|
|
358
419
|
spinner.fail();
|
|
359
|
-
if (e
|
|
360
|
-
|
|
420
|
+
if (e instanceof child_process_promisified_1.SpawnError &&
|
|
421
|
+
e.context.stderr.toLowerCase().includes(`repository not found`)) {
|
|
422
|
+
throw new HandledError_1.HandledError(`Error pushing to https://github.com/${repoForkOwner}/${options.repoName}. Repository does not exist. Either fork the repository (https://github.com/${options.repoOwner}/${options.repoName}) or disable fork mode via "--no-fork".\nRead more about fork mode in the docs: https://github.com/sqren/backport/blob/main/docs/configuration.md#fork`);
|
|
423
|
+
}
|
|
424
|
+
if (e instanceof child_process_promisified_1.SpawnError &&
|
|
425
|
+
e.context.stderr.includes(`could not read Username for`)) {
|
|
426
|
+
throw new HandledError_1.HandledError(`Invalid credentials: ${e.message}`);
|
|
361
427
|
}
|
|
362
428
|
throw e;
|
|
363
429
|
}
|
|
364
430
|
}
|
|
365
431
|
exports.pushBackportBranch = pushBackportBranch;
|
|
366
432
|
// retrieve path to local repo (cwd) if it matches `repoName` / `repoOwner`
|
|
367
|
-
async function
|
|
433
|
+
async function getLocalSourceRepoPath(options) {
|
|
368
434
|
const remotes = await getRepoInfoFromGitRemotes({ cwd: options.cwd });
|
|
369
435
|
const hasMatchingGitRemote = remotes.some((remote) => remote.repoName === options.repoName &&
|
|
370
436
|
remote.repoOwner === options.repoOwner);
|
|
371
437
|
return hasMatchingGitRemote ? getGitProjectRootPath(options.cwd) : undefined;
|
|
372
438
|
}
|
|
373
|
-
exports.
|
|
439
|
+
exports.getLocalSourceRepoPath = getLocalSourceRepoPath;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|