Skip to content

Commit

Permalink
feat: store git binary path in localstorage
Browse files Browse the repository at this point in the history
fix #283
  • Loading branch information
Vinzent03 committed Aug 24, 2022
1 parent c4bf4eb commit bd8bafc
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const DEFAULT_SETTINGS: ObsidianGitSettings = {
showStatusBar: true,
updateSubmodules: false,
syncMethod: 'merge',
gitPath: "",
customMessageOnAutoBackup: false,
autoBackupAfterFileChange: false,
treeStructure: false,
Expand Down
2 changes: 1 addition & 1 deletion src/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export abstract class GitManager {
template = template.replace("{{numFiles}}", String(numFiles));
}
if (template.includes("{{hostname}}")) {
const hostname = localStorage.getItem(this.plugin.manifest.id + ":hostname") || "";
const hostname = this.plugin.localStorage.getHostname() || "";
template = template.replace("{{hostname}}", hostname);
}

Expand Down
6 changes: 3 additions & 3 deletions src/isomorphicGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class IsomorphicGit extends GitManager {
onAuth: () => {
return {
username: this.plugin.settings.username,
password: localStorage.getItem(this.plugin.manifest.id + ":password")
password: this.plugin.localStorage.getPassword()
};
},
http: {
Expand Down Expand Up @@ -123,7 +123,7 @@ export class IsomorphicGit extends GitManager {
try {
this.plugin.setState(PluginState.commit);
const formatMessage = await this.formatCommitMessage(message);
const hadConflict = localStorage.getItem(this.plugin.manifest.id + ":conflict") === "true";
const hadConflict = this.plugin.localStorage.getConflict() === "true";
let parent: string[] = undefined;

if (hadConflict) {
Expand All @@ -136,7 +136,7 @@ export class IsomorphicGit extends GitManager {
message: formatMessage,
parent: parent,
}));
localStorage.setItem(this.plugin.manifest.id + ":conflict", "false");
this.plugin.localStorage.setConflict("false");
return;
} catch (error) {
this.plugin.displayError(error);
Expand Down
64 changes: 64 additions & 0 deletions src/localStorageSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import ObsidianGit from "./main";

export class LocalStorageSettings {
private prefix: string;
constructor(private readonly plugin: ObsidianGit) {
this.prefix = this.plugin.manifest.id;
};

getPassword(): string {
return localStorage.getItem(this.prefix + ":password");
}

setPassword(value: string): void {
return localStorage.setItem(this.prefix + ":password", value);
}

getHostname(): string {
return localStorage.getItem(this.prefix + ":hostname");
}

setHostname(value: string): void {
return localStorage.setItem(this.prefix + ":hostname", value);
}

getConflict(): string {
return localStorage.getItem(this.prefix + ":conflict");
}

setConflict(value: string): void {
return localStorage.setItem(this.prefix + ":conflict", value);
}

getLastAutoPull(): string {
return localStorage.getItem(this.prefix + ":lastAutoPull");
}

setLastAutoPull(value: string): void {
return localStorage.setItem(this.prefix + ":lastAutoPull", value);
}

getLastAutoBackup(): string {
return localStorage.getItem(this.prefix + ":lastAutoBackup");
}

setLastAutoBackup(value: string): void {
return localStorage.setItem(this.prefix + ":lastAutoBackup", value);
}

getLastAutoPush(): string {
return localStorage.getItem(this.prefix + ":lastAutoPush");
}

setLastAutoPush(value: string): void {
return localStorage.setItem(this.prefix + ":lastAutoPush", value);
}

getGitPath(): string {
return localStorage.getItem(this.prefix + ":gitPath");
}

setGitPath(value: string): void {
return localStorage.setItem(this.prefix + ":gitPath", value);
}
}
28 changes: 18 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CustomMessageModal } from "src/ui/modals/customMessageModal";
import { DEFAULT_SETTINGS, DIFF_VIEW_CONFIG, GIT_VIEW_CONFIG } from "./constants";
import { GitManager } from "./gitManager";
import { IsomorphicGit } from "./isomorphicGit";
import { LocalStorageSettings } from "./localStorageSettings";
import { openHistoryInGitHub, openLineInGitHub } from "./openInGitHub";
import { SimpleGit } from "./simpleGit";
import { FileStatusResult, ObsidianGitSettings, PluginState, Status, UnstagedFile } from "./types";
Expand All @@ -17,6 +18,7 @@ import GitView from "./ui/sidebar/sidebarView";

export default class ObsidianGit extends Plugin {
gitManager: GitManager;
localStorage: LocalStorageSettings;
settings: ObsidianGitSettings;
statusBar: StatusBar;
state: PluginState;
Expand Down Expand Up @@ -73,6 +75,7 @@ export default class ObsidianGit extends Plugin {

async onload() {
console.log('loading ' + this.manifest.name + " plugin");
this.localStorage = new LocalStorageSettings(this);

await this.loadSettings();
this.migrateSettings();
Expand Down Expand Up @@ -363,6 +366,11 @@ export default class ObsidianGit extends Plugin {
this.settings.autoCommitMessage = this.settings.commitMessage;
this.saveSettings();
}
if (this.settings.gitPath != undefined) {
this.localStorage.setGitPath(this.settings.gitPath);
this.settings.gitPath = undefined;
this.saveSettings();
}
}

unloadPlugin() {
Expand Down Expand Up @@ -400,19 +408,19 @@ export default class ObsidianGit extends Plugin {

async saveLastAuto(date: Date, mode: "backup" | "pull" | "push") {
if (mode === "backup") {
window.localStorage.setItem(this.manifest.id + ":lastAutoBackup", date.toString());
this.localStorage.setLastAutoBackup(date.toString());
} else if (mode === "pull") {
window.localStorage.setItem(this.manifest.id + ":lastAutoPull", date.toString());
this.localStorage.setLastAutoPull(date.toString());
} else if (mode === "push") {
window.localStorage.setItem(this.manifest.id + ":lastAutoPush", date.toString());
this.localStorage.setLastAutoPush(date.toString());
}
}

async loadLastAuto(): Promise<{ "backup": Date, "pull": Date; "push": Date; }> {
return {
"backup": new Date(window.localStorage.getItem(this.manifest.id + ":lastAutoBackup") ?? ""),
"pull": new Date(window.localStorage.getItem(this.manifest.id + ":lastAutoPull") ?? ""),
"push": new Date(window.localStorage.getItem(this.manifest.id + ":lastAutoPush") ?? ""),
"backup": new Date(this.localStorage.getLastAutoBackup() ?? ""),
"pull": new Date(this.localStorage.getLastAutoPull() ?? ""),
"push": new Date(this.localStorage.getLastAutoPush() ?? ""),
};
}

Expand Down Expand Up @@ -602,7 +610,7 @@ export default class ObsidianGit extends Plugin {
async commit(fromAutoBackup: boolean, requestCustomMessage: boolean = false, onlyStaged: boolean = false): Promise<boolean> {
if (!await this.isAllInitialized()) return false;

const hadConflict = localStorage.getItem(this.manifest.id + ":conflict") === "true";
const hadConflict = this.localStorage.getConflict() === "true";

let changedFiles: { vault_path: string; }[];
let status: Status | undefined;
Expand Down Expand Up @@ -715,8 +723,7 @@ export default class ObsidianGit extends Plugin {
}

const file = this.app.vault.getAbstractFileByPath(this.conflictOutputFile);
const hadConflict = localStorage.getItem(this.manifest.id + ":conflict") === "true";

const hadConflict = this.localStorage.getConflict();
if (this.gitManager instanceof SimpleGit && file) await this.app.vault.delete(file);

// Refresh because of pull
Expand Down Expand Up @@ -894,7 +901,8 @@ export default class ObsidianGit extends Plugin {

async handleConflict(conflicted?: string[]): Promise<void> {
this.setState(PluginState.conflicted);
localStorage.setItem(this.manifest.id + ":conflict", "true");

this.localStorage.setConflict("true");
let lines: string[];
if (conflicted !== undefined) {
lines = [
Expand Down
11 changes: 5 additions & 6 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
.setDesc('Specify custom hostname for every device.')
.addText((text) =>
text
.setValue(localStorage.getItem(plugin.manifest.id + ":hostname"))
.setValue(plugin.localStorage.getHostname())
.onChange(async (value) => {
localStorage.setItem(plugin.manifest.id + ":hostname", value);
plugin.localStorage.setHostname(value);
})
);

Expand Down Expand Up @@ -388,11 +388,10 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
new Setting(containerEl)
.setName("Custom Git binary path")
.addText((cb) => {
cb.setValue(plugin.settings.gitPath);
cb.setValue(plugin.localStorage.getGitPath());
cb.setPlaceholder("git");
cb.onChange((value) => {
plugin.settings.gitPath = value;
plugin.saveSettings();
plugin.localStorage.setGitPath(value);
plugin.gitManager.updateGitPath(value || "git");
});
});
Expand All @@ -418,7 +417,7 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
cb.inputEl.autocomplete = "off";
cb.inputEl.spellcheck = false;
cb.onChange((value) => {
localStorage.setItem(plugin.manifest.id + ":password", value);
plugin.localStorage.setPassword(value);
});
});
if (gitReady)
Expand Down
4 changes: 2 additions & 2 deletions src/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SimpleGit extends GitManager {

this.git = simpleGit({
baseDir: basePath,
binary: this.plugin.settings.gitPath || undefined,
binary: this.plugin.localStorage.getGitPath() || undefined,
config: ["core.quotepath=off"]
});
this.git.cwd(await this.git.revparse("--show-toplevel"));
Expand Down Expand Up @@ -405,7 +405,7 @@ export class SimpleGit extends GitManager {

private isGitInstalled(): boolean {
// https://github.com/steveukx/git-js/issues/402
const command = spawnSync(this.plugin.settings.gitPath || 'git', ['--version'], {
const command = spawnSync(this.plugin.localStorage.getGitPath() || 'git', ['--version'], {
stdio: 'ignore'
});

Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export interface ObsidianGitSettings {
listChangedFilesInMessageBody: boolean;
showStatusBar: boolean;
updateSubmodules: boolean;
gitPath: string;
/**
* @deprecated Using `localstorage` instead
*/
gitPath?: string;
customMessageOnAutoBackup: boolean;
autoBackupAfterFileChange: boolean;
treeStructure: boolean;
Expand Down

0 comments on commit bd8bafc

Please sign in to comment.