Skip to content

Commit

Permalink
fix: catch more git errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Feb 19, 2022
1 parent 10c3072 commit 153fd82
Showing 1 changed file with 34 additions and 36 deletions.
70 changes: 34 additions & 36 deletions src/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class SimpleGit extends GitManager {
conflicted: string[];
}> {
this.plugin.setState(PluginState.status);
const status = await this.git.status();
const status = await this.git.status((err) => this.onError(err));

this.plugin.setState(PluginState.idle);
return {
Expand Down Expand Up @@ -82,15 +82,15 @@ export class SimpleGit extends GitManager {
async commitAll(message?: string): Promise<number> {
if (this.plugin.settings.updateSubmodules) {
this.plugin.setState(PluginState.commit);
await this.git.subModule(["foreach", "--recursive", `git add -A && if [ ! -z "$(git status --porcelain)" ]; then git commit -m "${message ?? await this.formatCommitMessage()}"; fi`], (err: any) => this.onError(err));
await this.git.subModule(["foreach", "--recursive", `git add -A && if [ ! -z "$(git status --porcelain)" ]; then git commit -m "${message ?? await this.formatCommitMessage()}"; fi`], (err) => this.onError(err));
}
this.plugin.setState(PluginState.add);
await this.git.add(
"./*", (err: any) => this.onError(err)
);

await this.git.add("./*", (err) => this.onError(err));

this.plugin.setState(PluginState.commit);

return (await this.git.commit(await this.formatCommitMessage(message))).summary.changes;
return (await this.git.commit(await this.formatCommitMessage(message), (err) => this.onError(err))).summary.changes;
}

async commit(message?: string): Promise<number> {
Expand All @@ -104,32 +104,30 @@ export class SimpleGit extends GitManager {

async stage(filepath: string): Promise<void> {
this.plugin.setState(PluginState.add);
await this.git.add(
filepath, (err: any) => this.onError(err)
);
await this.git.add(filepath, (err) => this.onError(err));
this.plugin.setState(PluginState.idle);
}

async stageAll(): Promise<void> {
this.plugin.setState(PluginState.add);
await this.git.add(
"./*", (err: any) => this.onError(err)
"./*", (err) => this.onError(err)
);
this.plugin.setState(PluginState.idle);
}

async unstageAll(): Promise<void> {
this.plugin.setState(PluginState.add);
await this.git.reset(
["--", "./*"], (err: any) => this.onError(err)
["--", "./*"], (err) => this.onError(err)
);
this.plugin.setState(PluginState.idle);
}

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

Expand All @@ -138,21 +136,21 @@ export class SimpleGit extends GitManager {
async discard(filepath: string): Promise<void> {
this.plugin.setState(PluginState.add);
await this.git.checkout(
["--", filepath], (err: any) => this.onError(err)
["--", filepath], (err) => this.onError(err)
);
this.plugin.setState(PluginState.idle);
}

async pull(): Promise<number> {
this.plugin.setState(PluginState.pull);
if (this.plugin.settings.updateSubmodules)
await this.git.subModule(["update", "--remote", "--merge", "--recursive"], (err: any) => this.onError(err));
await this.git.subModule(["update", "--remote", "--merge", "--recursive"], (err) => this.onError(err));

const branchInfo = await this.branchInfo();
const localCommit = await this.git.revparse([branchInfo.current]);
const localCommit = await this.git.revparse([branchInfo.current], (err) => this.onError(err));

await this.git.fetch((err: any) => this.onError(err));
const upstreamCommit = await this.git.revparse([branchInfo.tracking]);
await this.git.fetch((err) => this.onError(err));
const upstreamCommit = await this.git.revparse([branchInfo.tracking], (err) => this.onError(err));

if (localCommit !== upstreamCommit) {
if (this.plugin.settings.syncMethod === 'merge' || this.plugin.settings.syncMethod === 'rebase') {
Expand All @@ -166,8 +164,8 @@ export class SimpleGit extends GitManager {

}
} catch (err) {
this.plugin.displayError(`Sync failed (${this.plugin.settings.syncMethod}): ${err.message}`);
const status = await this.git.status();
this.plugin.displayError(`Pull failed (${this.plugin.settings.syncMethod}): ${err.message}`);
const status = await this.status();
if (status.conflicted.length > 0) {
this.plugin.handleConflict(status.conflicted);
}
Expand All @@ -176,7 +174,7 @@ export class SimpleGit extends GitManager {

} else if (this.plugin.settings.syncMethod === 'reset') {
try {
await this.git.raw(['update-ref', `refs/heads/${branchInfo.current}`, upstreamCommit]);
await this.git.raw(['update-ref', `refs/heads/${branchInfo.current}`, upstreamCommit], (err) => this.onError(err));
await this.unstageAll();
} catch (err) {
this.plugin.displayError(`Sync failed (${this.plugin.settings.syncMethod}): ${err.message}`);
Expand All @@ -199,10 +197,10 @@ export class SimpleGit extends GitManager {

this.plugin.setState(PluginState.push);
if (this.plugin.settings.updateSubmodules) {
await this.git.env({ ...process.env, "OBSIDIAN_GIT": 1 }).subModule(["foreach", "--recursive", `tracking=$(git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)"); echo $tracking; if [ ! -z "$(git diff --shortstat $tracking)" ]; then git push; fi`], (err: any) => this.onError(err));
await this.git.env({ ...process.env, "OBSIDIAN_GIT": 1 }).subModule(["foreach", "--recursive", `tracking=$(git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)"); echo $tracking; if [ ! -z "$(git diff --shortstat $tracking)" ]; then git push; fi`], (err) => this.onError(err));

}
await this.git.env({ ...process.env, "OBSIDIAN_GIT": 1 }).push((err: any) => this.onError(err));
await this.git.env({ ...process.env, "OBSIDIAN_GIT": 1 }).push((err) => this.onError(err));

return remoteChangedFiles;
}
Expand All @@ -213,7 +211,7 @@ export class SimpleGit extends GitManager {
if (this.plugin.settings.updateSubmodules === true) {
return true;
}
const status = await this.git.status((err: any) => this.onError(err));
const status = await this.git.status((err) => this.onError(err));
const trackingBranch = status.tracking;
const currentBranch = status.current;
const remoteChangedFiles = (await this.git.diffSummary([currentBranch, trackingBranch])).changed;
Expand All @@ -232,8 +230,8 @@ export class SimpleGit extends GitManager {
}

async branchInfo(): Promise<BranchInfo> {
const status = await this.git.status((err: any) => this.onError(err));
const branches = await this.git.branch(["--no-color"], (err: any) => this.onError(err));
const status = await this.git.status((err) => this.onError(err));
const branches = await this.git.branch(["--no-color"], (err) => this.onError(err));

return {
current: status.current,
Expand All @@ -243,41 +241,41 @@ export class SimpleGit extends GitManager {
}

async checkout(branch: string): Promise<void> {
await this.git.checkout(branch, (err: any) => this.onError(err));
await this.git.checkout(branch, (err) => this.onError(err));
}

async init(): Promise<void> {
await this.git.init(false, (err: any) => this.onError(err));
await this.git.init(false, (err) => this.onError(err));
}

async clone(url: string, dir: string): Promise<void> {
await this.git.clone(url, path.join((this.app.vault.adapter as FileSystemAdapter).getBasePath(), dir), [], (err: any) => this.onError(err));
await this.git.clone(url, path.join((this.app.vault.adapter as FileSystemAdapter).getBasePath(), dir), [], (err) => this.onError(err));
}

async setConfig(path: string, value: any): Promise<void> {
await this.git.addConfig(path, value, (err: any) => this.onError(err));
await this.git.addConfig(path, value, (err) => this.onError(err));
}

async getConfig(path: string): Promise<any> {
const config = await this.git.listConfig((err: any) => this.onError(err));
const config = await this.git.listConfig((err) => this.onError(err));
return config.all[path];
}

async fetch(remote?: string): Promise<void> {
await this.git.fetch(remote != undefined ? [remote] : [], (err: any) => this.onError(err));
await this.git.fetch(remote != undefined ? [remote] : [], (err) => this.onError(err));
}

async setRemote(name: string, url: string): Promise<void> {
if ((await this.getRemotes()).includes(name))
await this.git.remote(["set-url", name, url], (err: any) => this.onError(err));
await this.git.remote(["set-url", name, url], (err) => this.onError(err));

else {
await this.git.remote(["add", name, url], (err: any) => this.onError(err));
await this.git.remote(["add", name, url], (err) => this.onError(err));
}
}

async getRemoteBranches(remote: string): Promise<string[]> {
const res = await this.git.branch(["-r", "--list", `${remote}*`], (err: any) => this.onError(err));
const res = await this.git.branch(["-r", "--list", `${remote}*`], (err) => this.onError(err));
const list = [];
for (var item in res.branches) {
list.push(res.branches[item].name);
Expand All @@ -286,7 +284,7 @@ export class SimpleGit extends GitManager {
}

async getRemotes() {
const res = await this.git.remote([], (err: any) => this.onError(err));
const res = await this.git.remote([], (err) => this.onError(err));
if (res) {
return res.trim().split("\n");
} else {
Expand All @@ -299,7 +297,7 @@ export class SimpleGit extends GitManager {
}

async updateUpstreamBranch(remoteBranch: string) {
await this.git.push(["--set-upstream", ...remoteBranch.split("/")], (err: any) => this.onError(err));
await this.git.push(["--set-upstream", ...remoteBranch.split("/")], (err) => this.onError(err));

}

Expand Down

0 comments on commit 153fd82

Please sign in to comment.