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
//! # oxen push
//!
//! Push data from your local machine to a remote.
//!
use crate::api;
use crate::core::index::{pusher, EntryIndexer};
use crate::error::OxenError;
use crate::model::{Branch, LocalRepository, RemoteBranch, RemoteRepository};
/// # Get a log of all the commits
///
/// ```
/// # use liboxen::api;
/// # use liboxen::test;
/// use liboxen::command;
/// use liboxen::util;
/// # use liboxen::error::OxenError;
/// # use std::path::Path;
/// # #[tokio::main]
/// # async fn main() -> Result<(), OxenError> {
/// # test::init_test_env();
/// // Initialize the repository
/// let base_dir = Path::new("repo_dir_push");
/// let mut repo = command::init(base_dir)?;
///
/// // Write file to disk
/// let hello_file = base_dir.join("hello.txt");
/// util::fs::write_to_path(&hello_file, "Hello World");
///
/// // Stage the file
/// command::add(&repo, &hello_file)?;
///
/// // Commit staged
/// command::commit(&repo, "My commit message")?;
///
/// // Set the remote server
/// command::config::set_remote(&mut repo, "origin", "http://localhost:3000/repositories/hello");
///
/// let remote_repo = api::remote::repositories::create(&repo, "repositories", "hello", "localhost:3000").await?;
///
/// // Push the file
/// command::push(&repo).await;
///
/// # util::fs::remove_dir_all(base_dir)?;
/// # api::remote::repositories::delete(&remote_repo).await?;
/// # Ok(())
/// # }
/// ```
pub async fn push(repo: &LocalRepository) -> Result<RemoteRepository, OxenError> {
let indexer = EntryIndexer::new(repo)?;
let mut rb = RemoteBranch::default();
// Push the currently checked out branch
if let Some(current_branch) = api::local::branches::current_branch(repo)? {
rb.branch = current_branch.name;
}
indexer.push(&rb).await
}
/// Push to a specific remote branch on the default remote repository
pub async fn push_remote_branch(
repo: &LocalRepository,
remote: &str,
branch: &str,
) -> Result<RemoteRepository, OxenError> {
let indexer = EntryIndexer::new(repo)?;
let rb = RemoteBranch {
remote: String::from(remote),
branch: String::from(branch),
};
indexer.push(&rb).await
}
/// Push to a specific remote repository
pub async fn push_remote_repo_branch(
local_repo: LocalRepository,
remote_repo: RemoteRepository,
branch: Branch,
) -> Result<RemoteRepository, OxenError> {
pusher::push_remote_repo(&local_repo, remote_repo, branch).await
}
/// Push to a specific remote repository, given a branch name
pub async fn push_remote_repo_branch_name(
local_repo: LocalRepository,
remote_repo: RemoteRepository,
branch_name: &str,
) -> Result<RemoteRepository, OxenError> {
let branch = api::local::branches::get_by_name(&local_repo, branch_name)?
.ok_or(OxenError::local_branch_not_found(branch_name))?;
push_remote_repo_branch(local_repo, remote_repo, branch).await
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use crate::api;
use crate::command;
use crate::constants;
use crate::constants::DEFAULT_BRANCH_NAME;
use crate::core::index::CommitEntryReader;
use crate::error::OxenError;
use crate::test;
use crate::util;
// Test that we cannot clone separate repos with separate histories, then push to the same history
// 1) Clone repo A with data
// 2) Clone repo B with data
// 3) Push Repo A
// 4) Push repo B to repo A and fail
#[tokio::test]
async fn test_tree_cannot_push_two_separate_cloned_repos() -> Result<(), OxenError> {
// Push the first repo with data
test::run_training_data_fully_sync_remote(|_, remote_repo_1| async move {
let remote_repo_1_copy = remote_repo_1.clone();
// Push the second repo with data
test::run_training_data_fully_sync_remote(|_, remote_repo_2| async move {
let remote_repo_2_copy = remote_repo_2.clone();
// Clone the first repo
test::run_empty_dir_test_async(|first_repo_dir| async move {
let first_cloned_repo = command::clone_url(
&remote_repo_1.remote.url,
&first_repo_dir.join("first_repo_dir"),
)
.await?;
// Clone the second repo
test::run_empty_dir_test_async(|second_repo_dir| async move {
let mut second_cloned_repo = command::clone_url(
&remote_repo_2.remote.url,
&second_repo_dir.join("second_repo_dir"),
)
.await?;
// Add to the first repo, after we have the second repo cloned
let new_file = "new_file.txt";
let new_file_path = first_cloned_repo.path.join(new_file);
let new_file_path =
test::write_txt_file_to_path(new_file_path, "new file")?;
command::add(&first_cloned_repo, &new_file_path)?;
command::commit(&first_cloned_repo, "Adding first file path.")?;
command::push(&first_cloned_repo).await?;
// Reset the remote on the second repo to the first repo
let first_remote = test::repo_remote_url_from(&first_cloned_repo.dirname());
command::config::set_remote(
&mut second_cloned_repo,
constants::DEFAULT_REMOTE_NAME,
&first_remote,
)?;
// Adding two commits to have a longer history that also should fail
let new_file = "new_file_2.txt";
let new_file_path = second_cloned_repo.path.join(new_file);
let new_file_path =
test::write_txt_file_to_path(new_file_path, "new file 2")?;
command::add(&second_cloned_repo, &new_file_path)?;
command::commit(&second_cloned_repo, "Adding second file path.")?;
let new_file = "new_file_3.txt";
let new_file_path = second_cloned_repo.path.join(new_file);
let new_file_path =
test::write_txt_file_to_path(new_file_path, "new file 3")?;
command::add(&second_cloned_repo, &new_file_path)?;
command::commit(&second_cloned_repo, "Adding third file path.")?;
// Push should FAIL
let result = command::push(&second_cloned_repo).await;
assert!(result.is_err());
Ok(second_repo_dir)
})
.await?;
Ok(first_repo_dir)
})
.await?;
Ok(remote_repo_2_copy)
})
.await?;
Ok(remote_repo_1_copy)
})
.await
}
// Test that we cannot push when the remote repo is ahead
// 1) Clone repo to user A
// 2) Clone repo to user B
// 3) User A makes commit with `new_file.txt`` and pushes
// 4) User B makes commit with `another_file.txt` pushes and succeeds
// 5) User B pulls user A's changes, pushes and succeeds
#[tokio::test]
async fn test_tree_can_push_when_remote_repo_is_ahead_new_file() -> 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 = command::clone_url(
&remote_repo.remote.url,
&user_a_repo_dir_copy.join("new_repo"),
)
.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 = command::clone_url(
&remote_repo.remote.url,
&user_b_repo_dir_copy.join("New_repo"),
)
.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")?;
command::add(&user_a_repo, &new_file_path)?;
command::commit(&user_a_repo, "Adding first file path.")?;
command::push(&user_a_repo).await?;
// User B adds a different file and pushes
let different_file = "another_file.txt";
let new_file_path = user_b_repo.path.join(different_file);
let new_file_path = test::write_txt_file_to_path(new_file_path, "newer file")?;
command::add(&user_b_repo, &new_file_path)?;
command::commit(&user_b_repo, "Adding second file path.")?;
// Push should succeed now!!! there are no conflicts
let result = command::push(&user_b_repo).await;
assert!(result.is_ok());
// Pull should succeed
command::pull(&user_b_repo).await?;
// Push should now succeed
command::push(&user_b_repo).await?;
Ok(user_b_repo_dir_copy)
})
.await?;
Ok(user_a_repo_dir_copy)
})
.await?;
Ok(remote_repo_copy)
})
.await
}
// Test that we cannot push when the remote repo is ahead
// * Clone repo to user A
// * Clone repo to user B
// * User A makes commit modifying `README.md` and pushes
// * User B makes commit modifying `README.md` pushes and fails
// * User B pulls user A's changes and there is a conflict
// * User B fixes the conflict and pushes and succeeds
#[tokio::test]
async fn test_tree_cannot_push_when_remote_repo_is_ahead_same_file() -> 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 = command::clone_url(
&remote_repo.remote.url,
&user_a_repo_dir_copy.join("new_repo"),
)
.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 = command::clone_url(
&remote_repo.remote.url,
&user_b_repo_dir_copy.join("New_repo"),
)
.await?;
// User A modifies the README.md and pushes
let mod_file = "README.md";
let a_mod_file_path = user_a_repo.path.join(mod_file);
let a_mod_file_path =
test::write_txt_file_to_path(a_mod_file_path, "I am the README now")?;
command::add(&user_a_repo, &a_mod_file_path)?;
command::commit(&user_a_repo, "User A modifying the README.")?;
command::push(&user_a_repo).await?;
// User B tries to modify the same README.md and push
let b_mod_file_path = user_b_repo.path.join(mod_file);
let b_mod_file_path =
test::write_txt_file_to_path(b_mod_file_path, "I be the README now.")?;
command::add(&user_b_repo, &b_mod_file_path)?;
command::commit(&user_b_repo, "User B modifying the README.")?;
// Push should fail! Remote is ahead
let first_push_result = command::push(&user_b_repo).await;
assert!(first_push_result.is_err());
// Pull should succeed
command::pull(&user_b_repo).await?;
// There should be conflicts
let status = command::status(&user_b_repo)?;
assert!(status.has_merge_conflicts());
// User B resolves conflicts
let b_mod_file_path = user_b_repo.path.join(mod_file);
let b_mod_file_path = test::write_txt_file_to_path(
b_mod_file_path,
"No for real. I be the README now.",
)?;
command::add(&user_b_repo, &b_mod_file_path)?;
command::commit(&user_b_repo, "User B resolving conflicts.")?;
// Push should now succeed
let third_push_result = command::push(&user_b_repo).await;
assert!(third_push_result.is_ok());
Ok(user_b_repo_dir_copy)
})
.await?;
Ok(user_a_repo_dir_copy)
})
.await?;
Ok(remote_repo_copy)
})
.await
}
// Test that we cannot push when the remote repo multiple commits is ahead
// * Create repo for user A
// * Add data for user A
// * Push data for user A
// * Clone repo to user B
// * User A makes commit modifying `README.md` and pushes
// * User A makes commit modifying `annotations/train/bounding_box.csv` and pushes
// * User B makes commit modifying `README.md` pushes and fails
// * User B pulls user A's changes and there is a conflict
// * User B fixes the conflict and pushes and succeeds
#[tokio::test]
async fn test_tree_cannot_push_when_remote_repo_is_2_commits_ahead_same_file(
) -> Result<(), OxenError> {
// Create Repo for User A
test::run_empty_dir_test_async(|user_a_repo_dir| async move {
let mut user_a_repo = command::init(&user_a_repo_dir)?;
// Add data for User A
let mod_file = "README.md";
let a_mod_file_path = user_a_repo.path.join(mod_file);
let a_mod_file_path =
test::write_txt_file_to_path(a_mod_file_path, "I am the original README")?;
// Make a directory for the of random data
let random_data_dir = user_a_repo.path.join("random_data");
util::fs::create_dir_all(&random_data_dir)?;
// add 10 text files to the random data directory
for i in 0..10 {
let file_name = format!("random_file_{}.txt", i);
let file_path = random_data_dir.join(file_name);
let file_path = test::write_txt_file_to_path(file_path, "random data")?;
command::add(&user_a_repo, &file_path)?;
}
command::add(&user_a_repo, &a_mod_file_path)?;
command::commit(&user_a_repo, "User A adding the README.")?;
// Set the proper remote
let remote = test::repo_remote_url_from(&user_a_repo.dirname());
command::config::set_remote(&mut user_a_repo, constants::DEFAULT_REMOTE_NAME, &remote)?;
// Create the remote repo
let remote_repo = test::create_remote_repo(&user_a_repo).await?;
// Push data for User A
println!("Pushing README.md for user A...");
command::push(&user_a_repo).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 = command::clone_url(
&remote_repo.remote.url,
&user_b_repo_dir_copy.join("New_repo"),
)
.await?;
// User A modifies the bounding_box.csv and pushes
let mod_file = PathBuf::from("bounding_box.csv");
let a_mod_file_path = user_a_repo.path.join(mod_file);
let a_mod_file_path =
test::write_txt_file_to_path(a_mod_file_path, "path,annotation")?;
command::add(&user_a_repo, &a_mod_file_path)?;
command::commit(&user_a_repo, "User A adds bounding_box.csv.")?;
println!("Pushing bounding_box.csv for user A...");
command::push(&user_a_repo).await?;
// User B modifies the README.md and pushes
let mod_file = "README.md";
let b_mod_file_path = user_b_repo.path.join(mod_file);
let b_mod_file_path =
test::write_txt_file_to_path(b_mod_file_path, "I be the README now.")?;
command::add(&user_b_repo, &b_mod_file_path)?;
println!("Pushing README.md for user B...");
command::commit(&user_b_repo, "User B modifying the README.")?;
// Push from B should succeed!
let first_push_result = command::push(&user_b_repo).await;
assert!(first_push_result.is_ok());
// User A modifies tries to modify the same README.md and pushes
let a_mod_file_path = user_a_repo.path.join(mod_file);
let a_mod_file_path =
test::write_txt_file_to_path(a_mod_file_path, "I am the README now")?;
command::add(&user_a_repo, &a_mod_file_path)?;
command::commit(&user_a_repo, "User A modifying the README.")?;
// Push should fail! Remote is ahead
println!("Pushing README.md for user A...");
let second_push_a = command::push(&user_a_repo).await;
assert!(second_push_a.is_err());
// Try it again - I don't know why this is succeeding the second time
let second_push_again = command::push(&user_a_repo).await;
assert!(second_push_again.is_err());
// Pull A should succeed
let pull_a = command::pull(&user_a_repo).await;
assert!(pull_a.is_ok());
// There should be conflicts in A
let status = command::status(&user_a_repo)?;
assert!(status.has_merge_conflicts());
// User A resolves conflicts
let a_mod_file_path = user_a_repo.path.join(mod_file);
let a_mod_file_path = test::write_txt_file_to_path(
a_mod_file_path,
"No for real. I am the README now.",
)?;
command::add(&user_a_repo, &a_mod_file_path)?;
command::commit(&user_a_repo, "User A resolving conflicts.")?;
// Push should now succeed
println!("Final pushing README.md for user A...");
let third_push_result = command::push(&user_a_repo).await;
assert!(third_push_result.is_ok());
// Return repo B because that is the closure we are in
Ok(user_b_repo_dir_copy)
})
.await?;
Ok(user_a_repo_dir)
})
.await?;
Ok(())
}
#[tokio::test]
async fn test_tree_can_push_when_remote_repo_is_many_commits_ahead_new_file(
) -> 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 = command::clone_url(
&remote_repo.remote.url,
&user_a_repo_dir_copy.join("new_repo"),
)
.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 = command::clone_url(
&remote_repo.remote.url,
&user_b_repo_dir_copy.join("new_repo"),
)
.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")?;
command::add(&user_a_repo, &new_file_path)?;
command::commit(&user_a_repo, "Adding first file path.")?;
command::push(&user_a_repo).await?;
// User A adds a file and pushes
let new_file = "new_file_2.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")?;
command::add(&user_a_repo, &new_file_path)?;
command::commit(&user_a_repo, "Adding second file path.")?;
command::push(&user_a_repo).await?;
// User A adds a file and pushes
let new_file = "new_file_3.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")?;
command::add(&user_a_repo, &new_file_path)?;
command::commit(&user_a_repo, "Adding third file path.")?;
command::push(&user_a_repo).await?;
// User B adds a different file and pushes
let different_file = "another_file.txt";
let new_file_path = user_b_repo.path.join(different_file);
let new_file_path = test::write_txt_file_to_path(new_file_path, "newer file")?;
command::add(&user_b_repo, &new_file_path)?;
command::commit(&user_b_repo, "User B adding second file path.")?;
// This should now succeed! Used to fail, but auto-merges now.
log::debug!("pushing b...");
let result = command::push(&user_b_repo).await;
assert!(result.is_ok());
command::pull(&user_b_repo).await?;
command::push(&user_b_repo).await?;
// Full pull
command::pull_all(&user_b_repo).await?;
// Push should now succeed
command::push(&user_b_repo).await?;
Ok(user_b_repo_dir_copy)
})
.await?;
Ok(user_a_repo_dir_copy)
})
.await?;
Ok(remote_repo_copy)
})
.await
}
#[tokio::test]
async fn test_tree_can_push_when_remote_is_many_commits_ahead_no_tree_conflicts(
) -> 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.clone();
let user_a_repo =
command::clone_url(&remote_repo.remote.url, &user_a_repo_dir.join("new_repo"))
.await?;
// Log out all files in this directory with fs
let files = util::fs::rlist_paths_in_dir(&user_a_repo_dir);
for item in files {
log::debug!("\nfile or dir: {:?}\n", item)
}
// User A: add files in `nlp`
// User B: add files in `annotations`
// Clone Repo to User B
test::run_empty_dir_test_async(|user_b_repo_dir| async move {
let user_a_modify_dir = "nlp";
let user_b_modify_dir = "annotations";
let user_b_repo_dir_copy = user_b_repo_dir.clone();
let user_b_repo = command::clone_url(
&remote_repo.remote.url,
&user_b_repo_dir.join("new_repo"),
)
.await?;
// User A adds a file and pushes
let new_file = "new_file.txt";
let new_file_path = user_a_repo.path.join(user_a_modify_dir).join(new_file);
let new_file_path = test::write_txt_file_to_path(new_file_path, "new file")?;
command::add(&user_a_repo, &new_file_path)?;
command::commit(&user_a_repo, "Adding first file path.")?;
command::push(&user_a_repo).await?;
// User A adds a file and pushes
let new_file = "new_file_2.txt";
let new_file_path = user_a_repo.path.join(user_a_modify_dir).join(new_file);
let new_file_path = test::write_txt_file_to_path(new_file_path, "new file")?;
command::add(&user_a_repo, &new_file_path)?;
command::commit(&user_a_repo, "Adding second file path.")?;
command::push(&user_a_repo).await?;
// User A adds a file and pushes
let new_file = "new_file_3.txt";
let new_file_path = user_a_repo.path.join(user_a_modify_dir).join(new_file);
let new_file_path = test::write_txt_file_to_path(new_file_path, "new file")?;
command::add(&user_a_repo, &new_file_path)?;
command::commit(&user_a_repo, "Adding third file path.")?;
command::push(&user_a_repo).await?;
// User B adds a different file and pushes
let different_file = "another_file.txt";
let new_file_path = user_b_repo
.path
.join(user_b_modify_dir)
.join(different_file);
let new_file_path = test::write_txt_file_to_path(new_file_path, "newer file")?;
command::add(&user_b_repo, &new_file_path)?;
command::commit(&user_b_repo, "User B adding second file path.")?;
// Push should succeed - different dirs!
command::push(&user_b_repo).await?;
Ok(user_b_repo_dir_copy)
})
.await?;
Ok(user_a_repo_dir_copy)
})
.await?;
Ok(remote_repo_copy)
})
.await
}
#[tokio::test]
async fn test_tree_cannot_push_when_remote_is_many_commits_ahead_tree_conflicts(
) -> 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.clone();
let user_a_repo =
command::clone_url(&remote_repo.remote.url, &user_a_repo_dir.join("new_repo"))
.await?;
// Log out all files in this directory with fs
let files = util::fs::rlist_paths_in_dir(&user_a_repo_dir);
for item in files {
log::debug!("\nfile or dir: {:?}\n", item)
}
// User A: add files in `nlp`
// User B: add files in `annotations`
// 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.clone();
let user_b_repo = command::clone_url(
&remote_repo.remote.url,
&user_b_repo_dir.join("new_repo"),
)
.await?;
// User A adds a file and pushes
let modify_path_a = user_a_repo
.path
.join("annotations")
.join("train")
.join("annotations.txt");
let modify_path_b = user_b_repo
.path
.join("annotations")
.join("train")
.join("annotations.txt");
test::write_txt_file_to_path(&modify_path_a, "new file")?;
command::add(&user_a_repo, &modify_path_a)?;
command::commit(&user_a_repo, "Adding first file path.")?;
command::push(&user_a_repo).await?;
// User B adds a different file and pushe
test::write_txt_file_to_path(&modify_path_b, "newer file")?;
command::add(&user_b_repo, &modify_path_b)?;
command::commit(&user_b_repo, "User B adding second file path.")?;
// Push should fail - this creates a merge conflict.
let res = command::push(&user_b_repo).await;
assert!(res.is_err());
Ok(user_b_repo_dir_copy)
})
.await?;
Ok(user_a_repo_dir_copy)
})
.await?;
Ok(remote_repo_copy)
})
.await
}
#[tokio::test]
async fn test_tree_can_push_tree_no_conflict_added_file() -> 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.clone();
let user_a_repo =
command::clone_url(&remote_repo.remote.url, &user_a_repo_dir.join("new_repo"))
.await?;
// Log out all files in this directory with fs
let files = util::fs::rlist_paths_in_dir(&user_a_repo_dir);
for item in files {
log::debug!("\nfile or dir: {:?}\n", item)
}
// 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.clone();
log::debug!("About to clone b...");
let user_b_repo = command::clone_url(
&remote_repo.remote.url,
&user_b_repo_dir.join("new_repo"),
)
.await?;
log::debug!("finished cloning b.");
// User A adds a file and pushes
let modify_path_a = user_a_repo
.path
.join("annotations")
.join("train")
.join("averynewfile.txt");
let modify_path_b = user_b_repo
.path
.join("annotations")
.join("train")
.join("anothernewfile.txt");
test::write_txt_file_to_path(&modify_path_a, "new file")?;
command::add(&user_a_repo, &modify_path_a)?;
command::commit(&user_a_repo, "Adding first file path.")?;
command::push(&user_a_repo).await?;
// User B adds a different file and pushe
test::write_txt_file_to_path(&modify_path_b, "newer file")?;
command::add(&user_b_repo, &modify_path_b)?;
command::commit(&user_b_repo, "User B adding second file path.")?;
// Push should succeed - different dirs!
command::push(&user_b_repo).await?;
Ok(user_b_repo_dir_copy)
})
.await?;
Ok(user_a_repo_dir_copy)
})
.await?;
Ok(remote_repo_copy)
})
.await
}
#[tokio::test]
async fn test_tree_cannot_push_tree_conflict_deleted_file() -> 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.clone();
let user_a_repo =
command::clone_url(&remote_repo.remote.url, &user_a_repo_dir.join("new_repo"))
.await?;
// Log out all files in this directory with fs
let files = util::fs::rlist_paths_in_dir(&user_a_repo_dir);
for item in files {
log::debug!("\nfile or dir: {:?}\n", item)
}
// User A: add files in `nlp`
// User B: add files in `annotations`
// 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.clone();
let user_b_repo = command::clone_url(
&remote_repo.remote.url,
&user_b_repo_dir.join("new_repo"),
)
.await?;
// User A deletes the file and commits
let modify_path_a = user_a_repo
.path
.join("annotations")
.join("train")
.join("annotations.txt");
let modify_path_b = user_b_repo
.path
.join("annotations")
.join("train")
.join("annotations.txt");
let _add_path_b = user_b_repo
.path
.join("annotations")
.join("train")
.join("averynewfile.txt");
// print all files in annotations/train
let files = util::fs::rlist_paths_in_dir(
&user_b_repo.path.join("annotations").join("train"),
);
for item in files {
log::debug!("\npre file or dir: {:?}\n", item)
}
// User A modifies
test::write_txt_file_to_path(&modify_path_a, "fancy new file contents")?;
command::add(&user_a_repo, &modify_path_a)?;
let commit_a = command::commit(&user_a_repo, "modifying first file path.")?;
command::push(&user_a_repo).await?;
// User B deletes at user a path A modified, causing conflicts.
util::fs::remove_file(&modify_path_b)?;
let files = util::fs::rlist_paths_in_dir(
&user_b_repo.path.join("annotations").join("train"),
);
for item in files {
log::debug!("\npost file or dir: {:?}\n", item)
}
command::add(&user_b_repo, &modify_path_b)?;
// also add a file
// test::write_txt_file_to_path(&add_path_b, "new file")?;
// command::add(&user_b_repo, &add_path_b)?;
// Before this commit, init a reader at b's head
let pre_b = CommitEntryReader::new_from_head(&user_b_repo)?;
// get head commit
let head = api::local::commits::head_commit(&user_b_repo)?;
log::debug!("b head before is {:?}", head);
let maybe_b_entry = pre_b.get_entry(
&PathBuf::from("annotations")
.join("train")
.join("annotations.txt"),
)?;
log::debug!("maybe_b_entry before commit is {:?}", maybe_b_entry);
let commit_b = command::commit(&user_b_repo, "user B deleting file path.")?;
let post_b = CommitEntryReader::new_from_head(&user_b_repo)?;
let maybe_b_entry = post_b.get_entry(
&PathBuf::from("annotations")
.join("train")
.join("annotations.txt"),
)?;
log::debug!("maybe_b_entry after commitis {:?}", maybe_b_entry);
log::debug!("commit_a is {:?}", commit_a);
log::debug!("commit_b is {:?}", commit_b);
let commit_a =
api::local::commits::get_by_id(&user_a_repo, &commit_a.id)?.unwrap();
let commit_b =
api::local::commits::get_by_id(&user_b_repo, &commit_b.id)?.unwrap();
log::debug!("commit_a pre is {:?}", commit_a);
log::debug!("commit_b pre is {:?}", commit_b);
// Push should fail
let res = command::push(&user_b_repo).await;
log::debug!("here's the result and why it failed: {:?}", res);
assert!(res.is_err());
Ok(user_b_repo_dir_copy)
})
.await?;
Ok(user_a_repo_dir_copy)
})
.await?;
Ok(remote_repo_copy)
})
.await
}
#[tokio::test]
async fn test_tree_can_push_tree_no_conflict_deleted_file() -> 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.clone();
let user_a_repo =
command::clone_url(&remote_repo.remote.url, &user_a_repo_dir.join("new_repo"))
.await?;
// Log out all files in this directory with fs
let files = util::fs::rlist_paths_in_dir(&user_a_repo_dir);
for item in files {
log::debug!("\nfile or dir: {:?}\n", item)
}
// User A: add files in `nlp`
// User B: add files in `annotations`
// 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.clone();
let user_b_repo = command::clone_url(
&remote_repo.remote.url,
&user_b_repo_dir.join("new_repo"),
)
.await?;
// User A adds a file and pushes
let modify_path_a = user_a_repo
.path
.join("annotations")
.join("train")
.join("averynewfile.txt");
let modify_path_b = user_b_repo
.path
.join("annotations")
.join("train")
.join("anothernewfile.txt");
test::write_txt_file_to_path(&modify_path_a, "new file")?;
command::add(&user_a_repo, &modify_path_a)?;
command::commit(&user_a_repo, "Adding first file path.")?;
command::push(&user_a_repo).await?;
// User B adds a different file and pushe
test::write_txt_file_to_path(&modify_path_b, "newer file")?;
command::add(&user_b_repo, &modify_path_b)?;
command::commit(&user_b_repo, "User B adding second file path.")?;
// Push should succeed - different dirs!
command::push(&user_b_repo).await?;
Ok(user_b_repo_dir_copy)
})
.await?;
Ok(user_a_repo_dir_copy)
})
.await?;
Ok(remote_repo_copy)
})
.await
}
#[tokio::test]
async fn test_tree_merge_on_push_to_branch() -> Result<(), OxenError> {
let new_branch = "new_branch";
// 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.clone();
let user_a_repo = command::deep_clone_url(
&remote_repo.remote.url,
&user_a_repo_dir.join("new_repo"),
)
.await?;
// Save the current head of main
let main_head = api::local::commits::head_commit(&user_a_repo)?;
// User a checkout a branch
command::create_checkout(&user_a_repo, new_branch)?;
// 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.clone();
let user_b_repo = command::deep_clone_url(
&remote_repo.remote.url,
&user_b_repo_dir.join("new_repo"),
)
.await?;
// User b checkout the same branch
command::create_checkout(&user_b_repo, new_branch)?;
// User A adds a file and pushes
let modify_path_a = user_a_repo
.path
.join("annotations")
.join("train")
.join("averynewfile.txt");
let modify_path_b = user_b_repo
.path
.join("annotations")
.join("train")
.join("anothernewfile.txt");
test::write_txt_file_to_path(&modify_path_a, "new file")?;
command::add(&user_a_repo, &modify_path_a)?;
command::commit(&user_a_repo, "Adding first file path.")?;
command::push(&user_a_repo).await?;
// User B adds a different file and pushe
test::write_txt_file_to_path(&modify_path_b, "newer file")?;
command::add(&user_b_repo, &modify_path_b)?;
command::commit(&user_b_repo, "User B adding second file path.")?;
// Push should succeed - different dirs!
command::push(&user_b_repo).await?;
// Get the new branch head
let new_main =
api::remote::branches::get_by_name(&remote_repo, DEFAULT_BRANCH_NAME)
.await?
.unwrap();
let new_branch = api::remote::branches::get_by_name(&remote_repo, new_branch)
.await?
.unwrap();
// Assert commits have updated in the right place
assert_eq!(new_main.commit_id, main_head.id);
// Head at new_branch should be a merge commit
let new_branch_head =
api::remote::commits::get_by_id(&remote_repo, &new_branch.commit_id)
.await?
.unwrap();
// Must be a merge commit
assert_eq!(new_branch_head.parent_ids.len(), 2);
Ok(user_b_repo_dir_copy)
})
.await?;
Ok(user_a_repo_dir_copy)
})
.await?;
Ok(remote_repo_copy)
})
.await
}
}