Skip to content

Commit

Permalink
feat: use git diff, doesn't play well with...
Browse files Browse the repository at this point in the history
current architecture.
  • Loading branch information
kometenstaub committed Mar 18, 2022
1 parent 9c3d718 commit fa0da96
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/git_diff_view.ts
@@ -1,3 +1,4 @@
import { html } from 'diff2html';
import type { App, TFile } from 'obsidian';
import DiffView from './abstract_diff_view';
import { GIT_WARNING } from './constants';
Expand All @@ -19,19 +20,23 @@ export default class GitDiffView extends DiffView {
async onOpen() {
super.onOpen();
await this.getInitialVersions();
const diff = await this.getDiff();
const gitDiff = await this.app.plugins.plugins[
'obsidian-git'
].gitManager.git.diff([this.versions[1].hash, '--', this.file.path])
const diff = html(gitDiff)
this.makeHistoryLists(GIT_WARNING);
this.basicHtml(diff, 'Git Diff');
this.appendVersions();
this.makeMoreGeneralHtml();
}

async getDiff(): Promise<string> {
return await this.app.plugins.plugins['obsidian-git'].gitManager.diff(
const gitDiff = await this.app.plugins.plugins['obsidian-git'].gitManager.diff(
this.file.path,
this.versions[this.leftActive].hash,
this.versions[this.rightActive].hash
);
return html(gitDiff)
}

async getInitialVersions(): Promise<void> {
Expand All @@ -47,24 +52,25 @@ export default class GitDiffView extends DiffView {
message: '',
refs: '',
});
this.versions.concat(gitVersions);
this.versions.push(...gitVersions);
console.log(this.versions)
const diskContent = await this.app.vault.read(this.file);
const latestCommit = await gitManager.show(
this.versions[1].hash,
this.file.path
);
[this.leftContent, this.rightContent] = [latestCommit, diskContent];
// normally done by .makeMoreGeneralHTML, but needed in .getDiff because the diffs
// are generated differently
// are generated from the hashes which need the active file already
this.rightActive = 0;
this.leftActive = 1;
}

appendVersions(): void {
this.leftVList.concat(
this.leftVList.push(...
this.appendGitVersions(this.leftHistory[1], this.versions, true)
);
this.rightVList.concat(
this.rightVList.push(...
this.appendGitVersions(this.rightHistory[1], this.versions, false)
);
}
Expand All @@ -79,7 +85,23 @@ export default class GitDiffView extends DiffView {
const version = versions[i];
const div = el.createDiv({
cls: 'sync-history-list-item',
text: version.date,
text: version.message,
});
const infoDiv = div.createDiv({
cls: ['u-small', 'u-muted'],
});
const date = infoDiv.createDiv({
text: version.date
});
const author = infoDiv.createDiv({
text: version.author_name,
});
const hash = infoDiv.createDiv({
text: version.hash.slice(0, 7),
});
hash.style.cursor = 'copy';
hash.addEventListener('click', async () => {
await navigator.clipboard.writeText(version.hash);
});
versionList.push({
html: div,
Expand All @@ -97,8 +119,15 @@ export default class GitDiffView extends DiffView {
this.leftContent = await this.app.plugins.plugins[
'obsidian-git'
].gitManager.show(clickedEl.v.hash, this.file.path);
this.syncHistoryContentContainer.innerHTML =
await this.getDiff();
if (this.leftActive === 1 && this.rightActive === 0) {
const gitDiff = await this.app.plugins.plugins[
'obsidian-git'
].gitManager.git.diff([this.versions[1].hash, '--', this.file.path])
this.syncHistoryContentContainer.innerHTML = html(gitDiff)
} else {
this.syncHistoryContentContainer.innerHTML =
await this.getDiff();
}
} else {
const clickedEl = (await this.generateVersionListener(
div,
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces.ts
Expand Up @@ -31,6 +31,9 @@ declare module 'obsidian' {
commitHash1: string,
commitHash2: string
): Promise<string>;
git: {
diff(options: string[]): Promise<string>;
}
};
};
};
Expand Down
16 changes: 16 additions & 0 deletions src/main.ts
Expand Up @@ -4,6 +4,7 @@ import OpenSyncHistorySettingTab from './settings';
import DiffUtils from './diff_utils';
import SyncDiffView from './diff_view';
import RecoveryDiffView from './recovery_diff_view';
import GitDiffView from './git_diff_view';

const DEFAULT_SETTINGS: OpenSyncHistorySettings = {
//context: '3',
Expand All @@ -24,6 +25,10 @@ export default class OpenSyncHistoryPlugin extends Plugin {
return newCommand;
};

openGitDiffModal(file: TFile): void {
new GitDiffView(this, this.app, file).open();
}

openRecoveryDiffModal(file: TFile): void {
new RecoveryDiffView(this, this.app, file).open();
}
Expand Down Expand Up @@ -79,12 +84,23 @@ export default class OpenSyncHistoryPlugin extends Plugin {
};
}

returnGitDiffCommand(): Command {
return {
id: 'open-git-diff-view',
name: 'Show Git Diff view for active file',
checkCallback: this.giveCallback(this.openGitDiffModal.bind(this))
};
}

async onload() {
console.log('loading Version History Diff plugin');

this.addCommand(this.returnOpenCommand());
this.addCommand(this.returnDiffCommand());
this.addCommand(this.returnRecoveryDiffCommand());
//if (this.app.plugins.plugins['obsidian-git']) {
this.addCommand(this.returnGitDiffCommand());
//}

await this.loadSettings();

Expand Down

0 comments on commit fa0da96

Please sign in to comment.