Skip to content

Commit e15cdc0

Browse files
committed
feat: ctrl + enter to commit-and-sync
In addition, when some files are already staged pressing the commit-and-sync button or new via ctrl + enter will only commit the staged files and not all changes. #901
1 parent c258fe2 commit e15cdc0

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

src/automaticsManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export default class AutomaticsManager {
166166
if (this.plugin.settings.differentIntervalCommitAndPush) {
167167
await this.plugin.commit({ fromAuto: true });
168168
} else {
169-
await this.plugin.commitAndSync(true);
169+
await this.plugin.commitAndSync({ fromAutoBackup: true });
170170
}
171171
return true;
172172
},

src/commands.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,17 @@ export function addCommmands(plugin: ObsidianGit) {
153153
id: "push",
154154
name: "Commit-and-sync",
155155
callback: () =>
156-
plugin.promiseQueue.addTask(() => plugin.commitAndSync(false)),
156+
plugin.promiseQueue.addTask(() =>
157+
plugin.commitAndSync({ fromAutoBackup: false })
158+
),
157159
});
158160

159161
plugin.addCommand({
160162
id: "backup-and-close",
161163
name: "Commit-and-sync and then close Obsidian",
162164
callback: () =>
163165
plugin.promiseQueue.addTask(async () => {
164-
await plugin.commitAndSync(false);
166+
await plugin.commitAndSync({ fromAutoBackup: false });
165167
window.close();
166168
}),
167169
});
@@ -171,7 +173,10 @@ export function addCommmands(plugin: ObsidianGit) {
171173
name: "Commit-and-sync with specific message",
172174
callback: () =>
173175
plugin.promiseQueue.addTask(() =>
174-
plugin.commitAndSync(false, true)
176+
plugin.commitAndSync({
177+
fromAutoBackup: false,
178+
requestCustomMessage: true,
179+
})
175180
),
176181
});
177182

src/main.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,17 @@ export default class ObsidianGit extends Plugin {
751751
this.setPluginState({ gitAction: CurrentGitAction.idle });
752752
}
753753

754-
async commitAndSync(
755-
fromAutoBackup: boolean,
754+
async commitAndSync({
755+
fromAutoBackup,
756756
requestCustomMessage = false,
757-
commitMessage?: string
758-
): Promise<void> {
757+
commitMessage,
758+
onlyStaged = false,
759+
}: {
760+
fromAutoBackup: boolean;
761+
requestCustomMessage?: boolean;
762+
commitMessage?: string;
763+
onlyStaged?: boolean;
764+
}): Promise<void> {
759765
if (!(await this.isAllInitialized())) return;
760766

761767
if (
@@ -769,6 +775,7 @@ export default class ObsidianGit extends Plugin {
769775
fromAuto: fromAutoBackup,
770776
requestCustomMessage,
771777
commitMessage,
778+
onlyStaged,
772779
});
773780
if (!commitSuccessful) {
774781
return;

src/ui/sourceControl/sourceControl.svelte

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { Platform, setIcon } from "obsidian";
2+
import { Platform, Scope, setIcon } from "obsidian";
33
import { SOURCE_CONTROL_VIEW_CONFIG } from "src/constants";
44
import type ObsidianGit from "src/main";
55
import type {
@@ -81,6 +81,11 @@
8181
});
8282
});
8383
84+
view.scope = new Scope(plugin.app.scope);
85+
view.scope.register(["Ctrl"], "Enter", (_: KeyboardEvent) =>
86+
commitAndSync()
87+
);
88+
8489
async function commit() {
8590
loading = true;
8691
if (status) {
@@ -100,9 +105,16 @@
100105
function commitAndSync() {
101106
loading = true;
102107
if (status) {
108+
// If staged files exist only commit them, but if not, commit all.
109+
// I hope this is the most intuitive way.
110+
const onlyStaged = status.staged.length > 0;
103111
plugin.promiseQueue.addTask(() =>
104112
plugin
105-
.commitAndSync(false, false, commitMessage)
113+
.commitAndSync({
114+
fromAutoBackup: false,
115+
commitMessage,
116+
onlyStaged,
117+
})
106118
.then(() => {
107119
commitMessage = plugin.settings.commitMessage;
108120
})

0 commit comments

Comments
 (0)