Skip to content

Commit

Permalink
feat: discard directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Oct 17, 2022
1 parent 56afe51 commit 149805f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export abstract class GitManager {

abstract discard(filepath: string): Promise<void>;

abstract discardAll(_: { dir: string, status?: Status; }): Promise<void>;

abstract pull(): Promise<FileStatusResult[] | undefined>;

abstract push(): Promise<number>;
Expand Down
16 changes: 16 additions & 0 deletions src/isomorphicGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,22 @@ export class IsomorphicGit extends GitManager {
}
}

async discardAll({ dir, status }: { dir: string, status?: Status; }): Promise<void> {
let files: string[] = [];
if (status) {
files = status.changed.filter(file => file.path.startsWith(dir)).map(file => file.path);
} else {
files = (await this.getUnstagedFiles(dir)).map(({ filepath }) => filepath);
}

try {
await this.wrapFS(git.checkout({ ...this.getRepo(), filepaths: files, force: true }));
} catch (error) {
this.plugin.displayError(error);
throw error;
}
}

getProgressText(action: string, event: GitProgressEvent): string {
let out = `${action} progress:`;
if (event.phase) {
Expand Down
4 changes: 4 additions & 0 deletions src/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ export class SimpleGit extends GitManager {
this.plugin.setState(PluginState.idle);
}

async discardAll({ dir }: { dir?: string; }): Promise<void> {
return this.discard(dir ?? "");
}

async pull(): Promise<FileStatusResult[] | undefined> {
this.plugin.setState(PluginState.pull);
if (this.plugin.settings.updateSubmodules)
Expand Down
48 changes: 47 additions & 1 deletion src/ui/sidebar/components/treeComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<script lang="ts">
import ObsidianGit from "src/main";
import { FileType, RootTreeItem, TreeItem } from "src/types";
import { DiscardModal } from "src/ui/modals/discardModal";
import { slide } from "svelte/transition";
import GitView from "../sidebarView";
import FileComponent from "./fileComponent.svelte";
Expand All @@ -13,7 +14,6 @@
export let fileType: FileType;
export let topLevel = false;
const closed: Record<string, boolean> = {};
function stage(path: string) {
plugin.gitManager.stageAll({ dir: path }).finally(() => {
dispatchEvent(new CustomEvent("git-refresh"));
Expand All @@ -24,6 +24,26 @@
dispatchEvent(new CustomEvent("git-refresh"));
});
}
function discard(item: TreeItem) {
new DiscardModal(
view.app,
false,
plugin.gitManager.getVaultPath(item.path)
)
.myOpen()
.then((shouldDiscard) => {
if (shouldDiscard === true) {
plugin.gitManager
.discardAll({
dir: item.path,
status: plugin.cachedStatus,
})
.finally(() => {
dispatchEvent(new CustomEvent("git-refresh"));
});
}
});
}
function fold(item: TreeItem) {
closed[item.title] = !closed[item.title];
}
Expand Down Expand Up @@ -107,6 +127,32 @@
>
</div>
{:else}
<div
data-icon="skip-back"
aria-label="Discard"
on:click={() => discard(entity)}
class="clickable-icon"
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="svg-icon lucide-skip-back"
><polygon
points="19 20 9 12 19 4 19 20"
/><line
x1="5"
y1="19"
x2="5"
y2="5"
/></svg
>
</div>
<div
data-icon="plus"
aria-label="Stage"
Expand Down

0 comments on commit 149805f

Please sign in to comment.