From d1a92ae0e18eaa4abe6f30ba86ea6c45d711e513 Mon Sep 17 00:00:00 2001 From: Erasmus Cedernaes Date: Tue, 1 Dec 2026 14:53:21 +0100 Subject: [PATCH] Add shortcut for [un]committing patches --- CHANGELOG.md | 4 +++ package.json | 22 ++++++++++++++- src/stgit.ts | 10 +++++++ src/test/suite/extension.test.ts | 48 ++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fd102e..cec827c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Added keyboard shortcuts for committing and uncommitting patches (i.e. `stg commit` and `stg uncommit`) using the `ctrl+shift+.` and `ctrl+shift+,` keybindings. + ## [2.0.0] - 2026-12-01 ### Changed diff --git a/package.json b/package.json index bb7a16f..9e40375 100644 --- a/package.json +++ b/package.json @@ -328,6 +328,16 @@ "category": "StGit", "title": "Convert Patches to GIT Commits (or vice versa)" }, + { + "command": "stgit.commitPatch", + "category": "StGit", + "title": "Commit Bottom Patch" + }, + { + "command": "stgit.uncommitPatch", + "category": "StGit", + "title": "Uncommit Git Commit" + }, { "command": "stgit.toggleChanges", "category": "StGit", @@ -531,7 +541,7 @@ "when": "activeWebviewPanelId == stgit && !stgitEditing && !inQuickOpen && !terminalFocus && !inputFocus" }, { - "key": "ctrl+c u", + "key": "ctrl+c shift+u", "command": "stgit.selectUpstreamBranch", "when": "activeWebviewPanelId == stgit && !stgitEditing && !inQuickOpen && !terminalFocus && !inputFocus" }, @@ -570,6 +580,16 @@ "command": "stgit.commitOrUncommitPatches", "when": "activeWebviewPanelId == stgit && !stgitEditing && !inQuickOpen && !terminalFocus && !inputFocus" }, + { + "key": "ctrl+shift+.", + "command": "stgit.commitPatch", + "when": "activeWebviewPanelId == stgit && !stgitEditing && !inQuickOpen && !terminalFocus && !inputFocus" + }, + { + "key": "ctrl+shift+,", + "command": "stgit.uncommitPatch", + "when": "activeWebviewPanelId == stgit && !stgitEditing && !inQuickOpen && !terminalFocus && !inputFocus" + }, { "key": "h", "command": "stgit.help", diff --git a/src/stgit.ts b/src/stgit.ts index bb2e481..9a39cb5 100644 --- a/src/stgit.ts +++ b/src/stgit.ts @@ -1499,6 +1499,14 @@ class StGitDoc { } this.reload(); } + async commitPatch() { + await runAndReportErrors("stg", ["commit"]); + this.reload(); + } + async uncommitPatch() { + await runAndReportErrors("stg", ["uncommit", "-n1"]); + this.reload(); + } toggleShowingUnknownFiles() { this.unknownFilesVisible = !this.unknownFilesVisible; this.reloadWorkTree(); @@ -1625,6 +1633,8 @@ class StGitMode { cmd("openDiff", () => this.stgit?.openDiff()), cmd("setHistorySize", () => this.stgit?.setHistorySize()), cmd("commitOrUncommitPatches", () => this.stgit?.commitOrUncommitPatches()), + cmd("commitPatch", () => this.stgit?.commitPatch()), + cmd("uncommitPatch", () => this.stgit?.uncommitPatch()), cmd("revertChanges", () => this.stgit?.revertChanges()), cmd("undo", () => this.stgit?.undo()), cmd("hardUndo", () => this.stgit?.hardUndo()), diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index 0da71c9..41b4c37 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -1,4 +1,7 @@ import * as assert from "assert"; +import { execFileSync } from "child_process"; +import * as fs from "fs/promises"; +import * as path from "path"; // You can import and use all API from the 'vscode' module // as well as import your extension to test it @@ -6,6 +9,7 @@ import * as vscode from "vscode"; import { CommentDraft } from "../../comment-discard"; import { findHunkTargetLine, HunkTarget } from "../../diff-mode"; import { RepoReader } from "../../repo-reader"; +import { RepositoryInfo } from "../../repo"; import { correspondingLine } from "../../stage-selection"; import { formatCommitDescription, History, StGitPatch, WorkTree } from "../../stgit"; @@ -331,6 +335,50 @@ suite("Extension Test Suite", () => { assert.ok(registered.includes("stgit.submitComment")); }); + test("Commits and uncommits one patch independently of the selected row", async function () { + this.timeout(30000); + const temporaryRoot = path.resolve(__dirname, "../../../.tmp"); + await fs.mkdir(temporaryRoot, { recursive: true }); + await fs.writeFile(path.join(temporaryRoot, ".gitignore"), "*\n", { flag: "wx" }).catch((error: NodeJS.ErrnoException) => { + if (error.code !== "EEXIST") { + throw error; + } + }); + const dir = await fs.mkdtemp(path.join(temporaryRoot, "conversion-")); + const previousRepo = await RepositoryInfo.getSelectedRepo(); + const execute = (command: string, ...args: string[]) => execFileSync(command, args, { cwd: dir, encoding: "utf8" }).trim(); + try { + execute("git", "init", "-q"); + execute("git", "config", "user.name", "Test"); + execute("git", "config", "user.email", "test@example.org"); + const file = path.join(dir, "file.txt"); + await fs.writeFile(file, "base\n"); + execute("git", "add", "file.txt"); + execute("git", "commit", "-qm", "base"); + execute("stg", "init"); + for (const name of ["first", "second"]) { + execute("stg", "new", "-m", name, name); + await fs.appendFile(file, `${name}\n`); + execute("stg", "refresh"); + } + const head = execute("git", "rev-parse", "HEAD"); + await vscode.extensions.getExtension("cedernaes.stgit-revived")!.activate(); + await vscode.window.showTextDocument(await vscode.workspace.openTextDocument(file)); + await vscode.commands.executeCommand("stgit.open"); + await vscode.commands.executeCommand("stgit.commitPatch"); + assert.strictEqual(execute("stg", "series", "--applied", "--noprefix"), "second"); + assert.strictEqual(execute("git", "rev-parse", "HEAD"), head); + await vscode.commands.executeCommand("stgit.uncommitPatch"); + assert.strictEqual(execute("stg", "series", "--applied", "--noprefix"), "first\nsecond"); + assert.strictEqual(execute("git", "rev-parse", "HEAD"), head); + } finally { + await vscode.commands.executeCommand("workbench.action.closeActiveEditor"); + await vscode.commands.executeCommand("workbench.action.closeActiveEditor"); + RepositoryInfo.setSelectedRepo(previousRepo); + await fs.rm(dir, { recursive: true, force: true }); + } + }); + test("Keeps the cursor on its file when a redraw changes line counts", () => { const before = "Branch\nIndex\n Modified file-a\nWork Tree"; const after = "Branch\nIndex\n Modified file-b\n" + " Modified file-a\nWork Tree";