use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::mpsc::{self, Receiver, Sender};
use std::thread;
use std::time::{Duration, Instant};
const NETWORK_TIMEOUT: Duration = Duration::from_secs(20);
const PUSH_TIMEOUT: Duration = Duration::from_secs(8);
const DEFAULT_BRANCH: &str = "main";
pub struct Sync {
jobs: Sender<Job>,
results: Receiver<Result<(), String>>,
}
enum Job {
Pull,
CommitPush,
}
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::<Result<(), String>>();
thread::spawn(move || {
if ensure_repo(&repo, remote.as_deref()).is_err() {
}
while let Ok(job) = jobs_rx.recv() {
let result = match job {
Job::Pull => pull(&repo, remote.as_deref()),
Job::CommitPush => commit_push(&repo, remote.as_deref()),
};
if results_tx.send(result).is_err() {
break;
}
}
});
Self {
jobs: jobs_tx,
results: results_rx,
}
}
pub fn pull(&self) -> Result<(), String> {
self.jobs
.send(Job::Pull)
.map_err(|_| "sync worker stopped".to_string())
}
pub fn commit_push(&self) -> Result<(), String> {
self.jobs
.send(Job::CommitPush)
.map_err(|_| "sync worker stopped".to_string())
}
pub fn poll(&self) -> Option<Result<(), String>> {
self.results.try_recv().ok()
}
}
pub fn commit_now(repo: &Path, remote: Option<&str>) -> Result<(), String> {
ensure_repo(repo, remote)?;
commit_push(repo, remote)
}
pub fn pull_now(repo: &Path, remote: Option<&str>) -> Result<(), String> {
ensure_repo(repo, remote)?;
pull(repo, remote)
}
pub fn probe_remote(url: &str) -> Result<(), String> {
let mut cmd = Command::new("git");
cmd.args(["ls-remote", url]);
run_bounded(&mut cmd, NETWORK_TIMEOUT)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Failure {
NotFound,
Auth,
Network,
Other,
}
impl Failure {
pub fn classify(error: &str) -> Self {
let error = error.to_ascii_lowercase();
let has = |needle: &str| error.contains(needle);
if has("repository not found")
|| has("does not appear to be a git repository")
|| (has("repository") && has("not found"))
{
Self::NotFound
} else if has("authentication failed")
|| has("could not read username")
|| has("could not read password")
|| has("terminal prompts disabled")
|| has("permission denied")
|| has("403")
{
Self::Auth
} else if has("could not resolve host")
|| has("could not resolve")
|| has("unable to access")
|| has("failed to connect")
|| has("timed out")
|| has("network is unreachable")
{
Self::Network
} else {
Self::Other
}
}
pub fn reason(self) -> &'static str {
match self {
Self::NotFound => "repository not found",
Self::Auth => "authentication failed",
Self::Network => "can't reach the host",
Self::Other => "sync failed",
}
}
pub fn footer(self) -> &'static str {
match self {
Self::NotFound => "remote not found",
Self::Auth => "auth failed",
Self::Network => "offline",
Self::Other => "sync failed",
}
}
pub fn fix(self) -> &'static str {
match self {
Self::NotFound => {
"check the owner/repo, or create the repository (private is fine), then retry"
}
Self::Auth => {
"no stored credentials or no write access \u{2014} over HTTPS git needs a token, or use an SSH remote"
}
Self::Network => "check your network and the host name, then retry",
Self::Other => "the git error is above",
}
}
}
fn ensure_repo(repo: &Path, remote: Option<&str>) -> Result<(), String> {
if repo.join(".git").exists() {
ensure_gitignore(repo);
if let Some(url) = remote {
set_origin(repo, url);
if !is_settled(repo, url)
&& let Ok(branch) = remote_default_branch_result(url)
{
if let Some(branch) = branch {
name_branch(repo, &branch);
}
mark_settled(repo, url);
}
}
return Ok(());
}
let Some(url) = remote else {
return Ok(());
};
std::fs::create_dir_all(repo).map_err(|err| err.to_string())?;
let empty = std::fs::read_dir(repo)
.map(|mut entries| entries.next().is_none())
.unwrap_or(true);
if empty && clone(url, repo).is_ok() {
settle_head(repo, &target_branch(url));
ensure_gitignore(repo);
mark_settled(repo, url);
return Ok(());
}
let local_board = std::fs::read(repo.join("todos.json")).ok();
let branch = target_branch(url);
git(repo, &["init", "-q"])?;
name_branch(repo, &branch);
let _ = git(repo, &["remote", "add", "origin", url]);
let fetched = git(
repo,
&[
"fetch",
"--quiet",
"origin",
&format!("+refs/heads/{branch}:refs/tasu/remote"),
],
)
.is_ok()
|| git(
repo,
&["fetch", "--quiet", "origin", "+HEAD:refs/tasu/remote"],
)
.is_ok();
if fetched {
let _ = git(repo, &["reset", "--hard", "refs/tasu/remote"]);
}
let remote_board = if fetched {
git_bytes(repo, &["show", "refs/tasu/remote:todos.json"])
} else {
None
};
let board = match (local_board, remote_board) {
(Some(local), Some(remote)) => crate::store::merge_files(&local, &remote).or(Some(local)),
(board, None) => board,
(None, board) => board,
};
if let Some(bytes) = board.filter(|bytes| !bytes.is_empty()) {
std::fs::write(repo.join("todos.json"), bytes).map_err(|err| err.to_string())?;
}
ensure_gitignore(repo);
let _ = git(repo, &["add", "-A"]);
let _ = git(
repo,
&[
"-c",
"user.name=tasu",
"-c",
"user.email=tasu@localhost",
"commit",
"--quiet",
"-m",
"tasu: local",
],
);
mark_settled(repo, url);
Ok(())
}
fn set_origin(repo: &Path, url: &str) {
if git(repo, &["remote", "set-url", "origin", url]).is_err() {
let _ = git(repo, &["remote", "add", "origin", url]);
}
}
fn target_branch(remote: &str) -> String {
remote_default_branch(remote).unwrap_or_else(|| DEFAULT_BRANCH.to_string())
}
fn remote_default_branch(remote: &str) -> Option<String> {
remote_default_branch_result(remote).ok().flatten()
}
fn remote_default_branch_result(remote: &str) -> Result<Option<String>, String> {
let mut cmd = Command::new("git");
cmd.args(["ls-remote", "--symref", remote, "HEAD"]);
let output = run_output(&mut cmd, NETWORK_TIMEOUT)?;
Ok(output.lines().find_map(|line| {
let name = line
.strip_prefix("ref:")?
.trim()
.strip_prefix("refs/heads/")?;
name.split_whitespace().next().map(str::to_string)
}))
}
fn mark_settled(repo: &Path, url: &str) {
let _ = git(repo, &["config", "tasu.remote", url]);
}
fn is_settled(repo: &Path, url: &str) -> bool {
git_string(repo, &["config", "--get", "tasu.remote"]).as_deref() == Some(url)
}
fn current_branch(repo: &Path) -> Option<String> {
let output = git_cmd(repo, &["symbolic-ref", "--short", "-q", "HEAD"])
.output()
.ok()?;
if !output.status.success() {
return None;
}
let name = String::from_utf8_lossy(&output.stdout).trim().to_string();
(!name.is_empty()).then_some(name)
}
fn settle_head(repo: &Path, target: &str) {
if git_ok(repo, &["rev-parse", "--verify", "--quiet", "HEAD"]) {
name_branch(repo, target);
return;
}
if git_ok(
repo,
&[
"show-ref",
"--verify",
"--quiet",
&format!("refs/remotes/origin/{target}"),
],
) {
let _ = git(repo, &["checkout", "--quiet", target]);
} else if let Some(branch) = first_remote_branch(repo) {
let _ = git(repo, &["checkout", "--quiet", &branch]);
} else {
name_branch(repo, target);
}
}
fn first_remote_branch(repo: &Path) -> Option<String> {
let output = git_cmd(
repo,
&[
"for-each-ref",
"--format=%(refname:short)",
"refs/remotes/origin/",
],
)
.output()
.ok()?;
if !output.status.success() {
return None;
}
String::from_utf8_lossy(&output.stdout)
.lines()
.map(str::trim)
.filter(|name| !name.is_empty() && *name != "origin/HEAD")
.filter_map(|name| name.strip_prefix("origin/"))
.map(str::to_string)
.next()
}
fn name_branch(repo: &Path, target: &str) {
let Some(current) = current_branch(repo) else {
let _ = git(
repo,
&["symbolic-ref", "HEAD", &format!("refs/heads/{target}")],
);
return;
};
if current == target {
return;
}
if git_ok(repo, &["rev-parse", "--verify", "--quiet", "HEAD"]) {
if git(repo, &["branch", "-m", target]).is_ok() {
eprintln!("tasu: sync branch {current} renamed to {target}");
} else {
let _ = git(repo, &["checkout", "--quiet", target]);
}
} else {
let _ = git(
repo,
&["symbolic-ref", "HEAD", &format!("refs/heads/{target}")],
);
}
}
fn ensure_gitignore(repo: &Path) {
let path = repo.join(".gitignore");
let mut text = std::fs::read_to_string(&path).unwrap_or_default();
let mut changed = false;
for entry in ["config.json", "*.tmp", "*.corrupt-*"] {
if !text.lines().any(|line| line.trim() == entry) {
if !text.is_empty() && !text.ends_with('\n') {
text.push('\n');
}
text.push_str(entry);
text.push('\n');
changed = true;
}
}
if changed && let Err(err) = std::fs::write(&path, &text) {
eprintln!("tasu: could not write {}: {err}", path.display());
}
if git_ok(repo, &["ls-files", "--error-unmatch", "config.json"]) {
let _ = git(repo, &["rm", "--cached", "--quiet", "config.json"]);
}
}
fn clone(url: &str, repo: &Path) -> Result<(), String> {
let mut cmd = Command::new("git");
cmd.args(["clone", "--quiet", url]).arg(repo);
run_bounded(&mut cmd, NETWORK_TIMEOUT)
}
fn pull(repo: &Path, remote: Option<&str>) -> Result<(), String> {
if remote.is_none() {
return Ok(());
}
if !git_ok(repo, &["rev-parse", "--verify", "--quiet", "HEAD"]) {
return Ok(());
}
let Some(branch) = current_branch(repo) else {
return Ok(());
};
let mut cmd = git_cmd(
repo,
&[
"pull",
"--rebase",
"--autostash",
"--quiet",
"origin",
&branch,
],
);
match run_bounded(&mut cmd, NETWORK_TIMEOUT) {
Ok(()) => Ok(()),
Err(err) => {
let _ = git(repo, &["rebase", "--abort"]);
Err(err)
}
}
}
fn commit_push(repo: &Path, remote: Option<&str>) -> Result<(), String> {
commit_push_with(repo, remote, PUSH_TIMEOUT)
}
fn commit_push_with(repo: &Path, remote: Option<&str>, timeout: Duration) -> Result<(), String> {
if remote.is_none() {
return Ok(());
}
ensure_gitignore(repo);
git(repo, &["add", "-A"])?;
match git(
repo,
&[
"-c",
"user.name=tasu",
"-c",
"user.email=tasu@localhost",
"commit",
"--quiet",
"-m",
"tasu: sync",
],
) {
Ok(()) => {}
Err(message) if message.contains("nothing to commit") => {}
Err(message) => return Err(message),
}
let branch = current_branch(repo).unwrap_or_else(|| DEFAULT_BRANCH.to_string());
let mut cmd = git_cmd(
repo,
&[
"push",
"--quiet",
"-u",
"origin",
&format!("HEAD:refs/heads/{branch}"),
],
);
run_bounded(&mut cmd, timeout)?;
Ok(())
}
fn git_cmd(repo: &Path, args: &[&str]) -> Command {
let mut cmd = Command::new("git");
cmd.arg("-C").arg(repo).args(args);
cmd
}
fn git(repo: &Path, args: &[&str]) -> Result<(), String> {
let output = git_cmd(repo, args)
.output()
.map_err(|err| err.to_string())?;
check(output)
}
fn git_ok(repo: &Path, args: &[&str]) -> bool {
git_cmd(repo, args)
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
fn git_bytes(repo: &Path, args: &[&str]) -> Option<Vec<u8>> {
let output = git_cmd(repo, args).output().ok()?;
output.status.success().then_some(output.stdout)
}
fn git_string(repo: &Path, args: &[&str]) -> Option<String> {
let bytes = git_bytes(repo, args)?;
Some(String::from_utf8_lossy(&bytes).trim().to_string())
}
fn run_bounded(cmd: &mut Command, timeout: Duration) -> Result<(), String> {
let mut child = cmd
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.map_err(|err| err.to_string())?;
let deadline = Instant::now() + timeout;
loop {
match child.try_wait() {
Ok(Some(status)) if status.success() => return Ok(()),
Ok(Some(_)) => {
let mut message = String::new();
if let Some(mut stderr) = child.stderr.take() {
let _ = stderr.read_to_string(&mut message);
}
let message = message.trim();
return Err(if message.is_empty() {
"git command failed".to_string()
} else {
message.to_string()
});
}
Ok(None) => {
if Instant::now() >= deadline {
let _ = child.kill();
let _ = child.wait();
return Err("git command timed out".to_string());
}
thread::sleep(Duration::from_millis(50));
}
Err(err) => return Err(err.to_string()),
}
}
}
fn run_output(cmd: &mut Command, timeout: Duration) -> Result<String, String> {
let mut child = cmd
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|err| err.to_string())?;
let deadline = Instant::now() + timeout;
loop {
match child.try_wait() {
Ok(Some(status)) => {
let mut stdout = String::new();
let mut stderr = String::new();
if let Some(mut out) = child.stdout.take() {
let _ = out.read_to_string(&mut stdout);
}
if let Some(mut err) = child.stderr.take() {
let _ = err.read_to_string(&mut stderr);
}
if status.success() {
return Ok(stdout);
}
let message = stderr.trim();
return Err(if message.is_empty() {
"git command failed".to_string()
} else {
message.to_string()
});
}
Ok(None) => {
if Instant::now() >= deadline {
let _ = child.kill();
let _ = child.wait();
return Err("git command timed out".to_string());
}
thread::sleep(Duration::from_millis(50));
}
Err(err) => return Err(err.to_string()),
}
}
}
fn check(output: std::process::Output) -> Result<(), String> {
if output.status.success() {
return Ok(());
}
let mut message = String::from_utf8_lossy(&output.stderr).trim().to_string();
if message.is_empty() {
message = String::from_utf8_lossy(&output.stdout).trim().to_string();
}
Err(message)
}
#[cfg(test)]
mod tests {
use super::*;
fn commit_push(repo: &Path, remote: Option<&str>) -> Result<(), String> {
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"), "{\"version\":1,\"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 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"), "{\"version\":1,\"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"), "{\"version\":1,\"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"), "{\"version\":1,\"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, titles: &[&str]) {
let mut board = crate::domain::Board::new();
for title in titles {
board.add(*title, chrono::Local::now());
}
crate::store::Store::new(dir.join("todos.json"))
.save(&board)
.unwrap();
}
#[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"]);
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"]);
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"), "{\"version\":1,\"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"), "{\"version\":1,\"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"), "{\"version\":1,\"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"), "{\"version\":1,\"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 classifies_common_git_failures() {
assert_eq!(
Failure::classify("remote: Repository not found."),
Failure::NotFound
);
assert_eq!(
Failure::classify(
"fatal: could not read Username for 'https://github.com': terminal prompts disabled"
),
Failure::Auth
);
assert_eq!(
Failure::classify("fatal: unable to access 'x': Could not resolve host: github.com"),
Failure::Network
);
assert_eq!(Failure::classify("something else"), Failure::Other);
}
#[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 config_file_is_not_synced() {
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"), "{\"version\":1,\"tasks\":[]}").unwrap();
std::fs::write(data.join("config.json"), "{\"remote\":\"x\"}").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", "refs/heads/main"])
.output()
.unwrap();
let files = String::from_utf8_lossy(&tree.stdout);
assert!(files.contains("todos.json"), "board missing: {files}");
assert!(
!files.contains("config.json"),
"machine-local config must not be synced: {files}"
);
}
#[test]
fn a_committed_config_is_untracked_and_not_synced() {
let dir = tempfile::tempdir().unwrap();
let seed = dir.path().join("seed");
std::fs::create_dir_all(&seed).unwrap();
std::fs::write(seed.join("todos.json"), "{\"version\":1,\"tasks\":[]}").unwrap();
std::fs::write(seed.join("config.json"), "{\"remote\":null}").unwrap();
git(&seed, &["init", "-q", "-b", "main"]).unwrap();
git(&seed, &["add", "-A"]).unwrap();
git(
&seed,
&[
"-c",
"user.name=t",
"-c",
"user.email=t@t",
"commit",
"-q",
"-m",
"seed",
],
)
.unwrap();
let remote = bare_remote(dir.path());
git(&seed, &["remote", "add", "origin", &remote]).unwrap();
git(&seed, &["push", "-q", "origin", "main"]).unwrap();
let data = dir.path().join("data");
std::fs::create_dir_all(&data).unwrap();
write_board(&data, &["local"]);
ensure_repo(&data, Some(&remote)).unwrap();
commit_push(&data, Some(&remote)).unwrap();
assert!(
!git_ok(&data, &["ls-files", "--error-unmatch", "config.json"]),
"config.json is still tracked locally"
);
let tree = Command::new("git")
.arg("--git-dir")
.arg(&remote)
.args(["ls-tree", "-r", "--name-only", "main"])
.output()
.unwrap();
let files = String::from_utf8_lossy(&tree.stdout);
assert!(
!files.contains("config.json"),
"config.json is still tracked on the remote: {files}"
);
}
#[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"), "{\"version\":1,\"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);
}
}