feat: add option to convert ExcludeFilesAndFolders to include list

This commit is contained in:
shumadrid 2025-04-03 01:11:27 +02:00
parent b7ea0b94be
commit fd92aea22a
7 changed files with 73 additions and 12 deletions

View file

@ -3,8 +3,10 @@ export const PLUGIN_NAME_SENTENCE_CASE = 'Git changelog';
export const MIN_COMPATIBLE_GIT_PLUGIN_VERSION = '2.31.1';
export const MAX_TESTED_GIT_PLUGIN_VERSION = '2.32.1';
// Strings
export const FEEDBACK_URL =
'https://github.com/shumadrid/obsidian-git-changelog/issues';
export const EXCLUDE_FILES_AND_FOLDERS = 'Exclude files and folders';
// Icons
export const ADDITIONS_ICON = 'list-plus';

View file

@ -69,6 +69,12 @@ export class VaultChangelogManager extends ChangelogManager<VaultChangelogEntry>
) {
return true;
}
if (
oldSettings.vaultChangelogGenerationSettings.convertToInclude !==
newSettings.vaultChangelogGenerationSettings.convertToInclude
) {
return true;
}
return super.generationSettingsChanged(oldSettings, newSettings);
}

View file

@ -74,7 +74,9 @@ export async function runRepoDiff({
}
const pathSpec = convertGitIgnoreToPathspec(
plugin.settings.vaultChangelogGenerationSettings.excludeFilesAndFoldersLines
plugin.settings.vaultChangelogGenerationSettings
.excludeFilesAndFoldersLines,
plugin.settings.vaultChangelogGenerationSettings.convertToInclude
);
const numstatArguments = [

View file

@ -35,6 +35,7 @@ export interface ChangelogGenerationSettings {
export interface VaultChangelogGenerationSettings {
excludeFilesAndFoldersLines: string[];
convertToInclude: boolean;
interval: ChangelogInterval;
}
@ -119,6 +120,7 @@ export class GitChangelogPluginSettings
export const DEFAULT_VAULT_CHANGELOG_GENERATION_SETTINGS: VaultChangelogGenerationSettings =
{
excludeFilesAndFoldersLines: [],
convertToInclude: false,
interval: ChangelogInterval.Daily
} as const;

View file

@ -4,6 +4,7 @@ import { PluginSettingsTabBase } from 'obsidian-dev-utils/obsidian/Plugin/Plugin
import { CustomLocale } from 'settings/ui/CustomLocale.ts';
import { DiffAlgorithmOptions } from 'settings/ui/DiffAlgorithmOptions.ts';
import { IgnoreBlankLinesToggle } from 'settings/ui/IgnoreBlankLinesToggle.ts';
import { IncludeItemsToggle } from 'settings/ui/IncludeItemsToggle.ts';
import { MiscellaneousButtons } from 'settings/ui/MiscellaneousButtons.ts';
import { WhitespaceIgnoreModeOptions } from 'settings/ui/WhitespaceIgnoreMode.ts';
import { WhitespaceSettingsHeading } from 'settings/ui/WhitespaceSettingsHeading.ts';
@ -69,6 +70,11 @@ export class GitChangelogSettingsTab extends PluginSettingsTabBase<GitChangelogP
// ).display();
new ExcludeFilesAndFolders({ containerEl, plugin }).display();
new IncludeItemsToggle({
containerEl,
plugin
}).display();
new RenameDetectionStrictnessSlider({
containerEl,
plugin

View file

@ -2,6 +2,7 @@
import type { ReadonlyDeep } from 'type-fest';
import { EXCLUDE_FILES_AND_FOLDERS } from 'constants.ts';
import { GitChangelogSetting } from 'settings/components/setting.ts';
import { DEFAULT_SETTINGS } from 'settings/settings.ts';
@ -10,7 +11,7 @@ export class ExcludeFilesAndFolders extends GitChangelogSetting {
this.createSetting()
.setName(
createFragment((fragment) => {
fragment.appendText(`Exclude files and folders`);
fragment.appendText(EXCLUDE_FILES_AND_FOLDERS);
fragment
.createEl('span', {
cls: 'nav-file-tag git-changelog-experimental'
@ -86,11 +87,14 @@ export function parseGitIgnoreLine(gitIgnoreLine: string): string {
}
export function convertGitIgnoreToPathspec(
gitIgnoreRules: ReadonlyDeep<string[]>
gitIgnoreRules: ReadonlyDeep<string[]>,
include: boolean
): string[] {
try {
// Always exclude .git directory
const excludes: string[] = [':(exclude,glob)**/.git/**'];
const items: string[] = [];
// eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare
const excludePrefix = include === true ? '' : 'exclude,';
for (const line of gitIgnoreRules.map((element) =>
parseGitIgnoreLine(element)
@ -104,21 +108,20 @@ export function convertGitIgnoreToPathspec(
// Handle patterns differently based on their format:
if (line.endsWith('/*')) {
// Single-level match
excludes.push(`:(exclude,glob)${formatted}`);
items.push(`:(${excludePrefix}glob)${formatted}`);
} else if (line.endsWith('/**') || line.endsWith('/')) {
// Recursive match
excludes.push(`:(exclude,glob)${formatted}`);
items.push(`:(${excludePrefix}glob)${formatted}`);
} else {
// All other patterns - match both item and contents
// Regardless of whether they contain special characters
excludes.push(
`:(exclude,glob)${formatted}`,
`:(exclude,glob)${formatted}/**`
items.push(
`:(${excludePrefix}glob)${formatted}`,
`:(${excludePrefix}glob)${formatted}/**`
);
}
}
return excludes;
return items;
} catch {
return [];
}

View file

@ -0,0 +1,40 @@
import { EXCLUDE_FILES_AND_FOLDERS } from 'constants.ts';
import { appendCodeBlock } from 'obsidian-dev-utils/HTMLElement';
import { GitChangelogSetting } from 'settings/components/setting.ts';
export class IncludeItemsToggle extends GitChangelogSetting {
public display(): void {
this.createSetting()
.setName(
createFragment((fragment) => {
fragment.appendText(`Include items`);
fragment
.createEl('span', { cls: 'nav-file-tag git-changelog-new' })
.setText('NEW');
})
)
.setDesc(
createFragment((fragment) => {
fragment.appendText('Include the items listed in ');
appendCodeBlock(fragment, EXCLUDE_FILES_AND_FOLDERS);
fragment.appendText(
` instead of excluding them, and exclude everything else.`
);
})
)
.addToggle((toggle) =>
toggle
.setValue(
this.plugin.settings.vaultChangelogGenerationSettings
.convertToInclude
)
.onChange((value) => {
const newSettings = this.plugin.settingsClone;
newSettings.vaultChangelogGenerationSettings.convertToInclude =
value;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.plugin.saveSettings(newSettings);
})
);
}
}