-
Notifications
You must be signed in to change notification settings - Fork 5
refactor: consolidate duplicated methods into BaseGitService #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -88,10 +88,43 @@ | |||||||||||||||||||
| return cleanRoot + cleanPath; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| protected encodeContent(content: string): string { | ||||||||||||||||||||
| const bytes = new TextEncoder().encode(content); | ||||||||||||||||||||
| let binary = ''; | ||||||||||||||||||||
| for (let i = 0; i < bytes.byteLength; i++) { | ||||||||||||||||||||
| const byte = bytes[i]; | ||||||||||||||||||||
| if (byte !== undefined) { | ||||||||||||||||||||
| binary += String.fromCodePoint(byte); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| return btoa(binary); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| protected decodeContent(base64: string): string { | ||||||||||||||||||||
| const binary = atob(base64.replace(/\s/g, '')); | ||||||||||||||||||||
| const bytes = new Uint8Array(binary.length); | ||||||||||||||||||||
| for (let i = 0; i < binary.length; i++) { | ||||||||||||||||||||
| const cp = binary.codePointAt(i); | ||||||||||||||||||||
| bytes[i] = cp !== undefined ? cp : 0; | ||||||||||||||||||||
|
Check warning on line 108 in src/services/git-service-base.ts
|
||||||||||||||||||||
| } | ||||||||||||||||||||
| return new TextDecoder().decode(bytes); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+103
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In protected decodeContent(base64: string): string {
const binary = atob(base64.replace(/\s/g, ''));
const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
return new TextDecoder().decode(bytes);
} |
||||||||||||||||||||
|
|
||||||||||||||||||||
| protected handleFileNotFound(e: unknown): GitFile { | ||||||||||||||||||||
| if (e instanceof Error && e.message.includes('404')) { | ||||||||||||||||||||
| return { content: '', sha: '' }; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| throw e; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| async getRepoGitignores(branch: string): Promise<string[]> { | ||||||||||||||||||||
| const allFiles = await this.listFiles(branch); | ||||||||||||||||||||
| return allFiles.filter(p => p.endsWith('.gitignore')); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+120
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation of
Consider implementing a way to list all files in the repository without the
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| abstract getFile(path: string, branch: string): Promise<GitFile>; | ||||||||||||||||||||
| abstract pushFile(path: string, content: string, branch: string, message: string, sha?: string): Promise<string>; | ||||||||||||||||||||
| abstract listFiles(branch: string): Promise<string[]>; | ||||||||||||||||||||
| abstract deleteFile(path: string, branch: string, message: string): Promise<void>; | ||||||||||||||||||||
| abstract testConnection(): Promise<boolean>; | ||||||||||||||||||||
| abstract getRepoGitignores(branch: string): Promise<string[]>; | ||||||||||||||||||||
| } | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
encodeContentuses a loop with string concatenation for every byte. This is inefficient for large files as it leads to repeated string allocations and memory pressure. A more performant approach is to convert theUint8Arrayto a binary string usingArray.fromwith a mapping function before joining, which is better optimized in modern JavaScript engines.