|
| 1 | +use std::collections::HashSet; |
| 2 | + |
1 | 3 | use anyhow::{Context, Result};
|
2 | 4 | use bstr::ByteSlice;
|
3 | 5 | use but_core::diff::tree_changes;
|
| 6 | +use but_hunk_assignment::HunkAssignmentRequest; |
4 | 7 | use but_workspace::DiffSpec;
|
5 | 8 | use gitbutler_branch_actions::update_workspace_commit;
|
6 | 9 | use gitbutler_command_context::CommandContext;
|
7 | 10 | use gitbutler_stack::VirtualBranchesHandle;
|
8 | 11 |
|
9 |
| -use crate::rub::undo::stack_id_by_commit_id; |
| 12 | +use crate::rub::{assign::branch_name_to_stack_id, undo::stack_id_by_commit_id}; |
10 | 13 |
|
11 | 14 | pub fn commited_file_to_another_commit(
|
12 | 15 | ctx: &mut CommandContext,
|
@@ -45,3 +48,80 @@ pub fn commited_file_to_another_commit(
|
45 | 48 |
|
46 | 49 | Ok(())
|
47 | 50 | }
|
| 51 | + |
| 52 | +pub fn commited_file_to_unassigned_stack( |
| 53 | + ctx: &mut CommandContext, |
| 54 | + path: &str, |
| 55 | + source_id: gix::ObjectId, |
| 56 | + target_branch: &str, |
| 57 | +) -> Result<()> { |
| 58 | + let source_stack = stack_id_by_commit_id(ctx, &source_id)?; |
| 59 | + let target_stack = branch_name_to_stack_id(ctx, Some(target_branch))?; |
| 60 | + |
| 61 | + let repo = ctx.gix_repo()?; |
| 62 | + |
| 63 | + let source_commit = repo.find_commit(source_id)?; |
| 64 | + let source_commit_parent_id = source_commit.parent_ids().next().context("First parent")?; |
| 65 | + |
| 66 | + let (tree_changes, _) = tree_changes(&repo, Some(source_commit_parent_id.detach()), source_id)?; |
| 67 | + let relevant_changes = tree_changes |
| 68 | + .into_iter() |
| 69 | + .filter(|tc| tc.path.to_str_lossy() == path) |
| 70 | + .map(Into::into) |
| 71 | + .collect::<Vec<DiffSpec>>(); |
| 72 | + |
| 73 | + // If we want to assign the changes after uncommitting, we could try to do |
| 74 | + // something with the hunk headers, but this is not precise as the hunk |
| 75 | + // headers might have changed from what they were like when they were |
| 76 | + // committed. |
| 77 | + // |
| 78 | + // As such, we take all the old assignments, and all the new assignments from after the |
| 79 | + // uncommit, and find the ones that are not present in the old assignments. |
| 80 | + // We then convert those into assignment requests for the given stack. |
| 81 | + let before_assignments = but_hunk_assignment::assignments_with_fallback( |
| 82 | + ctx, |
| 83 | + false, |
| 84 | + None::<Vec<but_core::TreeChange>>, |
| 85 | + None, |
| 86 | + )? |
| 87 | + .0; |
| 88 | + |
| 89 | + but_workspace::remove_changes_from_commit_in_stack( |
| 90 | + &ctx, |
| 91 | + source_stack, |
| 92 | + source_id, |
| 93 | + relevant_changes, |
| 94 | + ctx.app_settings().context_lines, |
| 95 | + )?; |
| 96 | + |
| 97 | + let vb_state = VirtualBranchesHandle::new(ctx.project().gb_dir()); |
| 98 | + update_workspace_commit(&vb_state, ctx)?; |
| 99 | + |
| 100 | + let (after_assignments, _) = but_hunk_assignment::assignments_with_fallback( |
| 101 | + ctx, |
| 102 | + false, |
| 103 | + None::<Vec<but_core::TreeChange>>, |
| 104 | + None, |
| 105 | + )?; |
| 106 | + |
| 107 | + let before_assignments = before_assignments |
| 108 | + .into_iter() |
| 109 | + .filter_map(|a| a.id) |
| 110 | + .collect::<HashSet<_>>(); |
| 111 | + |
| 112 | + let to_assign = after_assignments |
| 113 | + .into_iter() |
| 114 | + .filter(|a| a.id.is_some_and(|id| !before_assignments.contains(&id))) |
| 115 | + .map(|a| HunkAssignmentRequest { |
| 116 | + hunk_header: a.hunk_header, |
| 117 | + path_bytes: a.path_bytes, |
| 118 | + stack_id: target_stack, |
| 119 | + }) |
| 120 | + .collect::<Vec<_>>(); |
| 121 | + |
| 122 | + but_hunk_assignment::assign(ctx, to_assign, None)?; |
| 123 | + |
| 124 | + println!("Uncommitted changes"); |
| 125 | + |
| 126 | + Ok(()) |
| 127 | +} |
0 commit comments