use forge_git::blame::blame; use forge_git::branches::{ahead_behind, create_branch, delete_branch}; use forge_git::command::ObjectSource; use forge_git::history::{commit_diff_ws, commit_patch, commit_raw_diff, list_files, range_patch}; use crate::common::{BIG, WorkRepo}; #[tokio::test] async fn ignore_whitespace_drops_whitespace_only_files() { let f = WorkRepo::new(); f.write("x.txt", "a\nb\n"); f.write("y.txt", "c\n"); f.commit("one"); f.write("x.txt", "a \nb\n"); f.write("y.txt", "c\nd\n"); let head = f.commit("two"); let full = commit_diff_ws(&f.env, &f.git_dir, &head, BIG, false) .await .unwrap(); assert_eq!(full.files.len(), 2); let ws = commit_diff_ws(&f.env, &f.git_dir, &head, BIG, true) .await .unwrap(); assert_eq!(ws.files.len(), 1); assert_eq!(ws.files[0].new_path.as_deref(), Some("y.txt")); assert_eq!(ws.files[0].additions, 1); } #[tokio::test] async fn patch_and_diff_outputs() { let f = WorkRepo::new(); f.write("x.txt", "a\n"); let root = f.commit("root commit"); f.write("x.txt", "a\nb\n"); let head = f.commit("add b"); let patch = commit_patch(&f.env, &f.git_dir, &head).await.unwrap(); assert!(patch.complete); let text = String::from_utf8(patch.data).unwrap(); assert!(text.starts_with(&format!("From {head} "))); assert!(text.contains("Subject: [PATCH] add b")); assert!(text.contains("+b")); let root_patch = commit_patch(&f.env, &f.git_dir, &root).await.unwrap(); assert!( String::from_utf8(root_patch.data) .unwrap() .contains("+++ b/x.txt") ); let diff = commit_raw_diff(&f.env, &f.git_dir, &head).await.unwrap(); let diff = String::from_utf8(diff.data).unwrap(); assert!(diff.starts_with("diff --git a/x.txt b/x.txt")); let range = range_patch(&f.env, &f.git_dir, ObjectSource::default(), &root, &head) .await .unwrap(); let range = String::from_utf8(range.data).unwrap(); assert_eq!(range.matches("\nSubject: ").count(), 1); assert!(range.contains("add b")); assert!(!range.contains("root commit")); } #[tokio::test] async fn list_files_is_recursive_and_capped() { let f = WorkRepo::new(); f.write("a.txt", "1"); f.write("dir/b.txt", "2"); f.write("dir/sub/c.txt", "3"); let head = f.commit("files"); let (all, cut) = list_files(&f.env, &f.git_dir, &head, 100).await.unwrap(); assert!(!cut); assert_eq!(all, ["a.txt", "dir/b.txt", "dir/sub/c.txt"]); let (two, cut) = list_files(&f.env, &f.git_dir, &head, 2).await.unwrap(); assert!(cut); assert_eq!(two.len(), 2); } #[tokio::test] async fn blame_groups_by_commit() { let f = WorkRepo::new(); f.write("f.txt", "one\ntwo\nthree\n"); let first = f.commit("first"); f.write("f.txt", "one\nTWO\nthree\nfour\n"); let second = f.commit("second"); let b = blame(&f.env, &f.git_dir, &second, "f.txt", false) .await .unwrap(); assert_eq!(b.total, 4); let shape: Vec<(&str, u32, u32)> = b .groups .iter() .map(|g| (g.commit.as_str(), g.start, g.lines)) .collect(); assert_eq!( shape, [ (first.as_str(), 1, 1), (second.as_str(), 2, 1), (first.as_str(), 3, 1), (second.as_str(), 4, 1), ] ); assert_eq!(b.commits[&second].summary, "second"); assert_eq!(b.commits[&second].author, "tahinli"); assert_eq!( b.commits[&second].previous, Some((first.clone(), "f.txt".to_owned())) ); } #[tokio::test] async fn branch_counts_create_and_delete() { let f = WorkRepo::new(); f.write("a.txt", "1"); let base = f.commit("base"); f.git(&["checkout", "-q", "-b", "feature"]); f.write("b.txt", "2"); f.commit("feature 1"); let tip = f.commit("feature 2"); f.git(&["checkout", "-q", "main"]); f.write("c.txt", "3"); let main_tip = f.commit("main moved"); let (ahead, behind) = ahead_behind(&f.env, &f.git_dir, &main_tip, &tip) .await .unwrap(); assert_eq!((ahead, behind), (2, 1)); create_branch(&f.env, &f.git_dir, "topic/x", &base) .await .unwrap(); assert_eq!( f.git(&["rev-parse", "refs/heads/topic/x"]).trim(), base.as_str() ); assert!( create_branch(&f.env, &f.git_dir, "topic/x", &base) .await .is_err() ); assert!( create_branch(&f.env, &f.git_dir, "bad..name", &base) .await .is_err() ); delete_branch(&f.env, &f.git_dir, "topic/x", &base) .await .unwrap(); assert!( delete_branch(&f.env, &f.git_dir, "topic/x", &base) .await .is_err() ); }