1use std::{
2 ffi::OsStr,
3 path::{Path, PathBuf},
4 process::{Child, Command, Stdio},
5};
6
7use super::{ExclusiveOption, SubCommand, Unselected};
8
9use crate::global::GlobalOpts;
10use crate::spawn::ParameterizedSpawn;
11
12#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
21pub struct BranchReverse;
22
23#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct Branch<B = Unselected> {
30 branch: String,
31 reverse: B,
32}
33
34impl ExclusiveOption for Branch<Unselected> {
35 fn inject_args(&self, command: &mut Command) {
36 command.arg("-b").arg(&self.branch);
37 }
38}
39
40impl ExclusiveOption for Branch<BranchReverse> {
41 fn inject_args(&self, command: &mut Command) {
42 command.arg("-b").arg(&self.branch).arg("-r");
43 }
44}
45
46#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct BranchOverride {
52 branch: String,
53 source: String,
54}
55
56impl ExclusiveOption for BranchOverride {
57 fn inject_args(&self, command: &mut Command) {
58 command
59 .arg("-b")
60 .arg(&self.branch)
61 .arg("-s")
62 .arg(&self.source);
63 }
64}
65
66#[cfg(not(feature = "lt2020_2"))]
68#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
69pub struct CopyFilesOnly;
70
71#[cfg(not(feature = "lt2020_2"))]
73#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
74pub struct CopyStreamSpecOnly;
75
76#[cfg_attr(
82 not(feature = "lt2020_2"),
83 doc = "The `S` type parameter tracks whether `-Af` (files only) or `-As` (stream spec only) has been supplied: [`Unselected`] copies both; [`CopyFilesOnly`] copies only the files; [`CopyStreamSpecOnly`] copies only the stream spec."
84)]
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub struct Stream<S = Unselected> {
87 stream: String,
88
89 parent: Option<String>,
90
91 reverse: bool,
92
93 force_stream_flow: bool,
94
95 strategy: S,
96}
97
98impl<S> Stream<S> {
99 fn inject_stream_args(&self, command: &mut Command) {
100 command.arg("-S").arg(&self.stream);
101 if let Some(parent) = &self.parent {
102 command.arg("-P").arg(parent);
103 }
104 if self.force_stream_flow {
105 command.arg("-F");
106 }
107 if self.reverse {
108 command.arg("-r");
109 }
110 }
111}
112
113impl ExclusiveOption for Stream<Unselected> {
114 fn inject_args(&self, command: &mut Command) {
115 self.inject_stream_args(command);
116 }
117}
118
119#[cfg(not(feature = "lt2020_2"))]
120impl ExclusiveOption for Stream<CopyFilesOnly> {
121 fn inject_args(&self, command: &mut Command) {
122 command.arg("-Af");
123 self.inject_stream_args(command);
124 }
125}
126
127#[cfg(not(feature = "lt2020_2"))]
128impl ExclusiveOption for Stream<CopyStreamSpecOnly> {
129 fn inject_args(&self, command: &mut Command) {
130 command.arg("-As");
131 self.inject_stream_args(command);
132 }
133}
134
135#[cfg_attr(
164 feature = "lt2020_2",
165 doc = "- [`Stream`] (entered via [`Self::stream`]): use a stream's generated branch view."
166)]
167#[cfg_attr(
168 not(feature = "lt2020_2"),
169 doc = "- [`Stream`] (entered via [`Self::stream`]): use a stream's generated branch view; `Copy::files_only` supplies `-Af` and `Copy::stream_spec_only` supplies `-As`, each transitioning to the corresponding state."
170)]
171#[derive(Debug, Clone, Default)]
172pub struct Copy<M = Unselected> {
173 bin: PathBuf,
174
175 global_opts: GlobalOpts,
176
177 changelist: Option<String>,
178
179 limit: Option<u64>,
180
181 preview: bool,
182
183 force: bool,
184
185 quiet: bool,
186
187 virtual_integration: bool,
188
189 #[cfg(not(feature = "lt2022_1"))]
190 suppress_keyword_expansion: bool,
191
192 mode: M,
193}
194
195impl Copy<Unselected> {
200 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
208 Self {
209 bin: bin.into(),
210 global_opts,
211 ..Default::default()
212 }
213 }
214
215 pub fn branch(self, v: impl Into<String>) -> Copy<Branch<Unselected>> {
221 Copy {
222 bin: self.bin,
223 global_opts: self.global_opts,
224 changelist: self.changelist,
225 limit: self.limit,
226 preview: self.preview,
227 force: self.force,
228 quiet: self.quiet,
229 virtual_integration: self.virtual_integration,
230 #[cfg(not(feature = "lt2022_1"))]
231 suppress_keyword_expansion: self.suppress_keyword_expansion,
232 mode: Branch {
233 branch: v.into(),
234 reverse: Unselected,
235 },
236 }
237 }
238
239 #[cfg_attr(feature = "lt2020_2", doc = "Uses the stream's generated branch view.")]
243 #[cfg_attr(
244 not(feature = "lt2020_2"),
245 doc = "Uses a generated branch view that maps the stream to its parent. Use `Copy::files_only` (`-Af`) or `Copy::stream_spec_only` (`-As`) to restrict what is copied."
246 )]
247 pub fn stream(self, v: impl Into<String>) -> Copy<Stream<Unselected>> {
248 Copy {
249 bin: self.bin,
250 global_opts: self.global_opts,
251 changelist: self.changelist,
252 limit: self.limit,
253 preview: self.preview,
254 force: self.force,
255 quiet: self.quiet,
256 virtual_integration: self.virtual_integration,
257 #[cfg(not(feature = "lt2022_1"))]
258 suppress_keyword_expansion: self.suppress_keyword_expansion,
259 mode: Stream {
260 stream: v.into(),
261 parent: None,
262 reverse: false,
263 force_stream_flow: false,
264 strategy: Unselected,
265 },
266 }
267 }
268}
269
270impl<M> Copy<M> {
275 pub fn get_bin(&self) -> &Path {
277 &self.bin
278 }
279
280 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
282 self.bin = bin.into();
283 self
284 }
285
286 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
288 self.set_bin(bin);
289 self
290 }
291
292 #[cfg_attr(
297 feature = "lt2014_2",
298 doc = "See the [Global Options](GlobalOpts) section."
299 )]
300 #[cfg_attr(
301 all(feature = "lt2015_1", not(feature = "lt2014_2")),
302 doc = "See the [“Global Options”](GlobalOpts) section."
303 )]
304 #[cfg_attr(
305 all(feature = "lt2017_1", not(feature = "lt2015_1")),
306 doc = "See [“Global Options”](GlobalOpts)."
307 )]
308 #[cfg_attr(
309 all(feature = "lt2018_2", not(feature = "lt2017_1")),
310 doc = "See [Global Options](GlobalOpts)."
311 )]
312 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
313 pub fn get_global_opts(&self) -> &GlobalOpts {
314 &self.global_opts
315 }
316
317 #[cfg_attr(
322 feature = "lt2014_2",
323 doc = "See the [Global Options](GlobalOpts) section."
324 )]
325 #[cfg_attr(
326 all(feature = "lt2015_1", not(feature = "lt2014_2")),
327 doc = "See the [“Global Options”](GlobalOpts) section."
328 )]
329 #[cfg_attr(
330 all(feature = "lt2017_1", not(feature = "lt2015_1")),
331 doc = "See [“Global Options”](GlobalOpts)."
332 )]
333 #[cfg_attr(
334 all(feature = "lt2018_2", not(feature = "lt2017_1")),
335 doc = "See [Global Options](GlobalOpts)."
336 )]
337 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
338 pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
339 self.global_opts = v;
340 self
341 }
342
343 #[cfg_attr(
348 feature = "lt2014_2",
349 doc = "See the [Global Options](GlobalOpts) section."
350 )]
351 #[cfg_attr(
352 all(feature = "lt2015_1", not(feature = "lt2014_2")),
353 doc = "See the [“Global Options”](GlobalOpts) section."
354 )]
355 #[cfg_attr(
356 all(feature = "lt2017_1", not(feature = "lt2015_1")),
357 doc = "See [“Global Options”](GlobalOpts)."
358 )]
359 #[cfg_attr(
360 all(feature = "lt2018_2", not(feature = "lt2017_1")),
361 doc = "See [Global Options](GlobalOpts)."
362 )]
363 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
364 pub fn global_opts(mut self, v: GlobalOpts) -> Self {
365 self.global_opts = v;
366 self
367 }
368
369 pub fn get_changelist(&self) -> Option<&String> {
376 self.changelist.as_ref()
377 }
378
379 pub fn set_changelist(&mut self, v: impl Into<String>) -> &mut Self {
386 self.changelist = Some(v.into());
387 self
388 }
389
390 pub fn changelist(mut self, v: impl Into<String>) -> Self {
397 self.changelist = Some(v.into());
398 self
399 }
400
401 pub fn get_limit(&self) -> Option<u64> {
408 self.limit
409 }
410
411 pub fn set_limit(&mut self, v: u64) -> &mut Self {
418 self.limit = Some(v);
419 self
420 }
421
422 pub fn limit(mut self, v: u64) -> Self {
429 self.limit = Some(v);
430 self
431 }
432
433 pub fn get_preview(&self) -> bool {
439 self.preview
440 }
441
442 pub fn set_preview(&mut self, v: bool) -> &mut Self {
448 self.preview = v;
449 self
450 }
451
452 pub fn preview(mut self, v: bool) -> Self {
458 self.preview = v;
459 self
460 }
461
462 pub fn get_force(&self) -> bool {
471 self.force
472 }
473
474 pub fn set_force(&mut self, v: bool) -> &mut Self {
483 self.force = v;
484 self
485 }
486
487 pub fn force(mut self, v: bool) -> Self {
496 self.force = v;
497 self
498 }
499
500 #[cfg_attr(
505 feature = "lt2018_1",
506 doc = "Quiet mode; suppress normal output messages about the list of",
507 doc = "files being integrated, copied, or merged. Messages regarding",
508 doc = "errors or exceptional conditions are displayed."
509 )]
510 #[cfg_attr(
511 not(feature = "lt2018_1"),
512 doc = "Quiet mode, which suppresses normal output messages about the list of",
513 doc = "files being integrated, copied, or merged. Messages regarding errors",
514 doc = "or exceptional conditions are displayed."
515 )]
516 pub fn get_quiet(&self) -> bool {
517 self.quiet
518 }
519
520 #[cfg_attr(
525 feature = "lt2018_1",
526 doc = "Quiet mode; suppress normal output messages about the list of",
527 doc = "files being integrated, copied, or merged. Messages regarding",
528 doc = "errors or exceptional conditions are displayed."
529 )]
530 #[cfg_attr(
531 not(feature = "lt2018_1"),
532 doc = "Quiet mode, which suppresses normal output messages about the list of",
533 doc = "files being integrated, copied, or merged. Messages regarding errors",
534 doc = "or exceptional conditions are displayed."
535 )]
536 pub fn set_quiet(&mut self, v: bool) -> &mut Self {
537 self.quiet = v;
538 self
539 }
540
541 #[cfg_attr(
546 feature = "lt2018_1",
547 doc = "Quiet mode; suppress normal output messages about the list of",
548 doc = "files being integrated, copied, or merged. Messages regarding",
549 doc = "errors or exceptional conditions are displayed."
550 )]
551 #[cfg_attr(
552 not(feature = "lt2018_1"),
553 doc = "Quiet mode, which suppresses normal output messages about the list of",
554 doc = "files being integrated, copied, or merged. Messages regarding errors",
555 doc = "or exceptional conditions are displayed."
556 )]
557 pub fn quiet(mut self, v: bool) -> Self {
558 self.quiet = v;
559 self
560 }
561
562 #[cfg_attr(
567 feature = "lt2025_2",
568 doc = "Do not sync the target files. By default, p4 copy syncs the target files.",
569 doc = "If a large number of files is involved and you do not require",
570 doc = "the files to be present in your workspace, you can minimize",
571 doc = "overhead and network traffic by specifying `-v`."
572 )]
573 #[cfg_attr(
574 not(feature = "lt2025_2"),
575 doc = "Performs a \"virtual\" integration that does not modify client workspace",
576 doc = "files unless target files need to be resolved. After submitting a",
577 doc = "\"virtual integration\", `p4 sync` can be used to update the workspace."
578 )]
579 pub fn get_virtual_integration(&self) -> bool {
580 self.virtual_integration
581 }
582
583 #[cfg_attr(
588 feature = "lt2025_2",
589 doc = "Do not sync the target files. By default, p4 copy syncs the target files.",
590 doc = "If a large number of files is involved and you do not require",
591 doc = "the files to be present in your workspace, you can minimize",
592 doc = "overhead and network traffic by specifying `-v`."
593 )]
594 #[cfg_attr(
595 not(feature = "lt2025_2"),
596 doc = "Performs a \"virtual\" integration that does not modify client workspace",
597 doc = "files unless target files need to be resolved. After submitting a",
598 doc = "\"virtual integration\", `p4 sync` can be used to update the workspace."
599 )]
600 pub fn set_virtual_integration(&mut self, v: bool) -> &mut Self {
601 self.virtual_integration = v;
602 self
603 }
604
605 #[cfg_attr(
610 feature = "lt2025_2",
611 doc = "Do not sync the target files. By default, p4 copy syncs the target files.",
612 doc = "If a large number of files is involved and you do not require",
613 doc = "the files to be present in your workspace, you can minimize",
614 doc = "overhead and network traffic by specifying `-v`."
615 )]
616 #[cfg_attr(
617 not(feature = "lt2025_2"),
618 doc = "Performs a \"virtual\" integration that does not modify client workspace",
619 doc = "files unless target files need to be resolved. After submitting a",
620 doc = "\"virtual integration\", `p4 sync` can be used to update the workspace."
621 )]
622 pub fn virtual_integration(mut self, v: bool) -> Self {
623 self.virtual_integration = v;
624 self
625 }
626
627 #[cfg(not(feature = "lt2022_1"))]
633 pub fn get_suppress_keyword_expansion(&self) -> bool {
634 self.suppress_keyword_expansion
635 }
636
637 #[cfg(not(feature = "lt2022_1"))]
643 pub fn set_suppress_keyword_expansion(&mut self, v: bool) -> &mut Self {
644 self.suppress_keyword_expansion = v;
645 self
646 }
647
648 #[cfg(not(feature = "lt2022_1"))]
654 pub fn suppress_keyword_expansion(mut self, v: bool) -> Self {
655 self.suppress_keyword_expansion = v;
656 self
657 }
658}
659
660impl Copy<Branch<Unselected>> {
665 pub fn get_branch(&self) -> &str {
671 &self.mode.branch
672 }
673
674 pub fn set_branch(&mut self, v: impl Into<String>) -> &mut Self {
680 self.mode.branch = v.into();
681 self
682 }
683
684 pub fn branch(mut self, v: impl Into<String>) -> Self {
690 self.mode.branch = v.into();
691 self
692 }
693
694 #[cfg_attr(feature = "lt2014_2", doc = "Requires the `-b` flag.")]
701 #[cfg_attr(not(feature = "lt2014_2"), doc = "Requires the `-b` option.")]
702 pub fn reverse(self) -> Copy<Branch<BranchReverse>> {
705 Copy {
706 bin: self.bin,
707 global_opts: self.global_opts,
708 changelist: self.changelist,
709 limit: self.limit,
710 preview: self.preview,
711 force: self.force,
712 quiet: self.quiet,
713 virtual_integration: self.virtual_integration,
714 #[cfg(not(feature = "lt2022_1"))]
715 suppress_keyword_expansion: self.suppress_keyword_expansion,
716 mode: Branch {
717 branch: self.mode.branch,
718 reverse: BranchReverse,
719 },
720 }
721 }
722
723 #[cfg_attr(
731 feature = "lt2014_2",
732 doc = "Overrides the `-r` flag, if specified. Requires `-b`."
733 )]
734 #[cfg_attr(
735 not(feature = "lt2014_2"),
736 doc = "Overrides the `-r` option, if specified. Requires `-b`."
737 )]
738 pub fn source(self, v: impl Into<String>) -> Copy<BranchOverride> {
741 Copy {
742 bin: self.bin,
743 global_opts: self.global_opts,
744 changelist: self.changelist,
745 limit: self.limit,
746 preview: self.preview,
747 force: self.force,
748 quiet: self.quiet,
749 virtual_integration: self.virtual_integration,
750 #[cfg(not(feature = "lt2022_1"))]
751 suppress_keyword_expansion: self.suppress_keyword_expansion,
752 mode: BranchOverride {
753 branch: self.mode.branch,
754 source: v.into(),
755 },
756 }
757 }
758}
759
760impl Copy<Branch<BranchReverse>> {
765 pub fn get_branch(&self) -> &str {
771 &self.mode.branch
772 }
773
774 pub fn set_branch(&mut self, v: impl Into<String>) -> &mut Self {
780 self.mode.branch = v.into();
781 self
782 }
783
784 pub fn branch(mut self, v: impl Into<String>) -> Self {
790 self.mode.branch = v.into();
791 self
792 }
793}
794
795impl Copy<BranchOverride> {
800 pub fn get_branch(&self) -> &str {
806 &self.mode.branch
807 }
808
809 pub fn set_branch(&mut self, v: impl Into<String>) -> &mut Self {
815 self.mode.branch = v.into();
816 self
817 }
818
819 pub fn branch(mut self, v: impl Into<String>) -> Self {
825 self.mode.branch = v.into();
826 self
827 }
828
829 #[cfg_attr(
837 feature = "lt2014_2",
838 doc = "Overrides the `-r` flag, if specified. Requires `-b`."
839 )]
840 #[cfg_attr(
841 not(feature = "lt2014_2"),
842 doc = "Overrides the `-r` option, if specified. Requires `-b`."
843 )]
844 pub fn get_source(&self) -> &str {
845 &self.mode.source
846 }
847
848 #[cfg_attr(
856 feature = "lt2014_2",
857 doc = "Overrides the `-r` flag, if specified. Requires `-b`."
858 )]
859 #[cfg_attr(
860 not(feature = "lt2014_2"),
861 doc = "Overrides the `-r` option, if specified. Requires `-b`."
862 )]
863 pub fn set_source(&mut self, v: impl Into<String>) -> &mut Self {
864 self.mode.source = v.into();
865 self
866 }
867
868 #[cfg_attr(
876 feature = "lt2014_2",
877 doc = "Overrides the `-r` flag, if specified. Requires `-b`."
878 )]
879 #[cfg_attr(
880 not(feature = "lt2014_2"),
881 doc = "Overrides the `-r` option, if specified. Requires `-b`."
882 )]
883 pub fn source(mut self, v: impl Into<String>) -> Self {
884 self.mode.source = v.into();
885 self
886 }
887}
888
889impl<S> Copy<Stream<S>> {
894 #[cfg_attr(
899 feature = "lt2014_2",
900 doc = "Specify the source stream. Changes are copied to its parent. You can use the",
901 doc = "`-r` flag to reverse direction. To submit copied stream files,",
902 doc = "the current client must be switched to the target stream or to a virtual child",
903 doc = "stream of the target stream."
904 )]
905 #[cfg_attr(
906 all(feature = "lt2020_2", not(feature = "lt2014_2")),
907 doc = "Specify the source stream. Changes are copied to its parent. You",
908 doc = "can use the `-r` option to reverse direction. To",
909 doc = "submit copied stream files, the current client must be switched",
910 doc = "to the target stream or to a virtual child stream of the target",
911 doc = "stream."
912 )]
913 #[cfg_attr(
914 not(feature = "lt2020_2"),
915 doc = "Causes p4 copy to use a generated branch view that maps the stream to its parent and copy from a stream to its parent. To reverse the copy direction, use `-r` with `-S`.",
916 doc = "",
917 doc = "Copies the source stream spec propagatable fields into the target stream spec and opens for edit both the target stream spec and the stream files."
918 )]
919 pub fn get_stream(&self) -> &str {
920 &self.mode.stream
921 }
922
923 #[cfg_attr(
928 feature = "lt2014_2",
929 doc = "Specify the source stream. Changes are copied to its parent. You can use the",
930 doc = "`-r` flag to reverse direction. To submit copied stream files,",
931 doc = "the current client must be switched to the target stream or to a virtual child",
932 doc = "stream of the target stream."
933 )]
934 #[cfg_attr(
935 all(feature = "lt2020_2", not(feature = "lt2014_2")),
936 doc = "Specify the source stream. Changes are copied to its parent. You",
937 doc = "can use the `-r` option to reverse direction. To",
938 doc = "submit copied stream files, the current client must be switched",
939 doc = "to the target stream or to a virtual child stream of the target",
940 doc = "stream."
941 )]
942 #[cfg_attr(
943 not(feature = "lt2020_2"),
944 doc = "Causes p4 copy to use a generated branch view that maps the stream to its parent and copy from a stream to its parent. To reverse the copy direction, use `-r` with `-S`.",
945 doc = "",
946 doc = "Copies the source stream spec propagatable fields into the target stream spec and opens for edit both the target stream spec and the stream files."
947 )]
948 pub fn set_stream(&mut self, v: impl Into<String>) -> &mut Self {
949 self.mode.stream = v.into();
950 self
951 }
952
953 #[cfg_attr(
958 feature = "lt2014_2",
959 doc = "Specify the source stream. Changes are copied to its parent. You can use the",
960 doc = "`-r` flag to reverse direction. To submit copied stream files,",
961 doc = "the current client must be switched to the target stream or to a virtual child",
962 doc = "stream of the target stream."
963 )]
964 #[cfg_attr(
965 all(feature = "lt2020_2", not(feature = "lt2014_2")),
966 doc = "Specify the source stream. Changes are copied to its parent. You",
967 doc = "can use the `-r` option to reverse direction. To",
968 doc = "submit copied stream files, the current client must be switched",
969 doc = "to the target stream or to a virtual child stream of the target",
970 doc = "stream."
971 )]
972 #[cfg_attr(
973 not(feature = "lt2020_2"),
974 doc = "Causes p4 copy to use a generated branch view that maps the stream to its parent and copy from a stream to its parent. To reverse the copy direction, use `-r` with `-S`.",
975 doc = "",
976 doc = "Copies the source stream spec propagatable fields into the target stream spec and opens for edit both the target stream spec and the stream files."
977 )]
978 pub fn stream(mut self, v: impl Into<String>) -> Self {
979 self.mode.stream = v.into();
980 self
981 }
982
983 pub fn get_parent(&self) -> Option<&String> {
990 self.mode.parent.as_ref()
991 }
992
993 pub fn set_parent(&mut self, v: impl Into<String>) -> &mut Self {
1000 self.mode.parent = Some(v.into());
1001 self
1002 }
1003
1004 pub fn parent(mut self, v: impl Into<String>) -> Self {
1011 self.mode.parent = Some(v.into());
1012 self
1013 }
1014
1015 pub fn get_reverse(&self) -> bool {
1022 self.mode.reverse
1023 }
1024
1025 pub fn set_reverse(&mut self, v: bool) -> &mut Self {
1032 self.mode.reverse = v;
1033 self
1034 }
1035
1036 pub fn reverse(mut self, v: bool) -> Self {
1043 self.mode.reverse = v;
1044 self
1045 }
1046
1047 pub fn get_force_stream_flow(&self) -> bool {
1054 self.mode.force_stream_flow
1055 }
1056
1057 pub fn set_force_stream_flow(&mut self, v: bool) -> &mut Self {
1064 self.mode.force_stream_flow = v;
1065 self
1066 }
1067
1068 pub fn force_stream_flow(mut self, v: bool) -> Self {
1075 self.mode.force_stream_flow = v;
1076 self
1077 }
1078}
1079
1080#[cfg(not(feature = "lt2020_2"))]
1082impl Copy<Stream<Unselected>> {
1083 pub fn files_only(self) -> Copy<Stream<CopyFilesOnly>> {
1091 Copy {
1092 bin: self.bin,
1093 global_opts: self.global_opts,
1094 changelist: self.changelist,
1095 limit: self.limit,
1096 preview: self.preview,
1097 force: self.force,
1098 quiet: self.quiet,
1099 virtual_integration: self.virtual_integration,
1100 #[cfg(not(feature = "lt2022_1"))]
1101 suppress_keyword_expansion: self.suppress_keyword_expansion,
1102 mode: Stream {
1103 stream: self.mode.stream,
1104 parent: self.mode.parent,
1105 reverse: self.mode.reverse,
1106 force_stream_flow: self.mode.force_stream_flow,
1107 strategy: CopyFilesOnly,
1108 },
1109 }
1110 }
1111
1112 pub fn stream_spec_only(self) -> Copy<Stream<CopyStreamSpecOnly>> {
1120 Copy {
1121 bin: self.bin,
1122 global_opts: self.global_opts,
1123 changelist: self.changelist,
1124 limit: self.limit,
1125 preview: self.preview,
1126 force: self.force,
1127 quiet: self.quiet,
1128 virtual_integration: self.virtual_integration,
1129 #[cfg(not(feature = "lt2022_1"))]
1130 suppress_keyword_expansion: self.suppress_keyword_expansion,
1131 mode: Stream {
1132 stream: self.mode.stream,
1133 parent: self.mode.parent,
1134 reverse: self.mode.reverse,
1135 force_stream_flow: self.mode.force_stream_flow,
1136 strategy: CopyStreamSpecOnly,
1137 },
1138 }
1139 }
1140}
1141
1142impl<M: ExclusiveOption> SubCommand for Copy<M> {
1147 fn name(&self) -> &str {
1148 "copy"
1149 }
1150
1151 fn inject_local_args(&self, command: &mut Command) {
1152 if let Some(changelist) = &self.changelist {
1153 command.arg("-c").arg(changelist);
1154 }
1155
1156 if self.preview {
1157 command.arg("-n");
1158 }
1159
1160 if self.force {
1161 command.arg("-f");
1162 }
1163
1164 #[cfg(not(feature = "lt2022_1"))]
1165 if self.suppress_keyword_expansion {
1166 command.arg("-K");
1167 }
1168
1169 if self.virtual_integration {
1170 command.arg("-v");
1171 }
1172
1173 if self.quiet {
1174 command.arg("-q");
1175 }
1176
1177 if let Some(limit) = self.limit {
1178 command.arg("-m").arg(limit.to_string());
1179 }
1180
1181 self.mode.inject_args(command);
1182 }
1183
1184 fn global_opts(&self) -> Option<&GlobalOpts> {
1185 Some(&self.global_opts)
1186 }
1187}
1188
1189impl<A, B> ParameterizedSpawn<(A, B)> for Copy<Unselected>
1195where
1196 A: AsRef<OsStr>,
1197 B: AsRef<OsStr>,
1198{
1199 type Output = Child;
1200 type Error = std::io::Error;
1201
1202 fn spawn_with(&mut self, (from_file, to_file): (A, B)) -> Result<Self::Output, Self::Error> {
1207 self.setup_command(&self.bin)
1208 .arg(from_file)
1209 .arg(to_file)
1210 .stdout(Stdio::piped())
1211 .stderr(Stdio::piped())
1212 .spawn()
1213 }
1214}
1215
1216impl<B> ParameterizedSpawn<()> for Copy<Branch<B>>
1218where
1219 Branch<B>: ExclusiveOption,
1220{
1221 type Output = Child;
1222 type Error = std::io::Error;
1223
1224 fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
1228 self.setup_command(&self.bin)
1229 .stdout(Stdio::piped())
1230 .stderr(Stdio::piped())
1231 .spawn()
1232 }
1233}
1234
1235impl<B, S, I> ParameterizedSpawn<(S,)> for Copy<Branch<B>>
1236where
1237 Branch<B>: ExclusiveOption,
1238 S: IntoIterator<Item = I>,
1239 I: AsRef<OsStr>,
1240{
1241 type Output = Child;
1242 type Error = std::io::Error;
1243
1244 fn spawn_with(&mut self, (to_files,): (S,)) -> Result<Self::Output, Self::Error> {
1248 self.setup_command(&self.bin)
1249 .args(to_files)
1250 .stdout(Stdio::piped())
1251 .stderr(Stdio::piped())
1252 .spawn()
1253 }
1254}
1255
1256impl ParameterizedSpawn<()> for Copy<BranchOverride> {
1258 type Output = Child;
1259 type Error = std::io::Error;
1260
1261 fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
1266 self.setup_command(&self.bin)
1267 .stdout(Stdio::piped())
1268 .stderr(Stdio::piped())
1269 .spawn()
1270 }
1271}
1272
1273impl<S, I> ParameterizedSpawn<(S,)> for Copy<BranchOverride>
1274where
1275 S: IntoIterator<Item = I>,
1276 I: AsRef<OsStr>,
1277{
1278 type Output = Child;
1279 type Error = std::io::Error;
1280
1281 fn spawn_with(&mut self, (to_files,): (S,)) -> Result<Self::Output, Self::Error> {
1286 self.setup_command(&self.bin)
1287 .args(to_files)
1288 .stdout(Stdio::piped())
1289 .stderr(Stdio::piped())
1290 .spawn()
1291 }
1292}
1293
1294impl<S> ParameterizedSpawn<()> for Copy<Stream<S>>
1296where
1297 Stream<S>: ExclusiveOption,
1298{
1299 type Output = Child;
1300 type Error = std::io::Error;
1301
1302 fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
1306 self.setup_command(&self.bin)
1307 .stdout(Stdio::piped())
1308 .stderr(Stdio::piped())
1309 .spawn()
1310 }
1311}
1312
1313impl<S, T, I> ParameterizedSpawn<(T,)> for Copy<Stream<S>>
1314where
1315 Stream<S>: ExclusiveOption,
1316 T: IntoIterator<Item = I>,
1317 I: AsRef<OsStr>,
1318{
1319 type Output = Child;
1320 type Error = std::io::Error;
1321
1322 fn spawn_with(&mut self, (to_files,): (T,)) -> Result<Self::Output, Self::Error> {
1327 self.setup_command(&self.bin)
1328 .args(to_files)
1329 .stdout(Stdio::piped())
1330 .stderr(Stdio::piped())
1331 .spawn()
1332 }
1333}
1334
1335#[cfg(test)]
1336mod tests {
1337 use super::*;
1338 use crate::cmd::args_of;
1339
1340 #[test]
1341 fn bin_accessors_update_executable_path() {
1342 let mut command = Copy::new("p4", GlobalOpts::new());
1343 assert_eq!(command.get_bin(), Path::new("p4"));
1344
1345 command.set_bin("/usr/local/bin/p4");
1346 assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
1347
1348 let command = command.bin("p4.exe");
1349 assert_eq!(command.get_bin(), Path::new("p4.exe"));
1350 }
1351
1352 #[cfg(not(feature = "lt2022_1"))]
1353 #[test]
1354 fn direct_form_assembles_options_and_args() {
1355 let copy = Copy::new("p4", GlobalOpts::new())
1356 .changelist("123")
1357 .limit(10)
1358 .preview(true)
1359 .force(true)
1360 .quiet(true)
1361 .virtual_integration(true)
1362 .suppress_keyword_expansion(true);
1363 let mut cmd = copy.setup_command("p4");
1364 cmd.arg("//depot/main/...").arg("//depot/dev/...");
1365 assert_eq!(
1366 args_of(&cmd),
1367 [
1368 "copy",
1369 "-c",
1370 "123",
1371 "-n",
1372 "-f",
1373 "-K",
1374 "-v",
1375 "-q",
1376 "-m",
1377 "10",
1378 "//depot/main/...",
1379 "//depot/dev/..."
1380 ]
1381 );
1382 }
1383
1384 #[cfg(feature = "lt2022_1")]
1385 #[test]
1386 fn direct_form_assembles_options_and_args() {
1387 let copy = Copy::new("p4", GlobalOpts::new())
1388 .changelist("123")
1389 .limit(10)
1390 .preview(true)
1391 .force(true)
1392 .quiet(true)
1393 .virtual_integration(true);
1394 let mut cmd = copy.setup_command("p4");
1395 cmd.arg("//depot/main/...").arg("//depot/dev/...");
1396 assert_eq!(
1397 args_of(&cmd),
1398 [
1399 "copy",
1400 "-c",
1401 "123",
1402 "-n",
1403 "-f",
1404 "-v",
1405 "-q",
1406 "-m",
1407 "10",
1408 "//depot/main/...",
1409 "//depot/dev/..."
1410 ]
1411 );
1412 }
1413
1414 #[test]
1415 fn branch_form() {
1416 let copy = Copy::new("p4", GlobalOpts::new()).branch("mybranch");
1417 assert_eq!(
1418 args_of(©.setup_command("p4")),
1419 ["copy", "-b", "mybranch"]
1420 );
1421 }
1422
1423 #[test]
1424 fn branch_reverse_form() {
1425 let copy = Copy::new("p4", GlobalOpts::new())
1426 .branch("mybranch")
1427 .reverse();
1428 assert_eq!(
1429 args_of(©.setup_command("p4")),
1430 ["copy", "-b", "mybranch", "-r"]
1431 );
1432 }
1433
1434 #[test]
1435 fn branch_override_form() {
1436 let copy = Copy::new("p4", GlobalOpts::new())
1437 .branch("mybranch")
1438 .source("//depot/main/...");
1439 assert_eq!(
1440 args_of(©.setup_command("p4")),
1441 ["copy", "-b", "mybranch", "-s", "//depot/main/..."]
1442 );
1443 }
1444
1445 #[test]
1446 fn stream_form() {
1447 let copy = Copy::new("p4", GlobalOpts::new()).stream("//stream/dev");
1448 assert_eq!(
1449 args_of(©.setup_command("p4")),
1450 ["copy", "-S", "//stream/dev"]
1451 );
1452 }
1453
1454 #[test]
1455 fn stream_with_parent_and_flags() {
1456 let copy = Copy::new("p4", GlobalOpts::new())
1457 .stream("//stream/dev")
1458 .parent("//stream/main")
1459 .reverse(true)
1460 .force_stream_flow(true);
1461 assert_eq!(
1462 args_of(©.setup_command("p4")),
1463 [
1464 "copy",
1465 "-S",
1466 "//stream/dev",
1467 "-P",
1468 "//stream/main",
1469 "-F",
1470 "-r"
1471 ]
1472 );
1473 }
1474
1475 #[cfg(not(feature = "lt2020_2"))]
1476 #[test]
1477 fn stream_files_only() {
1478 let copy = Copy::new("p4", GlobalOpts::new())
1479 .stream("//stream/dev")
1480 .files_only();
1481 assert_eq!(
1482 args_of(©.setup_command("p4")),
1483 ["copy", "-Af", "-S", "//stream/dev"]
1484 );
1485 }
1486
1487 #[cfg(not(feature = "lt2020_2"))]
1488 #[test]
1489 fn stream_spec_only() {
1490 let copy = Copy::new("p4", GlobalOpts::new())
1491 .stream("//stream/dev")
1492 .stream_spec_only();
1493 assert_eq!(
1494 args_of(©.setup_command("p4")),
1495 ["copy", "-As", "-S", "//stream/dev"]
1496 );
1497 }
1498
1499 #[test]
1500 fn common_options_preserved_across_transitions() {
1501 let copy = Copy::new("p4", GlobalOpts::new())
1502 .changelist("5")
1503 .preview(true)
1504 .branch("mybranch");
1505 assert_eq!(
1506 args_of(©.setup_command("p4")),
1507 ["copy", "-c", "5", "-n", "-b", "mybranch"]
1508 );
1509 }
1510
1511 #[test]
1512 fn branch_accessors() {
1513 let mut copy = Copy::new("p4", GlobalOpts::new()).branch("old");
1514 assert_eq!(copy.get_branch(), "old");
1515 copy.set_branch("new");
1516 assert_eq!(copy.get_branch(), "new");
1517 let copy = copy.branch("newer");
1518 assert_eq!(copy.get_branch(), "newer");
1519 }
1520
1521 #[test]
1522 fn stream_accessors() {
1523 let mut copy = Copy::new("p4", GlobalOpts::new())
1524 .stream("//stream/dev")
1525 .parent("//stream/main");
1526 assert_eq!(copy.get_stream(), "//stream/dev");
1527 assert_eq!(copy.get_parent(), Some(&String::from("//stream/main")));
1528 assert!(!copy.get_reverse());
1529 assert!(!copy.get_force_stream_flow());
1530
1531 copy.set_reverse(true);
1532 copy.set_force_stream_flow(true);
1533 assert!(copy.get_reverse());
1534 assert!(copy.get_force_stream_flow());
1535 }
1536
1537 #[test]
1538 fn global_opts_are_injected_once() {
1539 let copy = Copy::new("p4", GlobalOpts::new().port("localhost:1666"));
1540 assert_eq!(
1541 args_of(©.setup_command("p4")),
1542 ["-p", "localhost:1666", "copy"]
1543 );
1544 }
1545}