Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git fixes #35

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"preview": "vite preview"
},
"dependencies": {
"@logseq/libs": "^0.0.14",
"@logseq/libs": "^0.0.15",
"antd": "^4.18.9",
"react": "^17.0.2",
"react-dom": "^17.0.2"
Expand Down
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
30 changes: 22 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 All @@ -187,6 +190,17 @@ if (isDevelopment) {
},
() => operations.commitAndPush()
);
logseq.App.registerCommandPalette(
{
key: "logseq-plugin-git:rebase",
label: "Pull Rebase",
keybinding: {
binding: "mod+alt+s",
mode: "global",
},
},
() => operations.pullRebase()
);
});
}

Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@logseq/libs@^0.0.14":
version "0.0.14"
resolved "https://registry.yarnpkg.com/@logseq/libs/-/libs-0.0.14.tgz#2fbce790d61c28e124063a20153f748f90ffa352"
integrity sha512-QcNeVxb4LIvV4Tid0ABZXV7fxYdZHynzLlukSk6Ydkuus+hBzLcjfK15nzybIRbiV7ANqSgTooDZkV/E4WP57Q==
"@logseq/libs@^0.0.15":
version "0.0.15"
resolved "https://registry.yarnpkg.com/@logseq/libs/-/libs-0.0.15.tgz#03b8b81a0a1557e3f022d68c978ddcb501cd4421"
integrity sha512-Z4YrYGfu8Y3s9LTqVnPGkxlO+KZtP1YalH/A63zYgxP61cV5QtKlnHWNcjsKxsD5CkaSL4MlSN4mf7wNx/Fm0A==
dependencies:
csstype "3.1.0"
debug "4.3.4"
Expand Down Expand Up @@ -652,10 +652,10 @@
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==

"@types/node@^17.0.9":
version "17.0.9"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.9.tgz#0b7f161afb5b1cc12518d29b2cdc7175d5490628"
integrity sha512-5dNBXu/FOER+EXnyah7rn8xlNrfMOQb/qXnw4NQgLkCygKBKhdmF/CA5oXVOKZLBEahw8s2WP9LxIcN/oDDRgQ==
"@types/node@^18.15.12":
version "18.17.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.17.0.tgz#35d44267a33dd46b49ee0f73d31b05fd7407e290"
integrity sha512-GXZxEtOxYGFchyUzxvKI14iff9KZ2DI+A6a37o6EQevtg6uO9t+aUZKcaC1Te5Ng1OnLM7K9NVVj+FbecD9cJg==

"@types/normalize-package-data@^2.4.0":
version "2.4.1"
Expand Down
Loading