mirror of
https://gitbruv.vercel.app/api/git/bruv/gitbruv.git
synced 2025-12-20 23:24:09 +01:00
fix some performance?
This commit is contained in:
parent
91208e44a1
commit
a683b021e5
2 changed files with 25 additions and 77 deletions
|
|
@ -197,42 +197,18 @@ export async function getRepoFileTree(owner: string, repoName: string, branch: s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const entries = await Promise.all(
|
const entries = targetTree
|
||||||
targetTree.map(async (entry) => {
|
.map((entry) => ({
|
||||||
const filePath = dirPath ? `${dirPath}/${entry.path}` : entry.path;
|
name: entry.path,
|
||||||
let lastCommit: { message: string; timestamp: number } | null = null;
|
type: entry.type as "blob" | "tree",
|
||||||
|
oid: entry.oid,
|
||||||
try {
|
path: dirPath ? `${dirPath}/${entry.path}` : entry.path,
|
||||||
const fileCommits = await git.log({
|
}))
|
||||||
fs,
|
.sort((a, b) => {
|
||||||
gitdir: "/",
|
if (a.type === "tree" && b.type !== "tree") return -1;
|
||||||
ref: branch,
|
if (a.type !== "tree" && b.type === "tree") return 1;
|
||||||
filepath: filePath,
|
return a.name.localeCompare(b.name);
|
||||||
depth: 1,
|
});
|
||||||
});
|
|
||||||
if (fileCommits.length > 0) {
|
|
||||||
lastCommit = {
|
|
||||||
message: fileCommits[0].commit.message.split("\n")[0],
|
|
||||||
timestamp: fileCommits[0].commit.committer.timestamp * 1000,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
return {
|
|
||||||
name: entry.path,
|
|
||||||
type: entry.type as "blob" | "tree",
|
|
||||||
oid: entry.oid,
|
|
||||||
path: filePath,
|
|
||||||
lastCommit,
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
entries.sort((a, b) => {
|
|
||||||
if (a.type === "tree" && b.type !== "tree") return -1;
|
|
||||||
if (a.type !== "tree" && b.type === "tree") return 1;
|
|
||||||
return a.name.localeCompare(b.name);
|
|
||||||
});
|
|
||||||
|
|
||||||
return { files: entries, isEmpty: false };
|
return { files: entries, isEmpty: false };
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
|
|
@ -580,7 +556,6 @@ export type FileEntry = {
|
||||||
type: "blob" | "tree";
|
type: "blob" | "tree";
|
||||||
oid: string;
|
oid: string;
|
||||||
path: string;
|
path: string;
|
||||||
lastCommit: { message: string; timestamp: number } | null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
async function fetchFileTree(userId: string, repoName: string, defaultBranch: string) {
|
async function fetchFileTree(userId: string, repoName: string, defaultBranch: string) {
|
||||||
|
|
@ -603,35 +578,18 @@ async function fetchFileTree(userId: string, repoName: string, defaultBranch: st
|
||||||
|
|
||||||
const { tree } = await git.readTree({ fs, gitdir: "/", oid: commitOid });
|
const { tree } = await git.readTree({ fs, gitdir: "/", oid: commitOid });
|
||||||
|
|
||||||
const fileEntries = await Promise.all(
|
files = tree
|
||||||
tree.map(async (entry) => {
|
.map((entry) => ({
|
||||||
let lastCommit: { message: string; timestamp: number } | null = null;
|
name: entry.path,
|
||||||
try {
|
type: entry.type as "blob" | "tree",
|
||||||
const fileCommits = await git.log({ fs, gitdir: "/", ref: defaultBranch, filepath: entry.path, depth: 1 });
|
oid: entry.oid,
|
||||||
if (fileCommits.length > 0) {
|
path: entry.path,
|
||||||
lastCommit = {
|
}))
|
||||||
message: fileCommits[0].commit.message.split("\n")[0],
|
.sort((a, b) => {
|
||||||
timestamp: fileCommits[0].commit.committer.timestamp * 1000,
|
if (a.type === "tree" && b.type !== "tree") return -1;
|
||||||
};
|
if (a.type !== "tree" && b.type === "tree") return 1;
|
||||||
}
|
return a.name.localeCompare(b.name);
|
||||||
} catch {}
|
});
|
||||||
return {
|
|
||||||
name: entry.path,
|
|
||||||
type: entry.type as "blob" | "tree",
|
|
||||||
oid: entry.oid,
|
|
||||||
path: entry.path,
|
|
||||||
lastCommit,
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
fileEntries.sort((a, b) => {
|
|
||||||
if (a.type === "tree" && b.type !== "tree") return -1;
|
|
||||||
if (a.type !== "tree" && b.type === "tree") return 1;
|
|
||||||
return a.name.localeCompare(b.name);
|
|
||||||
});
|
|
||||||
|
|
||||||
files = fileEntries;
|
|
||||||
|
|
||||||
const readmeEntry = tree.find((e) => e.path.toLowerCase() === "readme.md" && e.type === "blob");
|
const readmeEntry = tree.find((e) => e.path.toLowerCase() === "readme.md" && e.type === "blob");
|
||||||
if (readmeEntry) {
|
if (readmeEntry) {
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,12 @@
|
||||||
|
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { Folder, FileCode, FileText, FileJson, File } from "lucide-react";
|
import { Folder, FileCode, FileText, FileJson, File } from "lucide-react";
|
||||||
import { formatDistanceToNow } from "date-fns";
|
|
||||||
|
|
||||||
type FileEntry = {
|
type FileEntry = {
|
||||||
name: string;
|
name: string;
|
||||||
type: "blob" | "tree";
|
type: "blob" | "tree";
|
||||||
oid: string;
|
oid: string;
|
||||||
path: string;
|
path: string;
|
||||||
lastCommit?: { message: string; timestamp: number } | null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const FILE_ICONS: Record<string, React.ElementType> = {
|
const FILE_ICONS: Record<string, React.ElementType> = {
|
||||||
|
|
@ -45,15 +43,7 @@ export function FileTree({ files, username, repoName, branch }: { files: FileEnt
|
||||||
return (
|
return (
|
||||||
<Link key={file.oid + file.name} href={href} className="flex items-center gap-3 px-4 py-2.5 hover:bg-muted/50 transition-colors group">
|
<Link key={file.oid + file.name} href={href} className="flex items-center gap-3 px-4 py-2.5 hover:bg-muted/50 transition-colors group">
|
||||||
<Icon className={`h-4 w-4 shrink-0 ${file.type === "tree" ? "text-accent" : "text-muted-foreground"}`} />
|
<Icon className={`h-4 w-4 shrink-0 ${file.type === "tree" ? "text-accent" : "text-muted-foreground"}`} />
|
||||||
<span className="text-sm group-hover:text-accent truncate min-w-0 shrink">{file.name}</span>
|
<span className="text-sm group-hover:text-accent truncate min-w-0">{file.name}</span>
|
||||||
{file.lastCommit && (
|
|
||||||
<>
|
|
||||||
<span className="text-sm text-muted-foreground truncate hidden sm:block flex-1 min-w-0">{file.lastCommit.message}</span>
|
|
||||||
<span className="text-sm text-muted-foreground whitespace-nowrap ml-auto">
|
|
||||||
{formatDistanceToNow(file.lastCommit.timestamp, { addSuffix: true })}
|
|
||||||
</span>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</Link>
|
</Link>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue