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
6 changes: 6 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ export async function getChangedFiles(cwd: string, gitPath: string): Promise<str
return [...new Set(files)];
}

export async function getChangedFileDiff(cwd: string, gitPath: string, filePath: string): Promise<string> {
const stdout = await runGit({ cwd, gitPath, args: ["diff", "--", filePath] });
if (!stdout) return "";
return stdout;
}

export async function commitAll(cwd: string, gitPath: string, message: string): Promise<void> {
await runGit({ cwd, gitPath, args: ["add", "-A"] });

Expand Down
3 changes: 3 additions & 0 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Translations = {
revertConfirmDesc: string;
revertConfirmButton: string;
revertCancelButton: string;
revertViewChanges: string,
revertNoChanges: string;
revertFileMenu: string;
noticeFileReverted: string;
Expand Down Expand Up @@ -199,6 +200,7 @@ const en: Translations = {
revertConfirmDesc: "The following files will be reverted:",
revertConfirmButton: "Revert",
revertCancelButton: "Cancel",
revertViewChanges: "(view changes)",
revertNoChanges: "No changes to revert.",
revertFileMenu: "Revert file changes",
noticeFileReverted: "GitAutoCommit: File reverted.",
Expand Down Expand Up @@ -333,6 +335,7 @@ const zhCN: Translations = {
revertConfirmDesc: "以下文件将被还原:",
revertConfirmButton: "还原",
revertCancelButton: "取消",
revertViewChanges: "(view changes)",
revertNoChanges: "没有可还原的修改。",
revertFileMenu: "还原文件修改",
noticeFileReverted: "GitAutoCommit: 文件已还原。",
Expand Down
14 changes: 12 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { EventRef, Menu, Notice, Platform, Plugin, TAbstractFile, TFile, 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 { getChangedFiles, commitAll, push, pull, getConflictFiles, markConflictsResolved, revertAll, revertFile, getChangedFilesSync, commitSyncAndPushDetached, setGitDebug, getChangedFileDiff } from "./git";
import { renderTemplate } from "./template";
import { t } from "./i18n";
import { RevertConfirmModal } from "./modals";
import { RevertConfirmModal, ViewDiffModal } from "./modals";
import { GitStatusBadgeManager } from "./statusBadges";
import { ProgressNotice } from "./notice";

Expand Down Expand Up @@ -362,6 +362,11 @@ export default class AutoGitPlugin extends Plugin {
new Notice(t().noticeRevertFailed((e as Error).message));
}
})();
}, (filePath: string) => {
void (async () => {
const diff = await getChangedFileDiff(cwd, this.settings.gitPath, filePath);
new ViewDiffModal(this.app, filePath, diff).open();
})();
}).open();
} catch (e) {
new Notice(t().noticeRevertFailed((e as Error).message));
Expand Down Expand Up @@ -394,6 +399,11 @@ export default class AutoGitPlugin extends Plugin {
new Notice(t().noticeFileRevertFailed((e as Error).message));
}
})();
}, (filePath: string) => {
void (async () => {
const diff = await getChangedFileDiff(this.getVaultPath(), this.settings.gitPath, filePath);
new ViewDiffModal(this.app, filePath, diff).open();
})();
}).open();
});
});
Expand Down
34 changes: 31 additions & 3 deletions src/modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ import { t } from "./i18n";
export class RevertConfirmModal extends Modal {
private files: string[];
private onConfirm: () => void;
private onDiffRequest: (filePath: string) => void;

constructor(app: App, files: string[], onConfirm: () => void) {
constructor(app: App, files: string[], onConfirm: () => void, onDiffRequest: (filePath: string) => void) {
super(app);
this.files = files;
this.onConfirm = onConfirm;
this.onDiffRequest = onDiffRequest;
}

onOpen() {
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" });
this.files.forEach((file) => {
listEl.createEl("li", { text: file });
listEl.createEl("li", { text: file + " " })
.createEl("a", { href: "#", text: i18n.revertViewChanges })
.addEventListener("click", () => this.onDiffRequest(file));
});

const buttonContainer = contentEl.createDiv({ cls: "modal-button-container" });
Expand All @@ -43,3 +47,27 @@ export class RevertConfirmModal extends Modal {
this.contentEl.empty();
}
}

export class ViewDiffModal extends Modal {
private title: string;
private diff: string;

constructor(app: App, title: string, diff: string) {
super(app);
this.title = title;
this.diff = diff;
}

onOpen() {
this.setTitle(this.title);
const preview = this.contentEl.createEl("div", { cls: "diff-preview-modal" });
preview.createEl("pre").setText(this.diff);
// MarkdownRenderer
// .render(this.app, "```diff\n" + this.diff + "\n```", preview, "", new MarkdownRenderChild(preview));
}

onClose() {
this.contentEl.empty();
}
}

5 changes: 5 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@
.git-status-badge[data-status="U"] {
color: #ff0000;
}

.diff-preview-modal {
font-size: var(--font-ui-small);
overflow: scroll;
}