Replace LocalFile | RemoteFile with AnyFile

This commit is contained in:
Mattias Granlund 2024-02-01 11:02:16 +01:00
parent c813dddcd9
commit 90e7b0f192
22 changed files with 64 additions and 70 deletions

View File

@ -6,7 +6,7 @@
import { tooltip } from '$lib/utils/tooltip';
import { writable } from 'svelte/store';
import type { BranchController } from '$lib/vbranches/branchController';
import type { BaseBranch, LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { BaseBranch, AnyFile } from '$lib/vbranches/types';
export let base: BaseBranch;
export let projectId: string;
@ -16,7 +16,7 @@
const mergeUpstreamWarningDismissed = projectMergeUpstreamWarningDismissed(
branchController.projectId
);
const selectedFiles = writable<(LocalFile | RemoteFile)[]>([]);
const selectedFiles = writable<AnyFile[]>([]);
let updateTargetModal: Modal;
let mergeUpstreamWarningDismissedCheckbox = false;

View File

@ -28,7 +28,7 @@
import type { BranchService } from '$lib/branches/service';
import type { GitHubService } from '$lib/github/service';
import type { BranchController } from '$lib/vbranches/branchController';
import type { BaseBranch, Branch, LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { BaseBranch, Branch, LocalFile } from '$lib/vbranches/types';
export let branch: Branch;
export let isUnapplied = false;

View File

@ -4,7 +4,7 @@
import type { BranchService } from '$lib/branches/service';
import type { GitHubService } from '$lib/github/service';
import type { BranchController } from '$lib/vbranches/branchController';
import type { BaseBranch, Branch, LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { BaseBranch, Branch, AnyFile } from '$lib/vbranches/types';
import type { Writable } from 'svelte/store';
export let project: Project;
@ -13,7 +13,7 @@
export let githubService: GitHubService;
export let branchController: BranchController;
export let branchService: BranchService;
export let selectedFiles: Writable<(LocalFile | RemoteFile)[]>;
export let selectedFiles: Writable<AnyFile[]>;
export let isUnapplied: boolean;
export let branchCount: number;
</script>

View File

@ -4,16 +4,16 @@
import Segment from '$lib/components/SegmentControl/Segment.svelte';
import SegmentedControl from '$lib/components/SegmentControl/SegmentedControl.svelte';
import type { Ownership } from '$lib/vbranches/ownership';
import type { LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { AnyFile } from '$lib/vbranches/types';
import type { Writable } from 'svelte/store';
export let files: (LocalFile | RemoteFile)[];
export let files: AnyFile[];
export let selectedOwnership: Writable<Ownership>;
export let showCheckboxes = false;
export let selectedListMode: string;
function selectAll(selectedOwnership: Writable<Ownership>, files: (LocalFile | RemoteFile)[]) {
function selectAll(selectedOwnership: Writable<Ownership>, files: AnyFile[]) {
files.forEach((f) =>
selectedOwnership.update((ownership) => ownership.addHunk(f.id, ...f.hunks.map((h) => h.id)))
);

View File

@ -2,15 +2,15 @@
import FileListItem from './FileListItem.svelte';
import { sortLikeFileTree } from '$lib/vbranches/filetree';
import type { Ownership } from '$lib/vbranches/ownership';
import type { LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { AnyFile } from '$lib/vbranches/types';
import type { Writable } from 'svelte/store';
export let branchId: string;
export let files: (LocalFile | RemoteFile)[];
export let files: AnyFile[];
export let selectedOwnership: Writable<Ownership>;
export let isUnapplied = false;
export let showCheckboxes = false;
export let selectedFiles: Writable<(LocalFile | RemoteFile)[]>;
export let selectedFiles: Writable<AnyFile[]>;
export let allowMultiple = false;
$: console.log(selectedFiles);

View File

@ -2,7 +2,13 @@
import BranchCard from './BranchCard.svelte';
import FileCard from './FileCard.svelte';
import { Ownership } from '$lib/vbranches/ownership';
import { RemoteFile, type BaseBranch, type Branch, type LocalFile } from '$lib/vbranches/types';
import {
RemoteFile,
type BaseBranch,
type Branch,
type LocalFile,
type AnyFile
} from '$lib/vbranches/types';
import { writable, type Writable } from 'svelte/store';
import type { User, getCloudApiClient } from '$lib/backend/cloud';
import type { Project } from '$lib/backend/projects';
@ -29,7 +35,7 @@
let commitBoxOpen: Writable<boolean>;
function setSelected(files: (LocalFile | RemoteFile)[], branch: Branch) {
function setSelected(files: AnyFile[], branch: Branch) {
if (files.length == 0) return undefined;
if (files.length == 1 && files[0] instanceof RemoteFile) return files[0];

View File

@ -6,13 +6,7 @@
import type { BranchService } from '$lib/branches/service';
import type { GitHubService } from '$lib/github/service';
import type { BranchController } from '$lib/vbranches/branchController';
import type {
BaseBranch,
Branch,
CommitStatus,
LocalFile,
RemoteFile
} from '$lib/vbranches/types';
import type { AnyFile, BaseBranch, Branch, CommitStatus } from '$lib/vbranches/types';
import type { Writable } from 'svelte/store';
export let branch: Branch;
@ -22,7 +16,7 @@
export let type: CommitStatus;
export let githubService: GitHubService;
export let branchService: BranchService;
export let selectedFiles: Writable<(LocalFile | RemoteFile)[]>;
export let selectedFiles: Writable<AnyFile[]>;
export let isUnapplied: boolean;
export let branchCount: number = 0;

View File

@ -16,8 +16,7 @@
type BaseBranch,
type Branch,
type Commit,
type LocalFile,
RemoteFile
type AnyFile
} from '$lib/vbranches/types';
import { get, type Writable } from 'svelte/store';
import type { Project } from '$lib/backend/projects';
@ -31,7 +30,7 @@
export let isChained: boolean;
export let isUnapplied = false;
export let branchController: BranchController;
export let selectedFiles: Writable<(LocalFile | RemoteFile)[]>;
export let selectedFiles: Writable<AnyFile[]>;
function acceptAmend(commit: Commit | RemoteCommit) {
if (commit instanceof RemoteCommit) {

View File

@ -12,12 +12,12 @@
import { slide } from 'svelte/transition';
import type { BranchController } from '$lib/vbranches/branchController';
import type { Ownership } from '$lib/vbranches/ownership';
import type { LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { AnyFile } from '$lib/vbranches/types';
import type { Writable } from 'svelte/store';
export let projectId: string;
export let branchId: string;
export let file: LocalFile | RemoteFile;
export let file: AnyFile;
export let conflicted: boolean;
export let projectPath: string | undefined;
export let branchController: BranchController;
@ -35,7 +35,7 @@
let sections: (HunkSection | ContentSection)[] = [];
function parseFile(file: LocalFile | RemoteFile) {
function parseFile(file: AnyFile) {
// When we toggle expansion status on sections we need to assign
// `sections = sections` to redraw, and why we do not use a reactive
// variable.

View File

@ -6,9 +6,9 @@
import { computeFileStatus } from '$lib/utils/fileStatus';
import { computeAddedRemovedByFiles } from '$lib/utils/metrics';
import { createEventDispatcher } from 'svelte';
import type { LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { AnyFile } from '$lib/vbranches/types';
export let file: LocalFile | RemoteFile;
export let file: AnyFile;
export let isFileLocked: boolean;
const dispatch = createEventDispatcher<{ close: void }>();

View File

@ -5,16 +5,16 @@
import { draggableFile } from '$lib/dragging/draggables';
import { getVSIFileIcon } from '$lib/ext-icons';
import type { Ownership } from '$lib/vbranches/ownership';
import type { LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { AnyFile } from '$lib/vbranches/types';
import type { Writable } from 'svelte/store';
export let branchId: string;
export let file: LocalFile | RemoteFile;
export let file: AnyFile;
export let isUnapplied: boolean;
export let selected: boolean;
export let showCheckbox: boolean = false;
export let selectedOwnership: Writable<Ownership>;
export let selectedFiles: Writable<(LocalFile | RemoteFile)[]>;
export let selectedFiles: Writable<AnyFile[]>;
let checked = false;
let indeterminate = false;

View File

@ -2,9 +2,9 @@
import FileStatusCircle from './FileStatusCircle.svelte';
import Icon from '$lib/components/Icon.svelte';
import { computeFileStatus } from '$lib/utils/fileStatus';
import type { LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { AnyFile } from '$lib/vbranches/types';
export let file: LocalFile | RemoteFile;
export let file: AnyFile;
$: isLocked = file.hunks.some((h) => h.locked);
</script>

View File

@ -7,7 +7,7 @@
import TreeListFolder from './TreeListFolder.svelte';
import type { TreeNode } from '$lib/vbranches/filetree';
import type { Ownership } from '$lib/vbranches/ownership';
import type { LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { AnyFile } from '$lib/vbranches/types';
import type { Writable } from 'svelte/store';
export let expanded = true;
@ -15,7 +15,7 @@
export let isRoot = false;
export let showCheckboxes = false;
export let selectedOwnership: Writable<Ownership>;
export let selectedFiles: Writable<(LocalFile | RemoteFile)[]>;
export let selectedFiles: Writable<AnyFile[]>;
export let branchId: string;
export let isUnapplied: boolean;
export let allowMultiple = false;

View File

@ -1,16 +1,16 @@
<script lang="ts">
import Button from '$lib/components/Button.svelte';
import CommitCard from '$lib/components/CommitCard.svelte';
import type { BranchController } from '$lib/vbranches/branchController';
import type { LocalFile, RemoteBranch, RemoteFile } from '$lib/vbranches/types';
import { writable } from 'svelte/store';
import type { BranchController } from '$lib/vbranches/branchController';
import type { AnyFile, RemoteBranch } from '$lib/vbranches/types';
export let branch: RemoteBranch | undefined;
export let projectId: string;
export let projectPath: string;
export let branchController: BranchController;
const selectedFiles = writable<(LocalFile | RemoteFile)[]>([]);
const selectedFiles = writable<AnyFile[]>([]);
</script>
{#if branch != undefined}

View File

@ -5,16 +5,16 @@
import { draggableFile } from '$lib/dragging/draggables';
import { getVSIFileIcon } from '$lib/ext-icons';
import type { Ownership } from '$lib/vbranches/ownership';
import type { LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { AnyFile } from '$lib/vbranches/types';
import type { Writable } from 'svelte/store';
export let branchId: string;
export let file: LocalFile | RemoteFile;
export let file: AnyFile;
export let selected: boolean;
export let isUnapplied: boolean;
export let showCheckbox: boolean = false;
export let selectedOwnership: Writable<Ownership>;
export let selectedFiles: Writable<(LocalFile | RemoteFile)[]>;
export let selectedFiles: Writable<AnyFile[]>;
let checked = false;
let indeterminate = false;

View File

@ -1,4 +1,4 @@
import type { Commit, LocalFile, Hunk, RemoteCommit, RemoteFile } from '../vbranches/types';
import type { AnyFile, Commit, LocalFile, Hunk, RemoteCommit } from '../vbranches/types';
import type { Writable } from 'svelte/store';
export function nonDraggable() {
@ -23,15 +23,11 @@ export function isDraggableHunk(obj: any): obj is DraggableHunk {
export type DraggableFile = {
branchId: string;
files: Writable<(LocalFile | RemoteFile)[]>;
files: Writable<AnyFile[]>;
current: LocalFile;
};
export function draggableFile(
branchId: string,
current: LocalFile | RemoteFile,
files: Writable<(LocalFile | RemoteFile)[]>
) {
export function draggableFile(branchId: string, current: AnyFile, files: Writable<AnyFile[]>) {
return { data: { branchId, current, files } };
}

View File

@ -1,6 +1,6 @@
import { LocalFile } from '$lib/vbranches/types';
import { plainToInstance } from 'class-transformer';
import type { Hunk, RemoteFile, RemoteHunk } from '$lib/vbranches/types';
import type { AnyFile, Hunk, RemoteHunk } from '$lib/vbranches/types';
export type Line = {
beforeLineNumber: number | undefined;
@ -146,7 +146,7 @@ export function parseHunkSection(hunk: Hunk | RemoteHunk): HunkSection {
return hunkSection;
}
export function parseFileSections(file: LocalFile | RemoteFile): (ContentSection | HunkSection)[] {
export function parseFileSections(file: AnyFile): (ContentSection | HunkSection)[] {
const hunkSections = file.hunks
.map(parseHunkSection)
.filter((hunkSection) => hunkSection !== undefined)

View File

@ -1,8 +1,8 @@
import { RemoteFile, type LocalFile } from '$lib/vbranches/types';
import { RemoteFile, type AnyFile } from '$lib/vbranches/types';
export type FileStatus = 'A' | 'M' | 'D';
export function computeFileStatus(file: LocalFile | RemoteFile): FileStatus {
export function computeFileStatus(file: AnyFile): FileStatus {
if (file instanceof RemoteFile) {
// TODO: How do we compute this for remote files?
return 'M';

View File

@ -1,7 +1,7 @@
import { HunkSection, type ContentSection } from './fileSections';
import type { LocalFile, RemoteFile } from '$lib/vbranches/types';
import type { AnyFile } from '$lib/vbranches/types';
export function computeAddedRemovedByFiles(...files: (LocalFile | RemoteFile)[]) {
export function computeAddedRemovedByFiles(...files: AnyFile[]) {
return files
.flatMap((f) => f.hunks)
.map((h) => h.diff.split('\n'))

View File

@ -4,11 +4,11 @@
* This module provides support for tranforming a list of files into a
* hirerarchical structure for easy rendering.
*/
import type { LocalFile, RemoteFile } from './types';
import type { AnyFile } from './types';
export interface TreeNode {
name: string;
file?: LocalFile | RemoteFile;
file?: AnyFile;
children: TreeNode[];
parent?: TreeNode;
}
@ -40,7 +40,7 @@ export function sortChildren(node: TreeNode) {
}
}
export function filesToFileTree(files: (LocalFile | RemoteFile)[]): TreeNode {
export function filesToFileTree(files: AnyFile[]): TreeNode {
const acc: TreeNode = { name: 'root', children: [] };
files.forEach((f) => {
const pathParts = f.path.split('/');
@ -51,8 +51,8 @@ export function filesToFileTree(files: (LocalFile | RemoteFile)[]): TreeNode {
return acc;
}
function fileTreeToList(node: TreeNode): (LocalFile | RemoteFile)[] {
const list: (LocalFile | RemoteFile)[] = [];
function fileTreeToList(node: TreeNode): AnyFile[] {
const list: AnyFile[] = [];
if (node.file) list.push(node.file);
node.children.forEach((child) => {
list.push(...fileTreeToList(child));
@ -61,6 +61,6 @@ function fileTreeToList(node: TreeNode): (LocalFile | RemoteFile)[] {
}
// Sorts a file list the same way it is sorted in a file tree
export function sortLikeFileTree(files: (LocalFile | RemoteFile)[]): (LocalFile | RemoteFile)[] {
export function sortLikeFileTree(files: AnyFile[]): AnyFile[] {
return fileTreeToList(filesToFileTree(files));
}

View File

@ -1,6 +1,6 @@
import type { Branch, LocalFile, RemoteFile } from './types';
import type { Branch, AnyFile } from './types';
export function filesToOwnership(files: (LocalFile | RemoteFile)[]) {
export function filesToOwnership(files: AnyFile[]) {
return files.map((f) => `${f.path}:${f.hunks.map(({ id }) => id).join(',')}`).join('\n');
}

View File

@ -1,6 +1,6 @@
import 'reflect-metadata';
import { Type, Transform } from 'class-transformer';
import { hashCode } from '$lib/utils/string';
import { Type, Transform } from 'class-transformer';
export type ChangeType =
/// Entry does not exist in old version
@ -23,6 +23,8 @@ export class Hunk {
changeType!: ChangeType;
}
export type AnyFile = LocalFile | RemoteFile;
export class LocalFile {
id!: string;
path!: string;
@ -39,8 +41,9 @@ export class LocalFile {
binary!: boolean;
large!: boolean;
get filename() {
return this.path.split('/').at(-1);
get filename(): string {
const parts = this.path.split('/');
return parts[parts.length - 1];
}
get justpath() {
@ -159,10 +162,6 @@ export class RemoteFile {
return this.path.split('/').slice(0, -1).join('/');
}
get locked() {
return false;
}
get large() {
return false;
}