Skip to content

feat: adding a setting for a tracked directory #894

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const DEFAULT_SETTINGS: ObsidianGitSettings = {
authorInHistoryView: "hide",
dateInHistoryView: false,
diffStyle: "split",
trackedDirectory: "",
lineAuthor: {
show: false,
followMovement: "inactive",
Expand Down
12 changes: 11 additions & 1 deletion src/gitManager/isomorphicGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,12 +862,22 @@ export class IsomorphicGit extends GitManager {
walkers: Walker[];
dir?: string;
}): Promise<WalkDifference[]> {
const trackedDir = this.plugin.settings.trackedDirectory;

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const res = await this.wrapFS(
git.walk({
...this.getRepo(),
trees: walkers,
map: async function (filepath, [A, B]) {
// Check if file is in tracked directory (if specified)
if (trackedDir && trackedDir.length > 0 &&
filepath !== trackedDir &&
!filepath.startsWith(trackedDir + "/")) {
return null;
}

// Original check with base path
if (!worthWalking(filepath, base)) {
return null;
}
Expand Down Expand Up @@ -960,7 +970,7 @@ export class IsomorphicGit extends GitManager {
}
}
// match against base path
if (!worthWalking(filepath, base)) {
if (!worthWalking(filepath, base, this.plugin.settings.trackedDirectory)) {
return null;
}
// Late filter against file names
Expand Down
89 changes: 63 additions & 26 deletions src/gitManager/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,25 +241,35 @@ export class SimpleGit extends GitManager {
const status = await this.git.status();
this.plugin.setPluginState({ gitAction: CurrentGitAction.idle });

const allFilesFormatted = status.files.map<FileStatusResult>((e) => {
const res = this.formatPath(e);
return {
path: res.path,
from: res.from,
index: e.index === "?" ? "U" : e.index,
workingDir: e.working_dir === "?" ? "U" : e.working_dir,
vaultPath: this.getRelativeVaultPath(res.path),
};
});
// Filter files by tracked directory if specified
const trackedDir = this.plugin.settings.trackedDirectory;
const filterByTrackedDir = (path: string) => {
if (!trackedDir || trackedDir.length === 0) return true;
return path === trackedDir || path.startsWith(trackedDir + "/");
};

const allFilesFormatted = status.files
.filter(e => filterByTrackedDir(e.path))
.map<FileStatusResult>((e) => {
const res = this.formatPath(e);
return {
path: res.path,
from: res.from,
index: e.index === "?" ? "U" : e.index,
workingDir: e.working_dir === "?" ? "U" : e.working_dir,
vaultPath: this.getRelativeVaultPath(res.path),
};
});

return {
all: allFilesFormatted,
changed: allFilesFormatted.filter((e) => e.workingDir !== " "),
staged: allFilesFormatted.filter(
(e) => e.index !== " " && e.index != "U"
),
conflicted: status.conflicted.map(
(path) => this.formatPath({ path }).path
),
conflicted: status.conflicted
.filter(filterByTrackedDir)
.map((path) => this.formatPath({ path }).path),
};
}

Expand Down Expand Up @@ -409,7 +419,16 @@ export class SimpleGit extends GitManager {
}
this.plugin.setPluginState({ gitAction: CurrentGitAction.add });

await this.git.add("-A");
// Add only files that match the tracked directory
if (this.plugin.settings.trackedDirectory) {
const trackedDir = this.plugin.settings.trackedDirectory;

// We could use git add with path to only add tracked directory
await this.git.add([trackedDir]);
} else {
// Add all files if no tracked directory is specified
await this.git.add("-A");
}

this.plugin.setPluginState({ gitAction: CurrentGitAction.commit });

Expand Down Expand Up @@ -502,7 +521,7 @@ export class SimpleGit extends GitManager {
}

async discardAll({ dir }: { dir?: string }): Promise<void> {
return this.discard(dir ?? ".");
return this.discard(dir ?? (this.plugin.settings.trackedDirectory ? this.plugin.settings.trackedDirectory : "."));
}

async pull(): Promise<FileStatusResult[] | undefined> {
Expand Down Expand Up @@ -577,16 +596,23 @@ export class SimpleGit extends GitManager {
"--name-only",
]);

return filesChanged
const files = filesChanged
.split(/\r\n|\r|\n/)
.filter((value) => value.length > 0)
.map((e) => {
return <FileStatusResult>{
path: e,
workingDir: "P",
vaultPath: this.getRelativeVaultPath(e),
};
});
.filter((value) => value.length > 0);

// Filter by tracked directory
const trackedDir = this.plugin.settings.trackedDirectory;
const filteredFiles = trackedDir && trackedDir.length > 0
? files.filter(file => file === trackedDir || file.startsWith(trackedDir + "/"))
: files;

return filteredFiles.map<FileStatusResult>((e) => {
return <FileStatusResult>{
path: e,
workingDir: "P",
vaultPath: this.getRelativeVaultPath(e),
};
});
} else {
return [];
}
Expand Down Expand Up @@ -624,6 +650,7 @@ export class SimpleGit extends GitManager {
currentBranch,
trackingBranch,
"--",
...(this.plugin.settings.trackedDirectory ? [this.plugin.settings.trackedDirectory] : [])
])
).changed;

Expand All @@ -645,7 +672,12 @@ export class SimpleGit extends GitManager {
}

const remoteChangedFiles = (
await this.git.diffSummary([currentBranch, trackingBranch, "--"])
await this.git.diffSummary([
currentBranch,
trackingBranch,
"--",
...(this.plugin.settings.trackedDirectory ? [this.plugin.settings.trackedDirectory] : [])
])
).changed;

return remoteChangedFiles;
Expand All @@ -663,7 +695,12 @@ export class SimpleGit extends GitManager {
return false;
}
const remoteChangedFiles = (
await this.git.diffSummary([currentBranch, trackingBranch, "--"])
await this.git.diffSummary([
currentBranch,
trackingBranch,
"--",
...(this.plugin.settings.trackedDirectory ? [this.plugin.settings.trackedDirectory] : [])
])
).changed;

return remoteChangedFiles !== 0;
Expand Down
14 changes: 14 additions & 0 deletions src/setting/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,20 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName("Tracked directory within repository")
.setDesc("Specify a directory within your repository to track. Changes outside this directory will be ignored. Leave empty to track the entire repository.")
.addText((cb) => {
cb.setValue(plugin.settings.trackedDirectory);
cb.setPlaceholder("directory/subdirectory");
cb.onChange(async (value) => {
plugin.settings.trackedDirectory = value;
await plugin.saveSettings();
// Trigger a refresh to update the UI
plugin.refresh().catch((e) => plugin.displayError(e));
});
});

new Setting(containerEl).setName("Support").setHeading();
new Setting(containerEl)
.setName("Donate")
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface ObsidianGitSettings {
authorInHistoryView: ShowAuthorInHistoryView;
dateInHistoryView: boolean;
diffStyle: "git_unified" | "split";
trackedDirectory: string; // Directory within repo to track, empty means track all
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import type { App, RGB, WorkspaceLeaf } from "obsidian";
import { Keymap, Menu, moment, TFile } from "obsidian";
import { BINARY_EXTENSIONS } from "./constants";

export const worthWalking = (filepath: string, root?: string) => {
export const worthWalking = (filepath: string, root?: string, trackedDir?: string) => {
// First check if the file is in the tracked directory, if specified
console.log("check tracked", trackedDir)
if (trackedDir && trackedDir.length > 0 && !filepath.startsWith(trackedDir) && filepath !== trackedDir) {
console.log("not tracked", filepath)
return false;
}

// Then do the original check
if (filepath === "." || root == null || root.length === 0 || root === ".") {
return true;
}
Expand Down
Loading