Skip to content

Commit

Permalink
refactor: better error handling and extra conflict statusbar icon
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Oct 13, 2024
1 parent ee3eff4 commit d989a71
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 277 deletions.
19 changes: 11 additions & 8 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
DIFF_VIEW_CONFIG,
} from "./constants";
import { openLineInGitHub, openHistoryInGitHub } from "./openInGitHub";
import { PluginState } from "./types";
import { ChangedFilesModal } from "./ui/modals/changedFilesModal";
import { GeneralModal } from "./ui/modals/generalModal";
import { IgnoreModal } from "./ui/modals/ignoreModal";
Expand Down Expand Up @@ -275,19 +274,22 @@ export function addCommmands(plugin: ObsidianGit) {
plugin.addCommand({
id: "edit-remotes",
name: "Edit remotes",
callback: async () => plugin.editRemotes(),
callback: () =>
plugin.editRemotes().catch((e) => plugin.displayError(e)),
});

plugin.addCommand({
id: "remove-remote",
name: "Remove remote",
callback: async () => plugin.removeRemote(),
callback: () =>
plugin.removeRemote().catch((e) => plugin.displayError(e)),
});

plugin.addCommand({
id: "set-upstream-branch",
name: "Set upstream branch",
callback: async () => plugin.setUpstreamBranch(),
callback: () =>
plugin.setUpstreamBranch().catch((e) => plugin.displayError(e)),
});

plugin.addCommand({
Expand Down Expand Up @@ -325,13 +327,15 @@ export function addCommmands(plugin: ObsidianGit) {
plugin.addCommand({
id: "init-repo",
name: "Initialize a new repo",
callback: async () => plugin.createNewRepo(),
callback: () =>
plugin.createNewRepo().catch((e) => plugin.displayError(e)),
});

plugin.addCommand({
id: "clone-repo",
name: "Clone an existing remote repo",
callback: async () => plugin.cloneNewRepo(),
callback: () =>
plugin.cloneNewRepo().catch((e) => plugin.displayError(e)),
});

plugin.addCommand({
Expand All @@ -341,8 +345,7 @@ export function addCommmands(plugin: ObsidianGit) {
if (!(await plugin.isAllInitialized())) return;

try {
const status = await plugin.gitManager.status();
plugin.setState(PluginState.idle);
const status = await plugin.updateCachedStatus();
if (status.changed.length + status.staged.length > 500) {
plugin.displayError("Too many changes to display");
return;
Expand Down
18 changes: 9 additions & 9 deletions src/gitManager/isomorphicGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
UnstagedFile,
WalkDifference,
} from "../types";
import { PluginState } from "../types";
import { CurrentGitAction } from "../types";
import { GeneralModal } from "../ui/modals/generalModal";
import { splitRemoteBranch, worthWalking } from "../utils";
import { GitManager } from "./gitManager";
Expand Down Expand Up @@ -154,7 +154,7 @@ export class IsomorphicGit extends GitManager {
);
}, 20000);
try {
this.plugin.setState(PluginState.status);
this.plugin.setPluginState({ gitAction: CurrentGitAction.status });
const status = (
await this.wrapFS(git.statusMatrix({ ...this.getRepo() }))
).map((row) => this.getFileStatusResult(row));
Expand Down Expand Up @@ -206,7 +206,7 @@ export class IsomorphicGit extends GitManager {
}): Promise<undefined> {
try {
await this.checkAuthorInfo();
this.plugin.setState(PluginState.commit);
this.plugin.setPluginState({ gitAction: CurrentGitAction.commit });
const formatMessage = await this.formatCommitMessage(message);
const hadConflict = this.plugin.localStorage.getConflict();
let parent: string[] | undefined = undefined;
Expand Down Expand Up @@ -240,7 +240,7 @@ export class IsomorphicGit extends GitManager {
vaultPath = this.getRelativeVaultPath(filepath);
}
try {
this.plugin.setState(PluginState.add);
this.plugin.setPluginState({ gitAction: CurrentGitAction.add });
if (await this.app.vault.adapter.exists(vaultPath)) {
await this.wrapFS(
git.add({ ...this.getRepo(), filepath: gitPath })
Expand Down Expand Up @@ -303,7 +303,7 @@ export class IsomorphicGit extends GitManager {

async unstage(filepath: string, relativeToVault: boolean): Promise<void> {
try {
this.plugin.setState(PluginState.add);
this.plugin.setPluginState({ gitAction: CurrentGitAction.add });
filepath = this.getRelativeRepoPath(filepath, relativeToVault);
await this.wrapFS(
git.resetIndex({ ...this.getRepo(), filepath: filepath })
Expand Down Expand Up @@ -344,7 +344,7 @@ export class IsomorphicGit extends GitManager {

async discard(filepath: string): Promise<void> {
try {
this.plugin.setState(PluginState.add);
this.plugin.setPluginState({ gitAction: CurrentGitAction.add });
await this.wrapFS(
git.checkout({
...this.getRepo(),
Expand Down Expand Up @@ -415,7 +415,7 @@ export class IsomorphicGit extends GitManager {
async pull(): Promise<FileStatusResult[]> {
const progressNotice = this.showNotice("Initializing pull");
try {
this.plugin.setState(PluginState.pull);
this.plugin.setPluginState({ gitAction: CurrentGitAction.pull });

const localCommit = await this.resolveRef("HEAD");
await this.fetch();
Expand Down Expand Up @@ -483,15 +483,15 @@ export class IsomorphicGit extends GitManager {
}
const progressNotice = this.showNotice("Initializing push");
try {
this.plugin.setState(PluginState.status);
this.plugin.setPluginState({ gitAction: CurrentGitAction.status });
const status = await this.branchInfo();
const trackingBranch = status.tracking;
const currentBranch = status.current;
const numChangedFiles = (
await this.getFileChangesCount(currentBranch!, trackingBranch!)
).length;

this.plugin.setState(PluginState.push);
this.plugin.setPluginState({ gitAction: CurrentGitAction.push });

await this.wrapFS(
git.push({
Expand Down
40 changes: 20 additions & 20 deletions src/gitManager/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
LogEntry,
Status,
} from "../types";
import { NoNetworkError, PluginState } from "../types";
import { NoNetworkError, CurrentGitAction } from "../types";
import { impossibleBranch, splitRemoteBranch } from "../utils";
import { GitManager } from "./gitManager";

Expand Down Expand Up @@ -119,9 +119,9 @@ export class SimpleGit extends GitManager {
}

async status(): Promise<Status> {
this.plugin.setState(PluginState.status);
this.plugin.setPluginState({ gitAction: CurrentGitAction.status });
const status = await this.git.status();
this.plugin.setState(PluginState.idle);
this.plugin.setPluginState({ gitAction: CurrentGitAction.idle });

const allFilesFormatted = status.files.map<FileStatusResult>((e) => {
const res = this.formatPath(e);
Expand Down Expand Up @@ -280,7 +280,7 @@ export class SimpleGit extends GitManager {

async commitAll({ message }: { message: string }): Promise<number> {
if (this.plugin.settings.updateSubmodules) {
this.plugin.setState(PluginState.commit);
this.plugin.setPluginState({ gitAction: CurrentGitAction.commit });
const submodulePaths = await this.getSubmodulePaths();
for (const item of submodulePaths) {
await this.git.cwd({ path: item, root: false }).add("-A");
Expand All @@ -289,11 +289,11 @@ export class SimpleGit extends GitManager {
.commit(await this.formatCommitMessage(message));
}
}
this.plugin.setState(PluginState.add);
this.plugin.setPluginState({ gitAction: CurrentGitAction.add });

await this.git.add("-A");

this.plugin.setState(PluginState.commit);
this.plugin.setPluginState({ gitAction: CurrentGitAction.commit });

const res = await this.git.commit(
await this.formatCommitMessage(message)
Expand All @@ -310,7 +310,7 @@ export class SimpleGit extends GitManager {
message: string;
amend?: boolean;
}): Promise<number> {
this.plugin.setState(PluginState.commit);
this.plugin.setPluginState({ gitAction: CurrentGitAction.commit });

const res = (
await this.git.commit(
Expand All @@ -320,42 +320,42 @@ export class SimpleGit extends GitManager {
).summary.changes;
this.app.workspace.trigger("obsidian-git:head-change");

this.plugin.setState(PluginState.idle);
this.plugin.setPluginState({ gitAction: CurrentGitAction.idle });
return res;
}

async stage(path: string, relativeToVault: boolean): Promise<void> {
this.plugin.setState(PluginState.add);
this.plugin.setPluginState({ gitAction: CurrentGitAction.add });

path = this.getRelativeRepoPath(path, relativeToVault);
await this.git.add(["--", path]);

this.plugin.setState(PluginState.idle);
this.plugin.setPluginState({ gitAction: CurrentGitAction.idle });
}

async stageAll({ dir }: { dir?: string }): Promise<void> {
this.plugin.setState(PluginState.add);
this.plugin.setPluginState({ gitAction: CurrentGitAction.add });
await this.git.add(dir ?? "-A");
this.plugin.setState(PluginState.idle);
this.plugin.setPluginState({ gitAction: CurrentGitAction.idle });
}

async unstageAll({ dir }: { dir?: string }): Promise<void> {
this.plugin.setState(PluginState.add);
this.plugin.setPluginState({ gitAction: CurrentGitAction.add });
await this.git.reset(dir != undefined ? ["--", dir] : []);
this.plugin.setState(PluginState.idle);
this.plugin.setPluginState({ gitAction: CurrentGitAction.idle });
}

async unstage(path: string, relativeToVault: boolean): Promise<void> {
this.plugin.setState(PluginState.add);
this.plugin.setPluginState({ gitAction: CurrentGitAction.add });

path = this.getRelativeRepoPath(path, relativeToVault);
await this.git.reset(["--", path]);

this.plugin.setState(PluginState.idle);
this.plugin.setPluginState({ gitAction: CurrentGitAction.idle });
}

async discard(filepath: string): Promise<void> {
this.plugin.setState(PluginState.add);
this.plugin.setPluginState({ gitAction: CurrentGitAction.add });
if (await this.isTracked(filepath)) {
await this.git.checkout(["--", filepath]);
} else {
Expand All @@ -364,7 +364,7 @@ export class SimpleGit extends GitManager {
true
);
}
this.plugin.setState(PluginState.idle);
this.plugin.setPluginState({ gitAction: CurrentGitAction.idle });
}

async hashObject(filepath: string): Promise<string> {
Expand All @@ -388,7 +388,7 @@ export class SimpleGit extends GitManager {
}

async pull(): Promise<FileStatusResult[] | undefined> {
this.plugin.setState(PluginState.pull);
this.plugin.setPluginState({ gitAction: CurrentGitAction.pull });
try {
if (this.plugin.settings.updateSubmodules)
await this.git.subModule([
Expand Down Expand Up @@ -478,7 +478,7 @@ export class SimpleGit extends GitManager {
}

async push(): Promise<number | undefined> {
this.plugin.setState(PluginState.push);
this.plugin.setPluginState({ gitAction: CurrentGitAction.push });
try {
if (this.plugin.settings.updateSubmodules) {
const res = await this.git
Expand Down
Loading

0 comments on commit d989a71

Please sign in to comment.