Skip to content

Add the option to modify the repo root relative path #73

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 17 additions & 3 deletions src/helper/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ export const BUTTONS = [
];

export const SETTINGS_SCHEMA: SettingSchemaDesc[] = [
{
key: "customRepoRoot",
title: "Custom Repository Root Path",
type: "string",
default: "/",
description:
"Custom repository root, relative to the current graph root (can contain `..` path nodes). Defaults to /, which means the graph root.",
},
{
key: "buttons",
title: "Buttons",
Expand Down Expand Up @@ -150,13 +158,19 @@ export const SETTINGS_SCHEMA: SettingSchemaDesc[] = [
default: "Default Message With Date",
description: "Type of commit message to use",
enumPicker: "select",
enumChoices: ['Custom Message' , 'Default Message', 'Custom Message With Date', 'Default Message With Date'],
enumChoices: [
"Custom Message",
"Default Message",
"Custom Message With Date",
"Default Message With Date",
],
},
{
key: "customCommitMessage",
title: "Custom Commit Message",
type: "string",
default: "",
description: "Custom commit message for plugin (valid only if commit message is set to Custom Message)",
}
description:
"Custom commit message for plugin (valid only if commit message is set to Custom Message)",
},
];
143 changes: 81 additions & 62 deletions src/helper/git.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
// https://logseq.github.io/plugins/interfaces/IAppProxy.html#execGitCommand
import type { IGitResult } from "@logseq/libs/dist/LSPlugin.user"
import type { IGitResult } from "@logseq/libs/dist/LSPlugin.user";
//import path from "path";

let _inProgress: Promise<IGitResult> | undefined = undefined
let _inProgress: Promise<IGitResult> | undefined = undefined;

export const execGitCommand = async (args: string[]) : Promise<IGitResult> => {
if (_inProgress) await _inProgress
export const execGitCommand = async (args: string[]): Promise<IGitResult> => {
if (_inProgress) await _inProgress;

let res
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
const currentGitFolder = (await logseq.App.getCurrentGraph())?.path;
const relCurrGitFolder = currentGitFolder
Copy link
Author

@orgrinrt orgrinrt Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new constant (and using it instead of currentGitFolder on the git exec args) is essentially the only change other than the settings addition in src/helper/constants.ts

I apologise for the linter, didn't think to check the diffs on the fly before sending this PR.

Again, I'd be happy to clean it up if this seems worth a merge.

? //path.resolve(
(((currentGitFolder as string) +
"/" +
logseq.settings?.customRepoRoot) as string)
: //)
(logseq.settings?.customRepoRoot as string);
const runArgs = relCurrGitFolder ? ["-C", relCurrGitFolder, ...args] : args;
//const runArgs = currentGitFolder ? ["-C", currentGitFolder, ...args] : args;
_inProgress = logseq.Git.execCommand(runArgs);
res = await _inProgress;
} finally {
_inProgress = undefined
_inProgress = undefined;
}
return res
}
return res;
};

export const inProgress = () => _inProgress
export const inProgress = () => _inProgress;

export const status = async (showRes = true): Promise<IGitResult> => {
// git status --porcelain | awk '{print $2}'
// git status --porcelain | wc -l
const res = await execGitCommand(['status', '--porcelain'])
console.log('[faiz:] === git status', res)
const res = await execGitCommand(["status", "--porcelain"]);
console.log("[faiz:] === git status", res);
if (showRes) {
if (res.exitCode === 0) {
logseq.UI.showMsg('Git status success')
logseq.UI.showMsg("Git status success");
} else {
logseq.UI.showMsg(`Git status failed\n${res.stderr}`, 'error')
logseq.UI.showMsg(`Git status failed\n${res.stderr}`, "error");
}
}
/**
Expand All @@ -48,106 +57,115 @@ export const status = async (showRes = true): Promise<IGitResult> => {
* }
*/
// changed files staged files
return res
}
return res;
};

// log with git log --pretty=format:"%h %ad | %s%d [%an]" --date=short
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 execGitCommand(['log', '--pretty=format:"%h %ad | %s [%an]"', '--date=format:"%Y-%m-%d %H:%M:%S"', '--name-status'])
console.log('[faiz:] === git log', res)
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) {
logseq.UI.showMsg('Git log success')
logseq.UI.showMsg("Git log success");
} else {
logseq.UI.showMsg(`Git log failed\n${res.stderr}`, 'error')
logseq.UI.showMsg(`Git log failed\n${res.stderr}`, "error");
}
}
return res
}
return res;
};

// git pull
export const pull = async (showRes = true): Promise<IGitResult> => {
const res = await execGitCommand(['pull'])
console.log('[faiz:] === git pull', res)
const res = await execGitCommand(["pull"]);
console.log("[faiz:] === git pull", res);
if (showRes) {
if (res.exitCode === 0) {
logseq.UI.showMsg('Git pull success')
logseq.UI.showMsg("Git pull success");
} else {
logseq.UI.showMsg(`Git pull failed\n${res.stderr}`, 'error')
logseq.UI.showMsg(`Git pull failed\n${res.stderr}`, "error");
}
}
return res
}
return res;
};

// git pull --rebase
export const pullRebase = async (showRes = true): Promise<IGitResult> => {
const res = await execGitCommand(['pull', '--rebase'])
console.log('[faiz:] === git pull --rebase', res)
const res = await execGitCommand(["pull", "--rebase"]);
console.log("[faiz:] === git pull --rebase", res);
if (showRes) {
if (res.exitCode === 0) {
logseq.UI.showMsg('Git pull --rebase success')
logseq.UI.showMsg("Git pull --rebase success");
} else {
logseq.UI.showMsg(`Git pull --rebase failed\n${res.stderr}`, 'error')
logseq.UI.showMsg(`Git pull --rebase failed\n${res.stderr}`, "error");
}
}
return res
}
return res;
};

// git checkout .
export const checkout = async (showRes = true): Promise<IGitResult> => {
const res = await execGitCommand(['checkout', '.'])
console.log('[faiz:] === git checkout .', res)
const res = await execGitCommand(["checkout", "."]);
console.log("[faiz:] === git checkout .", res);
if (showRes) {
if (res.exitCode === 0) {
logseq.UI.showMsg('Git checkout success')
logseq.UI.showMsg("Git checkout success");
} else {
logseq.UI.showMsg(`Git checkout failed\n${res.stderr}`, 'error')
logseq.UI.showMsg(`Git checkout failed\n${res.stderr}`, "error");
}
}
return res
}
return res;
};

// git commit
export const commit = async (showRes = true, message: string): Promise<IGitResult> => {
await execGitCommand(['add', '.'])
export const commit = async (
showRes = true,
message: string,
): Promise<IGitResult> => {
await execGitCommand(["add", "."]);
// git commit -m "message"
const res = await execGitCommand(['commit', '-m', message])
console.log('[faiz:] === git commit', res)
const res = await execGitCommand(["commit", "-m", message]);
console.log("[faiz:] === git commit", res);
if (showRes) {
if (res.exitCode === 0) {
logseq.UI.showMsg('Git commit success')
logseq.UI.showMsg("Git commit success");
} else {
logseq.UI.showMsg(`Git commit failed\n${res.stdout || res.stderr}`, 'error')
logseq.UI.showMsg(
`Git commit failed\n${res.stdout || res.stderr}`,
"error",
);
}
}
return res
}
return res;
};

// push
export const push = async (showRes = true): Promise<IGitResult> => {
// git push
const res = await execGitCommand(['push'])
console.log('[faiz:] === git push', res)
const res = await execGitCommand(["push"]);
console.log("[faiz:] === git push", res);
if (showRes) {
if (res.exitCode === 0) {
logseq.UI.showMsg('Git push success')
logseq.UI.showMsg("Git push success");
} else {
logseq.UI.showMsg(`Git push failed\n${res.stderr}`, 'error')
logseq.UI.showMsg(`Git push failed\n${res.stderr}`, "error");
}
}
return res
}

return res;
};

/**
* Returns the commit message based on the selected commit message type in the logseq settings.
* @returns The commit message.
*/
export const commitMessage = () : string => {

export const commitMessage = (): string => {
let defaultMessage = "[logseq-plugin-git:commit]";

switch (logseq.settings?.typeCommitMessage as string) {
Expand All @@ -163,7 +181,8 @@ export const commitMessage = () : string => {
return customMessage;
}
case "Custom Message With Date":
let customMessageWithDate = logseq.settings?.customCommitMessage as string;
let customMessageWithDate = logseq.settings
?.customCommitMessage as string;
if (customMessageWithDate.trim() === "") {
return defaultMessage + " " + new Date().toISOString();
} else {
Expand All @@ -172,4 +191,4 @@ export const commitMessage = () : string => {
default:
return defaultMessage;
}
}
};
Loading