use crate::config::{find_stack_store_path, read_stack_store, write_stack_store, GlobalConfig};
use anyhow::Result;
use giff_core::{FrameId, Stack, StackFrame, StackId};
use giff_git::{GitBackend, ShellGitBackend};
use uuid::Uuid;
pub fn run(branch: &str) -> Result<()> {
let cfg = GlobalConfig::load()?;
let repo_path = std::env::current_dir()?;
let backend = ShellGitBackend::new(repo_path);
let current = backend.current_branch()?;
let store_path = find_stack_store_path()?;
let mut store = read_stack_store(&store_path)?;
let parent_frame_id = store
.find_stack_for_branch(¤t)
.map(|(_, f)| f.id.clone());
if let Some((stack, _)) = store.find_stack_for_branch(¤t) {
let stack_id = stack.id.clone();
let new_frame = StackFrame {
id: FrameId(Uuid::new_v4().to_string()),
branch: branch.to_string(),
parent: parent_frame_id,
pr_number: None,
description: None,
};
let s = store.stacks.iter_mut().find(|s| s.id == stack_id).unwrap();
s.frames.push(new_frame);
} else if current == cfg.defaults.trunk {
let frame = StackFrame {
id: FrameId(Uuid::new_v4().to_string()),
branch: branch.to_string(),
parent: None,
pr_number: None,
description: None,
};
store.stacks.push(Stack {
id: StackId(Uuid::new_v4().to_string()),
name: branch.to_string(),
trunk: current.clone(),
frames: vec![frame],
});
} else {
let bottom = StackFrame {
id: FrameId(Uuid::new_v4().to_string()),
branch: current.clone(),
parent: None,
pr_number: None,
description: None,
};
let top = StackFrame {
id: FrameId(Uuid::new_v4().to_string()),
branch: branch.to_string(),
parent: Some(bottom.id.clone()),
pr_number: None,
description: None,
};
store.stacks.push(Stack {
id: StackId(Uuid::new_v4().to_string()),
name: branch.to_string(),
trunk: cfg.defaults.trunk.clone(),
frames: vec![bottom, top],
});
}
for s in &store.stacks {
s.validate()?;
}
backend.create_branch(branch, ¤t)?;
backend.checkout(branch)?;
write_stack_store(&store_path, &store)?;
if let Some(git_dir) = store_path.parent() {
crate::hooks::ensure_installed_quiet(git_dir);
}
println!("Created frame: {branch}");
Ok(())
}