| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1 | // git-data-service.ts |
| 2 | // Interface and implementation for fetching Git data |
| 3 | |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 4 | import { DiffFile, GitLogEntry } from "../types"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 5 | |
| 6 | // Re-export DiffFile as GitDiffFile |
| 7 | export type GitDiffFile = DiffFile; |
| 8 | |
| 9 | /** |
| 10 | * Interface for Git data services |
| 11 | */ |
| 12 | export interface GitDataService { |
| 13 | /** |
| 14 | * Fetches recent commit history |
| 15 | * @param initialCommit The initial commit hash to start from |
| 16 | * @returns List of commits |
| 17 | */ |
| 18 | getCommitHistory(initialCommit?: string): Promise<GitLogEntry[]>; |
| 19 | |
| 20 | /** |
| 21 | * Fetches diff between two commits |
| 22 | * @param from Starting commit hash |
| 23 | * @param to Ending commit hash (can be empty string for unstaged changes) |
| 24 | * @returns List of changed files |
| 25 | */ |
| 26 | getDiff(from: string, to: string): Promise<GitDiffFile[]>; |
| 27 | |
| 28 | /** |
| 29 | * Fetches diff for a single commit |
| 30 | * @param commit Commit hash |
| 31 | * @returns List of changed files |
| 32 | */ |
| 33 | getCommitDiff(commit: string): Promise<GitDiffFile[]>; |
| 34 | |
| 35 | /** |
| 36 | * Fetches file content from git using a file hash |
| 37 | * @param fileHash Git blob hash of the file to fetch |
| 38 | * @returns File content as string |
| 39 | */ |
| 40 | getFileContent(fileHash: string): Promise<string>; |
| 41 | |
| 42 | /** |
| 43 | * Gets file content from the current working directory |
| 44 | * @param filePath Path to the file within the repository |
| 45 | * @returns File content as string |
| 46 | */ |
| 47 | getWorkingCopyContent(filePath: string): Promise<string>; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 48 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Saves file content to the working directory |
| 51 | * @param filePath Path to the file within the repository |
| 52 | * @param content New content to save to the file |
| 53 | */ |
| 54 | saveFileContent(filePath: string, content: string): Promise<void>; |
| 55 | |
| 56 | /** |
| 57 | * Gets the base commit reference (often "sketch-base") |
| 58 | * @returns Base commit reference |
| 59 | */ |
| 60 | getBaseCommitRef(): Promise<string>; |
| 61 | |
| 62 | /** |
| 63 | * Fetches unstaged changes (diff between a commit and working directory) |
| 64 | * @param from Starting commit hash (defaults to HEAD if not specified) |
| 65 | * @returns List of changed files |
| 66 | */ |
| 67 | getUnstagedChanges(from?: string): Promise<GitDiffFile[]>; |
| Josh Bleecher Snyder | a8561f7 | 2025-07-15 23:47:59 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Fetches list of untracked files in the repository |
| 71 | * @returns List of untracked file paths |
| 72 | */ |
| 73 | getUntrackedFiles(): Promise<string[]>; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Default implementation of GitDataService for the real application |
| 78 | */ |
| 79 | export class DefaultGitDataService implements GitDataService { |
| 80 | private baseCommitRef: string | null = null; |
| 81 | |
| 82 | async getCommitHistory(initialCommit?: string): Promise<GitLogEntry[]> { |
| 83 | try { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 84 | const url = initialCommit |
| 85 | ? `git/recentlog?initialCommit=${encodeURIComponent(initialCommit)}` |
| 86 | : "git/recentlog"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 87 | const response = await fetch(url); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 88 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 89 | if (!response.ok) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 90 | throw new Error( |
| 91 | `Failed to fetch commit history: ${response.statusText}`, |
| 92 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 93 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 94 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 95 | return await response.json(); |
| 96 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 97 | console.error("Error fetching commit history:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 98 | throw error; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | async getDiff(from: string, to: string): Promise<GitDiffFile[]> { |
| 103 | try { |
| 104 | const url = `git/rawdiff?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}`; |
| 105 | const response = await fetch(url); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 106 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 107 | if (!response.ok) { |
| 108 | throw new Error(`Failed to fetch diff: ${response.statusText}`); |
| 109 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 110 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 111 | return await response.json(); |
| 112 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 113 | console.error("Error fetching diff:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 114 | throw error; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | async getCommitDiff(commit: string): Promise<GitDiffFile[]> { |
| 119 | try { |
| 120 | const url = `git/rawdiff?commit=${encodeURIComponent(commit)}`; |
| 121 | const response = await fetch(url); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 122 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 123 | if (!response.ok) { |
| 124 | throw new Error(`Failed to fetch commit diff: ${response.statusText}`); |
| 125 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 126 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 127 | return await response.json(); |
| 128 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 129 | console.error("Error fetching commit diff:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 130 | throw error; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | async getFileContent(fileHash: string): Promise<string> { |
| 135 | try { |
| 136 | // If the hash is marked as a working copy (special value '000000' or empty) |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 137 | if ( |
| 138 | fileHash === "0000000000000000000000000000000000000000" || |
| 139 | !fileHash |
| 140 | ) { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 141 | // This shouldn't happen, but if it does, return empty string |
| 142 | // Working copy content should be fetched through getWorkingCopyContent |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 143 | console.warn( |
| 144 | "Invalid file hash for getFileContent, returning empty string", |
| 145 | ); |
| 146 | return ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 147 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 148 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 149 | const url = `git/show?hash=${encodeURIComponent(fileHash)}`; |
| 150 | const response = await fetch(url); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 151 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 152 | if (!response.ok) { |
| 153 | throw new Error(`Failed to fetch file content: ${response.statusText}`); |
| 154 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 155 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 156 | const data = await response.json(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 157 | return data.output || ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 158 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 159 | console.error("Error fetching file content:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 160 | throw error; |
| 161 | } |
| 162 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 163 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 164 | async getWorkingCopyContent(filePath: string): Promise<string> { |
| 165 | try { |
| 166 | const url = `git/cat?path=${encodeURIComponent(filePath)}`; |
| 167 | const response = await fetch(url); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 168 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 169 | if (!response.ok) { |
| Josh Bleecher Snyder | 77bac8c | 2025-05-28 11:04:09 -0700 | [diff] [blame] | 170 | const errorText = await response.text(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 171 | throw new Error( |
| Josh Bleecher Snyder | 77bac8c | 2025-05-28 11:04:09 -0700 | [diff] [blame] | 172 | `Failed to fetch working copy content (${response.status}): ${errorText}`, |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 173 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 174 | } |
| Josh Bleecher Snyder | fadffe3 | 2025-07-10 00:08:38 +0000 | [diff] [blame] | 175 | if (response.status === 204) { |
| 176 | return ""; // file doesn't exist |
| 177 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 178 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 179 | const data = await response.json(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 180 | return data.output || ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 181 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 182 | console.error("Error fetching working copy content:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 183 | throw error; |
| 184 | } |
| 185 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 186 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 187 | async saveFileContent(filePath: string, content: string): Promise<void> { |
| 188 | try { |
| 189 | const url = `git/save`; |
| 190 | const response = await fetch(url, { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 191 | method: "POST", |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 192 | headers: { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 193 | "Content-Type": "application/json", |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 194 | }, |
| 195 | body: JSON.stringify({ |
| 196 | path: filePath, |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 197 | content: content, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 198 | }), |
| 199 | }); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 200 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 201 | if (!response.ok) { |
| 202 | const errorText = await response.text(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 203 | throw new Error( |
| 204 | `Failed to save file content: ${response.statusText} - ${errorText}`, |
| 205 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 206 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 207 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 208 | // Don't need to return the response, just ensure it was successful |
| 209 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 210 | console.error("Error saving file content:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 211 | throw error; |
| 212 | } |
| 213 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 214 | |
| 215 | async getUnstagedChanges(from: string = "HEAD"): Promise<GitDiffFile[]> { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 216 | try { |
| 217 | // To get unstaged changes, we diff the specified commit (or HEAD) with an empty 'to' |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 218 | return await this.getDiff(from, ""); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 219 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 220 | console.error("Error fetching unstaged changes:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 221 | throw error; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | async getBaseCommitRef(): Promise<string> { |
| 226 | // Cache the base commit reference to avoid multiple requests |
| 227 | if (this.baseCommitRef) { |
| 228 | return this.baseCommitRef; |
| 229 | } |
| 230 | |
| 231 | try { |
| 232 | // This could be replaced with a specific endpoint call if available |
| 233 | // For now, we'll use a fixed value or try to get it from the server |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 234 | this.baseCommitRef = "sketch-base"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 235 | return this.baseCommitRef; |
| 236 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 237 | console.error("Error fetching base commit reference:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 238 | throw error; |
| 239 | } |
| 240 | } |
| Josh Bleecher Snyder | a8561f7 | 2025-07-15 23:47:59 +0000 | [diff] [blame] | 241 | |
| 242 | async getUntrackedFiles(): Promise<string[]> { |
| 243 | try { |
| 244 | const response = await fetch("git/untracked"); |
| 245 | |
| 246 | if (!response.ok) { |
| 247 | throw new Error( |
| 248 | `Failed to fetch untracked files: ${response.statusText}`, |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | const data = await response.json(); |
| 253 | return data.untracked_files || []; |
| 254 | } catch (error) { |
| 255 | console.error("Error fetching untracked files:", error); |
| 256 | throw error; |
| 257 | } |
| 258 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 259 | } |