difit 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +106 -0
  2. package/dist/cli/index.d.ts +2 -0
  3. package/dist/cli/index.js +44 -0
  4. package/dist/cli/index.test.d.ts +1 -0
  5. package/dist/cli/index.test.js +676 -0
  6. package/dist/cli/utils.d.ts +1 -0
  7. package/dist/cli/utils.js +11 -0
  8. package/dist/cli/utils.test.d.ts +1 -0
  9. package/dist/cli/utils.test.js +214 -0
  10. package/dist/client/assets/index-BUtmfbD2.js +53 -0
  11. package/dist/client/assets/index-mZRIDsXW.css +1 -0
  12. package/dist/client/assets/prism-css-Bpx-unsJ.js +1 -0
  13. package/dist/client/assets/prism-json-xwnKirkR.js +1 -0
  14. package/dist/client/assets/prism-typescript-B2PMeEx1.js +1 -0
  15. package/dist/client/index.html +14 -0
  16. package/dist/server/comment-store.d.ts +13 -0
  17. package/dist/server/comment-store.js +63 -0
  18. package/dist/server/git-diff-tui.d.ts +2 -0
  19. package/dist/server/git-diff-tui.js +95 -0
  20. package/dist/server/git-diff.d.ts +10 -0
  21. package/dist/server/git-diff.js +124 -0
  22. package/dist/server/git-diff.test.d.ts +1 -0
  23. package/dist/server/git-diff.test.js +292 -0
  24. package/dist/server/server.d.ts +11 -0
  25. package/dist/server/server.js +128 -0
  26. package/dist/server/server.test.d.ts +1 -0
  27. package/dist/server/server.test.js +382 -0
  28. package/dist/tui/App.d.ts +8 -0
  29. package/dist/tui/App.js +92 -0
  30. package/dist/tui/App.test.d.ts +1 -0
  31. package/dist/tui/App.test.js +31 -0
  32. package/dist/tui/components/DiffViewer.d.ts +9 -0
  33. package/dist/tui/components/DiffViewer.js +88 -0
  34. package/dist/tui/components/FileList.d.ts +8 -0
  35. package/dist/tui/components/FileList.js +48 -0
  36. package/dist/tui/components/SideBySideDiffViewer.d.ts +9 -0
  37. package/dist/tui/components/SideBySideDiffViewer.js +237 -0
  38. package/dist/tui/components/StatusBar.d.ts +8 -0
  39. package/dist/tui/components/StatusBar.js +23 -0
  40. package/dist/tui/utils/parseDiff.d.ts +2 -0
  41. package/dist/tui/utils/parseDiff.js +68 -0
  42. package/dist/types/diff.d.ts +33 -0
  43. package/dist/types/diff.js +1 -0
  44. package/dist/utils/fileUtils.d.ts +12 -0
  45. package/dist/utils/fileUtils.js +21 -0
  46. package/package.json +91 -0
@@ -0,0 +1,292 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import { GitDiffParser } from './git-diff';
3
+ // Mock simple-git
4
+ vi.mock('simple-git', () => ({
5
+ simpleGit: vi.fn(() => ({
6
+ revparse: vi.fn(),
7
+ diffSummary: vi.fn(),
8
+ diff: vi.fn(),
9
+ })),
10
+ }));
11
+ // Mock child_process
12
+ vi.mock('child_process', async (importOriginal) => {
13
+ const actual = (await importOriginal());
14
+ return {
15
+ ...actual,
16
+ execSync: vi.fn(),
17
+ execFileSync: vi.fn(),
18
+ };
19
+ });
20
+ // Mock fs
21
+ vi.mock('fs', async (importOriginal) => {
22
+ const actual = (await importOriginal());
23
+ return {
24
+ ...actual,
25
+ readFileSync: vi.fn(),
26
+ };
27
+ });
28
+ describe('GitDiffParser', () => {
29
+ let parser;
30
+ let mockExecFileSync;
31
+ let mockReadFileSync;
32
+ beforeEach(async () => {
33
+ parser = new GitDiffParser('/test/repo');
34
+ vi.clearAllMocks();
35
+ // Get mocked functions
36
+ const childProcess = await import('child_process');
37
+ const fs = await import('fs');
38
+ mockExecFileSync = childProcess.execFileSync;
39
+ mockReadFileSync = fs.readFileSync;
40
+ });
41
+ afterEach(() => {
42
+ vi.restoreAllMocks();
43
+ });
44
+ describe('getBlobContent', () => {
45
+ it('reads from filesystem for working directory', async () => {
46
+ const mockBuffer = Buffer.from('test content');
47
+ mockReadFileSync.mockReturnValue(mockBuffer);
48
+ const result = await parser.getBlobContent('test.txt', 'working');
49
+ expect(mockReadFileSync).toHaveBeenCalledWith('test.txt');
50
+ expect(result).toBe(mockBuffer);
51
+ });
52
+ it('reads from filesystem for "." ref', async () => {
53
+ const mockBuffer = Buffer.from('test content');
54
+ mockReadFileSync.mockReturnValue(mockBuffer);
55
+ const result = await parser.getBlobContent('test.txt', '.');
56
+ expect(mockReadFileSync).toHaveBeenCalledWith('test.txt');
57
+ expect(result).toBe(mockBuffer);
58
+ });
59
+ it('uses git show for staged files', async () => {
60
+ const mockBuffer = Buffer.from('staged content');
61
+ mockExecFileSync.mockReturnValue(mockBuffer);
62
+ const result = await parser.getBlobContent('test.txt', 'staged');
63
+ expect(mockExecFileSync).toHaveBeenCalledWith('git', ['show', ':test.txt'], {
64
+ maxBuffer: 10 * 1024 * 1024,
65
+ });
66
+ expect(result).toBe(mockBuffer);
67
+ });
68
+ it('uses git cat-file for git refs', async () => {
69
+ const blobHash = 'abc123def456';
70
+ const mockBuffer = Buffer.from('git content');
71
+ mockExecFileSync
72
+ .mockReturnValueOnce(blobHash + '\n') // First call for rev-parse
73
+ .mockReturnValueOnce(mockBuffer); // Second call for cat-file
74
+ const result = await parser.getBlobContent('test.txt', 'HEAD');
75
+ expect(mockExecFileSync).toHaveBeenCalledWith('git', ['rev-parse', 'HEAD:test.txt'], {
76
+ encoding: 'utf8',
77
+ maxBuffer: 10 * 1024 * 1024,
78
+ });
79
+ expect(mockExecFileSync).toHaveBeenCalledWith('git', ['cat-file', 'blob', blobHash], {
80
+ maxBuffer: 10 * 1024 * 1024,
81
+ });
82
+ expect(result).toBe(mockBuffer);
83
+ });
84
+ it('handles file size limit errors', async () => {
85
+ const error = new Error('maxBuffer exceeded');
86
+ mockExecFileSync.mockImplementation(() => {
87
+ throw error;
88
+ });
89
+ await expect(parser.getBlobContent('large-file.jpg', 'HEAD')).rejects.toThrow('Image file large-file.jpg is too large to display (over 10MB limit)');
90
+ });
91
+ it('handles ENOBUFS errors', async () => {
92
+ const error = new Error('ENOBUFS: buffer overflow');
93
+ mockExecFileSync.mockImplementation(() => {
94
+ throw error;
95
+ });
96
+ await expect(parser.getBlobContent('large-file.jpg', 'HEAD')).rejects.toThrow('Image file large-file.jpg is too large to display (over 10MB limit)');
97
+ });
98
+ it('handles general git errors', async () => {
99
+ const error = new Error('fatal: Path does not exist');
100
+ mockExecFileSync.mockImplementation(() => {
101
+ throw error;
102
+ });
103
+ await expect(parser.getBlobContent('missing.txt', 'HEAD')).rejects.toThrow('Failed to get blob content for missing.txt at HEAD: fatal: Path does not exist');
104
+ });
105
+ });
106
+ describe('parseFileBlock with binary files', () => {
107
+ it('parses added binary file correctly', () => {
108
+ const diffLines = [
109
+ 'diff --git a/image.jpg b/image.jpg',
110
+ 'new file mode 100644',
111
+ 'index 0000000..abc123',
112
+ '--- /dev/null',
113
+ '+++ b/image.jpg',
114
+ 'Binary files /dev/null and b/image.jpg differ',
115
+ ];
116
+ const summary = {
117
+ insertions: 0,
118
+ deletions: 0,
119
+ };
120
+ // Access private method for testing
121
+ const result = parser.parseFileBlock(diffLines.join('\n'), summary);
122
+ expect(result).toEqual({
123
+ path: 'image.jpg',
124
+ oldPath: undefined,
125
+ status: 'added',
126
+ additions: 0,
127
+ deletions: 0,
128
+ chunks: [], // Binary files should have empty chunks
129
+ });
130
+ });
131
+ it('parses deleted binary file correctly', () => {
132
+ const diffLines = [
133
+ 'diff --git a/old-image.png b/old-image.png',
134
+ 'deleted file mode 100644',
135
+ 'index abc123..0000000',
136
+ '--- a/old-image.png',
137
+ '+++ /dev/null',
138
+ 'Binary files a/old-image.png and /dev/null differ',
139
+ ];
140
+ const summary = {
141
+ insertions: 0,
142
+ deletions: 0,
143
+ };
144
+ const result = parser.parseFileBlock(diffLines.join('\n'), summary);
145
+ expect(result).toEqual({
146
+ path: 'old-image.png',
147
+ oldPath: undefined,
148
+ status: 'deleted',
149
+ additions: 0,
150
+ deletions: 0,
151
+ chunks: [],
152
+ });
153
+ });
154
+ it('parses modified binary file correctly', () => {
155
+ const diffLines = [
156
+ 'diff --git a/photo.jpg b/photo.jpg',
157
+ 'index abc123..def456 100644',
158
+ '--- a/photo.jpg',
159
+ '+++ b/photo.jpg',
160
+ 'Binary files a/photo.jpg and b/photo.jpg differ',
161
+ ];
162
+ const summary = {
163
+ insertions: 0,
164
+ deletions: 0,
165
+ };
166
+ const result = parser.parseFileBlock(diffLines.join('\n'), summary);
167
+ expect(result).toEqual({
168
+ path: 'photo.jpg',
169
+ oldPath: undefined,
170
+ status: 'modified',
171
+ additions: 0,
172
+ deletions: 0,
173
+ chunks: [],
174
+ });
175
+ });
176
+ it('parses renamed binary file correctly', () => {
177
+ const diffLines = [
178
+ 'diff --git a/old-name.gif b/new-name.gif',
179
+ 'similarity index 100%',
180
+ 'rename from old-name.gif',
181
+ 'rename to new-name.gif',
182
+ ];
183
+ const summary = {
184
+ insertions: 0,
185
+ deletions: 0,
186
+ };
187
+ const result = parser.parseFileBlock(diffLines.join('\n'), summary);
188
+ expect(result).toEqual({
189
+ path: 'new-name.gif',
190
+ oldPath: 'old-name.gif',
191
+ status: 'renamed',
192
+ additions: 0,
193
+ deletions: 0,
194
+ chunks: [],
195
+ });
196
+ });
197
+ it('handles non-binary files normally', () => {
198
+ const diffLines = [
199
+ 'diff --git a/script.js b/script.js',
200
+ 'index abc123..def456 100644',
201
+ '--- a/script.js',
202
+ '+++ b/script.js',
203
+ '@@ -1,3 +1,4 @@',
204
+ ' console.log("hello");',
205
+ '+console.log("world");',
206
+ ' // end',
207
+ ];
208
+ const summary = {
209
+ insertions: 1,
210
+ deletions: 0,
211
+ };
212
+ const result = parser.parseFileBlock(diffLines.join('\n'), summary);
213
+ expect(result).toEqual({
214
+ path: 'script.js',
215
+ oldPath: undefined,
216
+ status: 'added',
217
+ additions: 1,
218
+ deletions: 0,
219
+ chunks: expect.any(Array), // Should have parsed chunks
220
+ });
221
+ // Verify chunks were parsed
222
+ expect(result.chunks).toHaveLength(1);
223
+ expect(result.chunks[0].header).toBe('@@ -1,3 +1,4 @@');
224
+ });
225
+ it('detects added files using /dev/null indicator', () => {
226
+ const diffLines = [
227
+ 'diff --git a/new-file.txt b/new-file.txt',
228
+ 'index 0000000..abc123 100644',
229
+ '--- /dev/null',
230
+ '+++ b/new-file.txt',
231
+ '@@ -0,0 +1,2 @@',
232
+ '+line 1',
233
+ '+line 2',
234
+ ];
235
+ const summary = {
236
+ insertions: 2,
237
+ deletions: 0,
238
+ };
239
+ const result = parser.parseFileBlock(diffLines.join('\n'), summary);
240
+ expect(result.status).toBe('added');
241
+ });
242
+ it('detects deleted files using /dev/null indicator', () => {
243
+ const diffLines = [
244
+ 'diff --git a/deleted-file.txt b/deleted-file.txt',
245
+ 'index abc123..0000000 100644',
246
+ '--- a/deleted-file.txt',
247
+ '+++ /dev/null',
248
+ '@@ -1,2 +0,0 @@',
249
+ '-line 1',
250
+ '-line 2',
251
+ ];
252
+ const summary = {
253
+ insertions: 0,
254
+ deletions: 2,
255
+ };
256
+ const result = parser.parseFileBlock(diffLines.join('\n'), summary);
257
+ expect(result.status).toBe('deleted');
258
+ });
259
+ });
260
+ describe('File status detection improvements', () => {
261
+ it('prioritizes new file mode over other indicators', () => {
262
+ const diffLines = [
263
+ 'diff --git a/test.txt b/test.txt',
264
+ 'new file mode 100644',
265
+ 'index 0000000..abc123',
266
+ '--- a/test.txt', // This might confuse simple parsers
267
+ '+++ b/test.txt',
268
+ ];
269
+ const summary = {
270
+ insertions: 5,
271
+ deletions: 0,
272
+ };
273
+ const result = parser.parseFileBlock(diffLines.join('\n'), summary);
274
+ expect(result.status).toBe('added');
275
+ });
276
+ it('prioritizes deleted file mode over other indicators', () => {
277
+ const diffLines = [
278
+ 'diff --git a/test.txt b/test.txt',
279
+ 'deleted file mode 100644',
280
+ 'index abc123..0000000',
281
+ '--- a/test.txt',
282
+ '+++ b/test.txt', // This might confuse simple parsers
283
+ ];
284
+ const summary = {
285
+ insertions: 0,
286
+ deletions: 5,
287
+ };
288
+ const result = parser.parseFileBlock(diffLines.join('\n'), summary);
289
+ expect(result.status).toBe('deleted');
290
+ });
291
+ });
292
+ });
@@ -0,0 +1,11 @@
1
+ interface ServerOptions {
2
+ commitish: string;
3
+ preferredPort?: number;
4
+ openBrowser?: boolean;
5
+ mode?: string;
6
+ }
7
+ export declare function startServer(options: ServerOptions): Promise<{
8
+ port: number;
9
+ url: string;
10
+ }>;
11
+ export {};
@@ -0,0 +1,128 @@
1
+ import express from 'express';
2
+ import { join, dirname } from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import open from 'open';
5
+ const __filename = fileURLToPath(import.meta.url);
6
+ const __dirname = dirname(__filename);
7
+ import { GitDiffParser } from './git-diff.js';
8
+ import { CommentStore } from './comment-store.js';
9
+ export async function startServer(options) {
10
+ const app = express();
11
+ const parser = new GitDiffParser();
12
+ const commentStore = new CommentStore();
13
+ let diffData = null;
14
+ app.use(express.json());
15
+ app.use((_req, res, next) => {
16
+ res.header('Access-Control-Allow-Origin', 'http://localhost:*');
17
+ res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
18
+ res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
19
+ next();
20
+ });
21
+ const isValidCommit = await parser.validateCommit(options.commitish);
22
+ if (!isValidCommit) {
23
+ throw new Error(`Invalid or non-existent commit: ${options.commitish}`);
24
+ }
25
+ diffData = await parser.parseDiff(options.commitish);
26
+ await commentStore.loadFromFile();
27
+ app.get('/api/diff', (_req, res) => {
28
+ res.json(diffData);
29
+ });
30
+ app.get('/api/comments', async (_req, res) => {
31
+ const comments = await commentStore.getComments();
32
+ res.json(comments);
33
+ });
34
+ app.post('/api/comments', async (req, res) => {
35
+ try {
36
+ const { file, line, body } = req.body;
37
+ if (!file || typeof line !== 'number' || !body) {
38
+ return res.status(400).json({ error: 'Missing required fields: file, line, body' });
39
+ }
40
+ const comment = await commentStore.addComment(file, line, body);
41
+ res.json(comment);
42
+ }
43
+ catch (error) {
44
+ res.status(500).json({ error: 'Failed to save comment' });
45
+ }
46
+ });
47
+ app.post('/api/comments/:id/prompt', async (req, res) => {
48
+ try {
49
+ const comments = await commentStore.getComments();
50
+ const comment = comments.find((c) => c.id === req.params.id);
51
+ if (!comment) {
52
+ return res.status(404).json({ error: 'Comment not found' });
53
+ }
54
+ const targetFile = diffData.files.find((f) => f.path === comment.file);
55
+ if (!targetFile) {
56
+ return res.status(404).json({ error: 'File not found in diff' });
57
+ }
58
+ let diffContent = '';
59
+ for (const chunk of targetFile.chunks) {
60
+ diffContent += chunk.header + '\n';
61
+ for (const line of chunk.lines) {
62
+ const prefix = line.type === 'add' ? '+' : line.type === 'delete' ? '-' : ' ';
63
+ diffContent += prefix + line.content + '\n';
64
+ }
65
+ }
66
+ const prompt = commentStore.generatePrompt(comment, diffContent);
67
+ res.json({ prompt });
68
+ }
69
+ catch (error) {
70
+ res.status(500).json({ error: 'Failed to generate prompt' });
71
+ }
72
+ });
73
+ const isProduction = process.env.NODE_ENV === 'production';
74
+ if (isProduction) {
75
+ // CLI実行ファイルの場所から相対的にクライアントファイルを探す
76
+ const distPath = join(__dirname, '..', 'client');
77
+ app.use(express.static(distPath));
78
+ app.get('*', (_req, res) => {
79
+ res.sendFile(join(distPath, 'index.html'));
80
+ });
81
+ }
82
+ else {
83
+ app.get('/', (_req, res) => {
84
+ res.send(`
85
+ <!DOCTYPE html>
86
+ <html>
87
+ <head>
88
+ <title>ReviewIt - Dev Mode</title>
89
+ </head>
90
+ <body>
91
+ <div id="root"></div>
92
+ <script>
93
+ console.log('ReviewIt development mode');
94
+ console.log('Diff data available at /api/diff');
95
+ </script>
96
+ </body>
97
+ </html>
98
+ `);
99
+ });
100
+ }
101
+ const port = await findAvailablePort(options.preferredPort || 3000);
102
+ app.listen(port, '127.0.0.1');
103
+ const url = `http://localhost:${port}`;
104
+ if (options.openBrowser) {
105
+ try {
106
+ await open(url);
107
+ }
108
+ catch (error) {
109
+ console.warn('Failed to open browser automatically');
110
+ }
111
+ }
112
+ return { port, url };
113
+ }
114
+ async function findAvailablePort(preferredPort) {
115
+ const net = await import('net');
116
+ return new Promise((resolve, reject) => {
117
+ const server = net.createServer();
118
+ server.listen(preferredPort, () => {
119
+ const port = server.address()?.port;
120
+ server.close(() => resolve(port));
121
+ });
122
+ server.on('error', () => {
123
+ findAvailablePort(preferredPort + 1)
124
+ .then(resolve)
125
+ .catch(reject);
126
+ });
127
+ });
128
+ }
@@ -0,0 +1 @@
1
+ export {};