Skip to content

Commit d05b99c

Browse files
committed
feat: run raw git commands
1 parent 4c102fb commit d05b99c

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/commands.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { openHistoryInGitHub, openLineInGitHub } from "./openInGitHub";
55
import { ChangedFilesModal } from "./ui/modals/changedFilesModal";
66
import { GeneralModal } from "./ui/modals/generalModal";
77
import { IgnoreModal } from "./ui/modals/ignoreModal";
8+
import { SimpleGit } from "./gitManager/simpleGit";
89

910
export function addCommmands(plugin: ObsidianGit) {
1011
const app = plugin.app;
@@ -392,6 +393,22 @@ export function addCommmands(plugin: ObsidianGit) {
392393
},
393394
});
394395

396+
plugin.addCommand({
397+
id: "raw-command",
398+
name: "Raw command",
399+
checkCallback: (checking) => {
400+
const gitManager = plugin.gitManager;
401+
if (checking) {
402+
// only available on desktop
403+
return gitManager instanceof SimpleGit;
404+
} else {
405+
plugin.tools
406+
.runRawCommand()
407+
.catch((e) => plugin.displayError(e));
408+
}
409+
},
410+
});
411+
395412
plugin.addCommand({
396413
id: "toggle-line-author-info",
397414
name: "Toggle line author information",

src/gitManager/simpleGit.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,12 @@ export class SimpleGit extends GitManager {
911911
return await this.git.diff([`${commit1}..${commit2}`, "--", file]);
912912
}
913913

914+
async rawCommand(command: string): Promise<string> {
915+
const parts = command.split(" "); // Very simple parsing, may need string-argv
916+
const res = await this.git.raw(parts[0], ...parts.slice(1));
917+
return res;
918+
}
919+
914920
async getSubmoduleOfFile(
915921
repositoryRelativeFile: string
916922
): Promise<{ submodule: string; relativeFilepath: string } | undefined> {

src/tools.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Platform, TFile } from "obsidian";
1+
import { Notice, Platform, TFile } from "obsidian";
22
import {
33
CONFLICT_OUTPUT_FILE,
44
DIFF_VIEW_CONFIG,
55
SPLIT_DIFF_VIEW_CONFIG,
66
} from "./constants";
77
import type ObsidianGit from "./main";
88
import { getNewLeaf, splitRemoteBranch } from "./utils";
9+
import { SimpleGit } from "./gitManager/simpleGit";
10+
import { GeneralModal } from "./ui/modals/generalModal";
911

1012
export default class Tools {
1113
constructor(private readonly plugin: ObsidianGit) {}
@@ -107,4 +109,34 @@ export default class Tools {
107109
});
108110
}
109111
}
112+
113+
async runRawCommand() {
114+
const gitManager = this.plugin.gitManager;
115+
if (!(gitManager instanceof SimpleGit)) {
116+
return;
117+
}
118+
const modal = new GeneralModal(this.plugin, {
119+
placeholder: "push origin master",
120+
allowEmpty: false,
121+
});
122+
const command = await modal.openAndGetResult();
123+
if (command === undefined) return;
124+
125+
this.plugin.promiseQueue.addTask(async () => {
126+
const notice = new Notice(`Running '${command}'...`, 999_999);
127+
128+
try {
129+
const res = await gitManager.rawCommand(command);
130+
if (res) {
131+
notice.setMessage(res);
132+
window.setTimeout(() => notice.hide(), 5000);
133+
} else {
134+
notice.hide();
135+
}
136+
} catch (e) {
137+
notice.hide();
138+
throw e;
139+
}
140+
});
141+
}
110142
}

0 commit comments

Comments
 (0)