Skip to content

Commit

Permalink
fix: stack git operations not to get errors when auto actions are alr…
Browse files Browse the repository at this point in the history
…eady performing

fix: commitAndPush was showing error if there were no changes to be committed
fix: #34 will use correct git folder if multiple windows are open
  • Loading branch information
k2s authored and haydenull committed Jul 26, 2023
1 parent f13d355 commit e78bb2c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
37 changes: 28 additions & 9 deletions src/helper/git.ts
Original file line number Diff line number Diff line change
@@ -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<IGitResult> | undefined = undefined

export const execGitCommand = async (args: string[]) : Promise<IGitResult> => {
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<IGitResult> => {
// 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) {
Expand Down Expand Up @@ -37,7 +56,7 @@ export const log = async (showRes = true): Promise<IGitResult> => {
// 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) {
Expand All @@ -51,7 +70,7 @@ export const log = async (showRes = true): Promise<IGitResult> => {

// git pull
export const pull = async (showRes = true): Promise<IGitResult> => {
const res = await logseq.Git.execCommand(['pull'])
const res = await execGitCommand(['pull'])
console.log('[faiz:] === git pull', res)
if (showRes) {
if (res.exitCode === 0) {
Expand All @@ -65,7 +84,7 @@ export const pull = async (showRes = true): Promise<IGitResult> => {

// git pull --rebase
export const pullRebase = async (showRes = true): Promise<IGitResult> => {
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) {
Expand All @@ -79,7 +98,7 @@ export const pullRebase = async (showRes = true): Promise<IGitResult> => {

// git checkout .
export const checkout = async (showRes = true): Promise<IGitResult> => {
const res = await logseq.Git.execCommand(['checkout', '.'])
const res = await execGitCommand(['checkout', '.'])
console.log('[faiz:] === git checkout .', res)
if (showRes) {
if (res.exitCode === 0) {
Expand All @@ -93,9 +112,9 @@ export const checkout = async (showRes = true): Promise<IGitResult> => {

// git commit
export const commit = async (showRes = true, message: string): Promise<IGitResult> => {
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) {
Expand All @@ -110,7 +129,7 @@ export const commit = async (showRes = true, message: string): Promise<IGitResul
// push
export const push = async (showRes = true): Promise<IGitResult> => {
// 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) {
Expand All @@ -120,4 +139,4 @@ export const push = async (showRes = true): Promise<IGitResult> => {
}
}
return res
}
}
14 changes: 10 additions & 4 deletions src/helper/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...");
Expand Down Expand Up @@ -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(
Expand Down
19 changes: 11 additions & 8 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
}
}
});
Expand Down

0 comments on commit e78bb2c

Please sign in to comment.