Skip to content

Commit 667db0d

Browse files
committed
fix: edit message for non-HEAD commits
1 parent eb783a0 commit 667db0d

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@
238238
},
239239
"undo": {
240240
"type": "boolean",
241-
"title": "Reset Last Commit (Soft)"
241+
"title": "Reset Last Commit"
242242
},
243243
"drop": {
244244
"type": "boolean",

src/dataSource.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,13 +1272,54 @@ export class DataSource extends Disposable {
12721272
}
12731273
return this.runGitCommand(args, repo);
12741274
} else {
1275-
return 'Editing commit messages for non-HEAD commits is not yet supported.';
1275+
return this.rebaseEditCommitMessage(repo, commitHash, message);
12761276
}
12771277
} catch (error) {
12781278
return error as ErrorInfo;
12791279
}
12801280
}
12811281

1282+
/**
1283+
* Edit a commit message for non-HEAD commits using interactive rebase.
1284+
* @param repo The path of the repository.
1285+
* @param commitHash The commit hash to edit.
1286+
* @param message The new commit message.
1287+
* @returns The ErrorInfo from the executed command.
1288+
*/
1289+
private async rebaseEditCommitMessage(repo: string, commitHash: string, message: string): Promise<ErrorInfo> {
1290+
const parentCommit = await this.spawnGit(['rev-parse', commitHash + '^'], repo, (stdout) => stdout.trim());
1291+
1292+
return new Promise<ErrorInfo>((resolve) => {
1293+
if (this.gitExecutable === null) {
1294+
return resolve(UNABLE_TO_FIND_GIT_MSG);
1295+
}
1296+
1297+
const args = ['rebase', '-i', parentCommit];
1298+
if (getConfig().signCommits) {
1299+
args.push('-S');
1300+
}
1301+
1302+
// Escape the message for shell execution
1303+
const escapedMessage = message
1304+
.replace(/\\/g, '\\\\')
1305+
.replace(/'/g, '\'"\'"\'');
1306+
1307+
// The GIT_EDITOR needs to be a command that accepts the filename as an argument
1308+
// We use a simple echo command that will write only our message to the file
1309+
const env = Object.assign({}, process.env, this.askpassEnv, {
1310+
GIT_SEQUENCE_EDITOR: `sed -i.bak "s/^pick ${commitHash.substring(0, 7)}/reword ${commitHash.substring(0, 7)}/" "$1"`,
1311+
GIT_EDITOR: `sh -c 'echo '"'"'${escapedMessage}'"'"' > "$@"' -- `
1312+
});
1313+
1314+
resolveSpawnOutput(cp.spawn(this.gitExecutable.path, args, { cwd: repo, env }))
1315+
.then(([status, stdout, stderr]) => {
1316+
resolve(status.code !== 0 ? getErrorMessage(status.error, stdout, stderr) : null);
1317+
})
1318+
.catch((errorMessage) => {
1319+
resolve(errorMessage);
1320+
});
1321+
});
1322+
}
12821323

12831324
/* Git Action Methods - Config */
12841325

@@ -1674,7 +1715,7 @@ export class DataSource extends Disposable {
16741715
// Show All
16751716
args.push('--branches');
16761717
if (includeTags) args.push('--tags');
1677-
else if(simplifyByDecoration) args.push('--decorate-refs-exclude=refs/tags/');
1718+
else if (simplifyByDecoration) args.push('--decorate-refs-exclude=refs/tags/');
16781719
if (includeCommitsMentionedByReflogs) args.push('--reflog');
16791720
if (includeRemotes) {
16801721
if (hideRemotes.length === 0) {

web/main.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,17 +1463,17 @@ class GitGraphView {
14631463
}
14641464
}
14651465
}, {
1466-
title: 'Reset Last Commit (Soft)' + ELLIPSIS,
1466+
title: 'Edit Message' + ELLIPSIS,
1467+
visible: visibility.editMessage,
1468+
onClick: () => this.editCommitMessageAction(target)
1469+
}, {
1470+
title: 'Reset Last Commit' + ELLIPSIS,
14671471
visible: visibility.undo && hash === this.commitHead,
14681472
onClick: () => {
14691473
dialog.showConfirmation('Are you sure you want to reset the last commit? This will keep all changes from the commit as uncommitted changes.', 'Yes, reset the last commit', () => {
14701474
runAction({ command: 'undoLastCommit', repo: this.currentRepo }, 'Resetting Last Commit');
14711475
}, target);
14721476
}
1473-
}, {
1474-
title: 'Edit Message' + ELLIPSIS,
1475-
visible: visibility.editMessage,
1476-
onClick: () => this.editCommitMessageAction(target)
14771477
}, {
14781478
title: 'Drop' + ELLIPSIS,
14791479
visible: visibility.drop && this.graph.dropCommitPossible(this.commitLookup[hash]),

0 commit comments

Comments
 (0)