Skip to content

Commit 0530c09

Browse files
committed
feat: add debug logging for git commands
- Add setGitDebug() to enable/disable logging - Log git commands and results to console.debug - Add debugLog setting with UI toggle - Add i18n translations for debug log setting
1 parent 9739f1a commit 0530c09

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

main.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/git.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,41 @@ interface GitRunOptions {
88
args: string[];
99
}
1010

11+
let debugEnabled = false;
12+
13+
export function setGitDebug(enabled: boolean): void {
14+
debugEnabled = enabled;
15+
}
16+
17+
function log(message: string, ...args: unknown[]): void {
18+
if (!debugEnabled) return;
19+
globalThis.console.debug(`[auto-git] ${message}`, ...args);
20+
}
21+
22+
function logCmd(args: string[]): void {
23+
if (!debugEnabled) return;
24+
globalThis.console.debug(`[auto-git] > git ${args.join(" ")}`);
25+
}
26+
1127
function runGitSync({ cwd, gitPath, args }: GitRunOptions): string {
12-
return execFileSync(gitPath, args, {
13-
cwd,
14-
windowsHide: true,
15-
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
16-
encoding: "utf8",
17-
});
28+
logCmd(args);
29+
try {
30+
const result = execFileSync(gitPath, args, {
31+
cwd,
32+
windowsHide: true,
33+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
34+
encoding: "utf8",
35+
});
36+
log("ok");
37+
return result;
38+
} catch (e) {
39+
log("error:", (e as Error).message);
40+
throw e;
41+
}
1842
}
1943

2044
function runGit({ cwd, gitPath, args }: GitRunOptions): Promise<string> {
45+
logCmd(args);
2146
return new Promise((resolve, reject) => {
2247
execFile(
2348
gitPath,
@@ -29,9 +54,11 @@ function runGit({ cwd, gitPath, args }: GitRunOptions): Promise<string> {
2954
},
3055
(err, stdout, stderr) => {
3156
if (err) {
57+
log("error:", stderr?.trim() || err.message);
3258
reject(new Error(stderr?.trim() || err.message));
3359
return;
3460
}
61+
log("ok");
3562
resolve(stdout);
3663
}
3764
);

src/i18n.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ type Translations = {
3030
ignoreObsidianName: string;
3131
ignoreObsidianDesc: string;
3232

33+
debugLogName: string;
34+
debugLogDesc: string;
35+
3336
includeFileListName: string;
3437
includeFileListDesc: string;
3538

@@ -159,6 +162,9 @@ const en: Translations = {
159162
ignoreObsidianName: "Ignore config directory",
160163
ignoreObsidianDesc: "Exclude Obsidian config folder from triggering auto-commits.",
161164

165+
debugLogName: "Debug logging",
166+
debugLogDesc: "Log git commands to console (Ctrl+Shift+I to view).",
167+
162168
includeFileListName: "Include file list in commit body",
163169
includeFileListDesc: "List changed files in commit message body, one per line.",
164170

@@ -283,6 +289,9 @@ const zhCN: Translations = {
283289
ignoreObsidianName: "忽略配置目录",
284290
ignoreObsidianDesc: "排除 Obsidian 配置文件夹触发自动提交。",
285291

292+
debugLogName: "调试日志",
293+
debugLogDesc: "将 git 命令输出到控制台(Ctrl+Shift+I 查看)。",
294+
286295
includeFileListName: "在提交正文中包含文件列表",
287296
includeFileListDesc: "在提交消息正文中列出变动的文件,每行一个。",
288297

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventRef, Menu, Notice, Platform, Plugin, TAbstractFile, TFile, FileSystemAdapter } from "obsidian";
22
import { AutoGitSettings, AutoGitSettingTab, DEFAULT_SETTINGS } from "./settings";
3-
import { getChangedFiles, commitAll, push, pull, getConflictFiles, markConflictsResolved, revertAll, revertFile, getChangedFilesSync, commitSyncAndPushDetached } from "./git";
3+
import { getChangedFiles, commitAll, push, pull, getConflictFiles, markConflictsResolved, revertAll, revertFile, getChangedFilesSync, commitSyncAndPushDetached, setGitDebug } from "./git";
44
import { renderTemplate } from "./template";
55
import { t } from "./i18n";
66
import { RevertConfirmModal } from "./modals";
@@ -111,6 +111,7 @@ export default class AutoGitPlugin extends Plugin {
111111

112112
async loadSettings() {
113113
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData() as Partial<AutoGitSettings>);
114+
setGitDebug(this.settings.debugLog);
114115
}
115116

116117
async saveSettings() {

src/settings.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { App, Notice, Platform, PluginSettingTab, Setting } from "obsidian";
22
import type AutoGitPlugin from "./main";
33
import { t } from "./i18n";
4-
import { isGitRepo, initRepo, getRemoteUrl, setRemoteUrl, hasConflicts, markConflictsResolved, detectRepoState, RepoState, connectToRemote, initAndPush, setUpstream } from "./git";
4+
import { isGitRepo, initRepo, getRemoteUrl, setRemoteUrl, hasConflicts, markConflictsResolved, detectRepoState, RepoState, connectToRemote, initAndPush, setUpstream, setGitDebug } from "./git";
55

66
export interface AutoGitSettings {
77
autoCommit: boolean;
@@ -16,6 +16,7 @@ export interface AutoGitSettings {
1616
showStatusBadge: boolean;
1717
showRibbonButton: boolean;
1818
badgeRefreshInterval: number; // 0 = disabled, otherwise seconds
19+
debugLog: boolean;
1920
}
2021

2122
export const DEFAULT_SETTINGS: AutoGitSettings = {
@@ -31,6 +32,7 @@ export const DEFAULT_SETTINGS: AutoGitSettings = {
3132
showStatusBadge: true,
3233
showRibbonButton: true,
3334
badgeRefreshInterval: 0, // 0 = disabled (event-driven only), otherwise seconds
35+
debugLog: false,
3436
};
3537

3638
export class AutoGitSettingTab extends PluginSettingTab {
@@ -216,6 +218,17 @@ export class AutoGitSettingTab extends PluginSettingTab {
216218
await this.plugin.saveSettings();
217219
})
218220
);
221+
222+
new Setting(containerEl)
223+
.setName(i18n.debugLogName)
224+
.setDesc(i18n.debugLogDesc)
225+
.addToggle((toggle) =>
226+
toggle.setValue(this.plugin.settings.debugLog).onChange(async (value) => {
227+
this.plugin.settings.debugLog = value;
228+
setGitDebug(value);
229+
await this.plugin.saveSettings();
230+
})
231+
);
219232
}
220233

221234
private async displaySetupSection(container: HTMLElement): Promise<void> {

0 commit comments

Comments
 (0)