Skip to content

Commit

Permalink
Reorder stack API stub
Browse files Browse the repository at this point in the history
  • Loading branch information
krlvi committed Oct 21, 2024
1 parent 3e131b4 commit 497d6bd
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
12 changes: 12 additions & 0 deletions crates/gitbutler-branch-actions/src/actions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::r#virtual as vbranch;
use crate::branch_upstream_integration;
use crate::move_commits;
use crate::reorder::{self, StackOrder};
use crate::reorder_commits;
use crate::upstream_integration::{
self, BaseBranchResolution, BaseBranchResolutionApproach, BranchStatuses, Resolution,
Expand Down Expand Up @@ -348,6 +349,17 @@ pub fn insert_blank_commit(
vbranch::insert_blank_commit(&ctx, branch_id, commit_oid, offset).map_err(Into::into)
}

pub fn reorder_stack(project: &Project, stack_id: StackId, stack_order: StackOrder) -> Result<()> {
let ctx = open_with_verify(project)?;
assure_open_workspace_mode(&ctx).context("Reordering a commit requires open workspace mode")?;
let mut guard = project.exclusive_worktree_access();
let _ = ctx.project().create_snapshot(
SnapshotDetails::new(OperationKind::ReorderCommit),
guard.write_permission(),
);
reorder::reorder_stack(&ctx, stack_id, stack_order, guard.write_permission())
}

pub fn reorder_commit(
project: &Project,
branch_id: StackId,
Expand Down
12 changes: 7 additions & 5 deletions crates/gitbutler-branch-actions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ pub use actions::{
get_uncommited_files_reusable, insert_blank_commit, integrate_upstream,
integrate_upstream_commits, list_local_branches, list_remote_commit_files,
list_virtual_branches, list_virtual_branches_cached, move_commit, move_commit_file,
push_base_branch, push_virtual_branch, reorder_commit, reset_files, reset_virtual_branch,
resolve_upstream_integration, save_and_unapply_virutal_branch, set_base_branch,
set_target_push_remote, squash, unapply_ownership, unapply_without_saving_virtual_branch,
undo_commit, update_branch_order, update_commit_message, update_virtual_branch,
upstream_integration_statuses,
push_base_branch, push_virtual_branch, reorder_commit, reorder_stack, reset_files,
reset_virtual_branch, resolve_upstream_integration, save_and_unapply_virutal_branch,
set_base_branch, set_target_push_remote, squash, unapply_ownership,
unapply_without_saving_virtual_branch, undo_commit, update_branch_order, update_commit_message,
update_virtual_branch, upstream_integration_statuses,
};

mod r#virtual;
Expand Down Expand Up @@ -47,6 +47,8 @@ pub mod conflicts;
pub mod branch_trees;
pub mod branch_upstream_integration;
mod move_commits;
mod reorder;
pub use reorder::{SeriesOrder, StackOrder};
mod reorder_commits;
mod undo_commit;

Expand Down
46 changes: 46 additions & 0 deletions crates/gitbutler-branch-actions/src/reorder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use anyhow::Result;
use gitbutler_command_context::CommandContext;
use gitbutler_project::access::WorktreeWritePermission;
use gitbutler_stack::StackId;

use serde::Serialize;

use crate::VirtualBranchesExt;

pub fn reorder_stack(
ctx: &CommandContext,
branch_id: StackId,
stack_order: StackOrder,
perm: &mut WorktreeWritePermission,
) -> Result<()> {
let state = ctx.project().virtual_branches();
let stack = state.get_branch(branch_id)?;
let repo = ctx.repository();
Ok(())
}

/// Represents the order of series (branches) and changes (commits) in a stack.
#[derive(Debug, PartialEq, Eq, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StackOrder {
/// The series are ordered from newest to oldest (most recent stacks go first)
series: Vec<SeriesOrder>,
}
/// Represents the order of changes (commits) in a series (branch).
#[derive(Debug, PartialEq, Eq, Clone, Serialize)]
pub struct SeriesOrder {
/// Unique name of the series (branch). Must already exist in the stack.
name: String,
/// The changes are ordered from newest to oldest (most recent changes go first)
/// The change ids must refer to commits that already exist in the stack.
change_ids: Vec<String>,
}

impl SeriesOrder {
/// Ensures that:
/// - The series exists in the stack
/// - The change ids refer to commits that already exist in the stack
fn validate(&self, repo: &git2::Repository) -> Result<()> {
Ok(())
}
}
16 changes: 15 additions & 1 deletion crates/gitbutler-tauri/src/virtual_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod commands {
};
use gitbutler_branch_actions::{
BaseBranch, BranchListing, BranchListingDetails, BranchListingFilter, RemoteBranch,
RemoteBranchData, RemoteBranchFile, RemoteCommit, VirtualBranches,
RemoteBranchData, RemoteBranchFile, RemoteCommit, StackOrder, VirtualBranches,
};
use gitbutler_command_context::CommandContext;
use gitbutler_project as projects;
Expand Down Expand Up @@ -397,6 +397,20 @@ pub mod commands {
Ok(())
}

#[tauri::command(async)]
#[instrument(skip(projects, windows), err(Debug))]
pub fn reorder_stack(
windows: State<'_, WindowState>,
projects: State<'_, projects::Controller>,
project_id: ProjectId,
branch_id: StackId,
stack_order: StackOrder,
) -> Result<(), Error> {
let project = projects.get(project_id)?;
gitbutler_branch_actions::reorder_stack(&project, branch_id, stack_order)?;
emit_vbranches(&windows, project_id);
Ok(())
}
#[tauri::command(async)]
#[instrument(skip(projects, windows), err(Debug))]
pub fn reorder_commit(
Expand Down

0 comments on commit 497d6bd

Please sign in to comment.