Skip to content

Commit

Permalink
feat: add submodules support
Browse files Browse the repository at this point in the history
close #93
  • Loading branch information
Vinzent03 committed Aug 11, 2021
1 parent 97ae387 commit 2a4ce6d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface ObsidianGitSettings {
disablePopups: boolean;
listChangedFilesInMessageBody: boolean;
showStatusBar: boolean;
updateSubmodules: boolean;
}
const DEFAULT_SETTINGS: ObsidianGitSettings = {
commitMessage: "vault backup: {{date}}",
Expand All @@ -31,6 +32,7 @@ const DEFAULT_SETTINGS: ObsidianGitSettings = {
disablePopups: false,
listChangedFilesInMessageBody: false,
showStatusBar: true,
updateSubmodules: false,
};

export default class ObsidianGit extends Plugin {
Expand Down Expand Up @@ -232,7 +234,7 @@ export default class ObsidianGit extends Plugin {


// Prevent plugin to pull/push at every call of createBackup. Only if unpushed commits are present
if (this.gitManager.canPush()) {
if (await this.gitManager.canPush()) {
if (this.settings.pullBeforePush) {
const pulledFilesLength = await this.gitManager.pull();
if (pulledFilesLength > 0) {
Expand Down
12 changes: 12 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName("Update submodules")
.setDesc('"Create backup" and "pull" takes care of submodules. Missing features: Conflicted files, count of pulled/pushed/committed files. Tracking branch needs to be set for each submodule')
.addToggle((toggle) =>
toggle
.setValue(plugin.settings.updateSubmodules)
.onChange((value) => {
plugin.settings.updateSubmodules = value;
plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Disable notifications")
.setDesc(
Expand Down
19 changes: 18 additions & 1 deletion src/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ 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));
}
this.plugin.setState(PluginState.add);
await this.git.add(
"./*", (err: any) => this.onError(err)
Expand All @@ -44,6 +48,9 @@ export class SimpleGit extends GitManager {

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));

const pullResult = await this.git.pull(["--no-rebase"],
async (err: Error | null) => {
if (err) {
Expand All @@ -60,19 +67,28 @@ export class SimpleGit extends GitManager {
}

async push(): Promise<number> {
this.plugin.setState(PluginState.push);
this.plugin.setState(PluginState.status);
const status = await this.git.status();
const trackingBranch = status.tracking;
const currentBranch = status.current;
const remoteChangedFiles = (await this.git.diffSummary([currentBranch, trackingBranch])).changed;

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 }).push((err: any) => this.onError(err));

return remoteChangedFiles;
}


async canPush(): Promise<boolean> {
// allow pushing in submodules even if the root has no changes.
if (this.plugin.settings.updateSubmodules === true) {
return true;
}
const status = await this.git.status((err: any) => this.onError(err));
const trackingBranch = status.tracking;
const currentBranch = status.current;
Expand Down Expand Up @@ -131,6 +147,7 @@ export class SimpleGit extends GitManager {
async fetch(): Promise<void> {
await this.git.fetch((err: any) => this.onError(err));
}

private isGitInstalled(): boolean {
// https://github.com/steveukx/git-js/issues/402
const command = spawnSync('git', ['--version'], {
Expand Down

0 comments on commit 2a4ce6d

Please sign in to comment.