liboxen 0.46.12

Oxen is a fast, unstructured data version control, to help version large machine learning datasets written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
//! # oxen checkout
//!
//! Checkout a branch or commit
//!

use std::path::Path;

use crate::core::df::tabular;
use crate::error::OxenError;
use crate::model::{Branch, LocalRepository};
use crate::opts::{DFOpts, RestoreOpts};
use crate::{repositories, util};

/// # Checkout a branch or commit id
/// This switches HEAD to point to the branch name or commit id,
/// it also updates all the local files to be from the commit that this branch references
pub async fn checkout(
    repo: &LocalRepository,
    value: impl AsRef<str>,
) -> Result<Option<Branch>, OxenError> {
    let value = value.as_ref();
    log::debug!("--- CHECKOUT START {value} ----");
    if repositories::branches::exists(repo, value)? {
        if repositories::branches::is_checked_out(repo, value) {
            println!("Already on branch {value}");
            return Ok(Some(repositories::branches::get_by_name(repo, value)?));
        }

        println!("Checkout branch: {value}");
        let commit = repositories::revisions::get(repo, value)?
            .ok_or_else(|| OxenError::RevisionNotFound(value.into()))?;
        let subtree_paths = match repo.subtree_paths() {
            Some(paths_vec) => paths_vec, // If Some(vec), take the inner vector
            None => vec![Path::new("").to_path_buf()],
        };
        let depth = repo.depth().unwrap_or(i32::MAX); //TODO: make repo depth not an option so that we use depth from the repo consistently.
        repositories::branches::checkout_subtrees_to_commit(repo, &commit, &subtree_paths, depth)
            .await?;
        repositories::branches::set_head(repo, value)?;
        Ok(Some(repositories::branches::get_by_name(repo, value)?))
    } else {
        // If we are already on the commit, do nothing
        if repositories::branches::is_checked_out(repo, value) {
            eprintln!("Commit already checked out {value}");
            return Ok(None);
        }

        let commit = repositories::revisions::get(repo, value)?
            .ok_or_else(|| OxenError::RevisionNotFound(value.into()))?;

        let previous_head_commit = repositories::commits::head_commit_maybe(repo)?;
        repositories::branches::checkout_commit_from_commit(repo, &commit, &previous_head_commit)
            .await?;
        repositories::branches::update(repo, value, &commit.id)?;
        repositories::branches::set_head(repo, value)?;

        if repo.is_remote_mode() {
            // Set workspace_name to new branch name
            let mut mut_repo = repo.clone();
            mut_repo.set_workspace(value)?;
            mut_repo.save()?;
        }

        Ok(None)
    }
}

/// # Checkout a file and take their changes
/// This overwrites the current file with the changes in the branch we are merging in
pub async fn checkout_theirs(
    repo: &LocalRepository,
    path: impl AsRef<Path>,
) -> Result<(), OxenError> {
    let conflicts = repositories::merge::list_conflicts(repo)?;
    log::debug!(
        "checkout_theirs {:?} conflicts.len() {}",
        path.as_ref(),
        conflicts.len()
    );

    // find the path that matches in the conflict, throw error if !found
    if let Some(conflict) = conflicts
        .iter()
        .find(|c| c.merge_entry.path == path.as_ref())
    {
        // Lookup the file for the merge commit entry and copy it over
        repositories::restore::restore(
            repo,
            RestoreOpts::from_path_ref(path, conflict.merge_entry.commit_id.clone()),
        )
        .await
    } else {
        Err(OxenError::could_not_find_merge_conflict(path))
    }
}

/// # Checkout a file and take our changes
/// This overwrites the current file with the changes we had in our current branch
pub async fn checkout_ours(
    repo: &LocalRepository,
    path: impl AsRef<Path>,
) -> Result<(), OxenError> {
    let conflicts = repositories::merge::list_conflicts(repo)?;
    log::debug!(
        "checkout_ours {:?} conflicts.len() {}",
        path.as_ref(),
        conflicts.len()
    );

    // find the path that matches in the conflict, throw error if !found
    if let Some(conflict) = conflicts
        .iter()
        .find(|c| c.merge_entry.path == path.as_ref())
    {
        // Lookup the file for the base commit entry and copy it over
        repositories::restore(
            repo,
            RestoreOpts::from_path_ref(path, conflict.base_entry.commit_id.clone()),
        )
        .await
    } else {
        Err(OxenError::could_not_find_merge_conflict(path))
    }
}

/// # Combine Conflicting Tabular Data Files
/// This overwrites the current file with the changes in their file
pub async fn checkout_combine<P: AsRef<Path>>(
    repo: &LocalRepository,
    path: P,
) -> Result<(), OxenError> {
    let conflicts = repositories::merge::list_conflicts(repo)?;

    log::debug!(
        "checkout_combine checking path {:?} -> [{}] conflicts",
        path.as_ref(),
        conflicts.len()
    );
    // find the path that matches in the conflict, throw error if !found
    if let Some(conflict) = conflicts
        .iter()
        .find(|c| c.merge_entry.path == path.as_ref())
    {
        if util::fs::is_tabular(&conflict.base_entry.path) {
            let version_store = repo.version_store()?;
            let df_base_path = version_store
                .get_version_path(&conflict.base_entry.hash)
                .await?;
            let df_base = tabular::maybe_read_df_with_extension(
                repo,
                &df_base_path,
                &conflict.base_entry.path,
                &conflict.base_entry.commit_id,
                &DFOpts::empty(),
            )
            .await?;
            let df_merge_path = version_store
                .get_version_path(&conflict.merge_entry.hash)
                .await?;
            let df_merge = tabular::maybe_read_df_with_extension(
                repo,
                df_merge_path,
                &conflict.merge_entry.path,
                &conflict.merge_entry.commit_id,
                &DFOpts::empty(),
            )
            .await?;

            log::debug!("GOT DF HEAD {df_base}");
            log::debug!("GOT DF MERGE {df_merge}");

            match df_base.vstack(&df_merge) {
                Ok(result) => {
                    log::debug!("GOT DF COMBINED {result}");
                    match result.unique_stable(None, polars::frame::UniqueKeepStrategy::First, None)
                    {
                        Ok(mut uniq) => {
                            log::debug!("GOT DF COMBINED UNIQUE {uniq}");
                            let output_path = repo.path.join(&conflict.base_entry.path);
                            tabular::write_df(&mut uniq, &output_path)
                        }
                        _ => Err(OxenError::basic_str("Could not uniq data")),
                    }
                }
                _ => Err(OxenError::basic_str(
                    "Could not combine data, make sure schema's match",
                )),
            }
        } else {
            Err(OxenError::basic_str(
                "Cannot use --combine on non-tabular data file.",
            ))
        }
    } else {
        Err(OxenError::could_not_find_merge_conflict(path))
    }
}

#[cfg(test)]
mod tests {
    use crate::api;
    use crate::constants::DEFAULT_BRANCH_NAME;
    use crate::error::OxenError;
    use crate::opts::FetchOpts;
    use crate::repositories;
    use crate::test;
    use crate::util;

    #[tokio::test]
    async fn test_command_checkout_non_existant_commit_id() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // This shouldn't work
            let checkout_result = repositories::checkout(&repo, "non-existent").await;
            assert!(checkout_result.is_err());

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_command_checkout_commit_id() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write a hello file
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;

            // Stage a hello file
            repositories::add(&repo, &hello_file).await?;
            // Commit the hello file
            let first_commit = repositories::commit(&repo, "Adding hello")?;

            // Write a world
            let world_file = repo.path.join("world.txt");
            util::fs::write_to_path(&world_file, "World")?;

            // Stage a world file
            repositories::add(&repo, &world_file).await?;

            // Commit the world file
            repositories::commit(&repo, "Adding world")?;

            // We have the world file
            assert!(world_file.exists());

            // We checkout the previous commit
            repositories::checkout(&repo, first_commit.id).await?;

            // // Then we do not have the world file anymore
            assert!(!world_file.exists());

            // // Check status
            let status = repositories::status(&repo)?;
            assert!(status.is_clean());

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_command_checkout_commit_then_merge_main() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write a hello file
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;

            // Track & commit the file
            repositories::add(&repo, &hello_file).await?;
            let first_commit = repositories::commit(&repo, "Added hello.txt")?;

            // Write a world
            let world_file = repo.path.join("world.txt");
            util::fs::write_to_path(&world_file, "World")?;

            // Stage a world file
            repositories::add(&repo, &world_file).await?;

            // Commit the world file
            let _ = repositories::commit(&repo, "Adding world")?;

            // Create and checkout branch
            let branch_name = "feature/my-branch";
            repositories::branches::create_checkout(&repo, branch_name)?;

            // Add a third file
            let branch_file = repo.path.join("branch.txt");
            util::fs::write_to_path(&branch_file, "Branch file")?;

            // Stage a world file
            repositories::add(&repo, &branch_file).await?;

            // Commit the world file
            repositories::commit(&repo, "Adding branch file")?;

            // We have the branch file
            assert!(branch_file.exists());

            // Checkout the previous commit
            repositories::checkout(&repo, first_commit.id).await?;

            // Then we do not have the branch file anymore
            assert!(!branch_file.exists());
            assert!(!world_file.exists());

            // Check status
            let status = repositories::status(&repo)?;
            assert!(status.is_clean());

            // Checkout branch again
            repositories::checkout(&repo, branch_name).await?;

            // // Merge to main again
            // let og_branch = repositories::branches::current_branch(&repo)?.unwrap();
            // // Checkout the branch
            // repositories::checkout(&repo, second_commit.id).await?;

            let has_merges = repositories::merge::merge(&repo, DEFAULT_BRANCH_NAME)
                .await
                .is_ok();

            // We should not have a merge because the current branch has all the old commits
            assert!(!has_merges);
            assert!(world_file.exists());
            assert!(hello_file.exists());

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_command_checkout_current_branch_name_does_nothing() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write the first file
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;

            // Track & commit the file
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Added hello.txt")?;

            // Create and checkout branch
            let branch_name = "feature/world-explorer";
            repositories::branches::create_checkout(&repo, branch_name)?;
            repositories::checkout(&repo, branch_name).await?;

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_cannot_checkout_branch_with_dots_in_name() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write the first file
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;

            // Track & commit the file
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Added hello.txt")?;

            // Create and checkout branch
            let branch_name = "test..ing";
            let result = repositories::branches::create_checkout(&repo, branch_name);
            assert!(result.is_err());

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_command_checkout_added_file() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write the first file
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;

            // Track & commit the file
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Added hello.txt")?;

            // Get the original branch name
            let orig_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create and checkout branch
            let branch_name = "feature/world-explorer";
            repositories::branches::create_checkout(&repo, branch_name)?;

            // Write a second file
            let world_file = repo.path.join("world.txt");
            util::fs::write_to_path(&world_file, "World")?;

            // Track & commit the second file in the branch
            repositories::add(&repo, &world_file).await?;
            repositories::commit(&repo, "Added world.txt")?;

            // Make sure we have both commits
            let commits = repositories::commits::list(&repo)?;
            assert_eq!(commits.len(), 2);

            let branches = repositories::branches::list(&repo)?;
            assert_eq!(branches.len(), 2);

            // Make sure we have both files on disk in our repo dir
            assert!(hello_file.exists());
            assert!(world_file.exists());

            // Go back to the main branch
            repositories::checkout(&repo, orig_branch.name).await?;

            // The world file should no longer be there
            assert!(hello_file.exists());
            assert!(!world_file.exists());

            // Go back to the world branch
            repositories::checkout(&repo, branch_name).await?;
            assert!(hello_file.exists());
            assert!(world_file.exists());

            Ok(())
        })
        .await
    }

    /*
     * Modify the file on a branch
     * Commit file on branch
     * Checkout main
     * Modify the file on main
     * Merge branch into main
     * Assert that the file on main is not overwritten
     */
    #[tokio::test]
    async fn test_command_merge_does_not_overwrite_modified_file() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write the first file
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;

            // Track & commit the file
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Added hello.txt")?;

            // Get the original branch name
            let orig_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create and checkout branch
            let branch_name = "feature/world-explorer";
            repositories::branches::create_checkout(&repo, branch_name)?;

            // Write a second file
            let world_file = repo.path.join("world.txt");
            util::fs::write_to_path(&world_file, "World")?;

            // Track & commit the second file in the branch
            repositories::add(&repo, &world_file).await?;
            repositories::commit(&repo, "Added world.txt")?;

            // Modify the hello file on the branch
            let hello_file = test::modify_txt_file(hello_file, "Hello from branch")?;
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Changed hello.txt on branch")?;

            // Checkout the main branch
            repositories::checkout(&repo, orig_branch.name).await?;

            // Modify the hello file on the main branch
            let hello_file = test::modify_txt_file(hello_file, "Hello from main")?;

            // Merge the branch into main while there are conflicts
            let result = repositories::merge::merge(&repo, branch_name).await;
            assert!(result.is_err());

            // Assert that the hello file on main is not overwritten
            assert_eq!(util::fs::read_from_path(&hello_file)?, "Hello from main");

            Ok(())
        })
        .await
    }

    /*
     * Modify the file on a branch
     * Commit file on branch
     * Checkout main
     * Add a new file on main
     * Merge branch into main
     * Assert that the new file on main is not overwritten
     */
    #[tokio::test]
    async fn test_command_merge_does_not_overwrite_new_file() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write the first file
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;

            // Track & commit the file
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Added hello.txt")?;

            // Get the original branch name
            let orig_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create and checkout branch
            let branch_name = "feature/world-explorer";
            repositories::branches::create_checkout(&repo, branch_name)?;

            // Write a second file
            let world_file = repo.path.join("world.txt");
            util::fs::write_to_path(&world_file, "World")?;

            // Track & commit the second file in the branch
            repositories::add(&repo, &world_file).await?;
            repositories::commit(&repo, "Added world.txt")?;

            // Modify the hello file on the branch
            let hello_file = test::modify_txt_file(hello_file, "Hello from branch")?;
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Changed hello.txt on branch")?;

            // Checkout the main branch
            repositories::checkout(&repo, orig_branch.name).await?;

            // Add a new file on main
            let new_file = repo.path.join("new_file.txt");
            util::fs::write_to_path(&new_file, "New file")?;

            // Merge the branch should not have conflicts
            repositories::merge::merge(&repo, branch_name).await?;

            // Assert that the new file on main is not overwritten
            assert_eq!(util::fs::read_from_path(&new_file)?, "New file");

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_command_checkout_added_file_keep_untracked() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write the first file
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;

            // Have another file lying around we will not remove
            let keep_file = repo.path.join("keep_me.txt");
            util::fs::write_to_path(&keep_file, "I am untracked, don't remove me")?;

            // Track & commit the file
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Added hello.txt")?;

            // Get the original branch name
            let orig_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create and checkout branch
            let branch_name = "feature/world-explorer";
            repositories::branches::create_checkout(&repo, branch_name)?;

            // Write a second file
            let world_file = repo.path.join("world.txt");
            util::fs::write_to_path(&world_file, "World")?;

            // Track & commit the second file in the branch
            repositories::add(&repo, &world_file).await?;
            repositories::commit(&repo, "Added world.txt")?;

            // Make sure we have both commits
            let commits = repositories::commits::list(&repo)?;
            assert_eq!(commits.len(), 2);

            let branches = repositories::branches::list(&repo)?;
            assert_eq!(branches.len(), 2);

            // Make sure we have all files on disk in our repo dir
            assert!(hello_file.exists());
            assert!(world_file.exists());
            assert!(keep_file.exists());

            // Go back to the main branch
            repositories::checkout(&repo, orig_branch.name).await?;

            // The world file should no longer be there
            assert!(hello_file.exists());
            assert!(!world_file.exists());
            assert!(keep_file.exists());

            // Go back to the world branch
            repositories::checkout(&repo, branch_name).await?;
            assert!(hello_file.exists());
            assert!(world_file.exists());
            assert!(keep_file.exists());

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_command_checkout_modified_file() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write the first file
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;

            // Track & commit the file
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Added hello.txt")?;

            // Get the original branch name
            let orig_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create and checkout branch
            let branch_name = "feature/world-explorer";
            repositories::branches::create_checkout(&repo, branch_name)?;

            // Modify the file
            let hello_file = test::modify_txt_file(hello_file, "World")?;

            // Track & commit the change in the branch
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Changed file to world")?;

            // It should say World at this point
            assert_eq!(util::fs::read_from_path(&hello_file)?, "World");

            // Go back to the main branch
            repositories::checkout(&repo, orig_branch.name).await?;

            // The file contents should be Hello, not World
            log::debug!("HELLO FILE NAME: {hello_file:?}");
            assert!(hello_file.exists());

            // It should be reverted back to Hello
            assert_eq!(util::fs::read_from_path(&hello_file)?, "Hello");

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_command_checkout_modified_file_in_subdirectory() -> Result<(), OxenError> {
        test::run_select_data_repo_test_no_commits_async("annotations", |repo| async move {
            // Track & commit the file
            let one_shot_path = repo.path.join("annotations/train/one_shot.csv");
            repositories::add(&repo, &one_shot_path).await?;
            repositories::commit(&repo, "Adding one shot")?;

            // Get the original branch name
            let orig_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Get OG file contents
            let og_content = util::fs::read_from_path(&one_shot_path)?;

            let branch_name = "feature/change-the-shot";
            repositories::branches::create_checkout(&repo, branch_name)?;

            let file_contents = "file,label\ntrain/cat_1.jpg,0\n";
            let one_shot_path = test::modify_txt_file(one_shot_path, file_contents)?;
            let status = repositories::status(&repo)?;
            assert_eq!(status.modified_files.len(), 1);
            status.print();
            repositories::add(&repo, &one_shot_path).await?;
            let status = repositories::status(&repo)?;
            status.print();
            repositories::commit(&repo, "Changing one shot")?;

            // checkout OG and make sure it reverts
            repositories::checkout(&repo, orig_branch.name).await?;
            let updated_content = util::fs::read_from_path(&one_shot_path)?;
            assert_eq!(og_content, updated_content);

            // checkout branch again and make sure it reverts
            repositories::checkout(&repo, branch_name).await?;
            let updated_content = util::fs::read_from_path(&one_shot_path)?;
            assert_eq!(file_contents, updated_content);

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_command_checkout_modified_file_from_fully_committed_repo() -> Result<(), OxenError>
    {
        test::run_select_data_repo_test_no_commits_async("annotations", |repo| async move {
            // Track & commit all the data
            let one_shot_path = repo.path.join("annotations/train/one_shot.csv");
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Adding one shot")?;

            // Get the original branch name
            let orig_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Get OG file contents
            let og_content = util::fs::read_from_path(&one_shot_path)?;

            let branch_name = "feature/modify-data";
            repositories::branches::create_checkout(&repo, branch_name)?;

            let file_contents = "file,label\ntrain/cat_1.jpg,0\n";
            let one_shot_path = test::modify_txt_file(one_shot_path, file_contents)?;
            let status = repositories::status(&repo)?;
            assert_eq!(status.modified_files.len(), 1);
            repositories::add(&repo, &one_shot_path).await?;
            let status = repositories::status(&repo)?;
            status.print();
            assert_eq!(status.modified_files.len(), 0);
            assert_eq!(status.staged_files.len(), 1);

            let status = repositories::status(&repo)?;
            status.print();
            repositories::commit(&repo, "Changing one shot")?;

            // checkout OG and make sure it reverts
            repositories::checkout(&repo, orig_branch.name).await?;
            let updated_content = util::fs::read_from_path(&one_shot_path)?;
            assert_eq!(og_content, updated_content);

            // checkout branch again and make sure it reverts
            repositories::checkout(&repo, branch_name).await?;
            let updated_content = util::fs::read_from_path(&one_shot_path)?;
            assert_eq!(file_contents, updated_content);

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_command_remove_dir_then_revert() -> Result<(), OxenError> {
        test::run_select_data_repo_test_no_commits_async("train", |repo| async move {
            // (dir already created in helper)
            let dir_to_remove = repo.path.join("train");
            let og_num_files = util::fs::rcount_files_in_dir(&dir_to_remove);

            // track the dir
            repositories::add(&repo, &dir_to_remove).await?;
            repositories::commit(&repo, "Adding train dir")?;

            // Get the original branch name
            let orig_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create a new branch to make the changes
            let branch_name = "feature/removing-train";
            repositories::branches::create_checkout(&repo, branch_name)?;

            // Delete the directory from disk
            util::fs::remove_dir_all(&dir_to_remove)?;

            // Track the deletion
            repositories::add(&repo, &dir_to_remove).await?;
            repositories::commit(&repo, "Removing train dir")?;

            // checkout OG and make sure it restores the train dir
            repositories::checkout(&repo, orig_branch.name).await?;
            assert!(dir_to_remove.exists());
            assert_eq!(util::fs::rcount_files_in_dir(&dir_to_remove), og_num_files);

            // checkout branch again and make sure it reverts
            repositories::checkout(&repo, branch_name).await?;
            assert!(!dir_to_remove.exists());

            Ok(())
        })
        .await
    }

    // Test the default clone (not --all or --shallow) can revert to files that are not local
    #[tokio::test]
    async fn test_checkout_deleted_after_clone() -> Result<(), OxenError> {
        test::run_training_data_fully_sync_remote(|local_repo, remote_repo| async move {
            let cloned_remote = remote_repo.clone();
            let og_commits = repositories::commits::list_all(&local_repo)?;

            // Clone with the --all flag
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let cloned_repo = repositories::clone_url(
                    &remote_repo.remote.url,
                    &new_repo_dir.join("new_repo"),
                )
                .await?;

                // Make sure we have all the commit objects
                let cloned_commits = repositories::commits::list_all(&cloned_repo)?;
                assert_eq!(og_commits.len(), cloned_commits.len());

                // Make sure we set the HEAD file
                let head_commit = repositories::commits::head_commit(&cloned_repo);
                assert!(head_commit.is_ok());

                // We remove the test/ directory in one of the commits, so make sure we can go
                // back in the history to that commit
                let test_dir_path = cloned_repo.path.join("test");
                let commit = repositories::commits::first_by_message(&cloned_repo, "Adding test/")?;
                assert!(commit.is_some());
                assert!(!test_dir_path.exists());

                // checkout the commit
                repositories::checkout(&cloned_repo, &commit.unwrap().id).await?;
                // Make sure we restored the directory
                assert!(test_dir_path.exists());

                // list files in test_dir_path
                let test_dir_files = util::fs::list_files_in_dir(&test_dir_path).await?;
                println!("test_dir_files: {:?}", test_dir_files.len());
                for file in test_dir_files.iter() {
                    println!("file: {file:?}");
                }
                assert_eq!(test_dir_files.len(), 4);

                assert!(test_dir_path.join("1.jpg").exists());
                assert!(test_dir_path.join("2.jpg").exists());
                assert!(test_dir_path.join("3.jpg").exists());
                assert!(test_dir_path.join("4.jpg").exists());

                Ok(())
            })
            .await?;

            Ok(cloned_remote)
        })
        .await
    }

    /*
    Checks workflow:

    $ oxen clone <URL>

    $ oxen checkout f412d166be1bead8 # earlier commit
    $ oxen checkout 55a4df7cd5d00eee # later commit

    Checkout commit: 55a4df7cd5d00eee
    Setting working directory to 55a4df7cd5d00eee
    IO(Os { code: 2, kind: NotFound, message: "No such file or directory" })

    */
    #[tokio::test]
    async fn test_clone_checkout_old_commit_checkout_new_commit() -> Result<(), OxenError> {
        test::run_training_data_fully_sync_remote(|_, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            test::run_empty_dir_test_async(|repo_dir| async move {
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &repo_dir.join("new_repo"))
                        .await?;

                let commits = repositories::commits::list(&cloned_repo)?;
                // iterate over commits in reverse order and checkout each one
                for commit in commits.iter().rev() {
                    println!(
                        "TEST checking out commit: {} -> '{}'",
                        commit.id, commit.message
                    );
                    repositories::checkout(&cloned_repo, &commit.id).await?;
                }

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_checkout_local_does_not_remove_untracked_files() -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_one_commit_sync_repo_test(|_, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            // Clone Repo to User A
            test::run_empty_dir_test_async(|user_a_repo_dir| async move {
                let user_a_repo_dir_copy = user_a_repo_dir.join("user_a_repo");
                let user_a_repo =
                    repositories::clone_url(&remote_repo.remote.url, &user_a_repo_dir_copy).await?;

                // Create a new branch
                let branch_name = "test-branch";
                repositories::branches::create_checkout(&user_a_repo, branch_name)?;

                // Back to main
                repositories::checkout(&user_a_repo, DEFAULT_BRANCH_NAME).await?;

                // Create some untracked files...
                let file_1 = user_a_repo.path.join("file_1.txt");
                let dir_1 = user_a_repo.path.join("dir_1");
                let file_in_dir_1 = dir_1.join("file_in_dir_1.txt");
                let dir_2 = user_a_repo.path.join("dir_2");
                let subdir_2 = dir_2.join("subdir_2");
                let file_in_dir_2 = subdir_2.join("file_in_dir_2.txt");
                let file_in_subdir_2 = subdir_2.join("file_in_subdir_2.txt");

                // Create the files and dirs
                std::fs::create_dir(&dir_1)?;
                std::fs::create_dir(&dir_2)?;
                std::fs::create_dir(&subdir_2)?;

                test::write_txt_file_to_path(&file_1, "this is file 1")?;
                test::write_txt_file_to_path(&file_in_dir_1, "this is file in dir 1")?;
                test::write_txt_file_to_path(&file_in_dir_2, "this is file in dir 2")?;
                test::write_txt_file_to_path(&file_in_subdir_2, "this is file in subdir 2")?;

                // Switch back over to the other branch
                repositories::checkout(&user_a_repo, branch_name).await?;

                // Files should exist
                assert!(file_1.exists());
                assert!(file_in_dir_1.exists());
                assert!(file_in_dir_2.exists());
                assert!(file_in_subdir_2.exists());

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_checkout_remote_does_not_remove_untracked_files() -> Result<(), OxenError> {
        test::run_training_data_fully_sync_remote(|_local_repo, remote_repo| async move {
            // Create additional branch on remote repo before clone

            let cloned_remote = remote_repo.clone();

            // Clone with the --all flag
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let cloned_repo = repositories::deep_clone_url(
                    &remote_repo.remote.url,
                    &new_repo_dir.join("new_repo"),
                )
                .await?;

                // Create untracked files
                let file_1 = cloned_repo.path.join("file_1.txt");
                let dir_1 = cloned_repo.path.join("dir_1");
                let file_in_dir_1 = dir_1.join("file_in_dir_1.txt");
                let dir_2 = cloned_repo.path.join("dir_2");
                let subdir_2 = dir_2.join("subdir_2");
                let file_in_dir_2 = subdir_2.join("file_in_dir_2.txt");

                // Create the files and dirs
                std::fs::create_dir(&dir_1)?;
                std::fs::create_dir(&dir_2)?;
                std::fs::create_dir(&subdir_2)?;

                test::write_txt_file_to_path(&file_1, "this is file 1")?;
                test::write_txt_file_to_path(&file_in_dir_1, "this is file in dir 1")?;
                test::write_txt_file_to_path(&file_in_dir_2, "this is file in dir 2")?;

                // Create a new branch after cloning (so we have to fetch the new commit from the remote)

                let branch_name = "test-branch";
                api::client::branches::create_from_branch(
                    &remote_repo,
                    branch_name,
                    DEFAULT_BRANCH_NAME,
                )
                .await?;

                repositories::fetch_all(&cloned_repo, &FetchOpts::new()).await?;

                // Checkout the new branch
                repositories::checkout(&cloned_repo, branch_name).await?;

                // Files should exist
                assert!(file_1.exists());
                assert!(file_in_dir_1.exists());
                assert!(file_in_dir_2.exists());

                Ok(())
            })
            .await?;

            Ok(cloned_remote)
        })
        .await
    }

    #[tokio::test]
    async fn test_checkout_old_commit_does_not_overwrite_untracked_files() -> Result<(), OxenError>
    {
        test::run_training_data_fully_sync_remote(|_local_repo, remote_repo| async move {
            // Create additional branch on remote repo before clone
            let branch_name = "test-branch";
            api::client::branches::create_from_branch(
                &remote_repo,
                branch_name,
                DEFAULT_BRANCH_NAME,
            )
            .await?;

            let cloned_remote = remote_repo.clone();

            // Clone with the --all flag
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let cloned_repo = repositories::deep_clone_url(
                    &remote_repo.remote.url,
                    &new_repo_dir.join("new_repo"),
                )
                .await?;

                let test_dir_path = cloned_repo.path.join("test");
                let commit = repositories::commits::first_by_message(&cloned_repo, "Adding test/")?;

                // Create untracked files
                let file_1 = cloned_repo.path.join("file_1.txt");
                let dir_1 = cloned_repo.path.join("dir_1");
                let file_in_dir_1 = dir_1.join("file_in_dir_1.txt");
                let dir_2 = cloned_repo.path.join("dir_2");
                let subdir_2 = dir_2.join("subdir_2");
                let file_in_dir_2 = subdir_2.join("file_in_dir_2.txt");

                // Create the files and dirs
                std::fs::create_dir(&dir_1)?;
                std::fs::create_dir(&dir_2)?;
                std::fs::create_dir(&subdir_2)?;

                test::write_txt_file_to_path(&file_1, "this is file 1")?;
                test::write_txt_file_to_path(&file_in_dir_1, "this is file in dir 1")?;
                test::write_txt_file_to_path(&file_in_dir_2, "this is file in dir 2")?;

                assert!(commit.is_some());
                assert!(!test_dir_path.exists());

                // checkout the commit
                repositories::checkout(&cloned_repo, &commit.unwrap().id).await?;
                // Make sure we restored the directory
                assert!(test_dir_path.exists());
                // Make sure the untracked files are still there
                assert!(file_1.exists());
                assert!(file_in_dir_1.exists());
                assert!(file_in_dir_2.exists());

                Ok(())
            })
            .await?;

            Ok(cloned_remote)
        })
        .await
    }

    #[tokio::test]
    async fn test_checkout_preserves_uncommitted_file_deletion() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write and commit two files
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;
            let world_file = repo.path.join("world.txt");
            util::fs::write_to_path(&world_file, "World")?;

            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Adding files")?;

            // Get the original branch name
            let orig_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create a feature branch with modifications to world.txt and a new file
            let branch_name = "feature/new-stuff";
            repositories::branches::create_checkout(&repo, branch_name)?;
            let world_file = test::modify_txt_file(world_file, "World modified")?;
            let new_file = repo.path.join("new.txt");
            util::fs::write_to_path(&new_file, "New")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Modified world.txt and added new.txt")?;

            // Go back to main
            repositories::checkout(&repo, &orig_branch.name).await?;

            // Delete hello.txt without committing
            std::fs::remove_file(&hello_file)?;
            assert!(!hello_file.exists());

            // Checkout the feature branch
            repositories::checkout(&repo, branch_name).await?;

            // hello.txt should still be deleted (uncommitted deletion preserved)
            assert!(!hello_file.exists());
            // world.txt should have the feature branch content
            assert!(world_file.exists());
            // new.txt should be restored from the feature branch
            assert!(new_file.exists());

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_checkout_conflicts_on_uncommitted_deletion_of_modified_file()
    -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Write and commit a file
            let hello_file = repo.path.join("hello.txt");
            util::fs::write_to_path(&hello_file, "Hello")?;

            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Adding hello.txt")?;

            // Get the original branch name
            let orig_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create a feature branch that modifies hello.txt
            let branch_name = "feature/modify-hello";
            repositories::branches::create_checkout(&repo, branch_name)?;
            test::modify_txt_file(hello_file.clone(), "Hello from feature")?;
            repositories::add(&repo, &hello_file).await?;
            repositories::commit(&repo, "Modified hello.txt")?;

            // Go back to main
            repositories::checkout(&repo, &orig_branch.name).await?;

            // Delete hello.txt without committing
            std::fs::remove_file(&hello_file)?;
            assert!(!hello_file.exists());

            // Checkout the feature branch should fail because hello.txt was deleted
            // locally but modified on the target branch
            let result = repositories::checkout(&repo, branch_name).await;
            assert!(result.is_err());

            Ok(())
        })
        .await
    }

    // a file that only exists on branch B should be removed
    // when checking out branch A, even if the user deleted a different file.
    #[tokio::test]
    async fn test_checkout_removes_branch_specific_file_when_switching() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Commit a file on main
            let common_file = repo.path.join("common.txt");
            util::fs::write_to_path(&common_file, "common content")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Initial commit with common.txt")?;

            let main_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create feature branch and add a new file
            let branch_name = "feature/add-new";
            repositories::branches::create_checkout(&repo, branch_name)?;
            let new_file = repo.path.join("feature_only.txt");
            util::fs::write_to_path(&new_file, "only on feature branch")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Add feature_only.txt")?;

            // feature_only.txt should exist on feature branch
            assert!(new_file.exists());

            // Delete a different (untracked) file before switching branches
            let untracked_file = repo.path.join("untracked.txt");
            util::fs::write_to_path(&untracked_file, "I will be deleted")?;
            assert!(untracked_file.exists());
            std::fs::remove_file(&untracked_file)?;
            assert!(!untracked_file.exists());

            // Checkout main — feature_only.txt should be removed
            repositories::checkout(&repo, &main_branch.name).await?;
            assert!(
                !new_file.exists(),
                "feature_only.txt should be removed when checking out main"
            );
            assert!(
                common_file.exists(),
                "common.txt should exist when checking out"
            );

            Ok(())
        })
        .await
    }

    // when a directory exists on branch B but not branch A,
    // checking out branch B should restore the directory and its files.
    #[tokio::test]
    async fn test_checkout_restores_directory_from_target_branch() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Commit files in a directory on main
            let dir = repo.path.join("data");
            std::fs::create_dir_all(&dir)?;
            let file1 = dir.join("file1.txt");
            let file2 = dir.join("file2.txt");
            util::fs::write_to_path(&file1, "data file 1")?;
            util::fs::write_to_path(&file2, "data file 2")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Add data directory")?;

            let main_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create a branch that removes the directory
            let branch_name = "feature/no-data";
            repositories::branches::create_checkout(&repo, branch_name)?;
            std::fs::remove_dir_all(&dir)?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Remove data directory")?;

            // Verify dir is gone on feature branch
            assert!(!dir.exists());

            // Checkout main — directory should be restored
            repositories::checkout(&repo, &main_branch.name).await?;
            assert!(
                dir.exists(),
                "data/ directory should be restored when checking out main"
            );
            assert!(
                file1.exists(),
                "data/file1.txt should be restored when checking out main"
            );
            assert!(
                file2.exists(),
                "data/file2.txt should be restored when checking out main"
            );

            Ok(())
        })
        .await
    }

    // round-trip checkout should give a clean working directory
    // matching the target branch each time.
    #[tokio::test]
    async fn test_checkout_roundtrip_restores_all_files() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Main branch: common.txt
            let common_file = repo.path.join("common.txt");
            util::fs::write_to_path(&common_file, "common")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Initial commit")?;

            let main_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Feature branch: common.txt + extra.txt + subdir/nested.txt + datadir/a.txt + datadir/b.txt
            let branch_name = "feature/extras";
            repositories::branches::create_checkout(&repo, branch_name)?;
            let extra_file = repo.path.join("extra.txt");
            util::fs::write_to_path(&extra_file, "extra content")?;
            let subdir = repo.path.join("subdir");
            std::fs::create_dir_all(&subdir)?;
            let nested_file = subdir.join("nested.txt");
            util::fs::write_to_path(&nested_file, "nested content")?;
            let datadir = repo.path.join("datadir");
            std::fs::create_dir_all(&datadir)?;
            let data_a = datadir.join("a.txt");
            let data_b = datadir.join("b.txt");
            util::fs::write_to_path(&data_a, "data a")?;
            util::fs::write_to_path(&data_b, "data b")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Add extra files")?;

            // Checkout main — extra files and directories should be removed
            repositories::checkout(&repo, &main_branch.name).await?;
            assert!(common_file.exists(), "common.txt should exist on main");
            assert!(!extra_file.exists(), "extra.txt should NOT exist on main");
            assert!(
                !nested_file.exists(),
                "subdir/nested.txt should NOT exist on main"
            );
            assert!(!datadir.exists(), "datadir/ should NOT exist on main");

            // Checkout feature again — all files and directories should come back
            repositories::checkout(&repo, branch_name).await?;
            assert!(common_file.exists(), "common.txt should exist on feature");
            assert!(
                extra_file.exists(),
                "extra.txt should be restored on feature"
            );
            assert!(
                nested_file.exists(),
                "subdir/nested.txt should be restored on feature"
            );
            assert!(
                datadir.is_dir(),
                "datadir/ should be restored as a directory on feature"
            );
            assert!(
                data_a.exists(),
                "datadir/a.txt should be restored on feature"
            );
            assert!(
                data_b.exists(),
                "datadir/b.txt should be restored on feature"
            );

            // And back to main once more
            repositories::checkout(&repo, &main_branch.name).await?;
            assert!(
                !extra_file.exists(),
                "extra.txt should be removed again on main"
            );
            assert!(
                !nested_file.exists(),
                "subdir/nested.txt should be removed again on main"
            );
            assert!(
                !datadir.exists(),
                "datadir/ should be removed again on main"
            );

            Ok(())
        })
        .await
    }

    // deleting a directory and checking out the same branch
    // that has the directory should restore it (e.g., `oxen checkout .` or
    // re-checkout of current branch after unstaged deletion).
    #[tokio::test]
    async fn test_checkout_restores_deleted_directory_same_branch_content() -> Result<(), OxenError>
    {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Both branches have the same directory content
            let dir = repo.path.join("models");
            std::fs::create_dir_all(&dir)?;
            let model_file = dir.join("model.bin");
            util::fs::write_to_path(&model_file, "model data")?;
            let config_file = dir.join("config.json");
            util::fs::write_to_path(&config_file, r#"{"lr": 0.01}"#)?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Add models directory")?;

            let main_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create feature branch with same models/ content but an extra file
            let branch_name = "feature/new-stuff";
            repositories::branches::create_checkout(&repo, branch_name)?;
            let new_file = repo.path.join("new_feature.txt");
            util::fs::write_to_path(&new_file, "new feature")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Add new_feature.txt")?;

            // Go back to main
            repositories::checkout(&repo, &main_branch.name).await?;

            // Delete the models directory (uncommitted)
            std::fs::remove_dir_all(&dir)?;
            assert!(!dir.exists());

            // Checkout feature branch — models/ has same content on both branches,
            // but it should still be restored because we're checking out a branch
            repositories::checkout(&repo, branch_name).await?;
            assert!(
                dir.exists(),
                "models/ directory should be restored when checking out feature branch"
            );
            assert!(model_file.exists(), "models/model.bin should be restored");
            assert!(
                config_file.exists(),
                "models/config.json should be restored"
            );
            assert!(
                new_file.exists(),
                "new_feature.txt should exist on feature branch"
            );

            // Now test re-checking out the same branch after uncommitted deletion.
            // Delete the models directory again (uncommitted) while on feature branch.
            std::fs::remove_dir_all(&dir)?;
            assert!(!dir.exists(), "models/ should be deleted");

            // Re-checkout the same branch is a no-op ("Already on branch"),
            // so the uncommitted deletion is preserved.
            repositories::checkout(&repo, branch_name).await?;
            assert!(
                !dir.exists(),
                "models/ should remain deleted — same-branch checkout is a no-op"
            );

            // But checking out main and back to feature should restore it
            repositories::checkout(&repo, &main_branch.name).await?;
            repositories::checkout(&repo, branch_name).await?;
            assert!(
                dir.exists(),
                "models/ directory should be restored after round-trip checkout"
            );
            assert!(
                model_file.exists(),
                "models/model.bin should be restored after round-trip checkout"
            );
            assert!(
                config_file.exists(),
                "models/config.json should be restored after round-trip checkout"
            );

            Ok(())
        })
        .await
    }

    // Regression test: when a file on the current branch has the same content
    // (same hash) as a different file on the target branch, checkout should
    // still remove it if it doesn't exist on the target branch.
    #[tokio::test]
    async fn test_checkout_removes_duplicate_content_file_at_different_path()
    -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Main branch: file1.txt with some content
            let file1 = repo.path.join("file1.txt");
            util::fs::write_to_path(&file1, "shared content")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Add file1.txt on main")?;

            let main_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // Create feature branch and add file2.txt with identical content
            let branch_name = "feature/dup-content";
            repositories::branches::create_checkout(&repo, branch_name)?;
            let file2 = repo.path.join("file2.txt");
            util::fs::write_to_path(&file2, "shared content")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Add file2.txt with same content as file1.txt")?;

            // Both files should exist on the feature branch
            assert!(file1.exists(), "file1.txt should exist on feature branch");
            assert!(file2.exists(), "file2.txt should exist on feature branch");

            // Checkout main — file2.txt should be removed even though its
            // content hash matches file1.txt on main
            repositories::checkout(&repo, &main_branch.name).await?;
            assert!(file1.exists(), "file1.txt should exist on main");
            assert!(
                !file2.exists(),
                "file2.txt should be removed on main even though it shares content hash with file1.txt"
            );

            Ok(())
        })
        .await
    }

    // Both branches share the same "data/" directory (identical content, so
    // the hash is in common_nodes). On disk, the user manually deletes the
    // directory and creates a regular file at "data" (uncommitted).
    // Checking out the other branch should detect that "data" on disk is NOT
    // a directory and restore the directory contents instead of taking the
    // fast-path exit that assumes the directory is already present.
    //
    // This exposes a bug where full_dir_path.exists() is used instead of
    // full_dir_path.is_dir(): the file satisfies .exists(), the hash is in
    // common_nodes, and the code returns Ok(()) — silently skipping the
    // entire subtree restoration.
    #[tokio::test]
    async fn test_checkout_restores_directory_when_file_exists_at_same_path()
    -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // -- main branch: "data/" is a directory with two files --
            let dir = repo.path.join("data");
            std::fs::create_dir_all(&dir)?;
            let file1 = dir.join("file1.txt");
            let file2 = dir.join("file2.txt");
            util::fs::write_to_path(&file1, "data file 1")?;
            util::fs::write_to_path(&file2, "data file 2")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Add data directory with files")?;

            let main_branch = repositories::branches::current_branch(&repo)?.unwrap();

            // -- feature branch: same "data/" directory (identical content) --
            // Plus an extra file so the branches diverge and checkout does work.
            repositories::branches::create_checkout(&repo, "feature/shared-data")?;
            let extra = repo.path.join("extra.txt");
            util::fs::write_to_path(&extra, "extra")?;
            repositories::add(&repo, &repo.path).await?;
            repositories::commit(&repo, "Add extra.txt, data/ is unchanged")?;

            // Sanity: data/ directory exists identically on both branches
            assert!(dir.is_dir());
            assert!(file1.exists());
            assert!(file2.exists());

            // -- Simulate user manually replacing "data/" dir with a file --
            std::fs::remove_dir_all(&dir)?;
            util::fs::write_to_path(repo.path.join("data"), "I am a plain file, not a dir")?;

            // Sanity: "data" is now a regular file on disk
            let data_path = repo.path.join("data");
            assert!(data_path.is_file(), "data should be a regular file");
            assert!(!data_path.is_dir(), "data should NOT be a directory");

            // -- checkout main: data/ directory should be fully restored --
            repositories::checkout(&repo, &main_branch.name).await?;

            assert!(
                dir.is_dir(),
                "data/ should be restored as a directory when checking out main"
            );
            assert!(
                file1.exists(),
                "data/file1.txt should be restored when checking out main"
            );
            assert!(
                file2.exists(),
                "data/file2.txt should be restored when checking out main"
            );

            Ok(())
        })
        .await
    }
}