Skip to content

Commit 529e83f

Browse files
authored
fix(version): enable changing commit message when using amend (lerna#3954)
1 parent 960bdd9 commit 529e83f

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

libs/commands/version/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class VersionCommand extends Command {
117117
signoffGitCommit?: boolean;
118118
signGitTag?: boolean;
119119
forceGitTag?: boolean;
120+
overrideMessage?: boolean;
120121
};
121122
savePrefix?: string;
122123
currentBranch?: string;
@@ -175,12 +176,14 @@ class VersionCommand extends Command {
175176
forceGitTag,
176177
tagVersionPrefix = "v",
177178
premajorVersionBump = "default",
179+
message,
178180
} = this.options;
179181

180182
this.gitRemote = gitRemote;
181183
this.tagPrefix = tagVersionPrefix;
182184
this.commitAndTag = gitTagVersion;
183185
this.pushToRemote = gitTagVersion && amend !== true && push;
186+
const overrideMessage: boolean = amend && !!message;
184187
this.premajorVersionBump = premajorVersionBump;
185188
// never automatically push to remote when amending a commit
186189

@@ -204,6 +207,7 @@ class VersionCommand extends Command {
204207
signoffGitCommit,
205208
signGitTag,
206209
forceGitTag,
210+
overrideMessage,
207211
};
208212

209213
// https://docs.npmjs.com/misc/config#save-prefix

libs/commands/version/src/lib/git-commit.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ const childProcess = require("@lerna/child-process");
77

88
export interface GitCommitOptions {
99
amend?: boolean;
10+
overrideMessage?: boolean;
1011
commitHooks?: boolean;
1112
signGitCommit?: boolean;
1213
signoffGitCommit?: boolean;
1314
}
1415

1516
export function gitCommit(
1617
message: string,
17-
{ amend, commitHooks, signGitCommit, signoffGitCommit }: GitCommitOptions,
18+
{ amend, commitHooks, signGitCommit, signoffGitCommit, overrideMessage }: GitCommitOptions,
1819
opts: ExecOptions
1920
) {
2021
log.silly("gitCommit", message);
@@ -32,13 +33,20 @@ export function gitCommit(
3233
args.push("--signoff");
3334
}
3435

36+
const shouldChangeMessage = amend ? amend && overrideMessage : true;
3537
if (amend) {
36-
args.push("--amend", "--no-edit");
37-
} else if (message.indexOf(EOL) > -1) {
38-
// Use tempfile to allow multi\nline strings.
39-
args.push("-F", tempWrite.sync(message, "lerna-commit.txt"));
38+
args.push("--amend");
39+
}
40+
41+
if (shouldChangeMessage) {
42+
if (message.indexOf(EOL) > -1) {
43+
// Use tempfile to allow multi\nline strings.
44+
args.push("-F", tempWrite.sync(message, "lerna-commit.txt"));
45+
} else {
46+
args.push("-m", message);
47+
}
4048
} else {
41-
args.push("-m", message);
49+
args.push("--no-edit");
4250
}
4351

4452
// TODO: refactor to address type issues

libs/commands/version/src/lib/version-command.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,13 @@ Changes:
636636
expect(checkWorkingTree).not.toHaveBeenCalled();
637637
});
638638

639-
it("ignores custom messages", async () => {
639+
it("considers custom messages", async () => {
640640
const testDir = await initFixture("normal", "preserved");
641-
await lernaVersion(testDir)("-m", "ignored", "--amend");
641+
await lernaVersion(testDir)("-m", "custom", "--amend");
642642

643643
const message = await getCommitMessage(testDir);
644-
expect(message).toBe("preserved");
644+
645+
expect(message).toBe("custom");
645646
});
646647
});
647648

0 commit comments

Comments
 (0)