+
Skip to content
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
15 changes: 13 additions & 2 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { DEFAULT_CUSTOM_LIST_ALL } from "./utils/constants";
import { registerGithubApi } from "./utils/github";
import { extensionStorage } from "./utils/storage";

registerGithubApi();

browser.runtime.onInstalled.addListener(({ reason }) => {
if (reason === "install") browser.runtime.openOptionsPage();
browser.runtime.onInstalled.addListener(async ({ reason }) => {
if (reason === "install") {
await initalizeStorage();
void browser.runtime.openOptionsPage();
}
});

async function initalizeStorage() {
await extensionStorage.setItem("customLists", {
all: DEFAULT_CUSTOM_LIST_ALL,
});
}
24 changes: 24 additions & 0 deletions src/components/CustomListItem.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script lang="ts" setup>
const props = defineProps<{
value: string;
}>();

const emits = defineEmits<{
(event: "update:value", newValue: string): void;
}>();
</script>

<template>
<div class="rounded border divide-y">
<div class="py-2 px-2.5 font-bold"><slot /></div>

<textarea
class="font-mono p-2 w-full resize-y m-0 outline-none -mb-1 min-h-[5rem]"
:placeholder="'Enter glob patterns:\n*.lock\n**/vendor/**'"
:value="value"
@input="
emits('update:value', ($event.target as HTMLTextAreaElement).value)
"
/>
</div>
</template>
47 changes: 47 additions & 0 deletions src/components/CustomListsPref.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<script lang="ts" setup>
import { CustomLists } from "../utils/storage";
import CustomListItem from "./CustomListItem.vue";

const props = defineProps<{
customLists: CustomLists;
}>();

const emits = defineEmits<{
(event: "update:customLists", newValue: CustomLists): void;
}>();

const customLists = useVModel(props, "customLists", emits);
const all = computed({
get() {
return customLists.value.all;
},
set(newAll) {
customLists.value = {
...customLists.value,
all: newAll,
};
},
});
</script>

<template>
<li class="flex flex-col gap-4">
<!-- Header -->
<div class="flex flex-col gap-2">
<p class="font-medium text-base-content text-lg">Custom Lists</p>
<p class="text-base">
Use
<a
class="link link-secondary"
href="https://github.com/isaacs/minimatch#features"
target="_blank"
>minimatch</a
>
glob patterns to mark files as generated accross all repos.
</p>
</div>

<!-- All Repos -->
<CustomListItem v-model:value="all">All Repos</CustomListItem>
</li>
</template>
16 changes: 13 additions & 3 deletions src/components/OptionsForm.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
<script lang="ts" setup>
import TokenPref from "./TokenPref.vue";
import ShowGeneratedCountPref from "./ShowGeneratedCountPref.vue";
import { extensionStorage } from "../utils/storage";
import CustomListsPref from "./CustomListsPref.vue";
import { CustomLists, extensionStorage } from "../utils/storage";
import { commitHashDiffsCache } from "../utils/global-cache";

const { state, hasChanges, reset, saveChanges } = useForm(
const { state, hasChanges, reset, saveChanges } = useForm<{
hideGeneratedLineCount: boolean;
githubPat: string;
customLists: CustomLists;
}>(
{
hideGeneratedLineCount: !!(await extensionStorage.getItem(
"hideGeneratedLineCount",
)),
githubPat: (await extensionStorage.getItem("githubPat")) ?? "",
// This value is set when extension is installed.
customLists: (await extensionStorage.getItem("customLists"))!,
},
async (newState) => {
await extensionStorage.setItem(
"hideGeneratedLineCount",
newState.hideGeneratedLineCount,
);
await extensionStorage.setItem("githubPat", newState.githubPat);
await extensionStorage.setItem("customLists", newState.customLists);

// Clear cache
await commitHashDiffsCache.clear();
Expand All @@ -25,12 +33,14 @@ const { state, hasChanges, reset, saveChanges } = useForm(
</script>

<template>
<form class="flex flex-col gap-4 pb-20" @submit.prevent="saveChanges">
<form class="flex flex-col gap-8 pb-20" @submit.prevent="saveChanges">
<TokenPref v-model:github-pat="state.githubPat" />
<ShowGeneratedCountPref
v-model:hide-generated-line-count="state.hideGeneratedLineCount"
/>

<CustomListsPref v-model:custom-lists="state.customLists" />

<div
class="fixed inset-x-0 bottom-0 bg-base-100 flex gap-4 p-4 border-t border-neutral border-opacity-10"
>
Expand Down
3 changes: 3 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DEFAULT_CUSTOM_LIST_ALL = `*.lock
*.lock.*
*-lock*`;
38 changes: 33 additions & 5 deletions src/utils/github/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ class GithubApi {
// 10s sleep for testing loading UI
// await sleep(10e3);

const [gitAttributes, prFiles] = await Promise.all([
const [gitAttributes, prFiles, settingsPatterns] = await Promise.all([
this.getGitAttributes({
owner: options.owner,
repo: options.repo,
ref,
}),
this.getPrFiles(options),
this.getPatternsFromSettings(),
]);

const include: DiffEntry[] = [];
Expand All @@ -74,10 +75,23 @@ class GithubApi {
return;
}

const evaluation = gitAttributes.evaluate(diff.filename);
const isGenerated = !!evaluation.attributes["linguist-generated"];
console.debug("Is generated?", diff.filename, isGenerated);
console.debug("Evaluation:", evaluation);
const gitAttributesEval = gitAttributes.evaluate(diff.filename);
const matchingSettings = settingsPatterns.filter(({ pattern }) =>
minimatch(diff.filename, pattern),
);
const isGitAttributesGenerated =
!!gitAttributesEval.attributes["linguist-generated"];
const isSettingsGenerated = matchingSettings.length > 0;
const isGenerated = isGitAttributesGenerated || isSettingsGenerated;

console.debug("Is generated?", diff.filename, {
isGitAttributesGenerated,
isSettingsGenerated,
isGenerated,
});
console.debug("Git attributes evaluation:", gitAttributesEval);
console.debug("Matched settings:", isSettingsGenerated);

if (isGenerated) exclude.push(diff);
else include.push(diff);
});
Expand All @@ -90,6 +104,7 @@ class GithubApi {
await this.cache.set(ref, result, 2 * HOUR);
return result;
}

/**
* Returns a list of generated files that should be excluded from diff counts.
*
Expand Down Expand Up @@ -130,6 +145,19 @@ class GithubApi {
}
}

private async getPatternsFromSettings(): Promise<
Array<{ source: string; pattern: string }>
> {
const res = await extensionStorage.getItem("customLists");
if (!res) return [];

const { all } = res;
return all.split("\n").map((line) => ({
pattern: line,
source: "Options: All Repos",
}));
}

/**
* Get the commit sha the PR is on.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { defineExtensionStorage } from "@webext-core/storage";

export interface CustomLists {
all: string;
}

export interface ExtensionStorageSchema {
/**
* The user's personal access token (PAT).
Expand All @@ -9,6 +13,12 @@ export interface ExtensionStorageSchema {
* When `true`, don't show the generated line counts next to additions and subtractions.
*/
hideGeneratedLineCount: boolean;
/**
* When `true`, don't show the generated line counts next to additions and subtractions.
*/
customLists: {
all: string;
};
}

export const extensionStorage = defineExtensionStorage<ExtensionStorageSchema>(
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载