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
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
//! # oxen pull
//!
//! Pull data from a remote branch
//!

use crate::core;
use crate::core::versions::MinOxenVersion;
use crate::error::OxenError;
use crate::model::LocalRepository;
use crate::opts::fetch_opts::FetchOpts;

/// Pull a repository's data from default branches origin/main
/// Defaults defined in
/// `constants::DEFAULT_REMOTE_NAME` and `constants::DEFAULT_BRANCH_NAME`
#[tracing::instrument(skip(repo), fields(repo_path = %repo.path.display()))]
pub async fn pull(repo: &LocalRepository) -> Result<(), OxenError> {
    #[cfg(feature = "metrics")]
    metrics::counter!("oxen_pull_total").increment(1);
    #[cfg(feature = "metrics")]
    let timer = std::time::Instant::now();

    let result = match repo.min_version() {
        MinOxenVersion::V0_10_0 => panic!("v0.10.0 no longer supported"),
        _ => core::v_latest::pull::pull(repo).await,
    };

    #[cfg(feature = "metrics")]
    {
        let end = timer.elapsed();
        metrics::histogram!("oxen_pull_duration_ms").record(end.as_millis() as f64);
    }

    result
}

#[tracing::instrument(skip(repo), fields(repo_path = %repo.path.display()))]
pub async fn pull_all(repo: &LocalRepository) -> Result<(), OxenError> {
    #[cfg(feature = "metrics")]
    metrics::counter!("oxen_pull_total").increment(1);
    match repo.min_version() {
        MinOxenVersion::V0_10_0 => panic!("v0.10.0 no longer supported"),
        _ => core::v_latest::pull::pull_all(repo).await,
    }
}

/// Pull a specific remote and branch
#[tracing::instrument(skip(repo, fetch_opts), fields(repo_path = %repo.path.display()))]
pub async fn pull_remote_branch(
    repo: &LocalRepository,
    fetch_opts: &FetchOpts,
) -> Result<(), OxenError> {
    match repo.min_version() {
        MinOxenVersion::V0_10_0 => panic!("v0.10.0 no longer supported"),
        _ => core::v_latest::pull::pull_remote_branch(repo, fetch_opts).await,
    }
}

#[cfg(test)]
mod tests {
    use crate::api;
    use crate::command;
    use crate::constants;
    use crate::constants::OXEN_HIDDEN_DIR;
    use crate::core;
    use crate::core::df::tabular;
    use crate::error::OxenError;
    use crate::opts::CloneOpts;
    use crate::opts::DFOpts;
    use crate::opts::FetchOpts;
    use crate::opts::PushOpts;
    use crate::opts::RmOpts;
    use crate::repositories;
    use crate::repositories::LocalRepository;
    use crate::test;
    use crate::util;
    use std::path::Path;
    use std::path::PathBuf;

    #[tokio::test]
    async fn test_command_push_clone_pull_push() -> Result<(), OxenError> {
        test::run_training_data_repo_test_no_commits_async(|mut repo| async move {
            // Track the file
            let train_dirname = "train";
            let train_dir = repo.path.join(train_dirname);
            let og_num_files = util::fs::rcount_files_in_dir(&train_dir);
            repositories::add(&repo, &train_dir).await?;
            // Commit the train dir
            repositories::commit(&repo, "Adding training data")?;

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create the remote repo
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it real good
            repositories::push(&repo).await?;

            // Add a new file
            let party_ppl_filename = "party_ppl.txt";
            let party_ppl_contents = String::from("Wassup Party Ppl");
            let party_ppl_file_path = repo.path.join(party_ppl_filename);
            util::fs::write_to_path(&party_ppl_file_path, &party_ppl_contents)?;

            // Add and commit and push
            repositories::add(&repo, &party_ppl_file_path).await?;
            let latest_commit = repositories::commit(&repo, "Adding party_ppl.txt")?;
            repositories::push(&repo).await?;

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let new_repo_dir = new_repo_dir.join("new_repo");
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &new_repo_dir).await?;
                let oxen_dir = cloned_repo.path.join(OXEN_HIDDEN_DIR);
                assert!(oxen_dir.exists());
                repositories::pull(&cloned_repo).await?;

                // Make sure we pulled all of the train dir
                let cloned_train_dir = cloned_repo.path.join(train_dirname);
                let cloned_num_files = util::fs::rcount_files_in_dir(&cloned_train_dir);
                assert_eq!(og_num_files, cloned_num_files);

                // Make sure we have the party ppl file from the next commit
                let cloned_party_ppl_path = cloned_repo.path.join(party_ppl_filename);
                assert!(cloned_party_ppl_path.exists());
                let cloned_contents = util::fs::read_from_path(&cloned_party_ppl_path)?;
                assert_eq!(cloned_contents, party_ppl_contents);

                // Make sure that pull updates local HEAD to be correct
                let head = repositories::commits::head_commit(&cloned_repo)?;
                assert_eq!(head.id, latest_commit.id);

                // Make sure we synced all the commits
                let repo_commits = repositories::commits::list(&repo)?;
                let cloned_commits = repositories::commits::list(&cloned_repo)?;
                assert_eq!(repo_commits.len(), cloned_commits.len());

                // Make sure we updated the dbs properly
                let status = repositories::status(&cloned_repo)?;
                assert!(status.is_clean());

                // Have this side add a file, and send it back over
                let send_it_back_filename = "send_it_back.txt";
                let send_it_back_contents = String::from("Hello from the other side");
                let send_it_back_file_path = cloned_repo.path.join(send_it_back_filename);
                util::fs::write_to_path(&send_it_back_file_path, &send_it_back_contents)?;

                // Add and commit and push
                repositories::add(&cloned_repo, &send_it_back_file_path).await?;
                repositories::commit(&cloned_repo, "Adding send_it_back.txt")?;
                repositories::push(&cloned_repo).await?;

                // Pull back from the OG Repo
                repositories::pull(&repo).await?;
                let old_repo_status = repositories::status(&repo)?;
                old_repo_status.print();
                // Make sure we don't modify the timestamps or anything of the OG data
                assert!(!old_repo_status.has_modified_entries());

                let pulled_send_it_back_path = repo.path.join(send_it_back_filename);
                assert!(pulled_send_it_back_path.exists());
                let pulled_contents = util::fs::read_from_path(&pulled_send_it_back_path)?;
                assert_eq!(pulled_contents, send_it_back_contents);

                // Modify the party ppl contents
                let party_ppl_contents = String::from("Late to the party");
                util::fs::write_to_path(&party_ppl_file_path, &party_ppl_contents)?;
                repositories::add(&repo, &party_ppl_file_path).await?;
                repositories::commit(&repo, "Modified party ppl contents")?;
                repositories::push(&repo).await?;

                // Pull the modifications
                repositories::pull(&cloned_repo).await?;
                let pulled_contents = util::fs::read_from_path(&cloned_party_ppl_path)?;
                assert_eq!(pulled_contents, party_ppl_contents);

                println!("----BEFORE-----");
                // Remove a file, add, commit, push the change
                util::fs::remove_file(&send_it_back_file_path)?;
                repositories::add(&cloned_repo, &send_it_back_file_path).await?;
                repositories::commit(&cloned_repo, "Removing the send it back file")?;
                repositories::push(&cloned_repo).await?;
                println!("----AFTER-----");

                // Pull down the changes and make sure the file is removed
                repositories::pull(&repo).await?;
                let pulled_send_it_back_path = repo.path.join(send_it_back_filename);
                assert!(!pulled_send_it_back_path.exists());

                api::client::repositories::delete(&remote_repo).await?;

                Ok(())
            })
            .await
        })
        .await
    }

    // This specific flow broke during a demo
    //   Because there ends up being no files in the root dir
    //   after we remove the labels.txt file, push, and pull

    // * add file *
    // push
    // pull
    // * modify file *
    // push
    // pull
    // * remove file *
    // push
    // pull
    #[tokio::test]
    async fn test_command_add_modify_remove_push_pull() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|mut repo| async move {
            // Track a file
            let filename = "labels.txt";
            let filepath = repo.path.join(filename);
            test::write_txt_file_to_path(&filepath, "I am the labels")?;
            repositories::add(&repo, &filepath).await?;
            repositories::commit(&repo, "Adding labels file")?;

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create Remote
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it real good
            repositories::push(&repo).await?;

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let new_repo_dir = new_repo_dir.join("new_repo");
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &new_repo_dir).await?;

                // Modify the file in the cloned dir
                let cloned_filepath = cloned_repo.path.join(filename);
                let changed_content = "messing up the labels";
                util::fs::write_to_path(&cloned_filepath, changed_content)?;
                repositories::add(&cloned_repo, &cloned_filepath).await?;
                repositories::commit(&cloned_repo, "I messed with the label file")?;

                // Push back to server
                repositories::push(&cloned_repo).await?;

                // Pull back to original guy
                repositories::pull(&repo).await?;

                // Make sure content changed
                let pulled_content = util::fs::read_from_path(&filepath)?;
                assert_eq!(pulled_content, changed_content);

                // Delete the file in the og filepath
                util::fs::remove_file(&filepath)?;

                // Stage & Commit & Push the removal
                repositories::add(&repo, &filepath).await?;
                repositories::commit(&repo, "You mess with it, I remove it")?;
                repositories::push(&repo).await?;

                // This pull should still work even with no files in the root dir
                repositories::pull(&cloned_repo).await?;
                assert!(!cloned_filepath.exists());

                api::client::repositories::delete(&remote_repo).await?;

                Ok(())
            })
            .await
        })
        .await
    }

    // Make sure we can push again after pulling on the other side, then pull again
    #[tokio::test]
    async fn test_push_pull_push_pull_on_branch() -> Result<(), OxenError> {
        test::run_training_data_repo_test_no_commits_async(|mut repo| async move {
            // Track a dir
            let train_path = repo.path.join("train");
            repositories::add(&repo, &train_path).await?;
            repositories::commit(&repo, "Adding train dir")?;

            // Track larger files
            let larger_dir = repo.path.join("large_files");
            repositories::add(&repo, &larger_dir).await?;
            repositories::commit(&repo, "Adding larger files")?;

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create Remote
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it
            repositories::push(&repo).await?;
            let og_num_files = util::fs::rcount_files_in_dir(&repo.path);

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let new_repo_dir = new_repo_dir.join("new_repo");
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &new_repo_dir).await?;

                let cloned_num_files = util::fs::rcount_files_in_dir(&cloned_repo.path);
                assert_eq!(8, cloned_num_files);
                let og_commits = repositories::commits::list(&repo)?;
                let cloned_commits = repositories::commits::list(&cloned_repo)?;
                assert_eq!(og_commits.len(), cloned_commits.len());

                // Create a branch to collab on
                let branch_name = "adding-training-data";
                repositories::branches::create_checkout(&cloned_repo, branch_name)?;

                // Track some more data in the cloned repo
                let hotdog_path = test::test_hotdog_1();
                let new_file_path = cloned_repo.path.join("train").join("hotdog_1.jpg");
                util::fs::copy(hotdog_path, &new_file_path)?;
                repositories::add(&cloned_repo, &new_file_path).await?;
                repositories::commit(&cloned_repo, "Adding one file to train dir")?;
                let opts = PushOpts {
                    remote: constants::DEFAULT_REMOTE_NAME.to_string(),
                    branch: branch_name.to_string(),
                    ..Default::default()
                };

                // Push it back
                repositories::push::push_remote_branch(&cloned_repo, &opts).await?;

                let fetch_opts = &FetchOpts {
                    remote: constants::DEFAULT_REMOTE_NAME.to_string(),
                    branch: branch_name.to_string(),
                    all: true,
                    ..FetchOpts::new()
                };
                repositories::fetch::fetch_branch(&repo, fetch_opts).await?;
                repositories::checkout::checkout(&repo, branch_name).await?;

                let num_new_files = util::fs::rcount_files_in_dir(&repo.path);
                // Now there should be a new hotdog file
                assert_eq!(og_num_files + 1, num_new_files);

                // Add another file on the OG side, and push it back
                let hotdog_path = test::test_hotdog_2();
                let new_file_path = train_path.join("hotdog_2.jpg");
                util::fs::copy(hotdog_path, &new_file_path)?;
                repositories::add(&repo, &train_path).await?;
                repositories::commit(&repo, "Adding next file to train dir")?;
                let opts = PushOpts {
                    remote: constants::DEFAULT_REMOTE_NAME.to_string(),
                    branch: branch_name.to_string(),
                    ..Default::default()
                };

                repositories::push::push_remote_branch(&repo, &opts).await?;

                // Pull it on the second side again
                repositories::pull_remote_branch(
                    &cloned_repo,
                    &FetchOpts {
                        remote: constants::DEFAULT_REMOTE_NAME.to_string(),
                        branch: branch_name.to_string(),
                        all: false,
                        ..FetchOpts::new()
                    },
                )
                .await?;
                let cloned_num_files = util::fs::rcount_files_in_dir(&cloned_repo.path);
                // Now there should be 9 train/ files and 1 in large_files/
                assert_eq!(10, cloned_num_files);

                api::client::repositories::delete(&remote_repo).await?;

                Ok(())
            })
            .await
        })
        .await
    }

    // Make sure we can push again after pulling on the other side, then pull again
    #[tokio::test]
    async fn test_push_pull_push_pull_on_other_branch() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|mut repo| async move {
            // Track a dir
            let train_dir = repo.path.join("train");
            let train_paths = [
                Path::new("data/test/images/cat_1.jpg"),
                Path::new("data/test/images/cat_2.jpg"),
                Path::new("data/test/images/cat_3.jpg"),
                Path::new("data/test/images/dog_1.jpg"),
                Path::new("data/test/images/dog_2.jpg"),
            ];
            util::fs::create_dir_all(&train_dir)?;
            for path in train_paths.iter() {
                util::fs::copy(
                    test::REPO_ROOT.join(path),
                    train_dir.join(path.file_name().unwrap()),
                )?;
            }

            repositories::add(&repo, &train_dir).await?;
            repositories::commit(&repo, "Adding train dir")?;

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

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create Remote
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it
            repositories::push(&repo).await?;

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let new_repo_dir = new_repo_dir.join("new_repo");
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &new_repo_dir).await?;

                let cloned_num_files = util::fs::rcount_files_in_dir(&cloned_repo.path);
                // the original training files
                assert_eq!(train_paths.len(), cloned_num_files);

                // Create a branch to collaborate on
                let branch_name = "adding-training-data";
                repositories::branches::create_checkout(&cloned_repo, branch_name)?;

                // Track some more data in the cloned repo
                let hotdog_path = test::test_hotdog_1();
                let new_file_path = cloned_repo.path.join("train").join("hotdog_1.jpg");
                util::fs::copy(hotdog_path, &new_file_path)?;
                repositories::add(&cloned_repo, &new_file_path).await?;
                repositories::commit(&cloned_repo, "Adding one file to train dir")?;
                let opts = PushOpts {
                    remote: constants::DEFAULT_REMOTE_NAME.to_string(),
                    branch: branch_name.to_string(),
                    ..Default::default()
                };

                // Push it back
                repositories::push::push_remote_branch(&cloned_repo, &opts).await?;
                // Pull it on the OG side
                repositories::pull_remote_branch(
                    &repo,
                    &FetchOpts {
                        remote: constants::DEFAULT_REMOTE_NAME.to_string(),
                        branch: og_branch.name.to_string(),
                        all: true,
                        ..FetchOpts::new()
                    },
                )
                .await?;
                let og_num_files = util::fs::rcount_files_in_dir(&repo.path);
                // Now there should be still be the original train files, not the new file
                assert_eq!(train_paths.len(), og_num_files);

                api::client::repositories::delete(&remote_repo).await?;
                Ok(())
            })
            .await
        })
        .await
    }

    #[tokio::test]
    async fn test_push_pull_file_without_extension() -> Result<(), OxenError> {
        test::run_training_data_repo_test_no_commits_async(|mut repo| async move {
            let filename = "LICENSE";
            let filepath = repo.path.join(filename);

            let og_content = "I am the License.";
            test::write_txt_file_to_path(&filepath, og_content)?;

            repositories::add(&repo, filepath).await?;
            let commit = repositories::commit(&repo, "Adding file without extension");

            assert!(commit.is_ok());

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create Remote
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it
            repositories::push(&repo).await?;

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let new_repo_dir = new_repo_dir.join("new_repo");
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &new_repo_dir).await?;

                let filepath = cloned_repo.path.join(filename);
                let content = util::fs::read_from_path(&filepath)?;
                assert_eq!(og_content, content);

                api::client::repositories::delete(&remote_repo).await?;

                Ok(())
            })
            .await
        })
        .await
    }

    /*
    Test this workflow:

    User 1: adds data and creates a branch with more data
        oxen init
        oxen add data/1.txt
        oxen add data/2.txt
        oxen commit -m "Adding initial data"
        oxen push
        oxen checkout -b feature/add-mooooore-data
        oxen add data/3.txt
        oxen add data/4.txt
        oxen add data/5.txt
        oxen push

    User 2: clones just the branch with more data, then switches to main branch and pulls
        oxen clone remote.url -b feature/add-mooooore-data
        oxen fetch
        oxen checkout main
        # should only have the data on main

    */
    #[tokio::test]
    async fn test_push_pull_separate_branch_less_files() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|mut repo| async move {
            // create 5 text files in the repo.path
            for i in 1..6 {
                let filename = format!("{i}.txt");
                let filepath = repo.path.join(&filename);
                test::write_txt_file_to_path(&filepath, &filename)?;
            }

            // add file 1.txt and 2.txt
            let filepath = repo.path.join("1.txt");
            repositories::add(&repo, &filepath).await?;
            let filepath = repo.path.join("2.txt");
            repositories::add(&repo, &filepath).await?;

            // Commit the files
            repositories::commit(&repo, "Adding initial data")?;

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create Remote
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it
            repositories::push(&repo).await?;

            // Create a branch to collab on
            let branch_name = "feature/add-mooooore-data";
            repositories::branches::create_checkout(&repo, branch_name)?;

            // Add the rest of the files
            for i in 3..6 {
                let filename = format!("{i}.txt");
                let filepath = repo.path.join(&filename);
                repositories::add(&repo, &filepath).await?;
            }

            // Commit the files
            repositories::commit(&repo, "Adding mooooore data")?;

            // Push it
            repositories::push(&repo).await?;

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                // Clone the branch
                let opts = CloneOpts::from_branch(
                    remote_repo.url(),
                    new_repo_dir.join("new_repo"),
                    branch_name,
                );
                let cloned_repo = repositories::clone(&opts).await?;

                // Make sure we have all the files from the branch
                let cloned_files = util::fs::rlist_files_in_dir(&cloned_repo.path);
                for file in cloned_files.iter() {
                    println!("Cloned file: {}", file.display());
                }
                let cloned_num_files = cloned_files.len();
                assert_eq!(cloned_num_files, 5);

                // Switch to main branch and pull
                repositories::fetch_all(&cloned_repo, &FetchOpts::new()).await?;
                repositories::checkout(&cloned_repo, "main").await?;

                let cloned_num_files = util::fs::rcount_files_in_dir(&cloned_repo.path);
                assert_eq!(cloned_num_files, 2);

                api::client::repositories::delete(&remote_repo).await?;

                Ok(())
            })
            .await
        })
        .await
    }

    #[tokio::test]
    async fn test_push_pull_separate_branch_more_files() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|mut repo| async move {
            // create 5 text files in the repo.path
            for i in 1..6 {
                let filename = format!("{i}.txt");
                let filepath = repo.path.join(&filename);
                test::write_txt_file_to_path(&filepath, &filename)?;
            }

            // add file 1.txt and 2.txt
            let filepath = repo.path.join("1.txt");
            repositories::add(&repo, &filepath).await?;
            let filepath = repo.path.join("2.txt");
            repositories::add(&repo, &filepath).await?;

            // Commit the files
            repositories::commit(&repo, "Adding initial data")?;

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create Remote
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it
            repositories::push(&repo).await?;

            // Create a branch to collab on
            let branch_name = "feature/add-mooooore-data";
            repositories::branches::create_checkout(&repo, branch_name)?;

            // Add the rest of the files
            for i in 3..6 {
                let filename = format!("{i}.txt");
                let filepath = repo.path.join(&filename);
                repositories::add(&repo, &filepath).await?;
            }

            // Commit the files
            repositories::commit(&repo, "Adding mooooore data")?;

            // Push it
            repositories::push(&repo).await?;

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                // Clone the branch
                let opts = CloneOpts::new(remote_repo.url(), new_repo_dir.join("new_repo"));
                let cloned_repo = repositories::clone(&opts).await?;

                // Make sure we have all the files from the branch
                let cloned_num_files = util::fs::rcount_files_in_dir(&cloned_repo.path);
                assert_eq!(cloned_num_files, 2);

                // Switch to main branch and pull
                repositories::fetch_all(&cloned_repo, &FetchOpts::new()).await?;

                repositories::checkout(&cloned_repo, branch_name).await?;

                let cloned_num_files = util::fs::rcount_files_in_dir(&cloned_repo.path);
                assert_eq!(cloned_num_files, 5);

                api::client::repositories::delete(&remote_repo).await?;

                Ok(())
            })
            .await
        })
        .await
    }

    #[tokio::test]
    async fn test_push_pull_moved_files() -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_training_data_fully_sync_remote(|local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();
            let contents = "this is the file";
            let path = &local_repo.path.join("a.txt");
            test::write_txt_file_to_path(path, contents)?;
            println!("Writing file to {}", path.display());
            repositories::add(&local_repo, path).await?;
            println!("adding file to index at path {}", path.display());
            repositories::commit(&local_repo, "Adding file for first time")?;
            // Write the same file to newfolder/a.txt

            let new_path = &local_repo.path.join("newfolder").join("a.txt");

            util::fs::create_dir_all(local_repo.path.join("newfolder"))?;
            test::write_txt_file_to_path(new_path, contents)?;
            repositories::add(&local_repo, new_path).await?;

            // Write the same file to newfolder/b.txt
            let new_path = &local_repo.path.join("newfolder").join("b.txt");

            test::write_txt_file_to_path(new_path, contents)?;
            repositories::add(&local_repo, new_path).await?;

            // Delete the original file at a.txt
            let path = "a.txt";
            let new_path = local_repo.path.join(path);
            util::fs::remove_file(&new_path)?;
            repositories::add(&local_repo, &new_path).await?;
            repositories::commit(
                &local_repo,
                "Moved file to 2 new places and deleted original",
            )?;
            repositories::push(&local_repo).await?;

            test::run_empty_dir_test_async(|repo_dir| async move {
                // Pull down this removal
                let repo_dir = repo_dir.join("repoo");
                let _cloned_repo =
                    repositories::deep_clone_url(&remote_repo.remote.url, &repo_dir).await?;
                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_push_new_branch_default_clone() -> Result<(), OxenError> {
        test::run_training_data_fully_sync_remote(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();
            test::run_empty_dir_test_async(|repo_dir| async move {
                // Clone the remote repo
                let repo_dir = repo_dir.join("repoo");
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &repo_dir).await?;

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

                // Add a file
                let contents = "this is the file";
                let path = &cloned_repo.path.join("a.txt");
                test::write_txt_file_to_path(path, contents)?;

                repositories::add(&cloned_repo, path).await?;
                let commit = repositories::commit(&cloned_repo, "Adding file for first time")?;
                let opts = PushOpts {
                    remote: constants::DEFAULT_REMOTE_NAME.to_string(),
                    branch: branch_name.to_string(),
                    ..Default::default()
                };

                // Try to push upstream branch
                let push_result = repositories::push::push_remote_branch(&cloned_repo, &opts).await;

                log::debug!("Push result: {push_result:?}");

                assert!(push_result.is_ok());

                // Get the remote branch
                let remote_branch = api::client::branches::get_by_name(&remote_repo, branch_name)
                    .await?
                    .unwrap();

                assert_eq!(remote_branch.commit_id, commit.id);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    // Deal with merge conflicts on subtree clones
    // 1) Clone subtree to user A
    // 2) Clone subtree to user B
    // 3) User A changes file commit and pushes
    // 4) User B changes same file, commits, and pushes and fails
    // 5) User B pulls user A's changes, there is a merge conflict
    // 6) User B cannot push until merge conflict is resolved
    #[tokio::test]
    async fn test_flags_merge_conflict_on_subtree_pull() -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_training_data_fully_sync_remote(|_, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            // Clone Repo to User A
            test::run_empty_dir_test_async(|repos_base_dir| async move {
                let user_a_repo_dir = repos_base_dir.join("user_a_repo");

                // Make sure to clone a subtree to test subtree merge conflicts
                let mut clone_opts = CloneOpts::new(&remote_repo.remote.url, &user_a_repo_dir);
                clone_opts.fetch_opts.subtree_paths =
                    Some(vec![PathBuf::from("nlp").join("classification")]);
                clone_opts.fetch_opts.depth = Some(2);
                let user_a_repo = repositories::clone(&clone_opts).await?;

                // Clone Repo to User B
                let user_b_repo_dir = repos_base_dir.join("user_b_repo");

                let mut clone_opts = CloneOpts::new(&remote_repo.remote.url, &user_b_repo_dir);
                clone_opts.fetch_opts.subtree_paths =
                    Some(vec![PathBuf::from("nlp").join("classification")]);
                clone_opts.fetch_opts.depth = Some(2);
                let user_b_repo = repositories::clone(&clone_opts).await?;

                // User A adds a file and pushes
                let new_file = PathBuf::from("nlp")
                    .join("classification")
                    .join("new_data.tsv");
                let new_file_path = user_a_repo.path.join(&new_file);
                let new_file_path = test::write_txt_file_to_path(new_file_path, "image\tlabel")?;
                repositories::add(&user_a_repo, &new_file_path).await?;
                repositories::commit(&user_a_repo, "User A adding new data.")?;
                repositories::push(&user_a_repo).await?;

                // User B adds the same file and pushes
                let new_file_path = user_b_repo.path.join(&new_file);
                let new_file_path =
                    test::write_txt_file_to_path(new_file_path, "I am user B, try to stop me")?;
                repositories::add(&user_b_repo, &new_file_path).await?;
                repositories::commit(&user_b_repo, "User B adding the same file.")?;

                // Push should fail
                let result = repositories::push(&user_b_repo).await;
                assert!(result.is_err());

                // Pull
                let result = repositories::pull(&user_b_repo).await;
                assert!(result.is_err());

                // Make sure it's not a full clone
                assert_eq!(user_b_repo.depth(), Some(2));
                assert_eq!(
                    user_b_repo.subtree_paths(),
                    Some(vec![PathBuf::from("nlp").join("classification")])
                );
                assert!(user_b_repo.path.join("nlp").join("classification").exists());
                assert!(!user_b_repo.path.join("train").exists());

                // Check for merge conflict
                let status = repositories::status(&user_b_repo)?;
                assert!(!status.merge_conflicts.is_empty());
                status.print();

                // Checkout your version and add the changes
                repositories::checkout::checkout_ours(&user_b_repo, new_file).await?;
                repositories::add(&user_b_repo, &new_file_path).await?;
                // Commit the changes
                repositories::commit(&user_b_repo, "Taking my changes")?;

                // Push should succeed
                repositories::push(&user_b_repo).await?;

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    // Deal with merge conflicts on ROOT subtree clones
    // 1) Clone subtree to user A
    // 2) Clone subtree to user B
    // 3) User A changes file commit and pushes
    // 4) User B changes same file, commits, and pushes and fails
    // 5) User B pulls user A's changes, there is a merge conflict
    // 6) User B cannot push until merge conflict is resolved
    #[tokio::test]
    async fn test_flags_merge_conflict_on_root_subtree_pull() -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_training_data_fully_sync_remote(|_, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            // Clone Repo to User A
            test::run_empty_dir_test_async(|repos_base_dir| async move {
                let user_a_repo_dir = repos_base_dir.join("user_a_repo");

                // Make sure to clone a subtree to test subtree merge conflicts
                let mut clone_opts = CloneOpts::new(&remote_repo.remote.url, &user_a_repo_dir);
                clone_opts.fetch_opts.subtree_paths = Some(vec![PathBuf::from(".")]);
                clone_opts.fetch_opts.depth = Some(1);
                let user_a_repo = repositories::clone(&clone_opts).await?;

                // Clone Repo to User B
                let user_b_repo_dir = repos_base_dir.join("user_b_repo");

                let mut clone_opts = CloneOpts::new(&remote_repo.remote.url, &user_b_repo_dir);
                clone_opts.fetch_opts.subtree_paths = Some(vec![PathBuf::from(".")]);
                clone_opts.fetch_opts.depth = Some(1);
                let user_b_repo = repositories::clone(&clone_opts).await?;

                // User A adds a file and pushes
                let new_file = PathBuf::from("README.md");
                let new_file_path = user_a_repo.path.join(&new_file);
                let new_file_path = test::write_txt_file_to_path(new_file_path, "User A's README")?;
                repositories::add(&user_a_repo, &new_file_path).await?;
                repositories::commit(&user_a_repo, "User A adding new data.")?;
                repositories::push(&user_a_repo).await?;

                // User B adds the same file and pushes
                let new_file_path = user_b_repo.path.join(&new_file);
                let new_file_path =
                    test::write_txt_file_to_path(new_file_path, "I am user B, try to stop me")?;
                repositories::add(&user_b_repo, &new_file_path).await?;
                repositories::commit(&user_b_repo, "User B adding the same README.")?;

                // Push should fail
                let result = repositories::push(&user_b_repo).await;
                assert!(result.is_err());

                // Pull
                let result = repositories::pull(&user_b_repo).await;
                assert!(result.is_err());

                // Make sure it's not a full clone
                assert_eq!(user_b_repo.depth(), Some(1));
                assert_eq!(user_b_repo.subtree_paths(), Some(vec![PathBuf::from("")]),);
                assert!(user_b_repo.path.join("README.md").exists());
                assert!(!user_b_repo.path.join("nlp").exists());
                assert!(!user_b_repo.path.join("train").exists());

                // Check for merge conflict
                let status = repositories::status(&user_b_repo)?;
                status.print();
                assert!(!status.merge_conflicts.is_empty());
                assert_eq!(status.merge_conflicts.len(), 1);
                assert_eq!(status.merge_conflicts[0].base_entry.path, new_file);
                assert_eq!(status.removed_files.len(), 0);

                // Checkout your version and add the changes
                repositories::checkout::checkout_ours(&user_b_repo, new_file).await?;
                repositories::add(&user_b_repo, &new_file_path).await?;
                // Commit the changes
                repositories::commit(&user_b_repo, "Taking my changes")?;

                // Push should succeed
                repositories::push(&user_b_repo).await?;

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    // Deal with merge conflicts on pull
    // 1) Clone repo to user A
    // 2) Clone repo to user B
    // 3) User A changes file commit and pushes
    // 4) User B changes same file, commits, and pushes and fails
    // 5) User B pulls user A's changes, there is a merge conflict
    // 6) User B cannot push until merge conflict is resolved
    #[tokio::test]
    async fn test_flags_merge_conflict_on_pull() -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_training_data_fully_sync_remote(|_, 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?;

                // Clone Repo to User B
                test::run_empty_dir_test_async(|user_b_repo_dir| async move {
                    let user_b_repo_dir_copy = user_b_repo_dir.join("user_b_repo");

                    let user_b_repo =
                        repositories::clone_url(&remote_repo.remote.url, &user_b_repo_dir_copy)
                            .await?;

                    // User A adds a file and pushes
                    let new_file = "new_file.txt";
                    let new_file_path = user_a_repo.path.join(new_file);
                    let new_file_path = test::write_txt_file_to_path(new_file_path, "new file")?;
                    repositories::add(&user_a_repo, &new_file_path).await?;
                    repositories::commit(&user_a_repo, "User A changing file.")?;
                    repositories::push(&user_a_repo).await?;

                    // User B changes the same file and pushes
                    let new_file_path = user_b_repo.path.join(new_file);
                    let new_file_path =
                        test::write_txt_file_to_path(new_file_path, "I am user B, try to stop me")?;
                    repositories::add(&user_b_repo, &new_file_path).await?;
                    repositories::commit(&user_b_repo, "User B changing file.")?;

                    // Push should fail
                    let result = repositories::push(&user_b_repo).await;
                    assert!(result.is_err());

                    // Pull
                    let result = repositories::pull(&user_b_repo).await;
                    assert!(result.is_err());

                    // Check for merge conflict
                    let status = repositories::status(&user_b_repo)?;
                    assert!(!status.merge_conflicts.is_empty());
                    status.print();

                    // Checkout your version and add the changes
                    repositories::checkout::checkout_ours(&user_b_repo, new_file).await?;
                    repositories::add(&user_b_repo, &new_file_path).await?;
                    // Commit the changes
                    repositories::commit(&user_b_repo, "Taking my changes")?;

                    // Push should succeed
                    repositories::push(&user_b_repo).await?;

                    Ok(())
                })
                .await?;

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_pull_does_not_remove_local_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?;

                // Clone Repo to User B
                test::run_empty_dir_test_async(|user_b_repo_dir| async move {
                    let user_b_repo_dir_copy = user_b_repo_dir.join("user_b_repo");
                    let user_b_repo =
                        repositories::clone_url(&remote_repo.remote.url, &user_b_repo_dir_copy)
                            .await?;

                    // Add file_1 and file_2 to user A repo
                    let file_1 = "file_1.txt";
                    test::write_txt_file_to_path(user_a_repo.path.join(file_1), "File 1")?;
                    let file_2 = "file_2.txt";
                    test::write_txt_file_to_path(user_a_repo.path.join(file_2), "File 2")?;

                    repositories::add(&user_a_repo, user_a_repo.path.join(file_1)).await?;
                    repositories::add(&user_a_repo, user_a_repo.path.join(file_2)).await?;

                    repositories::commit(&user_a_repo, "Adding file_1 and file_2")?;

                    // Push
                    repositories::push(&user_a_repo).await?;

                    // Add file_3 to user B repo
                    let file_3 = "file_3.txt";
                    test::write_txt_file_to_path(user_b_repo.path.join(file_3), "File 3")?;

                    repositories::add(&user_b_repo, user_b_repo.path.join(file_3)).await?;
                    repositories::commit(&user_b_repo, "Adding file_3")?;

                    // Pull changes without pushing first - fine since no conflict
                    repositories::pull(&user_b_repo).await?;

                    // Get new head commit of the pulled repo
                    repositories::commits::head_commit(&user_b_repo)?;

                    // Make sure we now have all three files
                    assert!(user_b_repo.path.join(file_1).exists());
                    assert!(user_b_repo.path.join(file_2).exists());
                    assert!(user_b_repo.path.join(file_3).exists());

                    Ok(())
                })
                .await?;

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }
    #[tokio::test]
    async fn test_pull_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?;

                // Clone Repo to User B
                test::run_empty_dir_test_async(|user_b_repo_dir| async move {
                    let user_b_repo_dir_copy = user_b_repo_dir.join("user_b_repo");
                    let user_b_repo =
                        repositories::clone_url(&remote_repo.remote.url, &user_b_repo_dir_copy)
                            .await?;

                    // Add file_1 and file_2 to user A repo
                    let file_1 = "file_1.txt";
                    test::write_txt_file_to_path(user_a_repo.path.join(file_1), "File 1")?;
                    let file_2 = "file_2.txt";
                    test::write_txt_file_to_path(user_a_repo.path.join(file_2), "File 2")?;

                    repositories::add(&user_a_repo, user_a_repo.path.join(file_1)).await?;
                    repositories::add(&user_a_repo, user_a_repo.path.join(file_2)).await?;

                    repositories::commit(&user_a_repo, "Adding file_1 and file_2")?;

                    // Push
                    repositories::push(&user_a_repo).await?;

                    let local_file_2 = "file_2.txt";
                    test::write_txt_file_to_path(
                        user_b_repo.path.join(local_file_2),
                        "wrong not correct content",
                    )?;

                    // Add file_3 to user B repo
                    let file_3 = "file_3.txt";
                    test::write_txt_file_to_path(user_b_repo.path.join(file_3), "File 3")?;

                    // Make a dir
                    let dir_1 = "dir_1";
                    std::fs::create_dir(user_b_repo.path.join(dir_1))?;

                    // Make another dir
                    let dir_2 = "dir_2";
                    std::fs::create_dir(user_b_repo.path.join(dir_2))?;

                    // Add files in dir_2
                    let file_4 = "file_4.txt";
                    test::write_txt_file_to_path(
                        user_b_repo.path.join(dir_2).join(file_4),
                        "File 4",
                    )?;
                    let file_5 = "file_5.txt";
                    test::write_txt_file_to_path(
                        user_b_repo.path.join(dir_2).join(file_5),
                        "File 5",
                    )?;

                    let dir_3 = "dir_3";
                    let subdir = "subdir";
                    util::fs::create_dir_all(user_b_repo.path.join(dir_3).join(subdir))?;

                    let subfile = "subfile.txt";
                    test::write_txt_file_to_path(
                        user_b_repo.path.join(dir_3).join(subdir).join(subfile),
                        "Subfile",
                    )?;

                    // Pull changes
                    let result = repositories::pull(&user_b_repo).await;

                    // There should be a conflict with file_2
                    assert!(result.is_err());

                    // Remove the file that is causing the conflict
                    util::fs::remove_file(user_b_repo.path.join(local_file_2))?;

                    // Pull again should succeed
                    repositories::pull(&user_b_repo).await?;

                    // Files from the other commit successfully pulled
                    assert!(user_b_repo.path.join(file_1).exists());
                    assert!(user_b_repo.path.join(file_2).exists());

                    // File 2 should be same as the remote file
                    let local_file_2_contents =
                        std::fs::read_to_string(user_b_repo.path.join(local_file_2))?;
                    assert_eq!(local_file_2_contents, "File 2");

                    // Untracked files not removed
                    assert!(user_b_repo.path.join(file_3).exists());
                    assert!(user_b_repo.path.join(dir_1).exists());
                    assert!(user_b_repo.path.join(dir_2).exists());
                    assert!(user_b_repo.path.join(dir_2).join(file_4).exists());
                    assert!(user_b_repo.path.join(dir_2).join(file_5).exists());
                    assert!(user_b_repo.path.join(dir_3).exists());
                    assert!(user_b_repo.path.join(dir_3).join(subdir).exists());
                    assert!(
                        user_b_repo
                            .path
                            .join(dir_3)
                            .join(subdir)
                            .join(subfile)
                            .exists()
                    );

                    Ok(())
                })
                .await?;

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_pull_multiple_commits() -> Result<(), OxenError> {
        test::run_training_data_repo_test_no_commits_async(|mut repo| async move {
            // Track a file
            let filename = "labels.txt";
            let file_path = repo.path.join(filename);
            repositories::add(&repo, &file_path).await?;
            repositories::commit(&repo, "Adding labels file")?;

            let train_path = repo.path.join("train");
            repositories::add(&repo, &train_path).await?;
            repositories::commit(&repo, "Adding train dir")?;

            let test_path = repo.path.join("test");
            repositories::add(&repo, &test_path).await?;
            repositories::commit(&repo, "Adding test dir")?;

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create Remote
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it
            repositories::push(&repo).await?;

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let new_repo_dir = new_repo_dir.join("repoo");
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &new_repo_dir).await?;
                let cloned_num_files = util::fs::rcount_files_in_dir(&cloned_repo.path);
                // 4 test, 7 train, 1 labels
                assert_eq!(12, cloned_num_files);

                api::client::repositories::delete(&remote_repo).await?;

                Ok(())
            })
            .await
        })
        .await
    }

    #[tokio::test]
    async fn test_pull_data_frame() -> Result<(), OxenError> {
        test::run_select_data_repo_test_no_commits_async("annotations", |mut repo| async move {
            // Track a file
            let filename = "annotations/train/bounding_box.csv";
            let file_path = repo.path.join(filename);
            let og_df = tabular::read_df(&file_path, DFOpts::empty()).await?;
            let og_contents = util::fs::read_from_path(&file_path)?;

            repositories::add(&repo, &file_path).await?;
            repositories::commit(&repo, "Adding bounding box file")?;

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create Remote
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it
            repositories::push(&repo).await?;

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let new_repo_dir = new_repo_dir.join("repoo");
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &new_repo_dir).await?;
                let file_path = cloned_repo.path.join(filename);

                let cloned_df = tabular::read_df(&file_path, DFOpts::empty()).await?;
                let cloned_contents = util::fs::read_from_path(&file_path)?;
                assert_eq!(og_df.height(), cloned_df.height());
                assert_eq!(og_df.width(), cloned_df.width());
                assert_eq!(cloned_contents, og_contents);

                // Status should be empty too
                let status = repositories::status(&cloned_repo)?;
                status.print();
                assert!(status.is_clean());

                // Make sure that the schema gets pulled
                let commit = repositories::commits::head_commit(&cloned_repo)?;
                let schemas = repositories::data_frames::schemas::list(&repo, &commit)?;
                assert!(!schemas.is_empty());

                api::client::repositories::delete(&remote_repo).await?;

                Ok(())
            })
            .await
        })
        .await
    }

    // Test that we pull down the proper data frames
    #[tokio::test]
    async fn test_pull_multiple_data_frames_multiple_schemas() -> Result<(), OxenError> {
        test::run_training_data_repo_test_fully_committed_async(|mut repo| async move {
            let filename = Path::new("nlp")
                .join("classification")
                .join("annotations")
                .join("train.tsv");
            let file_path = repo.path.join(filename);
            let og_df = tabular::read_df(&file_path, DFOpts::empty()).await?;
            let og_sentiment_contents = util::fs::read_from_path(&file_path)?;

            let commit = repositories::commits::head_commit(&repo)?;
            let schemas = repositories::data_frames::schemas::list(&repo, &commit)?;
            let num_schemas = schemas.len();

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create Remote
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it
            repositories::push(&repo).await?;

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let new_repo_dir = new_repo_dir.join("repoo");
                let cloned_repo =
                    repositories::clone_url(&remote_repo.remote.url, &new_repo_dir).await?;

                let filename = Path::new("nlp")
                    .join("classification")
                    .join("annotations")
                    .join("train.tsv");
                let file_path = cloned_repo.path.join(&filename);
                let cloned_df = tabular::read_df(&file_path, DFOpts::empty()).await?;
                let cloned_contents = util::fs::read_from_path(&file_path)?;
                assert_eq!(og_df.height(), cloned_df.height());
                assert_eq!(og_df.width(), cloned_df.width());
                assert_eq!(cloned_contents, og_sentiment_contents);
                println!("Cloned {filename:?} {cloned_df}");

                // Status should be empty too
                let status = repositories::status(&cloned_repo)?;
                status.print();
                assert!(status.is_clean());

                // Make sure we grab the same amount of schemas
                let head_commit = repositories::commits::head_commit(&cloned_repo)?;
                let pulled_schemas = repositories::data_frames::schemas::list(&repo, &head_commit)?;
                assert_eq!(pulled_schemas.len(), num_schemas);

                api::client::repositories::delete(&remote_repo).await?;

                Ok(())
            })
            .await
        })
        .await
    }

    #[tokio::test]
    async fn test_pull_full_commit_history() -> Result<(), OxenError> {
        test::run_training_data_repo_test_no_commits_async(|mut repo| async move {
            // First commit
            let filename = "labels.txt";
            let filepath = repo.path.join(filename);
            repositories::add(&repo, &filepath).await?;
            repositories::commit(&repo, "Adding labels file")?;

            // Second commit
            let new_filename = "new.txt";
            let new_filepath = repo.path.join(new_filename);
            util::fs::write_to_path(&new_filepath, "hallo")?;
            repositories::add(&repo, &new_filepath).await?;
            repositories::commit(&repo, "Adding a new file")?;

            // Third commit
            let train_path = repo.path.join("train");
            repositories::add(&repo, &train_path).await?;
            repositories::commit(&repo, "Adding train dir")?;

            // Fourth commit
            let test_path = repo.path.join("test");
            repositories::add(&repo, &test_path).await?;
            repositories::commit(&repo, "Adding test dir")?;

            // Get local history
            let local_history = repositories::commits::list(&repo)?;

            // Set the proper remote
            let remote = test::repo_remote_url_from(&repo.dirname());
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create Remote
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it
            repositories::push(&repo).await?;

            // run another test with a new repo dir that we are going to sync to
            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let new_repo_dir = new_repo_dir.join("repoo");
                let mut clone_opts = CloneOpts::new(&remote_repo.remote.url, &new_repo_dir);
                clone_opts.fetch_opts.all = true;
                let cloned_repo = repositories::clone(&clone_opts).await?;

                // Get cloned history, which should fall back to API if not found locally
                let cloned_history = repositories::commits::list(&cloned_repo)?;

                // Make sure the histories match
                assert_eq!(local_history.len(), cloned_history.len());

                api::client::repositories::delete(&remote_repo).await?;

                Ok(())
            })
            .await
        })
        .await
    }

    #[tokio::test]
    async fn test_pull_standard_clone_only_pulls_head() -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_training_data_fully_sync_remote(|_, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();
            test::run_empty_dir_test_async(|user_a_repo_dir| async move {
                let user_a_repo_dir_copy = user_a_repo_dir.join("repo_a");
                let user_a_repo =
                    repositories::clone_url(&remote_repo.remote.url, &user_a_repo_dir_copy).await?;

                // Deep copy pushes two new commits to advance the remote
                test::run_empty_dir_test_async(|user_b_repo_dir| async move {
                    let user_b_repo_dir_copy = user_b_repo_dir.join("repo_b");

                    let user_b_repo = repositories::deep_clone_url(
                        &remote_repo.remote.url,
                        &user_b_repo_dir_copy,
                    )
                    .await?;

                    let new_file = "new_file.txt";
                    let new_file_path = user_b_repo.path.join(new_file);
                    test::write_txt_file_to_path(&new_file_path, "hello from a file")?;
                    repositories::add(&user_b_repo, &new_file_path).await?;
                    repositories::commit(&user_b_repo, "Adding new file")?;

                    let new_file = "new_file_2.txt";
                    let new_file_path = user_b_repo.path.join(new_file);
                    test::write_txt_file_to_path(&new_file_path, "hello from a different")?;
                    repositories::add(&user_b_repo, &new_file_path).await?;
                    repositories::commit(&user_b_repo, "Adding new file 2")?;
                    repositories::push(&user_b_repo).await?;

                    Ok(())
                })
                .await?;

                // Pull again
                let all = false;
                repositories::pull_remote_branch(
                    &user_a_repo,
                    &FetchOpts {
                        all,
                        ..FetchOpts::new()
                    },
                )
                .await?;

                // Get all commits on the remote
                let remote_commits = repositories::commits::list(&user_a_repo)?;

                let mut synced_commits = 0;
                log::debug!("total n remote commits {}", remote_commits.len());
                for commit in remote_commits {
                    if core::commit_sync_status::commit_is_synced(&user_a_repo, &commit.id.parse()?)
                    {
                        synced_commits += 1;
                    }
                }

                // Two fully synced commits: the original clone, and the one we just grabbed.
                assert_eq!(synced_commits, 2);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_pull_full_commit_history_after_shallow_clone() -> Result<(), OxenError> {
        test::run_training_data_repo_test_fully_committed_async(|mut repo| async move {
            // Get the commits from the local repo to compare against later
            let og_commits = repositories::commits::list_all(&repo)?;

            // Set the proper remote
            let name = repo.dirname();
            let remote = test::repo_remote_url_from(&name);
            command::config::set_remote(&mut repo, constants::DEFAULT_REMOTE_NAME, &remote)?;

            // Create remote repo
            let remote_repo = test::create_remote_repo(&repo).await?;

            // Push it
            repositories::push(&repo).await?;

            test::run_empty_dir_test_async(|new_repo_dir| async move {
                let mut opts =
                    CloneOpts::new(&remote_repo.remote.url, new_repo_dir.join("new_repo"));
                opts.fetch_opts.subtree_paths = Some(vec![PathBuf::from("test")]);
                opts.fetch_opts.depth = Some(1);

                // Clone in shallow mode
                let cloned_repo = repositories::clone(&opts).await?;

                // Pull all the commits
                repositories::pull_all(&cloned_repo).await?;

                let pulled_commits = repositories::commits::list_all(&cloned_repo)?;
                assert_eq!(pulled_commits.len(), og_commits.len());

                Ok(())
            })
            .await
        })
        .await
    }

    /*
    When the remote advances, and you have local changes,
    you do not want to overwrite when pulling from the remote
    */
    #[tokio::test]
    async fn test_pull_does_not_overwrite_new_file_modified_by_remote() -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_select_data_sync_remote("README.md", |_, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();
            test::run_empty_dir_test_async(|user_a_repo_dir| async move {
                let user_a_repo_dir_copy = user_a_repo_dir.join("repo_a");
                let user_a_repo =
                    repositories::clone_url(&remote_repo.remote.url, &user_a_repo_dir_copy).await?;

                // Make a couple commits on the remote
                test::run_empty_dir_test_async(|user_b_repo_dir| async move {
                    let user_b_repo_dir_copy = user_b_repo_dir.join("repo_b");

                    let user_b_repo =
                        repositories::clone_url(&remote_repo.remote.url, &user_b_repo_dir_copy)
                            .await?;

                    let new_file = "new_file.txt";
                    let new_file_path = user_b_repo.path.join(new_file);
                    test::write_txt_file_to_path(&new_file_path, "hello from user b file")?;
                    repositories::add(&user_b_repo, &new_file_path).await?;
                    repositories::commit(&user_b_repo, "Adding new file")?;

                    // Push the remote
                    repositories::push(&user_b_repo).await?;

                    Ok(())
                })
                .await?;

                // Make some changes locally
                let new_file = "new_file.txt";
                let new_file_path = user_a_repo.path.join(new_file);
                test::write_txt_file_to_path(&new_file_path, "hello from user a file")?;

                // Pull changes
                let result = repositories::pull(&user_a_repo).await;

                // Assert that it failed
                assert!(result.is_err());

                // Make sure that the local changes are not overwritten
                let content = util::fs::read_from_path(&new_file_path)?;
                assert_eq!(content, "hello from user a file");

                // Pull again
                let result = repositories::pull(&user_a_repo).await;

                // Assert that it failed
                assert!(result.is_err());

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    /*
    When the remote advances, and you have local changes,
    you do not want to overwrite when pulling from the remote
    */
    #[tokio::test]
    async fn test_pull_does_not_overwrite_modified_files_after_remote_modification()
    -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_select_data_sync_remote("README.md", |_, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();
            test::run_empty_dir_test_async(|user_a_repo_dir| async move {
                let user_a_repo_dir_copy = user_a_repo_dir.join("repo_a");
                let user_a_repo =
                    repositories::clone_url(&remote_repo.remote.url, &user_a_repo_dir_copy).await?;

                // Make a couple commits on the remote
                test::run_empty_dir_test_async(|user_b_repo_dir| async move {
                    let user_b_repo_dir_copy = user_b_repo_dir.join("repo_b");

                    let user_b_repo =
                        repositories::clone_url(&remote_repo.remote.url, &user_b_repo_dir_copy)
                            .await?;

                    // Edit the README to cause a conflict
                    let readme_path = user_b_repo.path.join("README.md");
                    test::write_txt_file_to_path(
                        &readme_path,
                        "Hello from another user b README :(",
                    )?;
                    repositories::add(&user_b_repo, &readme_path).await?;
                    repositories::commit(&user_b_repo, "Updating the README on the remote")?;

                    // Push the remote
                    repositories::push(&user_b_repo).await?;

                    Ok(())
                })
                .await?;

                // Make some changes locally
                let modified_file = "README.md";
                let modified_file_path = user_a_repo.path.join(modified_file);
                test::write_txt_file_to_path(&modified_file_path, "# User A README")?;

                // Pull again
                let result = repositories::pull(&user_a_repo).await;

                // Assert that it failed
                assert!(result.is_err());

                // Make sure that the local changes are not overwritten
                let content = util::fs::read_from_path(&modified_file_path)?;
                assert_eq!(content, "# User A README");

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    /*
    This one tests modifying the file on the local before it is modified on the remote
    Regardless, the local file should not be overwritten
    */
    #[tokio::test]
    async fn test_pull_does_not_overwrite_modified_files_before_remote_modification()
    -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_select_data_sync_remote("README.md", |_, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();
            test::run_empty_dir_test_async(|user_a_repo_dir| async move {
                let user_a_repo_dir_copy = user_a_repo_dir.join("repo_a");
                let user_a_repo =
                    repositories::clone_url(&remote_repo.remote.url, &user_a_repo_dir_copy).await?;

                // Make some changes locally
                let modified_file = "README.md";
                let modified_file_path = user_a_repo.path.join(modified_file);
                test::write_txt_file_to_path(&modified_file_path, "# User A README")?;

                // Make a couple commits on the remote
                test::run_empty_dir_test_async(|user_b_repo_dir| async move {
                    let user_b_repo_dir_copy = user_b_repo_dir.join("repo_b");

                    let user_b_repo =
                        repositories::clone_url(&remote_repo.remote.url, &user_b_repo_dir_copy)
                            .await?;

                    // Edit the README to cause a conflict
                    let readme_path = user_b_repo.path.join("README.md");
                    test::write_txt_file_to_path(
                        &readme_path,
                        "Hello from another user b README :(",
                    )?;
                    repositories::add(&user_b_repo, &readme_path).await?;
                    repositories::commit(&user_b_repo, "Updating the README on the remote")?;

                    // Push the remote
                    repositories::push(&user_b_repo).await?;

                    Ok(())
                })
                .await?;

                // Pull again
                let result = repositories::pull(&user_a_repo).await;

                // Assert that it failed
                assert!(result.is_err());

                // Make sure that the local changes are not overwritten
                let content = util::fs::read_from_path(&modified_file_path)?;
                assert_eq!(content, "# User A README");

                // Pull again
                let result = repositories::pull(&user_a_repo).await;

                // Assert that it failed
                assert!(result.is_err());

                // Make sure that the local changes are not overwritten
                let content = util::fs::read_from_path(&modified_file_path)?;
                assert_eq!(content, "# User A README");

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    /*
    Modify a different file on the remote, and modify the readme locally, and make sure that the local readme is not overwritten
    */
    #[tokio::test]
    async fn test_pull_does_not_overwrite_modified_files_after_modifying_different_file()
    -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_select_data_sync_remote("README.md", |_, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();
            test::run_empty_dir_test_async(|user_a_repo_dir| async move {
                let user_a_repo_dir_copy = user_a_repo_dir.join("repo_a");
                let user_a_repo =
                    repositories::clone_url(&remote_repo.remote.url, &user_a_repo_dir_copy).await?;

                // Make a couple commits on the remote
                test::run_empty_dir_test_async(|user_b_repo_dir| async move {
                    let user_b_repo_dir_copy = user_b_repo_dir.join("repo_b");

                    let user_b_repo =
                        repositories::clone_url(&remote_repo.remote.url, &user_b_repo_dir_copy)
                            .await?;

                    // Edit a new file on the remote
                    let new_file = "new_file.txt";
                    let new_file_path = user_b_repo.path.join(new_file);
                    test::write_txt_file_to_path(
                        &new_file_path,
                        "Hello from another user b new file",
                    )?;
                    repositories::add(&user_b_repo, &new_file_path).await?;
                    repositories::commit(&user_b_repo, "Updating the new file on the remote")?;

                    // Push the remote
                    repositories::push(&user_b_repo).await?;

                    Ok(())
                })
                .await?;

                // Make some changes locally
                let modified_file = "README.md";
                let modified_file_path = user_a_repo.path.join(modified_file);
                test::write_txt_file_to_path(&modified_file_path, "# User A README")?;

                // Pull should succeed because we did not modify README on remote
                let result = repositories::pull(&user_a_repo).await;
                assert!(result.is_err());

                // Make sure that the local changes are not overwritten
                let content = util::fs::read_from_path(&modified_file_path)?;
                assert_eq!(content, "# User A README");

                // Pull again
                let result = repositories::pull(&user_a_repo).await;
                assert!(result.is_err());

                // Make sure that the local changes are still not overwritten
                let content = util::fs::read_from_path(&modified_file_path)?;
                assert_eq!(content, "# User A README");

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    /*
    Remove the README.md on the remote, and modify the readme locally, and make sure that the local readme is not overwritten
    */
    #[tokio::test]
    async fn test_pull_does_not_overwrite_modified_files_after_removing_file()
    -> Result<(), OxenError> {
        // Push the Remote Repo
        test::run_select_data_sync_remote("README.md", |_, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();
            test::run_empty_dir_test_async(|user_a_repo_dir| async move {
                let user_a_repo_dir_copy = user_a_repo_dir.join("repo_a");
                let user_a_repo =
                    repositories::clone_url(&remote_repo.remote.url, &user_a_repo_dir_copy).await?;

                // Make a couple commits on the remote
                test::run_empty_dir_test_async(|user_b_repo_dir| async move {
                    let user_b_repo_dir_copy = user_b_repo_dir.join("repo_b");

                    let user_b_repo =
                        repositories::clone_url(&remote_repo.remote.url, &user_b_repo_dir_copy)
                            .await?;

                    // Remove the README.md on the remote
                    let rm_opts = RmOpts {
                        path: PathBuf::from("README.md"),
                        ..Default::default()
                    };
                    repositories::rm(&user_b_repo, &rm_opts)?;
                    repositories::commit(&user_b_repo, "Removing the README.md on the remote")?;

                    // Push the remote
                    repositories::push(&user_b_repo).await?;

                    Ok(())
                })
                .await?;

                // Make some changes locally
                let modified_file = "README.md";
                let modified_file_path = user_a_repo.path.join(modified_file);
                test::write_txt_file_to_path(&modified_file_path, "# User A README")?;

                // Pull again
                let result = repositories::pull(&user_a_repo).await;

                // Assert that it failed
                assert!(result.is_err());

                // Make sure that the local changes are not overwritten
                let content = util::fs::read_from_path(&modified_file_path)?;
                assert_eq!(content, "# User A README");

                // Pull again
                let result = repositories::pull(&user_a_repo).await;

                // Assert that it failed
                assert!(result.is_err());

                // Make sure that the local changes are not overwritten
                let content = util::fs::read_from_path(&modified_file_path)?;
                assert_eq!(content, "# User A README");

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_subtree_clone_branch_push_pull() -> Result<(), OxenError> {
        // Push the Remote Repo
        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 repo_dir = repo_dir.join("subtree_repo");

                // 1. Clone repo with subtree filter
                let mut clone_opts = CloneOpts::new(&remote_repo.remote.url, &repo_dir);
                clone_opts.fetch_opts.subtree_paths = Some(vec![PathBuf::from("train")]);
                // clone_opts.fetch_opts.depth = Some(1);
                let repo = repositories::clone(&clone_opts).await?;

                // Verify we only have the subtree
                assert!(repo.path.join("train").exists());
                assert!(!repo.path.join("test").exists());

                // 2. Create and checkout new branch
                let branch_name = "branch1";
                repositories::branches::create_checkout(&repo, branch_name)?;

                // 3. Add new file in dir1
                let dir1 = repo.path.join("dir1");
                util::fs::create_dir_all(&dir1)?;
                let new_file = dir1.join("newfile.txt");
                test::write_txt_file_to_path(&new_file, "This is a new file")?;

                // 4. Add and commit the new file
                repositories::add(&repo, &new_file).await?;
                repositories::commit(&repo, "Adding new file in dir1")?;
                let opts = PushOpts {
                    remote: constants::DEFAULT_REMOTE_NAME.to_string(),
                    branch: branch_name.to_string(),
                    ..Default::default()
                };

                // 5. Push to origin branch1
                repositories::push::push_remote_branch(&repo, &opts).await?;

                // 6. Pull from main
                let pull_result = repositories::pull_remote_branch(
                    &repo,
                    &FetchOpts {
                        remote: constants::DEFAULT_REMOTE_NAME.to_string(),
                        branch: "main".to_string(),
                        all: false,
                        subtree_paths: Some(vec![PathBuf::from("train")]),
                        depth: None,
                        ..FetchOpts::new()
                    },
                )
                .await;

                assert_eq!(
                    pull_result.unwrap_err().to_string(),
                    OxenError::basic_str("No changes to commit").to_string()
                );

                // Verify the state after pull
                assert!(repo.path.join("train").exists());
                assert!(repo.path.join("dir1").join("newfile.txt").exists());

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    // Pull and merge a non-empty remote with a non-empty repo
    #[tokio::test]
    async fn test_pull_and_merge_non_empty_repos() -> Result<(), OxenError> {
        // Create a remote repo with a commit
        test::run_remote_created_and_readme_remote_repo_test(|remote_repo| async move {
            let remote_repo_clone = remote_repo.clone();
            test::run_empty_dir_test_async(|dir| async move {
                // Create an empty repo locally
                let mut local_repo = LocalRepository::new(dir, None)?;

                // Add and commit a new file
                let hello_file = local_repo.path.join("dir").join("hello.txt");
                util::fs::write_to_path(
                    &hello_file,
                    "Oxen.ai is the best data version control system.",
                )?;

                repositories::add(&local_repo, &hello_file).await?;
                repositories::commit(&local_repo, "Hello!")?;

                // Set the remote
                let url = remote_repo_clone.url();
                command::config::set_remote(&mut local_repo, "origin", url)?;

                // Pull the remote
                repositories::pull(&local_repo).await?;

                // Both files should exist in the local repo
                assert!(hello_file.exists());
                assert!(PathBuf::from("README.md").exists());

                // There should be 3 total commits: The two divergent commits, and the merge commit
                let commits = repositories::commits::list_all(&local_repo)?;
                assert_eq!(commits.len(), 3);

                Ok(())
            })
            .await?;

            Ok(remote_repo)
        })
        .await
    }

    #[tokio::test]
    async fn test_pull_missing_files_redownloads_deleted_versions() -> Result<(), OxenError> {
        test::run_one_commit_sync_repo_test(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

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

                // Verify the clone has version files
                let version_store = cloned_repo.version_store()?;
                let versions = version_store.list_versions().await?;
                assert!(
                    !versions.is_empty(),
                    "Cloned repo should have version files"
                );

                // Delete a version file to simulate corruption cleanup
                let deleted_hash = versions[0].clone();
                version_store.delete_version(&deleted_hash).await?;
                assert!(
                    !version_store.version_exists(&deleted_hash).await?,
                    "Version file should be deleted"
                );

                // Normal pull should say "up to date" and not re-download
                repositories::pull(&cloned_repo).await?;
                assert!(
                    !version_store.version_exists(&deleted_hash).await?,
                    "Normal pull should not re-download deleted version"
                );

                // Pull with --missing-files should re-download the deleted version
                let fetch_opts = FetchOpts {
                    missing_files: true,
                    ..FetchOpts::new()
                };
                repositories::pull_remote_branch(&cloned_repo, &fetch_opts).await?;
                assert!(
                    version_store.version_exists(&deleted_hash).await?,
                    "pull --missing-files should re-download deleted version"
                );

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }
}