Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventRef, Menu, Notice, Platform, Plugin, TAbstractFile, TFile, FileSystemAdapter } from "obsidian";
import { EventRef, Menu, Notice, Platform, Plugin, TAbstractFile, TFile, TFolder, FileSystemAdapter } from "obsidian";
import { AutoGitSettings, AutoGitSettingTab, DEFAULT_SETTINGS } from "./settings";
import { getChangedFiles, commitAll, push, pull, getConflictFiles, markConflictsResolved, revertAll, revertFile, getChangedFilesSync, commitSyncAndPushDetached, setGitDebug } from "./git";
import { renderTemplate } from "./template";
Expand Down Expand Up @@ -373,21 +373,37 @@ export default class AutoGitPlugin extends Plugin {

this.registerEvent(
this.app.workspace.on("file-menu", (menu, file) => {
if (!(file instanceof TFile)) return;
let filePaths = [];
if (file instanceof TFolder) {
const status = this.statusBadges?.getFolderStatus(file.path);
if (!status) return;
// We fetch all changes because if we only walked the current children we would miss deleted files...
const changedFiles = getChangedFilesSync(this.getVaultPath(), this.settings.gitPath);
const prefix = file.path + "/";
for (const filePath of changedFiles) {
if (filePath.startsWith(prefix)) {
filePaths.push(filePath);
}
}
} else {
const status = this.statusBadges?.getStatus(file.path);
if (!status) return;
filePaths.push(file.path);
}

const filePath = file.path;
const status = this.statusBadges?.getStatus(filePath);
if (!status) return;
if (filePaths.length === 0) return;

menu.addItem((item) => {
item.setTitle(t().revertFileMenu)
.setIcon("rotate-ccw")
.onClick(() => {
new RevertConfirmModal(this.app, [filePath], () => {
new RevertConfirmModal(this.app, filePaths, () => {
void (async () => {
try {
const cwd = this.getVaultPath();
await revertFile(cwd, this.settings.gitPath, filePath);
for (const filePath of filePaths) {
await revertFile(cwd, this.settings.gitPath, filePath);
}
new Notice(t().noticeFileReverted);
void this.statusBadges?.refresh();
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class RevertConfirmModal extends Modal {
const i18n = t();
const { contentEl } = this;

contentEl.createEl("h2", { text: i18n.revertConfirmTitle });
this.setTitle(i18n.revertConfirmTitle);
contentEl.createEl("p", { text: i18n.revertConfirmDesc });

const listEl = contentEl.createEl("ul", { cls: "revert-file-list" });
Expand Down
6 changes: 6 additions & 0 deletions src/statusBadges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ export class GitStatusBadgeManager {
return this.fileStatuses.get(path) ?? "";
}

getFolderStatus(path: string): FileStatus {
if (!this.enabled || this.opts.shouldIgnore(path)) return "";
if (this.conflicts.has(path)) return "U";
return this.folderStatuses.get(path) ?? "";
}

noteCreate(path: string): void {
if (!this.enabled || this.opts.shouldIgnore(path)) return;
if (!this.trackedLoaded) return;
Expand Down