-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitDiffer.tsx
More file actions
182 lines (152 loc) · 4.97 KB
/
Copy pathcommitDiffer.tsx
File metadata and controls
182 lines (152 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//import rehypeStringify from 'rehype-stringify'
import { Hono } from "hono";
import { serveStatic } from "hono/bun";
import { codeToHtml, ShikiTransformer } from "shiki";
import {
addDiffInsertedTransformer,
addDiffDeletedTransformer,
} from "./shiki-transformers";
import {
getDiffPairsForCurrentDiff,
getDiffPairsForDiff,
} from "./DiffPairProducer";
import { transformerRenderWhitespace } from "@shikijs/transformers";
import {
DiffPair,
FileWithDeletions,
FileWithInsertions,
FileWithDiff,
FileType,
} from "./types/difftypes";
import { Pair, CodeBlock } from "./types/common";
import { getGrepResults } from "./GitSearchUtil";
import {
Head,
DiffHolder,
FullDiffPage,
FullGitGrepPage,
GitGrepResultHolder,
GitGrepLineHolder,
} from "./components";
// TODO: handle when files are renamed (atm previous file is 1 empty line)
const getCodeBlocksForDiffPairs = async (
diffPairs: DiffPair<FileWithDeletions, FileWithInsertions>[]
) => {
const htmlPairs: Pair<CodeBlock, CodeBlock>[] = [];
for (const diffPair of diffPairs) {
const fileWithDeletions: FileWithDeletions = diffPair.old;
const fileWithInsertions: FileWithInsertions = diffPair.new;
const oldFilePath: string = fileWithDeletions.filePath;
const newFilePath: string = fileWithInsertions.filePath;
// TODO: for now deleted files will point to /dev/null and give an emtpy string
// yet it will be rendered as a file with 1 line. Probably it's better to just
// mark a file as deleted instead.
const [oldFileDecorated, newFileDecorated] = [
await getManuallyTransformedFileHtml(
fileWithDeletions,
fileWithDeletions.language
),
await getManuallyTransformedFileHtml(
fileWithInsertions,
fileWithInsertions.language
),
];
htmlPairs.push({
first: { title: oldFilePath, codeHtml: oldFileDecorated },
second: { title: newFilePath, codeHtml: newFileDecorated },
});
}
return htmlPairs;
};
const getSiteContent = async (): Promise<Pair<CodeBlock, CodeBlock>[]> => {
// run git diff and collect all files and pair them with their inserted/deleted lines
const diffPairs: DiffPair<FileWithDeletions, FileWithInsertions>[] =
await getDiffPairsForCurrentDiff();
return getCodeBlocksForDiffPairs(diffPairs);
};
const getContentForDiffs = async (
commitA: string,
commitB: string
): Promise<Pair<CodeBlock, CodeBlock>[]> => {
const diffPairs: DiffPair<FileWithDeletions, FileWithInsertions>[] =
await getDiffPairsForDiff(commitA, commitB);
//console.log("we got here!!!!");
return getCodeBlocksForDiffPairs(diffPairs);
};
const getInsertionOrDeletionTransformer = (
fileWithDiff: FileWithDiff
): ShikiTransformer => {
switch (fileWithDiff.fileType) {
case FileType.HAS_DELETIONS: {
const deletedLineSet: Set<number> = new Set<number>(
fileWithDiff.deletedLines
);
return addDiffDeletedTransformer(deletedLineSet);
}
case FileType.HAS_INSERTIONS: {
const insertedLineSet: Set<number> = new Set<number>(
fileWithDiff.insertedLines
);
return addDiffInsertedTransformer(insertedLineSet);
}
}
console.log("No case was hit");
console.log("fileWithDiff: ");
console.log(fileWithDiff);
};
const getManuallyTransformedFileHtml = async (
fileWithDiff: FileWithDiff,
lang: string
): Promise<string> => {
const codeContent = fileWithDiff.content;
const diffTransformer = getInsertionOrDeletionTransformer(fileWithDiff);
const html = await codeToHtml(codeContent, {
lang: lang,
theme: "monokai",
transformers: [diffTransformer, transformerRenderWhitespace()],
});
return html;
};
// Endpoints zone
const app = new Hono();
app.use("/diffStyles.css", serveStatic({ path: "./diffStyles.css" }));
app.get("/", async (c) => {
const content: Pair<CodeBlock, CodeBlock>[] = await getSiteContent();
const fullPage = <FullDiffPage diffPairs={content} />;
return c.html(fullPage);
});
app.get("/diff", async (c) => {
const { commitA, commitB } = c.req.query();
if (commitA && commitB) {
const content: Pair<CodeBlock, CodeBlock>[] = await getContentForDiffs(
commitA,
commitB
);
const fullPage = <FullDiffPage diffPairs={content} />;
return c.html(fullPage);
}
return c.html("");
});
app.get("/search", async (c) => {
const fullPage = <FullGitGrepPage />;
return c.html(fullPage);
});
app.get("/grep", async (c) => {
const { pattern } = c.req.query();
if (pattern) {
console.log("we gt pattern");
const content: Pair<string, string>[] = await getGrepResults(pattern);
//const fullPage = <FullDiffPage diffPairs={content} />;
console.log("we got pairs: ");
console.log(content);
const grepResultElement = <GitGrepResultHolder resultPairs={content} />;
return c.html(grepResultElement);
}
return c.html("");
});
const port = parseInt(process.env.PORT!) || 8080 || 3000;
console.log(`Running at http://localhost:${port}`);
export default {
port,
fetch: app.fetch,
};