Skip to content

Commit

Permalink
feat: run raw git commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Dec 31, 2024
1 parent 4c102fb commit d05b99c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { openHistoryInGitHub, openLineInGitHub } from "./openInGitHub";
import { ChangedFilesModal } from "./ui/modals/changedFilesModal";
import { GeneralModal } from "./ui/modals/generalModal";
import { IgnoreModal } from "./ui/modals/ignoreModal";
import { SimpleGit } from "./gitManager/simpleGit";

export function addCommmands(plugin: ObsidianGit) {
const app = plugin.app;
Expand Down Expand Up @@ -392,6 +393,22 @@ export function addCommmands(plugin: ObsidianGit) {
},
});

plugin.addCommand({
id: "raw-command",
name: "Raw command",
checkCallback: (checking) => {
const gitManager = plugin.gitManager;
if (checking) {
// only available on desktop
return gitManager instanceof SimpleGit;
} else {
plugin.tools
.runRawCommand()
.catch((e) => plugin.displayError(e));
}
},
});

plugin.addCommand({
id: "toggle-line-author-info",
name: "Toggle line author information",
Expand Down
6 changes: 6 additions & 0 deletions src/gitManager/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,12 @@ export class SimpleGit extends GitManager {
return await this.git.diff([`${commit1}..${commit2}`, "--", file]);
}

async rawCommand(command: string): Promise<string> {
const parts = command.split(" "); // Very simple parsing, may need string-argv
const res = await this.git.raw(parts[0], ...parts.slice(1));
return res;
}

async getSubmoduleOfFile(
repositoryRelativeFile: string
): Promise<{ submodule: string; relativeFilepath: string } | undefined> {
Expand Down
34 changes: 33 additions & 1 deletion src/tools.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Platform, TFile } from "obsidian";
import { Notice, Platform, TFile } from "obsidian";
import {
CONFLICT_OUTPUT_FILE,
DIFF_VIEW_CONFIG,
SPLIT_DIFF_VIEW_CONFIG,
} from "./constants";
import type ObsidianGit from "./main";
import { getNewLeaf, splitRemoteBranch } from "./utils";
import { SimpleGit } from "./gitManager/simpleGit";
import { GeneralModal } from "./ui/modals/generalModal";

export default class Tools {
constructor(private readonly plugin: ObsidianGit) {}
Expand Down Expand Up @@ -107,4 +109,34 @@ export default class Tools {
});
}
}

async runRawCommand() {
const gitManager = this.plugin.gitManager;
if (!(gitManager instanceof SimpleGit)) {
return;
}
const modal = new GeneralModal(this.plugin, {
placeholder: "push origin master",
allowEmpty: false,
});
const command = await modal.openAndGetResult();
if (command === undefined) return;

this.plugin.promiseQueue.addTask(async () => {
const notice = new Notice(`Running '${command}'...`, 999_999);

try {
const res = await gitManager.rawCommand(command);
if (res) {
notice.setMessage(res);
window.setTimeout(() => notice.hide(), 5000);
} else {
notice.hide();
}
} catch (e) {
notice.hide();
throw e;
}
});
}
}

0 comments on commit d05b99c

Please sign in to comment.