Skip to content

Commit

Permalink
Drop RemoteBranch in favor of RemoteBranchData
Browse files Browse the repository at this point in the history
- redundant data structure and api removed
- served same purpose as `find_git_branches`
  • Loading branch information
mtsgrd committed Nov 19, 2024
1 parent ee7a214 commit 202fcd1
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 174 deletions.
6 changes: 3 additions & 3 deletions apps/desktop/src/lib/branch/BranchPreviewHeader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import Modal from '@gitbutler/ui/Modal.svelte';
import Tooltip from '@gitbutler/ui/Tooltip.svelte';
import type { PullRequest } from '$lib/forge/interface/types';
import type { Branch } from '$lib/vbranches/types';
import type { BranchData } from '$lib/vbranches/types';
import { goto } from '$app/navigation';
export let localBranch: Branch | undefined;
export let remoteBranch: Branch | undefined;
export let localBranch: BranchData | undefined;
export let remoteBranch: BranchData | undefined;
export let pr: PullRequest | undefined;
$: branch = remoteBranch || localBranch!;
Expand Down
11 changes: 2 additions & 9 deletions apps/desktop/src/lib/branches/gitBranch.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { invoke } from '$lib/backend/ipc';
import { Branch, BranchData } from '$lib/vbranches/types';
import { BranchData } from '$lib/vbranches/types';
import { plainToInstance } from 'class-transformer';

export class GitBranchService {
constructor(private projectId: string) {}

async findBranches(name: string) {
return plainToInstance(
Branch,
await invoke<any[]>('find_git_branches', { projectId: this.projectId, branchName: name })
);
}

async getRemoteBranchData(refname: string): Promise<BranchData> {
return plainToInstance(
BranchData,
await invoke<any>('get_remote_branch_data', { projectId: this.projectId, refname })
await invoke<any[]>('find_git_branches', { projectId: this.projectId, branchName: name })
);
}
}
39 changes: 7 additions & 32 deletions apps/desktop/src/lib/components/BranchPreview.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import BranchPreviewHeader from '../branch/BranchPreviewHeader.svelte';
import Resizer from '../shared/Resizer.svelte';
import { GitBranchService } from '$lib/branches/gitBranch';
import CommitCard from '$lib/commit/CommitCard.svelte';
import { transformAnyCommit } from '$lib/commitLines/transformers';
import Markdown from '$lib/components/Markdown.svelte';
Expand All @@ -10,19 +9,18 @@
import ScrollableContainer from '$lib/scroll/ScrollableContainer.svelte';
import { SETTINGS, type Settings } from '$lib/settings/userSettings';
import { FileIdSelection } from '$lib/vbranches/fileIdSelection';
import { BranchData, type Branch } from '$lib/vbranches/types';
import { BranchData } from '$lib/vbranches/types';
import { getContext, getContextStoreBySymbol } from '@gitbutler/shared/context';
import Line from '@gitbutler/ui/commitLines/Line.svelte';
import { LineManagerFactory } from '@gitbutler/ui/commitLines/lineManager';
import lscache from 'lscache';
import { onMount, setContext } from 'svelte';
import type { PullRequest } from '$lib/forge/interface/types';
export let localBranch: Branch | undefined = undefined;
export let remoteBranch: Branch | undefined = undefined;
export let localBranch: BranchData | undefined = undefined;
export let remoteBranch: BranchData | undefined = undefined;
export let pr: PullRequest | undefined;
const gitBranchService = getContext(GitBranchService);
const forge = getForge();
const fileIdSelection = new FileIdSelection();
Expand All @@ -37,42 +35,19 @@
const userSettings = getContextStoreBySymbol<Settings>(SETTINGS);
const lineManagerFactory = getContext(LineManagerFactory);
let localBranchData: BranchData | undefined;
let remoteBranchData: BranchData | undefined;
// The remote branch service (which needs to be renamed) is responsible for
// fetching local and remote branches.
// We must manually set the branch data to undefined as the component
// doesn't get completely re-rendered on a page change.
$: if (localBranch) {
gitBranchService
.getRemoteBranchData(localBranch.name)
.then((branchData) => (localBranchData = branchData));
} else {
localBranchData = undefined;
}
$: if (remoteBranch) {
gitBranchService
.getRemoteBranchData(remoteBranch.name)
.then((branchData) => (remoteBranchData = branchData));
} else {
remoteBranchData = undefined;
}
$: remoteCommitShas = new Set(remoteBranchData?.commits.map((commit) => commit.id) || []);
$: remoteCommitShas = new Set(remoteBranch?.commits.map((commit) => commit.id) || []);
// Find commits common in the local and remote
$: localAndRemoteCommits =
localBranchData?.commits.filter((commit) => remoteCommitShas.has(commit.id)) || [];
localBranch?.commits.filter((commit) => remoteCommitShas.has(commit.id)) || [];
$: localAndRemoteCommitShas = new Set(localAndRemoteCommits.map((commit) => commit.id));
// Find the local and remote commits that are not shared
$: localCommits =
localBranchData?.commits.filter((commit) => !localAndRemoteCommitShas.has(commit.id)) || [];
localBranch?.commits.filter((commit) => !localAndRemoteCommitShas.has(commit.id)) || [];
$: remoteCommits =
remoteBranchData?.commits.filter((commit) => !localAndRemoteCommitShas.has(commit.id)) || [];
remoteBranch?.commits.filter((commit) => !localAndRemoteCommitShas.has(commit.id)) || [];
$: lineManager = lineManagerFactory.build({
remoteCommits: remoteCommits.map(transformAnyCommit),
Expand Down
21 changes: 17 additions & 4 deletions apps/desktop/src/lib/pr/PullRequestCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { getContext } from '@gitbutler/shared/context';
import Button from '@gitbutler/ui/Button.svelte';
import { type ComponentColor } from '@gitbutler/ui/utils/colorTypes';
import type { ForgeReview } from '$lib/forge/interface/forgeReviewService';
import type { DetailedPullRequest } from '$lib/forge/interface/types';
import type { MessageStyle } from '$lib/shared/InfoMessage.svelte';
import type iconsJson from '@gitbutler/ui/data/icons.json';
Expand All @@ -23,13 +24,21 @@
upstreamName: string;
pr: DetailedPullRequest;
checksMonitor?: ForgeChecksMonitor;
forgeReview?: ForgeReview;
reloadPR: () => void;
reopenPr: () => Promise<void>;
openPrDetailsModal: () => void;
}
const { upstreamName, pr, checksMonitor, reloadPR, reopenPr, openPrDetailsModal }: Props =
$props();
const {
upstreamName,
pr,
checksMonitor,
forgeReview,
reloadPR,
reopenPr,
openPrDetailsModal
}: Props = $props();
type StatusInfo = {
text: string;
Expand All @@ -55,6 +64,7 @@
const prService = getForgePrService();
const prMonitor = $derived(prNumber ? $prService?.prMonitor(prNumber) : undefined);
const reviewStatus = $derived(forgeReview?.status);
const checks = $derived(checksMonitor?.status);
Expand Down Expand Up @@ -212,7 +222,7 @@
>
{prStatusInfo.text}
</Button>
{#if !pr?.closedAt && checksTagInfo}
{#if !pr.closedAt && checksTagInfo}
<Button
size="tag"
clickable={false}
Expand All @@ -224,7 +234,10 @@
{checksTagInfo.text}
</Button>
{/if}
{#if pr?.htmlUrl}
{#if $reviewStatus?.approved}
<Button icon="success-small" size="tag" style="success" outline>Approved</Button>
{/if}
{#if pr.htmlUrl}
<Button
icon="open-link"
size="tag"
Expand Down
22 changes: 5 additions & 17 deletions apps/desktop/src/lib/vbranches/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export class VirtualBranch {
description!: string;
head!: string;
order!: number;
@Type(() => Branch)
upstream?: Branch;
@Type(() => BranchData)
upstream?: BranchData;
upstreamData?: BranchData;
upstreamName?: string;
conflicted!: boolean;
Expand Down Expand Up @@ -370,20 +370,6 @@ export interface Author {
isBot?: boolean;
}

export class Branch {
sha!: string;
name!: string;
upstream?: string;
lastCommitTimestampMs?: number | undefined;
lastCommitAuthor?: string | undefined;
givenName!: string;
isRemote!: boolean;

get displayName(): string {
return this.name.replace('refs/remotes/', '').replace('refs/heads/', '');
}
}

export class BranchData {
sha!: string;
name!: string;
Expand All @@ -393,6 +379,8 @@ export class BranchData {
commits!: Commit[];
isMergeable!: boolean | undefined;
forkPoint?: string | undefined;
isRemote!: boolean;
givenName!: string;

get ahead(): number {
return this.commits.length;
Expand All @@ -415,7 +403,7 @@ export class BranchData {
}

get displayName(): string {
return this.name.replace('refs/remotes/', '').replace('origin/', '').replace('refs/heads/', '');
return this.name.replace('refs/remotes/', '').replace('refs/heads/', '');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { GitBranchService } from '$lib/branches/gitBranch';
import BranchPreview from '$lib/components/BranchPreview.svelte';
import { getForgeListingService } from '$lib/forge/interface/forgeListingService';
import { Branch } from '$lib/vbranches/types';
import { BranchData } from '$lib/vbranches/types';
import { getContext } from '@gitbutler/shared/context';
import { page } from '$app/stores';
Expand All @@ -13,8 +13,8 @@
const prs = $derived($forgeListingService?.prs);
const pr = $derived($prs?.find((pr) => pr.sourceBranch === name));
let localBranch = $state<Branch>();
let remoteBranches = $state<Branch[]>([]);
let localBranch = $state<BranchData>();
let remoteBranches = $state<BranchData[]>([]);
let loading = $state(false);
$effect(() => {
Expand Down
9 changes: 2 additions & 7 deletions crates/gitbutler-branch-actions/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
branch_manager::BranchManagerExt,
file::RemoteBranchFile,
remote,
remote::{RemoteBranch, RemoteBranchData, RemoteCommit},
remote::{RemoteBranchData, RemoteCommit},
VirtualBranchesExt,
};
use anyhow::{Context, Result};
Expand Down Expand Up @@ -427,16 +427,11 @@ pub fn push_virtual_branch(
vbranch::push(&ctx, stack_id, with_force, askpass)
}

pub fn find_git_branches(project: Project, branch_name: &str) -> Result<Vec<RemoteBranch>> {
pub fn find_git_branches(project: Project, branch_name: &str) -> Result<Vec<RemoteBranchData>> {
let ctx = CommandContext::open(&project)?;
remote::find_git_branches(&ctx, branch_name)
}

pub fn get_remote_branch_data(project: &Project, refname: &Refname) -> Result<RemoteBranchData> {
let ctx = CommandContext::open(project)?;
remote::get_branch_data(&ctx, refname)
}

pub fn squash(project: &Project, stack_id: StackId, commit_oid: git2::Oid) -> Result<()> {
let ctx = open_with_verify(project)?;
assure_open_workspace_mode(&ctx).context("Squashing a commit requires open workspace mode")?;
Expand Down
11 changes: 5 additions & 6 deletions crates/gitbutler-branch-actions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ mod actions;
pub use actions::{
amend, can_apply_remote_branch, create_commit, create_virtual_branch,
create_virtual_branch_from_branch, delete_local_branch, fetch_from_remotes, find_commit,
find_git_branches, get_base_branch_data, get_remote_branch_data, get_uncommited_files,
get_uncommited_files_reusable, insert_blank_commit, integrate_upstream,
integrate_upstream_commits, list_commit_files, list_virtual_branches,
list_virtual_branches_cached, move_commit, move_commit_file, push_base_branch,
push_virtual_branch, reorder_stack, reset_files, reset_virtual_branch,
find_git_branches, get_base_branch_data, get_uncommited_files, get_uncommited_files_reusable,
insert_blank_commit, integrate_upstream, integrate_upstream_commits, list_commit_files,
list_virtual_branches, list_virtual_branches_cached, move_commit, move_commit_file,
push_base_branch, push_virtual_branch, reorder_stack, reset_files, reset_virtual_branch,
resolve_upstream_integration, save_and_unapply_virutal_branch, set_base_branch,
set_target_push_remote, squash, unapply_lines, unapply_ownership,
unapply_without_saving_virtual_branch, undo_commit, update_branch_order, update_commit_message,
Expand Down Expand Up @@ -43,7 +42,7 @@ mod file;
pub use file::{Get, RemoteBranchFile};

mod remote;
pub use remote::{RemoteBranch, RemoteBranchData, RemoteCommit};
pub use remote::{RemoteBranchData, RemoteCommit};

pub mod conflicts;

Expand Down
Loading

0 comments on commit 202fcd1

Please sign in to comment.