Skip to content

Commit

Permalink
feat: stage and unstage to context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Aug 16, 2022
1 parent 76e317b commit 081ad1d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export abstract class GitManager {

abstract commit(message?: string): Promise<number | undefined>;

abstract stageAll(status?: Status): Promise<void>;
abstract stageAll({ }: { dir?: string, status?: Status; }): Promise<void>;

abstract unstageAll(): Promise<void>;
abstract unstageAll({ }: { dir?: string, status?: Status; }): Promise<void>;

abstract stage(filepath: string, relativeToVault: boolean): Promise<void>;

Expand Down
18 changes: 11 additions & 7 deletions src/isomorphicGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class IsomorphicGit extends GitManager {

async commitAll(message?: string, status?: Status): Promise<number> {
try {
await this.stageAll(status);
await this.stageAll({ status: status });
return this.commit(message);
} catch (error) {
this.plugin.displayError(error);
Expand Down Expand Up @@ -164,15 +164,15 @@ export class IsomorphicGit extends GitManager {
}
}

async stageAll(status?: Status): Promise<void> {
async stageAll({ dir, status }: { dir?: string, status?: Status; }): Promise<void> {
try {
if (status) {
await Promise.all(
status.changed.map(file =>
(file.working_dir !== "D") ? this.wrapFS(git.add({ ...this.getRepo(), filepath: file.path })) : git.remove(({ ...this.getRepo(), filepath: file.path }))
));
} else {
const newStatus = await this.wrapFS(git.statusMatrix(this.getRepo()));
const newStatus = await this.wrapFS(git.statusMatrix({ ...this.getRepo(), filepaths: [dir ?? "."] }));
await Promise.all(
newStatus.map(([filepath, , worktreeStatus]) =>
worktreeStatus ? this.wrapFS(git.add({ ...this.getRepo(), filepath })) : git.remove(({ ...this.getRepo(), filepath }))
Expand All @@ -195,12 +195,16 @@ export class IsomorphicGit extends GitManager {
}
}

async unstageAll(): Promise<void> {
async unstageAll({ dir, status }: { dir?: string, status?: Status; }): Promise<void> {
try {
const changed = this.plugin.cachedStatus.staged;
for (const file of changed) {
await this.unstage(file.path, false);
let staged: string[];
if (status) {
staged = status.staged.map(file => file.path);
} else {
const newStatus = await this.wrapFS(git.statusMatrix({ ...this.getRepo(), filepaths: [dir ?? "."] }));
staged = newStatus.map(([filepath]) => filepath);
}
await Promise.all(staged.map(file => this.unstage(file, false)));
} catch (error) {
this.plugin.displayError(error);
throw error;
Expand Down
49 changes: 48 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debounce, Debouncer, EventRef, Notice, Plugin, TFile } from "obsidian";
import { debounce, Debouncer, EventRef, Menu, Notice, Plugin, TAbstractFile, TFile } from "obsidian";
import { PromiseQueue } from "src/promiseQueue";
import { ObsidianGitSettingsTab } from "src/settings";
import { StatusBar } from "src/statusBar";
Expand Down Expand Up @@ -290,6 +290,11 @@ export default class ObsidianGit extends Plugin {
}
});

this.registerEvent(
this.app.workspace.on('file-menu', (menu, file, source) => {
this.handleFileMenu(menu, file, source);
}));


if (this.settings.showStatusBar) {
// init statusBar
Expand All @@ -303,6 +308,48 @@ export default class ObsidianGit extends Plugin {

}

handleFileMenu(menu: Menu, file: TAbstractFile, source: string): void {
if (source !== "file-explorer-context-menu") {
return;
}
if (!file) {
return;
}
if (!this.gitReady) return;
menu.addItem((item) => {
item
.setTitle(`Git: Stage`)
.setIcon('plus-circle')
.setSection("action")
.onClick((_) => {
this.promiseQueue.addTask(async () => {
if (file instanceof TFile) {
await this.gitManager.stage(file.path, true);
} else {
await this.gitManager.stageAll({ dir: this.gitManager.getPath(file.path, true) });
}
this.displayMessage(`Staged ${file.path}`);
});
});
});
menu.addItem((item) => {
item
.setTitle(`Git: Unstage`)
.setIcon('minus-circle')
.setSection("action")
.onClick((_) => {
this.promiseQueue.addTask(async () => {
if (file instanceof TFile) {
await this.gitManager.unstage(file.path, true);
} else {
await this.gitManager.unstageAll({ dir: this.gitManager.getPath(file.path, true) });
}
this.displayMessage(`Unstaged ${file.path}`);
});
});
});
}

migrateSettings(): Promise<void> {
if (this.settings.mergeOnPull != undefined) {
this.settings.syncMethod = this.settings.mergeOnPull ? 'merge' : 'rebase';
Expand Down
4 changes: 2 additions & 2 deletions src/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ export class SimpleGit extends GitManager {
this.plugin.setState(PluginState.idle);
}

async stageAll(): Promise<void> {
async stageAll({ dir }: { dir?: string; }): Promise<void> {
this.plugin.setState(PluginState.add);
await this.git.add(
"-A", (err) => this.onError(err)
dir ?? "-A", (err) => this.onError(err)
);
this.plugin.setState(PluginState.idle);
}
Expand Down
6 changes: 4 additions & 2 deletions src/ui/sidebar/gitView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@
function stageAll() {
loading = true;
plugin.gitManager.stageAll(status).finally(triggerRefresh);
plugin.gitManager.stageAll({status: status}).finally(triggerRefresh);
}
function unstageAll() {
loading = true;
plugin.gitManager.unstageAll().finally(triggerRefresh);
plugin.gitManager.unstageAll({status:status}).finally(triggerRefresh);
}
function push() {
loading = true;
Expand Down

0 comments on commit 081ad1d

Please sign in to comment.