adds the ability to create branch refs from the UI

This commit is contained in:
Kiril Videlov 2024-08-23 18:07:24 +02:00
parent 4668a1ec3d
commit 33e7d7b269
No known key found for this signature in database
GPG Key ID: A4C733025427C471
5 changed files with 97 additions and 1 deletions

View File

@ -4,10 +4,12 @@
import { BaseBranch } from '$lib/baseBranch/baseBranch';
import CommitMessageInput from '$lib/commit/CommitMessageInput.svelte';
import { persistedCommitMessage } from '$lib/config/config';
import { featureBranchStacking } from '$lib/config/uiFeatureFlags';
import { draggableCommit } from '$lib/dragging/draggable';
import { DraggableCommit, nonDraggable } from '$lib/dragging/draggables';
import BranchFilesList from '$lib/file/BranchFilesList.svelte';
import { ModeService } from '$lib/modes/service';
import TextBox from '$lib/shared/TextBox.svelte';
import { copyToClipboard } from '$lib/utils/clipboard';
import { getContext, getContextStore, maybeGetContext } from '$lib/utils/context';
import { openExternalUrl } from '$lib/utils/url';
@ -48,6 +50,8 @@
const currentCommitMessage = persistedCommitMessage(project.id, branch?.id || '');
const branchStacking = featureBranchStacking();
let draggableCommitElement: HTMLElement | null = null;
let files: RemoteFile[] = [];
let showDetails = false;
@ -82,6 +86,14 @@
let commitMessageValid = false;
let description = '';
let createRefModal: Modal;
let createRefName = $baseBranch.remoteName + '/';
function openCreateRefModal(e: Event, commit: DetailedCommit | Commit) {
e.stopPropagation();
createRefModal.show(commit);
}
function openCommitMessageModal(e: Event) {
e.stopPropagation();
@ -156,6 +168,29 @@
{/snippet}
</Modal>
<Modal bind:this={createRefModal} width="small">
{#snippet children(commit)}
<TextBox label="Remote branch name" id="newRemoteName" bind:value={createRefName} focus />
<Button
style="pop"
kind="solid"
onclick={() => {
branchController.createBranchReference(
branch?.id || '',
'refs/remotes/' + createRefName,
commit.id
);
createRefModal.close();
}}
>
Ok
</Button>
{/snippet}
{#snippet controls(close)}
<Button style="ghost" outline type="reset" onclick={close}>Cancel</Button>
{/snippet}
</Modal>
<div
class="commit-row"
class:is-commit-open={showDetails}
@ -352,6 +387,15 @@
icon="edit-small"
onclick={openCommitMessageModal}>Edit message</Button
>
{#if $branchStacking}
<Button
size="tag"
style="ghost"
outline
icon="branch"
onclick={(e: Event) => {openCreateRefModal(e, commit)}}>Create ref</Button
>
{/if}
{/if}
{#if canEdit() && project.succeedingRebases}
<Button size="tag" style="ghost" outline onclick={editPatch}>

View File

@ -96,6 +96,26 @@ export class BranchController {
}
}
/*
* Creates a new GitButler reference associated with a branch.
* @param branchId
* @param reference in the format refs/remotes/origin/my-branch (must be remote)
* @param commitOid The commit oid to point the reference
*/
async createBranchReference(branchId: string, reference: string, commitOid: string) {
console.log('createBranchReference', branchId, reference, commitOid);
try {
await invoke<void>('create_branch_reference', {
projectId: this.projectId,
branchId: branchId,
reference: reference,
commitOid: commitOid
});
} catch (err) {
showError('Failed to create branch reference', err);
}
}
async updateBranchRemoteName(branchId: string, upstream: string) {
try {
await invoke<void>('update_virtual_branch', {

View File

@ -1,5 +1,7 @@
use anyhow::{Context, Result};
use gitbutler_branch::{BranchCreateRequest, BranchId, BranchOwnershipClaims, BranchUpdateRequest};
use gitbutler_branch::{
BranchCreateRequest, BranchId, BranchOwnershipClaims, BranchReference, BranchUpdateRequest,
};
use gitbutler_command_context::CommandContext;
use gitbutler_operating_modes::assure_open_workspace_mode;
use gitbutler_oplog::{
@ -355,6 +357,18 @@ impl VirtualBranchActions {
branch::insert_blank_commit(&ctx, branch_id, commit_oid, offset).map_err(Into::into)
}
pub fn create_branch_reference(
&self,
project: &Project,
branch_id: BranchId,
reference: ReferenceName,
commit_oid: git2::Oid,
) -> Result<BranchReference> {
let ctx = open_with_verify(project)?;
assure_open_workspace_mode(&ctx).context("Requires an open workspace mode")?;
gitbutler_repo::create_branch_reference(&ctx, branch_id, reference, commit_oid)
}
pub fn reorder_commit(
&self,
project: &Project,

View File

@ -171,6 +171,7 @@ fn main() {
virtual_branches::commands::move_commit_file,
virtual_branches::commands::undo_commit,
virtual_branches::commands::insert_blank_commit,
virtual_branches::commands::create_branch_reference,
virtual_branches::commands::reorder_commit,
virtual_branches::commands::update_commit_message,
virtual_branches::commands::list_remote_branches,

View File

@ -396,6 +396,23 @@ pub mod commands {
Ok(())
}
#[tauri::command(async)]
#[instrument(skip(projects, windows), err(Debug))]
pub fn create_branch_reference(
windows: State<'_, WindowState>,
projects: State<'_, projects::Controller>,
project_id: ProjectId,
branch_id: BranchId,
reference: ReferenceName,
commit_oid: String,
) -> Result<(), Error> {
let project = projects.get(project_id)?;
let commit_oid = git2::Oid::from_str(&commit_oid).map_err(|e| anyhow!(e))?;
VirtualBranchActions.create_branch_reference(&project, branch_id, reference, commit_oid)?;
emit_vbranches(&windows, project_id);
Ok(())
}
#[tauri::command(async)]
#[instrument(skip(projects, windows), err(Debug))]
pub fn reorder_commit(