diff --git a/src/helper/git.ts b/src/helper/git.ts index 4acc676..063de61 100644 --- a/src/helper/git.ts +++ b/src/helper/git.ts @@ -1,10 +1,29 @@ // https://logseq.github.io/plugins/interfaces/IAppProxy.html#execGitCommand import type { IGitResult } from "@logseq/libs/dist/LSPlugin.user" +let _inProgress: Promise | undefined = undefined + +export const execGitCommand = async (args: string[]) : Promise => { + if (_inProgress) await _inProgress + + let res + try { + const currentGitFolder = (await logseq.App.getCurrentGraph())?.path + const runArgs = currentGitFolder ? ['-C', currentGitFolder, ...args] : args + _inProgress = logseq.Git.execCommand(runArgs) + res = await _inProgress + } finally { + _inProgress = undefined + } + return res +} + +export const inProgress = () => true + export const status = async (showRes = true): Promise => { // git status --porcelain | awk '{print $2}' // git status --porcelain | wc -l - const res = await logseq.Git.execCommand(['status', '--porcelain']) + const res = await execGitCommand(['status', '--porcelain']) console.log('[faiz:] === git status', res) if (showRes) { if (res.exitCode === 0) { @@ -37,7 +56,7 @@ export const log = async (showRes = true): Promise => { // git log --pretty=format:"%h %s" -n 1 // git log --pretty=format:"%h %ad | %s%d [%an]" --date=short // return await logseq.App.execGitCommand(['log', '--pretty=format:"%h %s"']) - const res = await logseq.Git.execCommand(['log', '--pretty=format:"%h %ad | %s [%an]"', '--date=format:"%Y-%m-%d %H:%M:%S"', '--name-status']) + const res = await execGitCommand(['log', '--pretty=format:"%h %ad | %s [%an]"', '--date=format:"%Y-%m-%d %H:%M:%S"', '--name-status']) console.log('[faiz:] === git log', res) if (showRes) { if (res.exitCode === 0) { @@ -51,7 +70,7 @@ export const log = async (showRes = true): Promise => { // git pull export const pull = async (showRes = true): Promise => { - const res = await logseq.Git.execCommand(['pull']) + const res = await execGitCommand(['pull']) console.log('[faiz:] === git pull', res) if (showRes) { if (res.exitCode === 0) { @@ -65,7 +84,7 @@ export const pull = async (showRes = true): Promise => { // git pull --rebase export const pullRebase = async (showRes = true): Promise => { - const res = await logseq.Git.execCommand(['pull', '--rebase']) + const res = await execGitCommand(['pull', '--rebase']) console.log('[faiz:] === git pull --rebase', res) if (showRes) { if (res.exitCode === 0) { @@ -79,7 +98,7 @@ export const pullRebase = async (showRes = true): Promise => { // git checkout . export const checkout = async (showRes = true): Promise => { - const res = await logseq.Git.execCommand(['checkout', '.']) + const res = await execGitCommand(['checkout', '.']) console.log('[faiz:] === git checkout .', res) if (showRes) { if (res.exitCode === 0) { @@ -93,9 +112,9 @@ export const checkout = async (showRes = true): Promise => { // git commit export const commit = async (showRes = true, message: string): Promise => { - await logseq.Git.execCommand(['add', '.']) + await execGitCommand(['add', '.']) // git commit -m "message" - const res = await logseq.Git.execCommand(['commit', '-m', message]) + const res = await execGitCommand(['commit', '-m', message]) console.log('[faiz:] === git commit', res) if (showRes) { if (res.exitCode === 0) { @@ -110,7 +129,7 @@ export const commit = async (showRes = true, message: string): Promise => { // git push - const res = await logseq.Git.execCommand(['push']) + const res = await execGitCommand(['push']) console.log('[faiz:] === git push', res) if (showRes) { if (res.exitCode === 0) { @@ -120,4 +139,4 @@ export const push = async (showRes = true): Promise => { } } return res -} \ No newline at end of file +} diff --git a/src/helper/util.ts b/src/helper/util.ts index 4405a82..5269f15 100644 --- a/src/helper/util.ts +++ b/src/helper/util.ts @@ -4,7 +4,7 @@ import { INACTIVE_STYLE, SHOW_POPUP_STYLE, } from "./constants"; -import { status } from "./git"; +import { status, inProgress, execGitCommand } from "./git"; export const checkStatus = async () => { console.log("Checking status..."); @@ -69,13 +69,19 @@ export const checkStatusWithDebounce = debounce(() => { }, 2000); export const isRepoUpTodate = async () => { - await logseq.Git.execCommand(["fetch"]); - const local = await logseq.Git.execCommand(["rev-parse", "HEAD"]); - const remote = await logseq.Git.execCommand(["rev-parse", "@{u}"]); + await execGitCommand(["fetch"]); + const local = await execGitCommand(["rev-parse", "HEAD"]); + const remote = await execGitCommand(["rev-parse", "@{u}"]); + logseq.UI.showMsg(`${local.stdout} === ${remote.stdout}`, "success", { timeout: 30 }); return local.stdout === remote.stdout; }; export const checkIsSynced = async () => { + if (inProgress()) { + console.log("[faiz:] === checkIsSynced Git in progress, skip check"); + return + } + const isSynced = await isRepoUpTodate(); if (!isSynced) logseq.UI.showMsg( diff --git a/src/main.tsx b/src/main.tsx index b8e90e6..188f604 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -75,11 +75,16 @@ if (isDevelopment) { commitAndPush: debounce(async function () { setPluginStyle(LOADING_STYLE); hidePopup(); - const res = await commit( - true, - `[logseq-plugin-git:commit] ${new Date().toISOString()}` - ); - if (res.exitCode === 0) await push(true); + + const status = await checkStatus(); + const changed = status?.stdout !== ""; + if (changed) { + const res = await commit( + true, + `[logseq-plugin-git:commit] ${new Date().toISOString()}` + ); + if (res.exitCode === 0) await push(true); + } checkStatus(); }), log: debounce(async function () { @@ -168,9 +173,7 @@ if (isDevelopment) { // noChange void // changed commit push if (logseq.settings?.autoPush) { - const status = await checkStatus(); - const changed = status?.stdout !== ""; - if (changed) operations.commitAndPush(); + operations.commitAndPush(); } } });