Skip to content

Commit bde2b3d

Browse files
committed
feat: respect push.autoSetupRemote config
close #1020
1 parent b9a68e9 commit bde2b3d

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

src/gitManager/gitManager.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ export abstract class GitManager {
5858

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

61-
abstract push(): Promise<number | undefined>;
61+
/**
62+
* Pushes to the remote repository.
63+
*
64+
* @returns `numper`: number of pushed files
65+
* @returns `undefined` for other states, but a notification is done elsewhere
66+
* @returns `null` if push was successful, but changed files could not be determined
67+
*/
68+
abstract push(): Promise<number | undefined | null>;
6269

6370
abstract getUnpushedCommits(): Promise<number>;
6471

@@ -87,7 +94,10 @@ export abstract class GitManager {
8794
value: string | number | boolean | undefined
8895
): Promise<void>;
8996

90-
abstract getConfig(path: string): Promise<string | undefined>;
97+
abstract getConfig(
98+
path: string,
99+
scope?: string
100+
): Promise<string | undefined>;
91101

92102
abstract fetch(remote?: string): Promise<void>;
93103

src/gitManager/simpleGit.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ export class SimpleGit extends GitManager {
700700
}
701701
}
702702

703-
async push(): Promise<number | undefined> {
703+
async push(): Promise<number | undefined | null> {
704704
this.plugin.setPluginState({ gitAction: CurrentGitAction.push });
705705
try {
706706
if (this.plugin.settings.updateSubmodules) {
@@ -714,7 +714,7 @@ export class SimpleGit extends GitManager {
714714
console.log(res);
715715
}
716716
const status = await this.git.status();
717-
const trackingBranch = status.tracking!;
717+
const trackingBranch = status.tracking;
718718
const currentBranch = status.current!;
719719

720720
if (!trackingBranch && this.plugin.settings.updateSubmodules) {
@@ -723,14 +723,16 @@ export class SimpleGit extends GitManager {
723723
);
724724
return undefined;
725725
}
726-
727-
const remoteChangedFiles = (
728-
await this.git.diffSummary([
729-
currentBranch,
730-
trackingBranch,
731-
"--",
732-
])
733-
).changed;
726+
let remoteChangedFiles: number | null = null;
727+
if (trackingBranch) {
728+
remoteChangedFiles = (
729+
await this.git.diffSummary([
730+
currentBranch,
731+
trackingBranch,
732+
"--",
733+
])
734+
).changed;
735+
}
734736

735737
await this.git.env({ ...process.env, OBSIDIAN_GIT: 1 }).push();
736738

@@ -938,9 +940,17 @@ export class SimpleGit extends GitManager {
938940
}
939941
}
940942

941-
async getConfig(path: string): Promise<string | undefined> {
942-
const config = await this.git.listConfig("local");
943-
const res = config.all[path];
943+
async getConfig(
944+
path: string,
945+
scope: "local" | "global" | "all" = "local"
946+
): Promise<string | undefined> {
947+
let config: simple.ConfigListSummary;
948+
if (scope == "all") {
949+
config = await this.git.listConfig();
950+
} else {
951+
config = await this.git.listConfig(scope);
952+
}
953+
const res = config.all[path.toLowerCase()];
944954
if (typeof res === "string" || res == undefined) {
945955
return res;
946956
} else {

src/main.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,9 @@ export default class ObsidianGit extends Plugin {
10761076
const pushedFiles = await this.gitManager.push();
10771077

10781078
if (pushedFiles !== undefined) {
1079-
if (pushedFiles > 0) {
1079+
if (pushedFiles === null) {
1080+
this.displayMessage(`Pushed to remote`);
1081+
} else if (pushedFiles > 0) {
10801082
this.displayMessage(
10811083
`Pushed ${pushedFiles} ${
10821084
pushedFiles == 1 ? "file" : "files"
@@ -1272,6 +1274,13 @@ export default class ObsidianGit extends Plugin {
12721274
if (this.settings.updateSubmodules) {
12731275
return true;
12741276
}
1277+
if (
1278+
this.gitManager instanceof SimpleGit &&
1279+
(await this.gitManager.getConfig("push.autoSetupRemote", "all")) ==
1280+
"true"
1281+
) {
1282+
return true;
1283+
}
12751284
if (!(await this.gitManager.branchInfo()).tracking) {
12761285
new Notice("No upstream branch is set. Please select one.");
12771286
return await this.setUpstreamBranch();

0 commit comments

Comments
 (0)