From d52f51fec7ed020d9d44761c38891e517424cd84 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Tue, 28 Mar 2023 20:19:32 +0200 Subject: [PATCH] #549 - sync submodule changes --- CHANGELOG.md | 1 + package.json | 15 ++++++++++ src/constants/settings.ts | 3 ++ src/listeners/general/GitListener.ts | 41 ++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3770041b..8cb86d7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ - [#524](https://github.com/estruyf/vscode-front-matter/issues/524): Removed the **Global settings** view from the panel. You can still get it back by configuring a [custom view mode](https://frontmatter.codes/docs/panel#view-modes). - [#535](https://github.com/estruyf/vscode-front-matter/issues/535): Retain the scroll position after selecting a media file - [#538](https://github.com/estruyf/vscode-front-matter/issues/538): Added support to encode emojis in the string field +- [#549](https://github.com/estruyf/vscode-front-matter/issues/549): Git submodule support to sync changes ### ⚡️ Optimizations diff --git a/package.json b/package.json index a0ae2287..61a4cbaa 100644 --- a/package.json +++ b/package.json @@ -769,6 +769,21 @@ "markdownDescription": "Specify the commit message you want to use for the sync. [Check in the docs](https://frontmatter.codes/docs/settings/overview#frontmatter.git.commitmessage)", "default": "Synced by Front Matter" }, + "frontMatter.git.submodule.pull": { + "type": "boolean", + "markdownDescription": "Specify if you want to pull submodules when syncing. [Check in the docs](https://frontmatter.codes/docs/settings/overview#frontmatter.git.submodule.pull)", + "default": false + }, + "frontMatter.git.submodule.push": { + "type": "boolean", + "markdownDescription": "Specify if you want to push submodules when syncing. [Check in the docs](https://frontmatter.codes/docs/settings/overview#frontmatter.git.submodule.push)", + "default": false + }, + "frontMatter.git.submodule.branch": { + "type": "string", + "markdownDescription": "Specify the submodule branch to checkout. [Check in the docs](https://frontmatter.codes/docs/settings/overview#frontMatter.git.submodule.branch)", + "default": "" + }, "frontMatter.global.activeMode": { "type": [ "string", diff --git a/src/constants/settings.ts b/src/constants/settings.ts index db36a212..1e1c81a3 100644 --- a/src/constants/settings.ts +++ b/src/constants/settings.ts @@ -89,6 +89,9 @@ export const SETTING_SITE_BASEURL = 'site.baseURL'; export const SETTING_GIT_ENABLED = 'git.enabled'; export const SETTING_GIT_COMMIT_MSG = 'git.commitMessage'; +export const SETTING_GIT_SUBMODULE_PULL = 'git.submodule.pull'; +export const SETTING_GIT_SUBMODULE_PUSH = 'git.submodule.push'; +export const SETTING_GIT_SUBMODULE_BRANCH = 'git.submodule.branch'; /** * Sponsors only settings diff --git a/src/listeners/general/GitListener.ts b/src/listeners/general/GitListener.ts index fa4da68b..9599233d 100644 --- a/src/listeners/general/GitListener.ts +++ b/src/listeners/general/GitListener.ts @@ -1,3 +1,8 @@ +import { + SETTING_GIT_SUBMODULE_BRANCH, + SETTING_GIT_SUBMODULE_PULL, + SETTING_GIT_SUBMODULE_PUSH +} from './../../constants/settings'; import { Settings } from './../../helpers/SettingsHelper'; import { Dashboard } from '../../commands/Dashboard'; import { ExplorerView } from '../../explorerView/ExplorerView'; @@ -5,6 +10,7 @@ import { ArticleHelper, Extension, Logger, + Notifications, processKnownPlaceholders, Telemetry } from '../../helpers'; @@ -95,6 +101,19 @@ export class GitListener { return; } + const submoduleBranch = Settings.get(SETTING_GIT_SUBMODULE_BRANCH); + const submodulePull = Settings.get(SETTING_GIT_SUBMODULE_PULL); + + if (submoduleBranch) { + Logger.info(`Checking out the branch ${submoduleBranch} for submodules`); + await git.subModule(['foreach', 'git', 'checkout', submoduleBranch]); + } + + if (submodulePull) { + Logger.info(`Pulling from remote with submodules`); + await git.subModule(['update', '--remote', '--merge']); + } + Logger.info(`Pulling from remote`); await git.pull(); } @@ -113,6 +132,28 @@ export class GitListener { return; } + const submodulePush = Settings.get(SETTING_GIT_SUBMODULE_PUSH); + + if (submodulePush) { + Logger.info(`Pushing to remote with submodules`); + + try { + await git.subModule(['foreach', 'git', 'add', '.', '-A']); + await git.subModule([ + 'foreach', + 'git', + 'commit', + '-m', + commitMsg || 'Synced by Front Matter' + ]); + await git.subModule(['foreach', 'git', 'push']); + } catch (e) { + Notifications.error(`Failed to push submodules. Please check the logs for more details.`); + Logger.error((e as Error).message); + return; + } + } + Logger.info(`Pushing to remote`); const status = await git.status();