Skip to content

Commit

Permalink
Merge pull request #5335 from gitbutlerapp/kv-branch-1
Browse files Browse the repository at this point in the history
Fix - prevent pushing of already integrated branches when in a stack
  • Loading branch information
krlvi authored Oct 28, 2024
2 parents 3b8a96c + 3d4f165 commit 9e5496e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
23 changes: 20 additions & 3 deletions crates/gitbutler-branch-actions/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use gitbutler_commit::commit_ext::CommitExt;
use gitbutler_patch_reference::{CommitOrChangeId, PatchReference};
use gitbutler_project::Project;
use gitbutler_repo_actions::RepoActionsExt;
use gitbutler_stack::PatchReferenceUpdate;
use gitbutler_stack::{PatchReferenceUpdate, Series};
use gitbutler_stack::{Stack, StackId, Target};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -130,20 +130,27 @@ pub fn push_stack(project: &Project, branch_id: StackId, with_force: bool) -> Re
let stack = state.get_branch(branch_id)?;

let repo = ctx.repository();
let merge_base =
repo.find_commit(repo.merge_base(stack.head(), state.get_default_target()?.sha)?)?;
let default_target = state.get_default_target()?;
let merge_base = repo.find_commit(repo.merge_base(stack.head(), default_target.sha)?)?;
let merge_base = if let Some(change_id) = merge_base.change_id() {
CommitOrChangeId::ChangeId(change_id)
} else {
CommitOrChangeId::CommitId(merge_base.id().to_string())
};

// First fetch, because we dont want to push integrated series
ctx.fetch(&default_target.push_remote_name(), None)?;
let check_commit = IsCommitIntegrated::new(ctx, &default_target)?;
let stack_series = stack.list_series(ctx)?;
for series in stack_series {
if series.head.target == merge_base {
// Nothing to push for this one
continue;
}
if series_integrated(&check_commit, &series)? {
// Already integrated, nothing to push
continue;
}
let push_details = stack.push_details(ctx, series.head.name)?;
ctx.push(
push_details.head,
Expand All @@ -156,6 +163,16 @@ pub fn push_stack(project: &Project, branch_id: StackId, with_force: bool) -> Re
Ok(())
}

fn series_integrated(check_commit: &IsCommitIntegrated, series: &Series) -> Result<bool> {
let mut is_integrated = false;
for commit in series.clone().local_commits.iter().rev() {
if !is_integrated {
is_integrated = check_commit.is_integrated(commit)?;
}
}
Ok(is_integrated)
}

/// Returns the stack series for the API.
/// Newest first, oldest last in the list
pub(crate) fn stack_series(
Expand Down
2 changes: 1 addition & 1 deletion crates/gitbutler-branch-actions/src/virtual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ pub(crate) struct IsCommitIntegrated<'repo> {
}

impl<'repo> IsCommitIntegrated<'repo> {
fn new(ctx: &'repo CommandContext, target: &Target) -> anyhow::Result<Self> {
pub(crate) fn new(ctx: &'repo CommandContext, target: &Target) -> anyhow::Result<Self> {
let remote_branch = ctx
.repository()
.maybe_find_branch_by_refname(&target.branch.clone().into())?
Expand Down

0 comments on commit 9e5496e

Please sign in to comment.