mod failure;
mod git;
mod repo;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{self, Receiver, Sender};
use std::thread;
use std::time::Duration;
use crate::domain::Board;
use crate::store::Damage;
pub use failure::{Error, Kind};
use repo::{board_store, commit_all, current_branch, ensure_repo};
const NETWORK_TIMEOUT: Duration = Duration::from_secs(20);
const PUSH_TIMEOUT: Duration = Duration::from_secs(8);
pub struct Sync {
jobs: Sender<Job>,
results: Receiver<JobOutcome>,
init: Receiver<Result<(), Error>>,
}
enum Job {
Pull,
CommitPush,
}
pub enum Pulled {
UpToDate,
Merge(Option<Board>),
}
pub enum JobOutcome {
UpToDate,
Merged(Option<Board>),
Pushed,
Failed(Error),
}
impl Sync {
pub fn new(repo: PathBuf, remote: Option<String>) -> Self {
let (jobs_tx, jobs_rx) = mpsc::channel::<Job>();
let (results_tx, results_rx) = mpsc::channel::<JobOutcome>();
let (init_tx, init_rx) = mpsc::channel::<Result<(), Error>>();
thread::spawn(move || {
if init_tx.send(ensure_repo(&repo, remote.as_deref())).is_err() {
return;
}
while let Ok(job) = jobs_rx.recv() {
let outcome = match job {
Job::Pull => match pull(&repo, remote.as_deref()) {
Ok(Pulled::UpToDate) => JobOutcome::UpToDate,
Ok(Pulled::Merge(board)) => JobOutcome::Merged(board),
Err(err) => JobOutcome::Failed(err),
},
Job::CommitPush => match commit_push(&repo, remote.as_deref()) {
Ok(()) => JobOutcome::Pushed,
Err(err) => JobOutcome::Failed(err),
},
};
if results_tx.send(outcome).is_err() {
break;
}
}
});
Self {
jobs: jobs_tx,
results: results_rx,
init: init_rx,
}
}
pub fn pull(&self) -> Result<(), Error> {
self.jobs
.send(Job::Pull)
.map_err(|_| Error::io("sync worker stopped"))
}
pub fn commit_push(&self) -> Result<(), Error> {
self.jobs
.send(Job::CommitPush)
.map_err(|_| Error::io("sync worker stopped"))
}
pub fn poll(&self) -> Result<Option<JobOutcome>, Error> {
match self.results.try_recv() {
Ok(outcome) => Ok(Some(outcome)),
Err(mpsc::TryRecvError::Empty) => Ok(None),
Err(mpsc::TryRecvError::Disconnected) => Err(Error::io("sync worker stopped")),
}
}
pub fn poll_init(&self) -> Result<Option<Result<(), Error>>, Error> {
match self.init.try_recv() {
Ok(result) => Ok(Some(result)),
Err(mpsc::TryRecvError::Empty) => Ok(None),
Err(mpsc::TryRecvError::Disconnected) => Err(Error::io("sync worker stopped")),
}
}
}
pub fn commit_now(repo: &Path, remote: Option<&str>) -> Result<(), Error> {
ensure_repo(repo, remote)?;
commit_push(repo, remote)
}
pub fn pull_now(repo: &Path, remote: Option<&str>) -> Result<Option<Damage>, Error> {
ensure_repo(repo, remote)?;
apply_pull(repo, pull(repo, remote)?)
}
fn apply_pull(repo: &Path, pulled: Pulled) -> Result<Option<Damage>, Error> {
let Pulled::Merge(Some(remote)) = pulled else {
return Ok(None);
};
let store = board_store(repo);
let loaded = store.load();
let merged = remote.merged_with(&loaded.board);
store
.save(&merged)
.map_err(|err| Error::io(err.to_string()))?;
Ok(loaded.damaged)
}
pub fn probe_remote(url: &str) -> Result<(), Error> {
let mut cmd = git::program();
cmd.args(["ls-remote", url]);
git::bounded(&mut cmd, NETWORK_TIMEOUT).map(|_| ())
}
fn pull(repo: &Path, remote: Option<&str>) -> Result<Pulled, Error> {
if remote.is_none() {
return Ok(Pulled::UpToDate);
}
if !git::ok(repo, &["rev-parse", "--verify", "--quiet", "HEAD"]) {
return Ok(Pulled::UpToDate);
}
let Some(branch) = current_branch(repo) else {
return Ok(Pulled::UpToDate);
};
let tip = repo::fetch_remote_tip(repo, &branch)?;
if git::is_ancestor(repo, "refs/tasu/remote", "HEAD")? {
return Ok(Pulled::UpToDate);
}
let remote_board = match tip.board {
Some(bytes) => Some(parse_board(&bytes)?),
None => None,
};
git::run(repo, &["reset", "--soft", "refs/tasu/remote"])?;
Ok(Pulled::Merge(remote_board))
}
fn parse_board(bytes: &[u8]) -> Result<Board, Error> {
crate::store::board_from_bytes(bytes).ok_or_else(|| {
Error::io("the remote board could not be read; sync stopped to avoid dropping tasks")
})
}
fn commit_push(repo: &Path, remote: Option<&str>) -> Result<(), Error> {
commit_push_with(repo, remote, PUSH_TIMEOUT)
}
fn commit_push_with(repo: &Path, remote: Option<&str>, timeout: Duration) -> Result<(), Error> {
if remote.is_none() {
return Ok(());
}
commit_all(repo, "tasu: sync")?;
let Some(branch) = current_branch(repo) else {
return Err(Error::io("no branch checked out; refusing to push"));
};
let mut cmd = git::in_repo(
repo,
&[
"push",
"--quiet",
"-u",
"origin",
&format!("HEAD:refs/heads/{branch}"),
],
);
git::bounded(&mut cmd, timeout).map(|_| ())
}
#[cfg(test)]
mod tests {
use super::git::run as git;
use super::repo::is_settled;
use super::*;
fn commit_push(repo: &Path, remote: Option<&str>) -> Result<(), Error> {
commit_push_with(repo, remote, Duration::from_secs(60))
}
use std::process::Command;
fn bare_remote(dir: &Path) -> String {
let path = dir.join("remote.git");
let status = Command::new("git")
.args(["init", "--bare", "--quiet"])
.arg(&path)
.status()
.unwrap();
assert!(status.success(), "failed to create bare remote");
path.to_string_lossy().into_owned()
}
fn remote_refs(remote: &str) -> String {
let output = Command::new("git")
.arg("--git-dir")
.arg(remote)
.args(["for-each-ref", "--format=%(refname)"])
.output()
.unwrap();
String::from_utf8_lossy(&output.stdout).into_owned()
}
fn seeded_remote(dir: &Path, branch: &str) -> String {
let path = dir.join("remote.git");
assert!(
Command::new("git")
.args(["init", "--bare", "--quiet"])
.arg(&path)
.status()
.unwrap()
.success()
);
let url = path.to_string_lossy().into_owned();
let seed = dir.join("seed");
std::fs::create_dir_all(&seed).unwrap();
std::fs::write(seed.join("README.md"), "# tasu data\n").unwrap();
git(&seed, &["init", "-q", "-b", branch]).unwrap();
git(&seed, &["add", "-A"]).unwrap();
git(
&seed,
&[
"-c",
"user.name=t",
"-c",
"user.email=t@t",
"commit",
"-q",
"-m",
"seed",
],
)
.unwrap();
git(&seed, &["remote", "add", "origin", &url]).unwrap();
git(&seed, &["push", "-q", "origin", branch]).unwrap();
let _ = Command::new("git")
.arg("--git-dir")
.arg(&path)
.args(["symbolic-ref", "HEAD", &format!("refs/heads/{branch}")])
.status();
url
}
#[test]
fn pushes_a_local_board_to_the_remote() {
let dir = tempfile::tempdir().unwrap();
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("todos.json"), "{\"tasks\":[]}").unwrap();
let remote = bare_remote(dir.path());
ensure_repo(&data, Some(&remote)).unwrap();
commit_push(&data, Some(&remote)).unwrap();
assert!(
remote_refs(&remote).contains("refs/heads/"),
"remote received no branch"
);
}
#[test]
fn pushing_with_nothing_to_commit_is_a_noop() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("todos.json"), "{\"tasks\":[]}").unwrap();
ensure_repo(&data, Some(&remote)).unwrap();
commit_push(&data, Some(&remote)).unwrap();
commit_push(&data, Some(&remote)).unwrap();
}
#[test]
fn second_machine_clones_instead_of_init() {
let dir = tempfile::tempdir().unwrap();
let first = dir.path().join("first");
std::fs::create_dir_all(&first).unwrap();
std::fs::write(first.join("todos.json"), "{\"tasks\":[]}").unwrap();
let remote = bare_remote(dir.path());
ensure_repo(&first, Some(&remote)).unwrap();
commit_push(&first, Some(&remote)).unwrap();
let second = dir.path().join("second");
ensure_repo(&second, Some(&remote)).unwrap();
assert!(second.join(".git").exists(), "did not become a repo");
assert!(
second.join("todos.json").exists(),
"did not clone the board"
);
}
#[test]
fn grafts_onto_a_remote_that_already_has_history() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let seed = dir.path().join("seed");
std::fs::create_dir_all(&seed).unwrap();
std::fs::write(seed.join("README.md"), "# tasu data\n").unwrap();
git(&seed, &["init", "-q"]).unwrap();
git(&seed, &["add", "-A"]).unwrap();
git(
&seed,
&[
"-c",
"user.name=t",
"-c",
"user.email=t@t",
"commit",
"-q",
"-m",
"seed",
],
)
.unwrap();
git(&seed, &["remote", "add", "origin", remote.as_str()]).unwrap();
git(&seed, &["push", "-q", "-u", "origin", "HEAD"]).unwrap();
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("todos.json"), "{\"tasks\":[]}").unwrap();
ensure_repo(&data, Some(&remote)).unwrap();
commit_push(&data, Some(&remote)).unwrap();
let tree = Command::new("git")
.arg("--git-dir")
.arg(&remote)
.args(["ls-tree", "-r", "--name-only", "HEAD"])
.output()
.unwrap();
let files = String::from_utf8_lossy(&tree.stdout);
assert!(
files.contains("README.md"),
"remote history was lost: {files}"
);
assert!(files.contains("todos.json"), "local board was not pushed");
}
#[test]
fn graft_follows_the_remote_default_branch() {
let dir = tempfile::tempdir().unwrap();
let remote = seeded_remote(dir.path(), "trunk");
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("todos.json"), "{\"tasks\":[]}").unwrap();
ensure_repo(&data, Some(&remote)).unwrap();
commit_push(&data, Some(&remote)).unwrap();
assert_eq!(current_branch(&data).as_deref(), Some("trunk"));
let refs = remote_refs(&remote);
assert!(refs.contains("refs/heads/trunk"), "expected trunk: {refs}");
assert!(!refs.contains("refs/heads/master"), "stray branch: {refs}");
assert!(!refs.contains("refs/heads/main"), "stray branch: {refs}");
}
fn write_board(dir: &Path, tasks: &[(&str, u32)]) {
let mut board = crate::domain::Board::new();
for (title, day) in tasks {
board.add(*title, crate::domain::test_time::at(2026, 10, *day));
}
crate::store::Store::new(dir.join("todos.json"))
.save(&board)
.unwrap();
}
fn commit(repo: &Path, message: &str) {
git(repo, &["add", "-A"]).unwrap();
git(
repo,
&[
"-c",
"user.name=tasu",
"-c",
"user.email=tasu@localhost",
"commit",
"-q",
"-m",
message,
],
)
.unwrap();
}
fn add_task(repo: &Path, title: &str, day: u32) {
let store = crate::store::Store::new(repo.join("todos.json"));
let mut board = store.load().board;
board.add(title, crate::domain::test_time::at(2026, 10, day));
store.save(&board).unwrap();
}
fn clone_repo(remote: &str, into: &Path) {
let status = Command::new("git")
.args(["clone", "--quiet", remote])
.arg(into)
.status()
.unwrap();
assert!(status.success(), "clone failed");
}
#[test]
fn connecting_to_a_remote_that_has_a_board_unions_both() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let first = dir.path().join("first");
std::fs::create_dir_all(&first).unwrap();
write_board(&first, &[("remote task", 5)]);
ensure_repo(&first, Some(&remote)).unwrap();
commit_push(&first, Some(&remote)).unwrap();
let second = dir.path().join("second");
std::fs::create_dir_all(&second).unwrap();
write_board(&second, &[("local task", 6)]);
ensure_repo(&second, Some(&remote)).unwrap();
commit_push(&second, Some(&remote)).unwrap();
let board = Command::new("git")
.arg("--git-dir")
.arg(&remote)
.args(["show", "refs/heads/main:todos.json"])
.output()
.unwrap();
let board = String::from_utf8_lossy(&board.stdout);
assert!(
board.contains("remote task"),
"lost the remote board: {board}"
);
assert!(
board.contains("local task"),
"lost the local board: {board}"
);
}
#[test]
fn a_fresh_remote_gets_the_fixed_branch_not_the_host_default() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("todos.json"), "{\"tasks\":[]}").unwrap();
ensure_repo(&data, Some(&remote)).unwrap();
commit_push(&data, Some(&remote)).unwrap();
assert_eq!(current_branch(&data).as_deref(), Some("main"));
let refs = remote_refs(&remote);
assert!(refs.contains("refs/heads/main"), "expected main: {refs}");
assert!(!refs.contains("refs/heads/master"), "stray branch: {refs}");
}
#[test]
fn an_existing_repo_is_reconciled_with_the_remote_branch() {
let dir = tempfile::tempdir().unwrap();
let remote = seeded_remote(dir.path(), "main");
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("todos.json"), "{\"tasks\":[]}").unwrap();
git(&data, &["init", "-q"]).unwrap();
git(&data, &["add", "-A"]).unwrap();
git(
&data,
&[
"-c",
"user.name=tasu",
"-c",
"user.email=t@t",
"commit",
"-q",
"-m",
"local",
],
)
.unwrap();
git(&data, &["remote", "add", "origin", &remote]).unwrap();
ensure_repo(&data, Some(&remote)).unwrap();
assert_eq!(current_branch(&data).as_deref(), Some("main"));
}
#[test]
fn an_unreachable_remote_does_not_rename_the_branch() {
let dir = tempfile::tempdir().unwrap();
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("todos.json"), "{\"tasks\":[]}").unwrap();
git(&data, &["init", "-q", "-b", "master"]).unwrap();
git(&data, &["add", "-A"]).unwrap();
git(
&data,
&[
"-c",
"user.name=tasu",
"-c",
"user.email=t@t",
"commit",
"-q",
"-m",
"local",
],
)
.unwrap();
let missing = dir.path().join("nope.git");
ensure_repo(&data, Some(&missing.to_string_lossy())).unwrap();
assert_eq!(current_branch(&data).as_deref(), Some("master"));
}
#[test]
fn branch_reconciliation_is_remembered() {
let dir = tempfile::tempdir().unwrap();
let remote = seeded_remote(dir.path(), "trunk");
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("todos.json"), "{\"tasks\":[]}").unwrap();
ensure_repo(&data, Some(&remote)).unwrap();
assert_eq!(current_branch(&data).as_deref(), Some("trunk"));
assert!(
is_settled(&data, &remote),
"the remote should be remembered so later runs skip the ls-remote"
);
assert!(!is_settled(&data, "/some/other/remote.git"));
}
#[test]
fn probe_remote_rejects_a_missing_repository() {
let dir = tempfile::tempdir().unwrap();
let missing = dir.path().join("nope.git");
assert!(probe_remote(&missing.to_string_lossy()).is_err());
}
#[test]
fn without_a_remote_nothing_breaks() {
let dir = tempfile::tempdir().unwrap();
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("todos.json"), "{}").unwrap();
ensure_repo(&data, None).unwrap();
commit_push(&data, None).unwrap();
assert!(
!data.join(".git").exists(),
"local-only must not create a repository"
);
}
#[test]
fn diverged_histories_merge_instead_of_conflicting() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let seed = dir.path().join("seed");
std::fs::create_dir_all(&seed).unwrap();
write_board(&seed, &[("base", 5)]);
git(&seed, &["init", "-q", "-b", "main"]).unwrap();
git(&seed, &["add", "-A"]).unwrap();
commit(&seed, "base");
git(&seed, &["remote", "add", "origin", &remote]).unwrap();
git(&seed, &["push", "-q", "origin", "main"]).unwrap();
let _ = Command::new("git")
.arg("--git-dir")
.arg(&remote)
.args(["symbolic-ref", "HEAD", "refs/heads/main"])
.status();
let a = dir.path().join("a");
clone_repo(&remote, &a);
add_task(&a, "from A", 6);
commit(&a, "a");
let b = dir.path().join("b");
clone_repo(&remote, &b);
add_task(&b, "from B", 7);
commit(&b, "b");
git(&b, &["push", "-q", "origin", "main"]).unwrap();
pull_applied(&a, &remote);
let titles = board_titles(&a);
for title in ["base", "from A", "from B"] {
assert!(
titles.iter().any(|seen| seen == title),
"lost {title:?}: {titles:?}"
);
}
commit_push(&a, Some(&remote)).unwrap();
pull_applied(&b, &remote);
assert!(
board_titles(&b).iter().any(|seen| seen == "from A"),
"B did not get A"
);
}
fn pull_applied(repo: &Path, remote: &str) {
let _ = apply_pull(repo, pull(repo, Some(remote)).unwrap()).unwrap();
}
#[test]
fn pull_reads_the_remote_board_without_touching_the_local_file() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let seed = dir.path().join("seed");
std::fs::create_dir_all(&seed).unwrap();
write_board(&seed, &[("base", 5)]);
git(&seed, &["init", "-q", "-b", "main"]).unwrap();
git(&seed, &["add", "-A"]).unwrap();
commit(&seed, "base");
git(&seed, &["remote", "add", "origin", &remote]).unwrap();
git(&seed, &["push", "-q", "origin", "main"]).unwrap();
let _ = Command::new("git")
.arg("--git-dir")
.arg(&remote)
.args(["symbolic-ref", "HEAD", "refs/heads/main"])
.status();
let a = dir.path().join("a");
clone_repo(&remote, &a);
add_task(&a, "local only", 6);
commit(&a, "a");
let b = dir.path().join("b");
clone_repo(&remote, &b);
add_task(&b, "from B", 7);
commit(&b, "b");
git(&b, &["push", "-q", "origin", "main"]).unwrap();
let pulled = pull(&a, Some(&remote)).unwrap();
let Pulled::Merge(Some(remote_board)) = pulled else {
panic!("expected the remote board to be handed back");
};
let local = board_titles(&a);
assert!(local.iter().any(|t| t == "local only"), "{local:?}");
assert!(!local.iter().any(|t| t == "from B"), "{local:?}");
let titles: Vec<_> = remote_board
.tasks()
.iter()
.map(|task| task.title.as_str())
.collect();
assert!(titles.contains(&"from B"), "{titles:?}");
}
#[test]
fn pull_reports_a_corrupt_local_board() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let seed = dir.path().join("seed");
std::fs::create_dir_all(&seed).unwrap();
write_board(&seed, &[("base", 5)]);
git(&seed, &["init", "-q", "-b", "main"]).unwrap();
git(&seed, &["add", "-A"]).unwrap();
commit(&seed, "base");
git(&seed, &["remote", "add", "origin", &remote]).unwrap();
git(&seed, &["push", "-q", "origin", "main"]).unwrap();
let _ = Command::new("git")
.arg("--git-dir")
.arg(&remote)
.args(["symbolic-ref", "HEAD", "refs/heads/main"])
.status();
let a = dir.path().join("a");
clone_repo(&remote, &a);
std::fs::write(a.join("todos.json"), "{ not a board").unwrap();
add_task(&seed, "from seed", 6);
commit(&seed, "ahead");
git(&seed, &["push", "-q", "origin", "main"]).unwrap();
let damage = pull_now(&a, Some(&remote))
.expect("the pull itself succeeds")
.expect("the unreadable local board must be reported, not swallowed");
assert!(damage.backup.is_some(), "the corrupt board was moved aside");
}
#[test]
fn is_ancestor_distinguishes_a_git_error_from_no() {
let dir = tempfile::tempdir().unwrap();
let repo = dir.path().join("r");
std::fs::create_dir_all(&repo).unwrap();
git(&repo, &["init", "-q", "-b", "main"]).unwrap();
std::fs::write(repo.join("f"), "x").unwrap();
commit(&repo, "c");
assert!(super::git::is_ancestor(&repo, "HEAD", "HEAD").unwrap());
assert!(super::git::is_ancestor(&repo, "refs/heads/missing", "HEAD").is_err());
}
fn board_titles(repo: &Path) -> Vec<String> {
crate::store::Store::new(repo.join("todos.json"))
.load()
.board
.tasks()
.iter()
.map(|task| task.title.clone())
.collect()
}
#[test]
fn a_failed_graft_merge_leaves_the_local_board_in_place() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let seed = dir.path().join("seed");
std::fs::create_dir_all(&seed).unwrap();
std::fs::write(seed.join("todos.json"), "{ not a board").unwrap();
git(&seed, &["init", "-q", "-b", "main"]).unwrap();
commit(&seed, "seed");
git(&seed, &["remote", "add", "origin", &remote]).unwrap();
git(&seed, &["push", "-q", "origin", "main"]).unwrap();
let _ = Command::new("git")
.arg("--git-dir")
.arg(&remote)
.args(["symbolic-ref", "HEAD", "refs/heads/main"])
.status();
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
write_board(&data, &[("keep me", 5)]);
let err = ensure_repo(&data, Some(&remote)).unwrap_err();
assert!(err.detail().contains("could not be read"), "{err}");
let titles = board_titles(&data);
assert!(
titles.iter().any(|title| title == "keep me"),
"the local board was lost: {titles:?}"
);
}
#[test]
fn changing_the_remote_updates_origin() {
let dir = tempfile::tempdir().unwrap();
let first = bare_remote(dir.path());
let second = dir.path().join("second.git");
assert!(
Command::new("git")
.args(["init", "--bare", "--quiet"])
.arg(&second)
.status()
.unwrap()
.success()
);
let second = second.to_string_lossy().into_owned();
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
std::fs::write(data.join("todos.json"), "{\"tasks\":[]}").unwrap();
ensure_repo(&data, Some(&first)).unwrap();
ensure_repo(&data, Some(&second)).unwrap();
let out = Command::new("git")
.arg("-C")
.arg(&data)
.args(["remote", "get-url", "origin"])
.output()
.unwrap();
assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), second);
}
}