use std::io;
use std::path::PathBuf;
use recall_hooks::{exit, foreign_memory_slug, is_memory_file, payload};
use crate::project;
pub async fn push() -> anyhow::Result<i32> {
let hook = payload::parse_post_tool_use(io::stdin().lock());
if hook.tool_input.file_path.is_empty() {
return Ok(exit::OK);
}
let triggered = PathBuf::from(&hook.tool_input.file_path);
let here = project::resolve();
if !is_memory_file(&here.memory_dir(), &triggered) {
if let Some(slug) = foreign_memory_slug(&here.memory_root(), &here.memory_dir(), &triggered)
{
eprintln!(
"recall-push: that file is memory for {slug}, not for this project \
— nothing was pushed"
);
eprintln!(
"recall-push: standing in {}; a git worktree has its own memory directory",
here.root.display()
);
}
return Ok(exit::OK);
}
let ctx = here.hook_context()?;
match recall_hooks::push(&ctx, &triggered).await {
Ok(res) => {
if res.pushed.is_some() || !res.deleted.is_empty() {
eprintln!(
"recall-push: pushed {}, deleted {} for {}",
usize::from(res.pushed.is_some()),
res.deleted.len(),
ctx.project_key()
);
}
Ok(exit::OK)
}
Err(err) => {
eprintln!("recall-push: {err}");
Ok(exit::SERVER)
}
}
}
pub async fn pull() -> anyhow::Result<i32> {
let ctx = match project::resolve().hook_context() {
Ok(ctx) => ctx,
Err(err) => {
eprintln!("recall-pull: {err}, leaving local memory untouched");
return Ok(exit::OK);
}
};
match recall_hooks::pull(&ctx).await {
Ok(res) => {
eprintln!("{}", res.describe(ctx.project_key()));
Ok(exit::OK)
}
Err(err) => {
eprintln!("recall-pull: fetch failed ({err}), leaving local memory untouched");
Ok(exit::OK)
}
}
}