use crate::api_client::{ApiClient, CheckPoliciesRequest, SessionCheckData};
use crate::credentials::resolve_credentials;
use crate::resolution::{resolve_repo_by_name, ResolveRepoByNameError};
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::process::Command;
fn git_head_sha(project_root: &Path) -> Option<String> {
let out = Command::new("git")
.args(["rev-parse", "HEAD"])
.current_dir(project_root)
.output()
.ok()?;
if !out.status.success() {
return None;
}
let sha = String::from_utf8_lossy(&out.stdout).trim().to_string();
if sha.is_empty() {
None
} else {
Some(sha)
}
}
#[derive(Debug, PartialEq)]
pub(crate) struct PushRef {
pub local_sha: String,
pub remote_sha: String,
}
fn is_zero_sha(sha: &str) -> bool {
!sha.is_empty() && sha.chars().all(|c| c == '0')
}
pub(crate) fn parse_push_refs(stdin: &str) -> Vec<PushRef> {
stdin
.lines()
.filter_map(|line| {
let mut parts = line.split_whitespace();
let _local_ref = parts.next()?;
let local_sha = parts.next()?;
let _remote_ref = parts.next()?;
let remote_sha = parts.next()?;
if is_zero_sha(local_sha) {
return None;
}
Some(PushRef {
local_sha: local_sha.to_string(),
remote_sha: remote_sha.to_string(),
})
})
.collect()
}
fn changed_files_for_refs(project_root: &Path, refs: &[PushRef]) -> Vec<String> {
let mut seen = HashSet::new();
let mut out = Vec::new();
for r in refs {
let base = if is_zero_sha(&r.remote_sha) {
match git_empty_tree_sha(project_root) {
Some(t) => t,
None => continue,
}
} else {
r.remote_sha.clone()
};
let out_bytes = Command::new("git")
.args([
"diff",
"--name-only",
"-z",
"--no-renames",
&format!("{base}..{}", r.local_sha),
])
.current_dir(project_root)
.output();
let Ok(o) = out_bytes else { continue };
if !o.status.success() {
continue;
}
for p in split_nul_delimited_paths(&o.stdout) {
if seen.insert(p.clone()) {
out.push(p);
}
}
}
out
}
fn split_nul_delimited_paths(bytes: &[u8]) -> Vec<String> {
String::from_utf8_lossy(bytes)
.split('\0')
.filter(|p| !p.is_empty())
.map(|p| p.to_string())
.collect()
}
fn git_empty_tree_sha(project_root: &Path) -> Option<String> {
let out = Command::new("git")
.args(["hash-object", "-t", "tree", "/dev/null"])
.current_dir(project_root)
.output()
.ok()?;
if !out.status.success() {
return None;
}
let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
if s.is_empty() {
None
} else {
Some(s)
}
}
const STDIN_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(2);
fn read_with_timeout<F>(f: F, timeout: std::time::Duration) -> Option<String>
where
F: FnOnce() -> Option<String> + Send + 'static,
{
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let _ = tx.send(f());
});
rx.recv_timeout(timeout).ok().flatten()
}
fn read_stdin_with_timeout(timeout: std::time::Duration) -> Option<String> {
use std::io::Read;
read_with_timeout(
|| {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).ok().map(|_| buf)
},
timeout,
)
}
fn resolve_changed_paths(project_root: &Path) -> Option<Vec<String>> {
use std::io::IsTerminal;
if !std::io::stdin().is_terminal() {
if let Some(buf) = read_stdin_with_timeout(STDIN_READ_TIMEOUT) {
let refs = parse_push_refs(&buf);
if !refs.is_empty() {
let files = changed_files_for_refs(project_root, &refs);
return if files.is_empty() { None } else { Some(files) };
}
}
}
let out = Command::new("git")
.args([
"diff",
"--name-only",
"-z",
"--no-renames",
"@{upstream}..HEAD",
])
.current_dir(project_root)
.output()
.ok()?;
if !out.status.success() {
return None;
}
let files = split_nul_delimited_paths(&out.stdout);
if files.is_empty() {
None
} else {
Some(files)
}
}
fn collect_session_data(session_dir: &Path) -> Option<SessionCheckData> {
let session_id = session_dir.file_name()?.to_string_lossy().to_string();
let events_path = session_dir.join("events.jsonl");
let mut files_modified = Vec::new();
let mut files_seen = HashSet::new();
if events_path.exists() {
if let Ok(content) = fs::read_to_string(&events_path) {
for line in content.lines() {
let event: serde_json::Value = match serde_json::from_str(line) {
Ok(v) => v,
Err(_) => continue,
};
if let Some(path) = event
.get("tool_input")
.and_then(|v| v.get("file_path"))
.and_then(|v| v.as_str())
{
if files_seen.insert(path.to_string()) {
files_modified.push(path.to_string());
}
}
}
}
}
let meta_path = session_dir.join("metadata.json");
let metadata: Option<serde_json::Value> = meta_path
.exists()
.then(|| fs::read_to_string(&meta_path).ok())
.flatten()
.and_then(|c| serde_json::from_str(&c).ok());
let transcript_path = metadata
.as_ref()
.and_then(|m| m.get("transcript_path"))
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let mut tool_calls_map: std::collections::HashMap<String, i32> =
std::collections::HashMap::new();
let mut total_tool_calls: i32 = 0;
if let Some(path) = &transcript_path {
if let Ok(content) = fs::read_to_string(path) {
for line in content.lines() {
let entry: serde_json::Value = match serde_json::from_str(line) {
Ok(v) => v,
Err(_) => continue,
};
if entry.get("type").and_then(|v| v.as_str()) == Some("assistant") {
if let Some(content_arr) = entry
.get("message")
.and_then(|m| m.get("content"))
.and_then(|c| c.as_array())
{
for block in content_arr {
if block.get("type").and_then(|v| v.as_str()) == Some("tool_use") {
if let Some(name) = block.get("name").and_then(|v| v.as_str()) {
*tool_calls_map.entry(name.to_string()).or_insert(0) += 1;
total_tool_calls += 1;
}
}
}
}
}
}
}
}
let tool_calls = if tool_calls_map.is_empty() {
None
} else {
serde_json::to_value(&tool_calls_map).ok()
};
Some(SessionCheckData {
session_id,
tool_calls,
files_modified: if files_modified.is_empty() {
None
} else {
Some(files_modified)
},
total_tool_calls: if total_tool_calls > 0 {
Some(total_tool_calls)
} else {
None
},
})
}
pub fn select_worktree_sessions(
session_dirs: Vec<std::path::PathBuf>,
worktree_top: &str,
) -> (Vec<std::path::PathBuf>, bool) {
let belongs = |dir: &std::path::Path| -> bool {
match fs::read_to_string(dir.join("origin")) {
Ok(s) => s.trim() == worktree_top, Err(e) if e.kind() == std::io::ErrorKind::NotFound => true,
Err(_) => false,
}
};
let filtered: Vec<_> = session_dirs
.iter()
.filter(|d| belongs(d))
.cloned()
.collect();
if filtered.is_empty() && !session_dirs.is_empty() {
(session_dirs, true)
} else {
(filtered, false)
}
}
pub async fn check_policies(
project_root: &Path,
cwd: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
let (server_url, credential) = resolve_credentials(project_root)?;
let server_url = server_url
.ok_or("No server URL configured. Run `tracevault login --server-url=<url>` to set one.")?;
if credential.is_none() {
return Err(
"Not logged in. Run `tracevault login --server-url=<server_url>` to authenticate."
.into(),
);
}
let client = ApiClient::with_credential(&server_url, credential);
let repo = match resolve_repo_by_name(&client, project_root).await {
Ok(r) => r,
Err(ResolveRepoByNameError::ListFailed(e)) => {
return Err(connectivity_message(&e.to_string()).into());
}
Err(ResolveRepoByNameError::NotFound { repo_name }) => {
return Err(format!(
"Repo '{repo_name}' not found on server. Run `tracevault sync` first."
)
.into());
}
};
let sessions_dir = project_root.join(".tracevault").join("sessions");
let mut unpushed_dirs: Vec<std::path::PathBuf> = Vec::new();
if sessions_dir.exists() {
for entry in fs::read_dir(&sessions_dir)? {
let entry = entry?;
if !entry.file_type()?.is_dir() {
continue;
}
let session_dir = entry.path();
if session_dir.join(".pushed").exists() {
continue;
}
unpushed_dirs.push(session_dir);
}
}
let worktree_top = crate::paths::worktree_toplevel(cwd);
let (selected_dirs, fell_back) = select_worktree_sessions(unpushed_dirs, &worktree_top);
if fell_back {
eprintln!(
"Warning: no unpushed session matched this worktree by origin marker — \
checking all unpushed sessions to avoid skipping policy enforcement."
);
}
let mut sessions = Vec::new();
for session_dir in &selected_dirs {
if let Some(data) = collect_session_data(session_dir) {
sessions.push(data);
}
}
if sessions.is_empty() {
println!("No unpushed sessions to check.");
return Ok(());
}
println!("Checking {} session(s) against policies...", sessions.len());
let commit_sha = git_head_sha(cwd);
let changed_paths = resolve_changed_paths(cwd);
let result = client
.check_policies(
&repo.id,
CheckPoliciesRequest {
sessions,
commit_sha,
changed_paths,
},
)
.await
.map_err(|e| connectivity_message(&e.to_string()))?;
for r in &result.results {
let icon = match r.result.as_str() {
"pass" => "\x1b[32m✓\x1b[0m", "fail" if r.action == "block_push" => "\x1b[31m✗\x1b[0m", "fail" => "\x1b[33m!\x1b[0m", _ => " ",
};
println!(
" {} [{}] {} — {}",
icon, r.severity, r.rule_name, r.details
);
}
if result.blocked {
eprintln!("\n\x1b[31mPolicy check failed: push blocked.\x1b[0m");
std::process::exit(1);
} else if result.passed {
println!("\n\x1b[32mAll policy checks passed.\x1b[0m");
} else {
println!("\n\x1b[33mPolicy warnings found (push not blocked).\x1b[0m");
}
Ok(())
}
fn connectivity_message(raw: &str) -> String {
let lower = raw.to_ascii_lowercase();
if lower.contains("401") || lower.contains("unauthorized") {
return with_action(
raw,
"Session token may be expired. Run `tracevault login --server-url=<server_url>` to refresh.",
);
}
if lower.contains("403") || lower.contains("forbidden") {
return with_action(
raw,
"Your token is not authorized for this repo's policies. Confirm the token/service account has access and rerun `tracevault login`.",
);
}
if lower.contains("404") {
return with_action(
raw,
"This server may not recognize this endpoint yet — this can happen when the CLI is newer than the server. Confirm the server has been upgraded, or downgrade the CLI to match it.",
);
}
if is_server_error(&lower) {
return with_action(
raw,
"TraceVault server returned an error. The team has likely been paged; retry shortly.",
);
}
if lower.contains("dns")
|| lower.contains("connect")
|| lower.contains("timed out")
|| lower.contains("timeout")
{
return with_action(
raw,
"Could not reach the TraceVault server. Check network and `server_url` in .tracevault/config.toml.",
);
}
format!("Policy check could not run: {raw}.")
}
fn with_action(raw: &str, action: &str) -> String {
format!("Policy check could not run: {raw}.\n → {action}")
}
fn is_server_error(lower: &str) -> bool {
lower.contains("internal")
|| lower.contains("server error")
|| ["500", "501", "502", "503", "504"]
.iter()
.any(|code| lower.contains(code))
}
#[cfg(test)]
mod worktree_tests {
use super::select_worktree_sessions;
use std::path::PathBuf;
fn seed(tmp: &std::path::Path, id: &str, origin: Option<&str>) -> PathBuf {
let d = tmp.join(id);
std::fs::create_dir_all(&d).unwrap();
if let Some(o) = origin {
std::fs::write(d.join("origin"), o).unwrap();
}
d
}
#[test]
fn keeps_current_worktree_and_unmarked_excludes_other() {
let tmp = tempfile::tempdir().unwrap();
let mine = seed(tmp.path(), "mine", Some("/wt/here"));
let other = seed(tmp.path(), "other", Some("/wt/there"));
let legacy = seed(tmp.path(), "legacy", None);
let (kept, fell_back) = select_worktree_sessions(
vec![mine.clone(), other.clone(), legacy.clone()],
"/wt/here",
);
assert!(!fell_back);
assert!(kept.contains(&mine), "current worktree session kept");
assert!(
kept.contains(&legacy),
"unmarked legacy session kept (conservative)"
);
assert!(
!kept.contains(&other),
"other worktree's marked session excluded"
);
}
#[test]
fn excludes_session_with_unreadable_marker() {
let tmp = tempfile::tempdir().unwrap();
let mine = seed(tmp.path(), "mine", Some("/wt/here"));
let bad = tmp.path().join("bad");
std::fs::create_dir_all(bad.join("origin")).unwrap();
let (kept, fell_back) =
select_worktree_sessions(vec![mine.clone(), bad.clone()], "/wt/here");
assert!(!fell_back);
assert!(kept.contains(&mine), "current worktree session kept");
assert!(
!kept.contains(&bad),
"session with a present-but-unreadable marker must be excluded, not kept as legacy"
);
}
#[test]
fn falls_back_to_all_when_nothing_matches() {
let tmp = tempfile::tempdir().unwrap();
let a = seed(tmp.path(), "a", Some("/wt/there"));
let b = seed(tmp.path(), "b", Some("/wt/elsewhere"));
let (kept, fell_back) = select_worktree_sessions(vec![a.clone(), b.clone()], "/wt/here");
assert!(fell_back, "must flag fallback when filter matches nothing");
assert_eq!(
kept.len(),
2,
"never under-enforce: send all rather than zero"
);
}
}
#[cfg(test)]
mod tests {
use super::connectivity_message;
#[test]
fn connectivity_message_suggests_login_on_401() {
let m = connectivity_message("Stream failed (401 Unauthorized): bad token");
assert!(
m.contains("tracevault login"),
"401 errors must surface the login hint; got: {m}"
);
}
#[test]
fn connectivity_message_suggests_login_on_unauthorized_text() {
let m = connectivity_message("Server returned: unauthorized request");
assert!(m.contains("tracevault login"));
}
#[test]
fn connectivity_message_suggests_network_check_on_dns_error() {
let m = connectivity_message("error sending request for url: dns error");
assert!(
m.to_lowercase().contains("network") || m.to_lowercase().contains("server_url"),
"DNS errors must surface a network hint; got: {m}"
);
}
#[test]
fn connectivity_message_suggests_network_check_on_connection_refused() {
let m = connectivity_message("connection refused");
assert!(m.to_lowercase().contains("network"));
}
#[test]
fn connectivity_message_falls_back_when_unrecognized() {
let m = connectivity_message("some weird new error shape we have not seen before");
assert!(m.contains("some weird new error shape"));
assert!(!m.contains("→")); }
#[test]
fn connectivity_message_does_not_collide_on_403() {
let m = connectivity_message("403 Forbidden");
assert!(
m.to_lowercase().contains("authorized"),
"403 should mention authorization; got: {m}"
);
}
#[test]
fn connectivity_message_flags_404_as_version_mismatch() {
let m = connectivity_message("Failed to list repos (404 Not Found): ");
assert!(
m.to_lowercase().contains("newer than the server")
|| m.to_lowercase().contains("version mismatch"),
"404 should hint at a possible CLI/server version mismatch; got: {m}"
);
}
#[test]
fn connectivity_message_flags_500_as_server_error() {
let m = connectivity_message("Server returned 500 Internal Server Error: oops");
assert!(
m.to_lowercase().contains("server returned an error"),
"500 should surface the server-error hint; got: {m}"
);
}
#[test]
fn connectivity_message_flags_503_without_internal_text() {
let m = connectivity_message("Server returned 503 Service Unavailable: ");
assert!(
m.to_lowercase().contains("server returned an error"),
"503 should surface the server-error hint; got: {m}"
);
}
#[test]
fn connectivity_message_treats_504_as_server_not_network() {
let m = connectivity_message("Server returned 504 Gateway Timeout: ");
assert!(
m.to_lowercase().contains("server returned an error"),
"504 should be treated as a server error; got: {m}"
);
}
}
#[cfg(test)]
mod push_ref_tests {
use super::{is_zero_sha, parse_push_refs};
#[test]
fn parses_a_single_ref_line() {
let refs = parse_push_refs("refs/heads/main abc123 refs/heads/main def456\n");
assert_eq!(refs.len(), 1);
assert_eq!(refs[0].local_sha, "abc123");
assert_eq!(refs[0].remote_sha, "def456");
}
#[test]
fn parses_multiple_refs() {
let refs = parse_push_refs(
"refs/heads/a 111 refs/heads/a 222\nrefs/heads/b 333 refs/heads/b 444\n",
);
assert_eq!(refs.len(), 2);
}
#[test]
fn parses_a_single_ref_line_without_trailing_newline() {
let refs = parse_push_refs("refs/heads/main abc123 refs/heads/main def456");
assert_eq!(refs.len(), 1);
assert_eq!(refs[0].local_sha, "abc123");
assert_eq!(refs[0].remote_sha, "def456");
}
#[test]
fn parses_multiple_refs_when_last_line_has_no_trailing_newline() {
let refs =
parse_push_refs("refs/heads/a 111 refs/heads/a 222\nrefs/heads/b 333 refs/heads/b 444");
assert_eq!(refs.len(), 2);
assert_eq!(refs[0].local_sha, "111");
assert_eq!(refs[1].local_sha, "333");
}
#[test]
fn skips_branch_deletions() {
let zeros = "0".repeat(40);
let line = format!("(delete) {zeros} refs/heads/gone abc123\n");
assert!(parse_push_refs(&line).is_empty());
}
#[test]
fn keeps_new_branches_whose_remote_sha_is_zero() {
let zeros = "0".repeat(40);
let line = format!("refs/heads/new abc123 refs/heads/new {zeros}\n");
let refs = parse_push_refs(&line);
assert_eq!(refs.len(), 1);
assert!(is_zero_sha(&refs[0].remote_sha));
}
#[test]
fn skips_malformed_lines_without_panicking() {
assert!(parse_push_refs("garbage\n\nalso garbage here\n").is_empty());
}
#[test]
fn zero_sha_detection_handles_sha256_length() {
assert!(is_zero_sha(&"0".repeat(64)));
assert!(!is_zero_sha("0000000000000000000000000000000000000001"));
assert!(!is_zero_sha(""));
}
}
#[cfg(test)]
mod git_diff_tests {
use super::{changed_files_for_refs, git_empty_tree_sha, PushRef};
use crate::test_helpers::init_git_repo;
use std::path::Path;
use std::process::Command;
fn commit_file(dir: &Path, name: &str, content: &str) -> String {
std::fs::write(dir.join(name), content).unwrap();
let ok = Command::new("git")
.args(["-C", &dir.to_string_lossy(), "add", name])
.status()
.expect("git add failed")
.success();
assert!(ok, "git add must succeed");
let ok = Command::new("git")
.args([
"-C",
&dir.to_string_lossy(),
"commit",
"-m",
&format!("add {name}"),
])
.status()
.expect("git commit failed")
.success();
assert!(ok, "git commit must succeed");
head_sha(dir)
}
fn head_sha(dir: &Path) -> String {
let out = Command::new("git")
.args(["-C", &dir.to_string_lossy(), "rev-parse", "HEAD"])
.output()
.expect("git rev-parse failed");
assert!(out.status.success());
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
#[test]
fn two_dot_range_returns_only_the_newer_commits_files() {
let tmp = tempfile::tempdir().unwrap();
init_git_repo(tmp.path());
let sha1 = commit_file(tmp.path(), "a.txt", "a");
let sha2 = commit_file(tmp.path(), "b.txt", "b");
let refs = vec![PushRef {
local_sha: sha2,
remote_sha: sha1,
}];
let files = changed_files_for_refs(tmp.path(), &refs);
assert_eq!(files, vec!["b.txt".to_string()]);
}
#[test]
fn new_branch_zero_remote_sha_resolves_via_empty_tree() {
let tmp = tempfile::tempdir().unwrap();
init_git_repo(tmp.path());
commit_file(tmp.path(), "a.txt", "a");
let sha2 = commit_file(tmp.path(), "b.txt", "b");
let refs = vec![PushRef {
local_sha: sha2,
remote_sha: "0".repeat(40),
}];
let mut files = changed_files_for_refs(tmp.path(), &refs);
files.sort();
assert_eq!(files, vec!["a.txt".to_string(), "b.txt".to_string()]);
}
#[test]
fn identical_shas_yield_an_empty_file_list() {
let tmp = tempfile::tempdir().unwrap();
init_git_repo(tmp.path());
let sha = commit_file(tmp.path(), "a.txt", "a");
let refs = vec![PushRef {
local_sha: sha.clone(),
remote_sha: sha,
}];
let files = changed_files_for_refs(tmp.path(), &refs);
assert!(
files.is_empty(),
"a no-op push (same local and remote sha) must yield no files, got {files:?}"
);
}
#[test]
fn non_ascii_path_is_returned_raw_not_octal_quoted() {
let tmp = tempfile::tempdir().unwrap();
init_git_repo(tmp.path());
std::fs::create_dir_all(tmp.path().join("src")).unwrap();
let sha1 = commit_file(tmp.path(), "src/café.rs", "one");
let sha2 = commit_file(tmp.path(), "src/café.rs", "two");
let refs = vec![PushRef {
local_sha: sha2,
remote_sha: sha1,
}];
let files = changed_files_for_refs(tmp.path(), &refs);
assert_eq!(
files,
vec!["src/café.rs".to_string()],
"must be the raw UTF-8 path, not git's octal-quoted rendering; got {files:?}"
);
}
#[test]
fn path_containing_a_space_is_returned_intact() {
let tmp = tempfile::tempdir().unwrap();
init_git_repo(tmp.path());
std::fs::create_dir_all(tmp.path().join("src")).unwrap();
let sha1 = commit_file(tmp.path(), "src/has space.rs", "one");
let sha2 = commit_file(tmp.path(), "src/has space.rs", "two");
let refs = vec![PushRef {
local_sha: sha2,
remote_sha: sha1,
}];
let files = changed_files_for_refs(tmp.path(), &refs);
assert_eq!(files, vec!["src/has space.rs".to_string()]);
}
#[test]
fn rename_reports_both_source_and_destination() {
let tmp = tempfile::tempdir().unwrap();
init_git_repo(tmp.path());
std::fs::create_dir_all(tmp.path().join("src")).unwrap();
let sha1 = commit_file(tmp.path(), "src/plain.rs", "identical content");
let ok = Command::new("git")
.args([
"-C",
&tmp.path().to_string_lossy(),
"mv",
"src/plain.rs",
"src/renamed.rs",
])
.status()
.expect("git mv failed")
.success();
assert!(ok, "git mv must succeed");
let ok = Command::new("git")
.args([
"-C",
&tmp.path().to_string_lossy(),
"commit",
"-m",
"rename plain.rs to renamed.rs",
])
.status()
.expect("git commit failed")
.success();
assert!(ok, "git commit must succeed");
let sha2 = head_sha(tmp.path());
let refs = vec![PushRef {
local_sha: sha2,
remote_sha: sha1,
}];
let mut files = changed_files_for_refs(tmp.path(), &refs);
files.sort();
assert_eq!(
files,
vec!["src/plain.rs".to_string(), "src/renamed.rs".to_string()],
"a rename must surface BOTH the source and destination path; got {files:?}"
);
}
#[test]
fn git_empty_tree_sha_is_the_well_known_constant() {
let tmp = tempfile::tempdir().unwrap();
init_git_repo(tmp.path());
let empty_tree =
git_empty_tree_sha(tmp.path()).expect("git hash-object must resolve the empty tree");
assert_eq!(empty_tree, "4b825dc642cb6eb9a060e54bf8d69288fbee4904");
}
#[test]
fn upstream_fallback_diff_fails_cleanly_with_no_upstream_configured() {
let tmp = tempfile::tempdir().unwrap();
init_git_repo(tmp.path());
commit_file(tmp.path(), "a.txt", "a");
let out = Command::new("git")
.args([
"diff",
"--name-only",
"-z",
"--no-renames",
"@{upstream}..HEAD",
])
.current_dir(tmp.path())
.output()
.expect("git diff must run");
assert!(
!out.status.success(),
"no upstream is configured, so this must fail rather than \
silently resolve to something else"
);
}
#[test]
fn upstream_fallback_diff_is_empty_when_upstream_matches_head() {
let tmp = tempfile::tempdir().unwrap();
init_git_repo(tmp.path());
commit_file(tmp.path(), "a.txt", "a");
let branch_out = Command::new("git")
.args(["symbolic-ref", "--short", "HEAD"])
.current_dir(tmp.path())
.output()
.expect("git symbolic-ref must run");
assert!(branch_out.status.success());
let branch = String::from_utf8_lossy(&branch_out.stdout)
.trim()
.to_string();
for args in [
vec!["branch", "tracking", &branch],
vec!["branch", "--set-upstream-to=tracking", &branch],
] {
let ok = Command::new("git")
.args(&args)
.current_dir(tmp.path())
.status()
.expect("git command must run")
.success();
assert!(ok, "git {args:?} must succeed");
}
let out = Command::new("git")
.args([
"diff",
"--name-only",
"-z",
"--no-renames",
"@{upstream}..HEAD",
])
.current_dir(tmp.path())
.output()
.expect("git diff must run");
assert!(out.status.success());
assert!(
out.stdout.is_empty(),
"identical upstream/HEAD must diff to no files"
);
}
}
#[cfg(test)]
mod read_with_timeout_tests {
use super::read_with_timeout;
use std::time::Duration;
#[test]
fn returns_the_value_when_it_arrives_before_the_deadline() {
let got = read_with_timeout(|| Some("hi".to_string()), Duration::from_secs(1));
assert_eq!(got, Some("hi".to_string()));
}
#[test]
fn returns_none_when_the_closure_itself_returns_none() {
let got = read_with_timeout(|| None, Duration::from_secs(1));
assert_eq!(got, None);
}
#[test]
fn times_out_instead_of_hanging_on_a_closure_that_never_returns() {
let start = std::time::Instant::now();
let got = read_with_timeout(
|| {
std::thread::sleep(Duration::from_secs(10));
Some("too late".to_string())
},
Duration::from_millis(50),
);
assert_eq!(got, None, "must give up rather than wait for the closure");
assert!(
start.elapsed() < Duration::from_secs(1),
"must return promptly on timeout, not block on the stalled closure; took {:?}",
start.elapsed()
);
}
}