Skip to content

Commit

Permalink
feat: stage and unstage current file
Browse files Browse the repository at this point in the history
close #265
  • Loading branch information
Vinzent03 committed Jul 25, 2022
1 parent faf48d0 commit f014e52
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export abstract class GitManager {

abstract unstageAll(): Promise<void>;

abstract stage(filepath: string): Promise<void>;
abstract stage(filepath: string, relativeToVault: boolean): Promise<void>;

abstract unstage(filepath: string): Promise<void>;
abstract unstage(filepath: string, relativeToVault: boolean): Promise<void>;

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

Expand Down Expand Up @@ -72,10 +72,10 @@ export abstract class GitManager {
abstract diff(file: string, commit1: string, commit2: string): Promise<string>;

// https://github.com/kometenstaub/obsidian-version-history-diff/issues/3
abstract log(file: string): Promise<ReadonlyArray<DefaultLogFields>>;
abstract log(file: string, relativeToVault: boolean): Promise<ReadonlyArray<DefaultLogFields>>;

// https://github.com/kometenstaub/obsidian-version-history-diff/issues/3
abstract show(commitHash: string, file: string): Promise<string>;
abstract show(commitHash: string, file: string, relativeToVault: boolean): Promise<string>;


getTreeStructure(children: FileStatusResult[], beginLength: number = 0): TreeItem[] {
Expand Down
50 changes: 50 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,30 @@ export default class ObsidianGit extends Plugin {
callback: () => this.promiseQueue.addTask(() => this.push())
});

this.addCommand({
id: "stage-current-file",
name: "Stage current file",
checkCallback: (checking) => {
if (checking) {
return this.app.workspace.getActiveFile() !== null;
} else {
this.promiseQueue.addTask(() => this.stageCurrentFile());
}
}
});

this.addCommand({
id: "unstage-current-file",
name: "Unstage current file",
checkCallback: (checking) => {
if (checking) {
return this.app.workspace.getActiveFile() !== null;
} else {
this.promiseQueue.addTask(() => this.unstageCurrentFile());
}
}
});

this.addCommand({
id: "edit-remotes",
name: "Edit remotes",
Expand Down Expand Up @@ -533,6 +557,32 @@ export default class ObsidianGit extends Plugin {
return pulledFilesLength != 0;
}

async stageCurrentFile(): Promise<boolean> {
if (!await this.isAllInitialized()) return false;

const file = this.app.workspace.getActiveFile();
await this.gitManager.stage(file.path, true);
this.displayMessage(`Staged ${file.path}`);

dispatchEvent(new CustomEvent('git-refresh'));

this.setState(PluginState.idle);
return true;
}

async unstageCurrentFile(): Promise<boolean> {
if (!await this.isAllInitialized()) return false;

const file = this.app.workspace.getActiveFile();
await this.gitManager.unstage(file.path, true);
this.displayMessage(`Unstaged ${file.path}`);

dispatchEvent(new CustomEvent('git-refresh'));

this.setState(PluginState.idle);
return true;
}

async remotesAreSet(): Promise<boolean> {
if (!(await this.gitManager.branchInfo()).tracking) {
new Notice("No upstream branch is set. Please select one.");
Expand Down
25 changes: 16 additions & 9 deletions src/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@ export class SimpleGit extends GitManager {

}

async stage(filepath: string): Promise<void> {
async stage(path: string, relativeToVault: boolean): Promise<void> {
this.plugin.setState(PluginState.add);
await this.git.add(["--", filepath], (err) => this.onError(err));

path = this.getPath(path, relativeToVault);
await this.git.add(["--", path], (err) => this.onError(err));

this.plugin.setState(PluginState.idle);
}

Expand All @@ -190,13 +193,13 @@ export class SimpleGit extends GitManager {
this.plugin.setState(PluginState.idle);
}

async unstage(filepath: string): Promise<void> {
async unstage(path: string, relativeToVault: boolean): Promise<void> {
this.plugin.setState(PluginState.add);
await this.git.reset(
["--", filepath], (err) => this.onError(err)
);
this.plugin.setState(PluginState.idle);

path = this.getPath(path, relativeToVault);
await this.git.reset(["--", path], (err) => this.onError(err));

this.plugin.setState(PluginState.idle);
}

async discard(filepath: string): Promise<void> {
Expand Down Expand Up @@ -308,14 +311,14 @@ export class SimpleGit extends GitManager {
}

async log(file?: string, relativeToVault: boolean = true): Promise<ReadonlyArray<DefaultLogFields>> {
const path = (relativeToVault && this.plugin.settings.basePath.length > 0) ? file?.substring(this.plugin.settings.basePath.length + 1) : file;
const path = this.getPath(file, relativeToVault);

const res = await this.git.log({ file: path, }, (err) => this.onError(err));
return res.all;
}

async show(commitHash: string, file: string, relativeToVault: boolean = true): Promise<string> {
const path = (relativeToVault && this.plugin.settings.basePath.length > 0) ? file.substring(this.plugin.settings.basePath.length + 1) : file;
const path = this.getPath(file, relativeToVault);

return this.git.show([commitHash + ":" + path], (err) => this.onError(err));
}
Expand Down Expand Up @@ -392,6 +395,10 @@ export class SimpleGit extends GitManager {
this.setGitInstance(true);
}

getPath(path: string, relativeToVault: boolean): string {
return (relativeToVault && this.plugin.settings.basePath.length > 0) ? path.substring(this.plugin.settings.basePath.length + 1) : path;
}

async getDiffString(filePath: string, stagedChanges = false): Promise<string> {
if (stagedChanges)
return (await this.git.diff(["--cached", "--", filePath]));
Expand Down
6 changes: 4 additions & 2 deletions src/ui/sidebar/components/fileComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
function stage() {
manager.stage(change.path).finally(() => {
manager.stage(change.path, false).finally(() => {
dispatchEvent(new CustomEvent("git-refresh"));
});
}
Expand Down Expand Up @@ -102,7 +102,9 @@
<span
class="path"
aria-label-position={side}
aria-label={change.vault_path.split("/").last() != change.vault_path ? change.vault_path : ""}
aria-label={change.vault_path.split("/").last() != change.vault_path
? change.vault_path
: ""}
on:click|self={showDiff}
>
{change.vault_path.split("/").last().replace(".md", "")}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/sidebar/components/stagedFileComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
}
function unstage() {
manager.unstage(change.path).finally(() => {
manager.unstage(change.path, false).finally(() => {
dispatchEvent(new CustomEvent("git-refresh"));
});
}
Expand Down

0 comments on commit f014e52

Please sign in to comment.