|
1 | 1 | use but_settings::AppSettings;
|
2 | 2 | use colored::Colorize;
|
3 | 3 | use gitbutler_branch::BranchCreateRequest;
|
4 |
| -use gitbutler_branch_actions::{create_virtual_branch, create_virtual_branch_from_branch}; |
| 4 | +use gitbutler_branch_actions::{create_virtual_branch, create_virtual_branch_from_branch, unapply_stack}; |
5 | 5 | use gitbutler_command_context::CommandContext;
|
6 | 6 | use gitbutler_project::Project;
|
7 | 7 | use gitbutler_reference::Refname;
|
@@ -115,3 +115,86 @@ pub(crate) fn create_branch(
|
115 | 115 |
|
116 | 116 | Ok(())
|
117 | 117 | }
|
| 118 | + |
| 119 | +pub(crate) fn unapply_branch( |
| 120 | + repo_path: &Path, |
| 121 | + _json: bool, |
| 122 | + branch_id: &str, |
| 123 | +) -> anyhow::Result<()> { |
| 124 | + let project = Project::from_path(repo_path)?; |
| 125 | + let mut ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?; |
| 126 | + |
| 127 | + // Try to resolve the branch ID |
| 128 | + let cli_ids = CliId::from_str(&mut ctx, branch_id)?; |
| 129 | + |
| 130 | + if cli_ids.is_empty() { |
| 131 | + return Err(anyhow::anyhow!( |
| 132 | + "Branch '{}' not found. Try using a branch CLI ID or full branch name.", |
| 133 | + branch_id |
| 134 | + )); |
| 135 | + } |
| 136 | + |
| 137 | + if cli_ids.len() > 1 { |
| 138 | + let matches: Vec<String> = cli_ids.iter().map(|id| { |
| 139 | + match id { |
| 140 | + CliId::Branch { name } => format!("{} (branch '{}')", id.to_string(), name), |
| 141 | + _ => format!("{} ({})", id.to_string(), id.kind()) |
| 142 | + } |
| 143 | + }).collect(); |
| 144 | + return Err(anyhow::anyhow!( |
| 145 | + "Branch '{}' is ambiguous. Matches: {}. Try using more characters or the full branch name.", |
| 146 | + branch_id, |
| 147 | + matches.join(", ") |
| 148 | + )); |
| 149 | + } |
| 150 | + |
| 151 | + let cli_id = &cli_ids[0]; |
| 152 | + let stack_id = match cli_id { |
| 153 | + CliId::Branch { .. } => { |
| 154 | + // Find the stack ID for this branch |
| 155 | + let stacks = crate::log::stacks(&ctx)?; |
| 156 | + let stack = stacks.iter().find(|s| { |
| 157 | + s.heads.iter().any(|head| { |
| 158 | + if let CliId::Branch { name } = cli_id { |
| 159 | + head.name.to_string() == *name |
| 160 | + } else { |
| 161 | + false |
| 162 | + } |
| 163 | + }) |
| 164 | + }); |
| 165 | + |
| 166 | + match stack { |
| 167 | + Some(s) => s.id.ok_or_else(|| anyhow::anyhow!("Stack has no ID"))?, |
| 168 | + None => return Err(anyhow::anyhow!("No stack found for branch '{}'", branch_id)), |
| 169 | + } |
| 170 | + } |
| 171 | + _ => { |
| 172 | + return Err(anyhow::anyhow!( |
| 173 | + "ID '{}' does not refer to a branch (it's {})", |
| 174 | + branch_id, |
| 175 | + cli_id.kind() |
| 176 | + )); |
| 177 | + } |
| 178 | + }; |
| 179 | + |
| 180 | + let branch_name = match cli_id { |
| 181 | + CliId::Branch { name } => name, |
| 182 | + _ => unreachable!(), |
| 183 | + }; |
| 184 | + |
| 185 | + println!( |
| 186 | + "Unapplying branch '{}' ({})", |
| 187 | + branch_name.yellow().bold(), |
| 188 | + branch_id.blue().underline() |
| 189 | + ); |
| 190 | + |
| 191 | + unapply_stack(&ctx, stack_id, Vec::new())?; |
| 192 | + |
| 193 | + println!( |
| 194 | + "{} Branch '{}' unapplied successfully!", |
| 195 | + "✓".green().bold(), |
| 196 | + branch_name.yellow().bold() |
| 197 | + ); |
| 198 | + |
| 199 | + Ok(()) |
| 200 | +} |
0 commit comments