Implement new stackign api

This commit is contained in:
Kiril Videlov 2024-10-24 16:27:35 +02:00
parent 16135675bc
commit 7e1a5c54f2
4 changed files with 84 additions and 14 deletions

View File

@ -1,13 +1,19 @@
use anyhow::{bail, Result};
use anyhow::{bail, Context, Result};
use git2::Oid;
use gitbutler_command_context::CommandContext;
use gitbutler_project::access::WorktreeWritePermission;
use gitbutler_stack::{Series, StackId};
use gitbutler_repo::rebase::cherry_rebase_group;
use gitbutler_stack::{PatchReferenceUpdate, Series, StackId, TargetUpdate};
use itertools::Itertools;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use crate::VirtualBranchesExt;
use crate::{
branch_trees::{
checkout_branch_trees, compute_updated_branch_head_for_commits, BranchHeadAndTree,
},
VirtualBranchesExt,
};
/// This API allows the client to reorder commits in a stack.
/// Commits may be moved within the same series or between different series.
@ -24,17 +30,59 @@ pub fn reorder_stack(
ctx: &CommandContext,
branch_id: StackId,
stack_order: StackOrder,
_perm: &mut WorktreeWritePermission,
perm: &mut WorktreeWritePermission,
) -> Result<()> {
let state = ctx.project().virtual_branches();
let stack = state.get_branch(branch_id)?;
let repo = ctx.repository();
let mut stack = state.get_branch(branch_id)?;
let all_series = stack.list_series(ctx)?;
stack_order.validate(series_order(&all_series))?;
let default_target = state.get_default_target()?;
let default_target_commit = repo
.find_reference(&default_target.branch.to_string())?
.peel_to_commit()?;
let old_head = repo.find_commit(stack.head())?;
let merge_base = repo.merge_base(default_target_commit.id(), stack.head())?;
let ids_to_rebase = stack_order
.series
.iter()
.flat_map(|s| s.commit_ids.iter())
.cloned()
.collect_vec();
let new_head = cherry_rebase_group(repo, merge_base, &ids_to_rebase)?;
// Calculate the new head and tree
let BranchHeadAndTree {
head: new_head_oid,
tree: new_tree_oid,
} = compute_updated_branch_head_for_commits(repo, old_head.id(), old_head.tree_id(), new_head)?;
// Set the series heads accordingly
for (idx, series) in stack_order.series.iter().enumerate() {
let new_series_head_oid = series_head(&stack_order.series, merge_base);
let new_series_head = repo.find_commit(new_series_head_oid)?;
let preceding_head_name = stack_order.series.get(idx + 1).map(|s| s.name.clone());
let update = PatchReferenceUpdate {
target_update: Some(TargetUpdate {
target: new_series_head.into(),
preceding_head_name,
}),
..Default::default()
};
stack.update_series(ctx, series.name.clone(), &update)?;
}
stack.set_stack_head(ctx, new_head_oid, Some(new_tree_oid))?;
checkout_branch_trees(ctx, perm)?;
crate::integration::update_workspace_commit(&state, ctx)
.context("failed to update gitbutler workspace")?;
Ok(())
}
/// Represents the order of series (branches) and changes (commits) in a stack.
#[derive(Debug, PartialEq, Eq, Clone, Serialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StackOrder {
/// The series are ordered from newest to oldest (most recent stacks go first)
@ -42,7 +90,7 @@ pub struct StackOrder {
}
/// Represents the order of changes (commits) in a series (branch).
#[derive(Debug, PartialEq, Eq, Clone, Serialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct SeriesOrder {
/// Unique name of the series (branch). Must already exist in the stack.
name: String,
@ -53,6 +101,19 @@ pub struct SeriesOrder {
commit_ids: Vec<Oid>,
}
fn series_head(all_series: &Vec<SeriesOrder>, default_commit: Oid) -> Oid {
// The head is either:
// - the first commit in the series
// - the first commit in the next series
// - the merge base
for series in all_series {
if let Some(commit) = series.commit_ids.first() {
return *commit;
}
}
default_commit
}
impl StackOrder {
fn validate(&self, current_order: StackOrder) -> Result<()> {
// Ensure the number of series is the same between the reorder update request and the stack

View File

@ -353,10 +353,18 @@ impl Stack {
.ok_or_else(|| anyhow!("Series with name {} not found", branch_name))?;
new_head.target = target_update.target.clone();
validate_target(&new_head, ctx.repository(), self.head(), &state)?;
let preceding_head = update
let preceding_head = if let Some(preceding_head_name) = update
.target_update
.clone()
.and_then(|update| update.preceding_head);
.and_then(|update| update.preceding_head_name)
{
let (_, preceding_head) = get_head(&self.heads, &preceding_head_name)
.context("The specified preceding_head could not be found")?;
Some(preceding_head)
} else {
None
};
// drop the old head and add the new one
let (idx, _) = get_head(&updated_heads, &branch_name)?;
updated_heads.remove(idx);
@ -646,7 +654,7 @@ pub struct TargetUpdate {
pub target: CommitOrChangeId,
/// If there are multiple heads that point to the same patch, the order can be disambiguated by specifying the `preceding_head`.
/// Leaving this field empty will make the new head first in relation to other references pointing to this commit.
pub preceding_head: Option<PatchReference>,
pub preceding_head_name: Option<String>,
}
/// Push details to be supplied to `RepoActionsExt`'s `push` method.

View File

@ -505,7 +505,7 @@ fn update_series_target_fails_commit_not_in_stack() -> Result<()> {
name: None,
target_update: Some(TargetUpdate {
target: CommitOrChangeId::CommitId(other_commit_id.clone()),
preceding_head: None,
preceding_head_name: None,
}),
description: None,
};
@ -533,7 +533,7 @@ fn update_series_target_orphan_commit_fails() -> Result<()> {
name: Some("new-lol".into()),
target_update: Some(TargetUpdate {
target: CommitOrChangeId::ChangeId(first_commit_change_id.clone()),
preceding_head: None,
preceding_head_name: None,
}),
description: None,
};
@ -568,7 +568,7 @@ fn update_series_target_success() -> Result<()> {
name: None,
target_update: Some(TargetUpdate {
target: commit_1_change_id.clone(),
preceding_head: None,
preceding_head_name: None,
}),
description: None,
};

View File

@ -177,6 +177,7 @@ fn main() {
virtual_branches::commands::undo_commit,
virtual_branches::commands::insert_blank_commit,
virtual_branches::commands::reorder_commit,
virtual_branches::commands::reorder_stack,
virtual_branches::commands::update_commit_message,
virtual_branches::commands::list_local_branches,
virtual_branches::commands::list_branches,