|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use crate::rub::branch_name_to_stack_id; |
| 4 | +use anyhow::bail; |
| 5 | +use but_rules::Operation; |
| 6 | +use but_settings::AppSettings; |
| 7 | +use gitbutler_command_context::CommandContext; |
| 8 | +use gitbutler_project::Project; |
| 9 | +pub(crate) fn handle(repo_path: &Path, _json: bool, target_str: &str) -> anyhow::Result<()> { |
| 10 | + let project = Project::from_path(repo_path).expect("Failed to create project from path"); |
| 11 | + let ctx = &mut CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?; |
| 12 | + let target_result = crate::id::CliId::from_str(ctx, target_str)?; |
| 13 | + if target_result.len() != 1 { |
| 14 | + return Err(anyhow::anyhow!( |
| 15 | + "Target {} is ambiguous: {:?}", |
| 16 | + target_str, |
| 17 | + target_result |
| 18 | + )); |
| 19 | + } |
| 20 | + match target_result[0].clone() { |
| 21 | + crate::id::CliId::Branch { name } => mark_branch(ctx, name), |
| 22 | + crate::id::CliId::Commit { oid } => mark_commit(oid), |
| 23 | + _ => bail!("Nope"), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn mark_commit(_oid: gix::ObjectId) -> anyhow::Result<()> { |
| 28 | + bail!("Not implemented yet"); |
| 29 | +} |
| 30 | + |
| 31 | +fn mark_branch(ctx: &mut CommandContext, branch_name: String) -> anyhow::Result<()> { |
| 32 | + let stack_id = |
| 33 | + branch_name_to_stack_id(ctx, Some(&branch_name))?.expect("Cant find stack for this branch"); |
| 34 | + let action = but_rules::Action::Explicit(Operation::Assign { |
| 35 | + target: but_rules::StackTarget::StackId(stack_id.to_string()), |
| 36 | + }); |
| 37 | + let req = but_rules::CreateRuleRequest { |
| 38 | + trigger: but_rules::Trigger::FileSytemChange, |
| 39 | + filters: vec![but_rules::Filter::PathMatchesRegex(regex::Regex::new( |
| 40 | + ".*", |
| 41 | + )?)], |
| 42 | + action, |
| 43 | + }; |
| 44 | + but_rules::create_rule(ctx, req)?; |
| 45 | + println!("Changes will be assigned to → {}", branch_name); |
| 46 | + Ok(()) |
| 47 | +} |
0 commit comments