Skip to content

Commit abf395a

Browse files
committed
Create re-usable file service for reading git blobs
1 parent 850b53c commit abf395a

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
lines changed

apps/desktop/src/lib/file/FileDiff.svelte

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
<script lang="ts">
2-
import { invoke } from '$lib/backend/ipc';
32
import { Project } from '$lib/backend/projects';
3+
import { FileService } from '$lib/files/fileService';
44
import HunkViewer from '$lib/hunk/HunkViewer.svelte';
55
import InfoMessage from '$lib/shared/InfoMessage.svelte';
66
import LargeDiffMessage from '$lib/shared/LargeDiffMessage.svelte';
77
import { computeAddedRemovedByHunk } from '$lib/utils/metrics';
88
import { getLocalCommits, getLocalAndRemoteCommits } from '$lib/vbranches/contexts';
99
import { getLockText } from '$lib/vbranches/tooltip';
1010
import { getContext } from '@gitbutler/shared/context';
11+
import type { FileInfo } from '$lib/files/file';
1112
import type { HunkSection, ContentSection } from '$lib/utils/fileSections';
1213
13-
interface FileInfo {
14-
content: string;
15-
name?: string;
16-
mimeType?: string;
17-
size?: number;
18-
}
19-
2014
interface Props {
2115
filePath: string;
2216
isBinary: boolean;
@@ -43,6 +37,7 @@
4337
4438
let alwaysShow = $state(false);
4539
const project = getContext(Project);
40+
const fileService = getContext(FileService);
4641
const localCommits = isFileLocked ? getLocalCommits() : undefined;
4742
const remoteCommits = isFileLocked ? getLocalAndRemoteCommits() : undefined;
4843
@@ -80,18 +75,13 @@
8075
});
8176
8277
async function fetchBlobInfo() {
78+
if (!isBinary) {
79+
return;
80+
}
8381
try {
84-
const fetchedFileInfo: FileInfo = await invoke('get_blob_info', {
85-
relativePath: filePath,
86-
projectId: project.id,
87-
commitId
88-
});
89-
fileInfo = fetchedFileInfo;
90-
91-
// If file.size > 5mb; don't render it
92-
if (fileInfo.size && fileInfo.size > 5 * 1024 * 1024) {
93-
isLarge = true;
94-
}
82+
const file = await fileService.readFromWorkspace(filePath, project.id, commitId);
83+
fileInfo = file.data;
84+
isLarge = file.isLarge;
9585
} catch (error) {
9686
console.error(error);
9787
}

apps/desktop/src/lib/files/file.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type FileInfo = {
2+
content: string;
3+
name?: string;
4+
mimeType?: string;
5+
size?: number;
6+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Tauri } from '$lib/backend/tauri';
2+
import type { FileInfo } from './file';
3+
4+
export class FileService {
5+
constructor(private tauri: Tauri) {}
6+
7+
async readFromWorkspace(filePath: string, projectId: string, commitId: string | undefined) {
8+
const data: FileInfo = await this.tauri.invoke('get_blob_info', {
9+
relativePath: filePath,
10+
projectId: projectId,
11+
commitId
12+
});
13+
// TODO: isLarge should be returned by back end.
14+
const isLarge = data.size && data.size > 5 * 1024 * 1024 ? true : false;
15+
return {
16+
data,
17+
isLarge
18+
};
19+
}
20+
}

apps/desktop/src/routes/+layout.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { PromptService } from '$lib/backend/prompt';
1010
import { Tauri } from '$lib/backend/tauri';
1111
import { UpdaterService } from '$lib/backend/updater';
1212
import { loadAppSettings } from '$lib/config/appSettings';
13+
import { FileService } from '$lib/files/fileService';
1314
import { RemotesService } from '$lib/remotes/service';
1415
import { RustSecretService } from '$lib/secrets/secretsService';
1516
import { TokenMemoryService } from '$lib/stores/tokenMemoryService';
@@ -45,7 +46,8 @@ export const load: LayoutLoad = async () => {
4546
const tokenMemoryService = new TokenMemoryService();
4647
const httpClient = new HttpClient(window.fetch, PUBLIC_API_BASE_URL, tokenMemoryService.token);
4748
const authService = new AuthService();
48-
const updaterService = new UpdaterService(new Tauri(), posthog);
49+
const tauri = new Tauri();
50+
const updaterService = new UpdaterService(tauri, posthog);
4951
const promptService = new PromptService();
5052

5153
const userService = new UserService(httpClient, tokenMemoryService, posthog);
@@ -59,6 +61,7 @@ export const load: LayoutLoad = async () => {
5961
const aiPromptService = new AIPromptService();
6062
const lineManagerFactory = new LineManagerFactory();
6163
const stackingLineManagerFactory = new StackingLineManagerFactory();
64+
const fileService = new FileService(tauri);
6265

6366
return {
6467
commandService,
@@ -77,6 +80,8 @@ export const load: LayoutLoad = async () => {
7780
lineManagerFactory,
7881
stackingLineManagerFactory,
7982
secretsService,
80-
posthog
83+
posthog,
84+
tauri,
85+
fileService
8186
};
8287
};

apps/desktop/src/routes/[projectId]/+layout.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import { showHistoryView } from '$lib/config/config';
1919
import { cloudFunctionality } from '$lib/config/uiFeatureFlags';
2020
import { StackingReorderDropzoneManagerFactory } from '$lib/dragging/stackingReorderDropzoneManager';
21+
import { FileService } from '$lib/files/fileService';
2122
import { DefaultForgeFactory } from '$lib/forge/forgeFactory';
2223
import { octokitFromAccessToken } from '$lib/forge/github/octokit';
2324
import { createForgeStore } from '$lib/forge/interface/forge';
@@ -93,6 +94,7 @@
9394
setContext(SyncedSnapshotService, data.syncedSnapshotService);
9495
setContext(CloudBranchesService, data.cloudBranchesService);
9596
setContext(CloudBranchCreationService, data.cloudBranchCreationService);
97+
setContext(FileService, data.fileService);
9698
});
9799
98100
const routesService = getRoutesService();

0 commit comments

Comments
 (0)