@@ -1255,6 +1255,270 @@ mod tests {
12551255
12561256 Self { path, repo }
12571257 }
1258+
1259+ fn commit_file (
1260+ & self ,
1261+ relative_path : & str ,
1262+ content : & str ,
1263+ author_name : & str ,
1264+ author_email : & str ,
1265+ timestamp : i64 ,
1266+ message : & str ,
1267+ ) -> String {
1268+ let file_path = self . path . join ( relative_path) ;
1269+ file_path
1270+ . parent ( )
1271+ . map ( std:: fs:: create_dir_all)
1272+ . transpose ( )
1273+ . unwrap ( ) ;
1274+ std:: fs:: write ( & file_path, content) . unwrap ( ) ;
1275+
1276+ let mut index = self . repo . index ( ) . unwrap ( ) ;
1277+ index. add_path ( std:: path:: Path :: new ( relative_path) ) . unwrap ( ) ;
1278+ index. write ( ) . unwrap ( ) ;
1279+
1280+ let tree_id = index. write_tree ( ) . unwrap ( ) ;
1281+ let tree = self . repo . find_tree ( tree_id) . unwrap ( ) ;
1282+ let signature =
1283+ git2:: Signature :: new ( author_name, author_email, & git2:: Time :: new ( timestamp, 0 ) )
1284+ . unwrap ( ) ;
1285+ let parent = self
1286+ . repo
1287+ . head ( )
1288+ . ok ( )
1289+ . and_then ( |head| head. peel_to_commit ( ) . ok ( ) ) ;
1290+
1291+ let oid = match parent. as_ref ( ) {
1292+ Some ( parent_commit) => self . repo . commit (
1293+ Some ( "HEAD" ) ,
1294+ & signature,
1295+ & signature,
1296+ message,
1297+ & tree,
1298+ & [ parent_commit] ,
1299+ ) ,
1300+ None => self
1301+ . repo
1302+ . commit ( Some ( "HEAD" ) , & signature, & signature, message, & tree, & [ ] ) ,
1303+ }
1304+ . unwrap ( ) ;
1305+
1306+ oid. to_string ( )
1307+ }
1308+ }
1309+
1310+ #[ test]
1311+ fn test_commit_navigation_respects_order_and_reset ( ) {
1312+ let test_repo = TestRepo :: new ( ) ;
1313+ let oldest = test_repo. commit_file (
1314+ "history.txt" ,
1315+ "one\n " ,
1316+ "Alice Example" ,
1317+ 1318+ 1_700_000_000 ,
1319+ "first" ,
1320+ ) ;
1321+ let middle = test_repo. commit_file (
1322+ "history.txt" ,
1323+ "two\n " ,
1324+ "Bob Example" ,
1325+ 1326+ 1_700_000_060 ,
1327+ "second" ,
1328+ ) ;
1329+ let newest = test_repo. commit_file (
1330+ "history.txt" ,
1331+ "three\n " ,
1332+ "Carol Example" ,
1333+ 1334+ 1_700_000_120 ,
1335+ "third" ,
1336+ ) ;
1337+ let repo = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1338+
1339+ assert_eq ! ( repo. next_desc_commit( ) . unwrap( ) . hash, newest) ;
1340+ assert_eq ! ( repo. next_desc_commit( ) . unwrap( ) . hash, middle) ;
1341+
1342+ repo. reset_index ( ) ;
1343+
1344+ assert_eq ! ( repo. next_asc_commit( ) . unwrap( ) . hash, oldest) ;
1345+ assert_eq ! ( repo. next_asc_commit( ) . unwrap( ) . hash, middle) ;
1346+ assert_eq ! ( repo. next_asc_commit( ) . unwrap( ) . hash, newest) ;
1347+ assert ! ( repo. next_asc_commit( ) . is_err( ) ) ;
1348+ }
1349+
1350+ #[ test]
1351+ fn test_commit_filters_match_author_and_inclusive_dates ( ) {
1352+ let test_repo = TestRepo :: new ( ) ;
1353+ let alice = test_repo. commit_file (
1354+ "history.txt" ,
1355+ "one\n " ,
1356+ "Alice Example" ,
1357+ 1358+ 1_700_001_000 ,
1359+ "alice" ,
1360+ ) ;
1361+ let bob = test_repo. commit_file (
1362+ "history.txt" ,
1363+ "two\n " ,
1364+ "Bob Example" ,
1365+ 1366+ 1_700_001_060 ,
1367+ "bob" ,
1368+ ) ;
1369+ test_repo. commit_file (
1370+ "history.txt" ,
1371+ "three\n " ,
1372+ "Carol Example" ,
1373+ 1374+ 1_700_001_120 ,
1375+ "carol" ,
1376+ ) ;
1377+
1378+ let mut author_filtered = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1379+ author_filtered. set_author_filter ( Some ( "WORK.TEST" . to_string ( ) ) ) ;
1380+ assert_eq ! ( author_filtered. random_commit( ) . unwrap( ) . hash, alice) ;
1381+
1382+ let mut date_filtered = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1383+ let bob_date = DateTime :: from_timestamp ( 1_700_001_060 , 0 ) . unwrap ( ) ;
1384+ date_filtered. set_after_filter ( Some ( bob_date) ) ;
1385+ date_filtered. set_before_filter ( Some ( bob_date) ) ;
1386+ assert_eq ! ( date_filtered. next_desc_commit( ) . unwrap( ) . hash, bob) ;
1387+ assert ! ( date_filtered. next_desc_commit( ) . is_err( ) ) ;
1388+
1389+ let mut missing_author = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1390+ missing_author. set_author_filter ( Some ( "nobody" . to_string ( ) ) ) ;
1391+ assert ! ( missing_author. next_desc_commit( ) . is_err( ) ) ;
1392+ }
1393+
1394+ #[ test]
1395+ fn test_commit_range_lookup_and_validation ( ) {
1396+ let test_repo = TestRepo :: new ( ) ;
1397+ test_repo. commit_file (
1398+ "src/lib.rs" ,
1399+ "pub fn oldest() {}\n " ,
1400+ "Alice Example" ,
1401+ 1402+ 1_700_002_000 ,
1403+ "oldest" ,
1404+ ) ;
1405+ let middle = test_repo. commit_file (
1406+ "src/lib.rs" ,
1407+ "pub fn middle() {}\n " ,
1408+ "Bob Example" ,
1409+ 1410+ 1_700_002_060 ,
1411+ "middle" ,
1412+ ) ;
1413+ let newest = test_repo. commit_file (
1414+ "src/lib.rs" ,
1415+ "pub fn newest() {}\n " ,
1416+ "Carol Example" ,
1417+ 1418+ 1_700_002_120 ,
1419+ "newest" ,
1420+ ) ;
1421+
1422+ let repo = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1423+ let metadata = repo. get_commit ( & middle) . unwrap ( ) ;
1424+ assert_eq ! ( metadata. message, "middle" ) ;
1425+ assert_eq ! ( metadata. changes[ 0 ] . path, "src/lib.rs" ) ;
1426+
1427+ repo. set_commit_range ( "HEAD~2..HEAD" ) . unwrap ( ) ;
1428+ assert_eq ! ( repo. next_range_commit_asc( ) . unwrap( ) . hash, middle) ;
1429+ assert_eq ! ( repo. next_range_commit_asc( ) . unwrap( ) . hash, newest) ;
1430+ assert ! ( repo. next_range_commit_asc( ) . is_err( ) ) ;
1431+
1432+ let desc_repo = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1433+ desc_repo. set_commit_range ( "HEAD~2..HEAD" ) . unwrap ( ) ;
1434+ assert_eq ! ( desc_repo. next_range_commit_desc( ) . unwrap( ) . hash, newest) ;
1435+ assert_eq ! ( desc_repo. next_range_commit_desc( ) . unwrap( ) . hash, middle) ;
1436+
1437+ let random_repo = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1438+ random_repo. set_commit_range ( "HEAD~2..HEAD" ) . unwrap ( ) ;
1439+ let random_hash = random_repo. random_range_commit ( ) . unwrap ( ) . hash ;
1440+ assert ! ( [ middle. clone( ) , newest. clone( ) ] . contains( & random_hash) ) ;
1441+
1442+ let invalid_repo = GitRepository :: open ( & test_repo. path ) . unwrap ( ) ;
1443+ assert ! ( invalid_repo. set_commit_range( "HEAD...HEAD" ) . is_err( ) ) ;
1444+ assert ! ( invalid_repo. set_commit_range( "HEAD" ) . is_err( ) ) ;
1445+ }
1446+
1447+ #[ test]
1448+ fn test_parse_date_and_sorted_file_indices_cover_edge_cases ( ) {
1449+ let parsed = parse_date ( "2024-01-01" ) . unwrap ( ) ;
1450+ assert_eq ! (
1451+ parsed. with_timezone( & Local ) . format( "%Y-%m-%d" ) . to_string( ) ,
1452+ "2024-01-01"
1453+ ) ;
1454+ assert ! ( parse_date( "definitely-not-a-date" ) . is_err( ) ) ;
1455+
1456+ let metadata = CommitMetadata {
1457+ hash : "hash" . to_string ( ) ,
1458+ author : "author" . to_string ( ) ,
1459+ date : Utc :: now ( ) ,
1460+ message : "message" . to_string ( ) ,
1461+ changes : vec ! [
1462+ FileChange {
1463+ path: "src/main.rs" . to_string( ) ,
1464+ old_path: None ,
1465+ status: FileStatus :: Modified ,
1466+ is_binary: false ,
1467+ is_excluded: false ,
1468+ exclusion_reason: None ,
1469+ old_content: None ,
1470+ new_content: None ,
1471+ hunks: vec![ ] ,
1472+ diff: String :: new( ) ,
1473+ } ,
1474+ FileChange {
1475+ path: "Cargo.toml" . to_string( ) ,
1476+ old_path: None ,
1477+ status: FileStatus :: Added ,
1478+ is_binary: false ,
1479+ is_excluded: false ,
1480+ exclusion_reason: None ,
1481+ old_content: None ,
1482+ new_content: None ,
1483+ hunks: vec![ ] ,
1484+ diff: String :: new( ) ,
1485+ } ,
1486+ FileChange {
1487+ path: "src/lib.rs" . to_string( ) ,
1488+ old_path: None ,
1489+ status: FileStatus :: Modified ,
1490+ is_binary: false ,
1491+ is_excluded: false ,
1492+ exclusion_reason: None ,
1493+ old_content: None ,
1494+ new_content: None ,
1495+ hunks: vec![ ] ,
1496+ diff: String :: new( ) ,
1497+ } ,
1498+ FileChange {
1499+ path: "docs/guide.md" . to_string( ) ,
1500+ old_path: None ,
1501+ status: FileStatus :: Added ,
1502+ is_binary: false ,
1503+ is_excluded: false ,
1504+ exclusion_reason: None ,
1505+ old_content: None ,
1506+ new_content: None ,
1507+ hunks: vec![ ] ,
1508+ diff: String :: new( ) ,
1509+ } ,
1510+ ] ,
1511+ } ;
1512+
1513+ let sorted_paths: Vec < & str > = metadata
1514+ . sorted_file_indices ( )
1515+ . iter ( )
1516+ . map ( |& index| metadata. changes [ index] . path . as_str ( ) )
1517+ . collect ( ) ;
1518+ assert_eq ! (
1519+ sorted_paths,
1520+ vec![ "Cargo.toml" , "docs/guide.md" , "src/lib.rs" , "src/main.rs" ]
1521+ ) ;
12581522 }
12591523
12601524 #[ test]
0 commit comments