Skip to main content

perforce_cli/cmd/
copy.rs

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// ---------------------------------------------------------------------------
13// Mode structs
14// ---------------------------------------------------------------------------
15
16/// The `-r` (reverse) state of the branch form.
17///
18/// When selected, the mappings in the branch view are reversed, integrating
19/// from the target files to the source files.
20#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
21pub struct BranchReverse;
22
23/// The branch form: `p4 copy -b branch [-r] [toFile[rev] ...]`.
24///
25/// Uses a branch view to determine source and target files. The `B` type
26/// parameter tracks whether `-r` has been supplied: [`Unselected`] is the
27/// forward direction; [`BranchReverse`] reverses the mapping.
28#[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/// The branch-override form: `p4 copy -b branch -s fromFile[rev] [toFile ...]`.
47///
48/// Treats `fromFile` as the source and both sides of the branch view as the
49/// target. Overrides the `-r` option if specified.
50#[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/// The `-Af` (files only) state of the stream form.
67#[cfg(not(feature = "lt2020_2"))]
68#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
69pub struct CopyFilesOnly;
70
71/// The `-As` (stream spec only) state of the stream form.
72#[cfg(not(feature = "lt2020_2"))]
73#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
74pub struct CopyStreamSpecOnly;
75
76/// The stream form: `p4 copy -S stream [-Af | -As] [-P parent] [-F] [-r]
77/// [toFile[rev] ...]`.
78///
79/// Uses a generated branch view that maps the stream to its parent and copies
80/// from a stream to its parent.
81#[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// ---------------------------------------------------------------------------
136// Command struct
137// ---------------------------------------------------------------------------
138
139/// Type-safe builder for `p4 copy`.
140///
141/// Copy files and/or the stream spec from one location in the depot to
142/// another.
143///
144/// # Syntax
145///
146/// ```text
147/// p4 [g-opts] copy [-c change] [-n -f -K -v -q] [-m max] fromFile[rev] toFile
148/// p4 [g-opts] copy [-c change] [-n -f -K -v -q] [-m max] -b branch [-r] [toFile[rev] ...]
149/// p4 [g-opts] copy [-c change] [-n -f -K -v -q] [-m max] -b branch -s fromFile[rev] [toFile ...]
150/// p4 [g-opts] copy [-c change] [-n -f -K -v -q] [-m max] -S stream [-P parent] [-F] [-r] [toFile[rev] ...]
151/// p4 [g-opts] copy [options] -S stream [-Af | -As] [-P parent] [-F] [-r] [toFile[rev] ...]
152/// ```
153///
154/// The `M` type parameter tracks the command form at compile time, starting
155/// as [`Unselected`] from [`Copy::new`]. The [`Unselected`] state is the
156/// direct form `p4 copy fromFile[rev] toFile`: spawn it with the two file
157/// arguments to copy without a branch or stream mapping. Enter a branch or
158/// stream form with one of [`Self::branch`] or [`Self::stream`]:
159/// - [`Branch`] (entered via [`Self::branch`]): use a branch view;
160///   `Copy::reverse` supplies `-r` and transitions to the [`BranchReverse`]
161///   state; `Copy::source` supplies `-s fromFile[rev]` and transitions to the
162///   [`BranchOverride`] state.
163#[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
195// ---------------------------------------------------------------------------
196// Constructor
197// ---------------------------------------------------------------------------
198
199impl Copy<Unselected> {
200    /// Creates a new `p4 copy` command in the direct form
201    /// `p4 copy fromFile[rev] toFile`.
202    ///
203    /// `bin` is the path to the Perforce command-line executable. In this
204    /// state, spawn with the `fromFile[rev]` and `toFile` arguments via
205    /// [`ParameterizedSpawn::spawn_with`]; use [`Self::branch`] or
206    /// [`Self::stream`] to enter a branch or stream form.
207    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    /// Enters the branch form: `p4 copy -b branch [-r] [toFile[rev] ...]`.
216    ///
217    /// Uses the named branch view to determine source and target files. Use
218    /// `Copy::reverse` to supply `-r`, or `Copy::source` to supply `-s
219    /// fromFile[rev]` (which transitions to the [`BranchOverride`] form).
220    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    /// Enters the stream form: `p4 copy -S stream [-P parent] [-F] [-r]
240    /// [toFile[rev] ...]`.
241    ///
242    #[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
270// ---------------------------------------------------------------------------
271// Shared accessors (available in every mode)
272// ---------------------------------------------------------------------------
273
274impl<M> Copy<M> {
275    /// Get the Perforce command path.
276    pub fn get_bin(&self) -> &Path {
277        &self.bin
278    }
279
280    /// Set the Perforce command path.
281    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
282        self.bin = bin.into();
283        self
284    }
285
286    /// Set the Perforce command path.
287    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
288        self.set_bin(bin);
289        self
290    }
291
292    /// # Description
293    ///
294    /// g-opts
295    ///
296    #[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    /// # Description
318    ///
319    /// g-opts
320    ///
321    #[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    /// # Description
344    ///
345    /// g-opts
346    ///
347    #[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    /// # Description
370    ///
371    /// -c change
372    ///
373    /// Open the files in the specified pending changelist rather than in the
374    /// default changelist.
375    pub fn get_changelist(&self) -> Option<&String> {
376        self.changelist.as_ref()
377    }
378
379    /// # Description
380    ///
381    /// -c change
382    ///
383    /// Open the files in the specified pending changelist rather than in the
384    /// default changelist.
385    pub fn set_changelist(&mut self, v: impl Into<String>) -> &mut Self {
386        self.changelist = Some(v.into());
387        self
388    }
389
390    /// # Description
391    ///
392    /// -c change
393    ///
394    /// Open the files in the specified pending changelist rather than in the
395    /// default changelist.
396    pub fn changelist(mut self, v: impl Into<String>) -> Self {
397        self.changelist = Some(v.into());
398        self
399    }
400
401    /// # Description
402    ///
403    /// -m max
404    ///
405    /// Specify the maximum number of files to copy, to limit the size of the
406    /// operation.
407    pub fn get_limit(&self) -> Option<u64> {
408        self.limit
409    }
410
411    /// # Description
412    ///
413    /// -m max
414    ///
415    /// Specify the maximum number of files to copy, to limit the size of the
416    /// operation.
417    pub fn set_limit(&mut self, v: u64) -> &mut Self {
418        self.limit = Some(v);
419        self
420    }
421
422    /// # Description
423    ///
424    /// -m max
425    ///
426    /// Specify the maximum number of files to copy, to limit the size of the
427    /// operation.
428    pub fn limit(mut self, v: u64) -> Self {
429        self.limit = Some(v);
430        self
431    }
432
433    /// # Description
434    ///
435    /// -n
436    ///
437    /// Preview the copy.
438    pub fn get_preview(&self) -> bool {
439        self.preview
440    }
441
442    /// # Description
443    ///
444    /// -n
445    ///
446    /// Preview the copy.
447    pub fn set_preview(&mut self, v: bool) -> &mut Self {
448        self.preview = v;
449        self
450    }
451
452    /// # Description
453    ///
454    /// -n
455    ///
456    /// Preview the copy.
457    pub fn preview(mut self, v: bool) -> Self {
458        self.preview = v;
459        self
460    }
461
462    /// # Description
463    ///
464    /// -f
465    ///
466    /// Force the creation of extra revisions in order to explicitly record
467    /// that files have been copied. Deleted source files are copied if they do
468    /// not exist in the target, and files that are already identical are
469    /// copied if they are not connected by existing integration records.
470    pub fn get_force(&self) -> bool {
471        self.force
472    }
473
474    /// # Description
475    ///
476    /// -f
477    ///
478    /// Force the creation of extra revisions in order to explicitly record
479    /// that files have been copied. Deleted source files are copied if they do
480    /// not exist in the target, and files that are already identical are
481    /// copied if they are not connected by existing integration records.
482    pub fn set_force(&mut self, v: bool) -> &mut Self {
483        self.force = v;
484        self
485    }
486
487    /// # Description
488    ///
489    /// -f
490    ///
491    /// Force the creation of extra revisions in order to explicitly record
492    /// that files have been copied. Deleted source files are copied if they do
493    /// not exist in the target, and files that are already identical are
494    /// copied if they are not connected by existing integration records.
495    pub fn force(mut self, v: bool) -> Self {
496        self.force = v;
497        self
498    }
499
500    /// # Description
501    ///
502    /// -q
503    ///
504    #[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    /// # Description
521    ///
522    /// -q
523    ///
524    #[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    /// # Description
542    ///
543    /// -q
544    ///
545    #[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    /// # Description
563    ///
564    /// -v
565    ///
566    #[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    /// # Description
584    ///
585    /// -v
586    ///
587    #[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    /// # Description
606    ///
607    /// -v
608    ///
609    #[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    /// # Description
628    ///
629    /// -K
630    ///
631    /// Suppress keyword expansion when updating +k type files on the client.
632    #[cfg(not(feature = "lt2022_1"))]
633    pub fn get_suppress_keyword_expansion(&self) -> bool {
634        self.suppress_keyword_expansion
635    }
636
637    /// # Description
638    ///
639    /// -K
640    ///
641    /// Suppress keyword expansion when updating +k type files on the client.
642    #[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    /// # Description
649    ///
650    /// -K
651    ///
652    /// Suppress keyword expansion when updating +k type files on the client.
653    #[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
660// ---------------------------------------------------------------------------
661// Branch<Unselected>: reverse + source transitions + branch accessor
662// ---------------------------------------------------------------------------
663
664impl Copy<Branch<Unselected>> {
665    /// # Description
666    ///
667    /// -b branch
668    ///
669    /// The branch view used to determine source and target files.
670    pub fn get_branch(&self) -> &str {
671        &self.mode.branch
672    }
673
674    /// # Description
675    ///
676    /// -b branch
677    ///
678    /// The branch view used to determine source and target files.
679    pub fn set_branch(&mut self, v: impl Into<String>) -> &mut Self {
680        self.mode.branch = v.into();
681        self
682    }
683
684    /// # Description
685    ///
686    /// -b branch
687    ///
688    /// The branch view used to determine source and target files.
689    pub fn branch(mut self, v: impl Into<String>) -> Self {
690        self.mode.branch = v.into();
691        self
692    }
693
694    /// # Description
695    ///
696    /// -r
697    ///
698    /// Reverse the mappings in the branch view, integrating from the target
699    /// files to the source files.
700    #[cfg_attr(feature = "lt2014_2", doc = "Requires the `-b` flag.")]
701    #[cfg_attr(not(feature = "lt2014_2"), doc = "Requires the `-b` option.")]
702    ///
703    /// Transitions this command to the [`BranchReverse`] state.
704    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    /// # Description
724    ///
725    /// -s `fromFile[rev]`
726    ///
727    /// Treat `fromFile` as the source and both sides of the branch view as the
728    /// target. To restrict the scope of the target further, specify the
729    /// optional `toFile` parameter.
730    #[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    ///
739    /// Transitions this command to the [`BranchOverride`] state.
740    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
760// ---------------------------------------------------------------------------
761// Branch<BranchReverse>: branch accessor
762// ---------------------------------------------------------------------------
763
764impl Copy<Branch<BranchReverse>> {
765    /// # Description
766    ///
767    /// -b branch
768    ///
769    /// The branch view used to determine source and target files.
770    pub fn get_branch(&self) -> &str {
771        &self.mode.branch
772    }
773
774    /// # Description
775    ///
776    /// -b branch
777    ///
778    /// The branch view used to determine source and target files.
779    pub fn set_branch(&mut self, v: impl Into<String>) -> &mut Self {
780        self.mode.branch = v.into();
781        self
782    }
783
784    /// # Description
785    ///
786    /// -b branch
787    ///
788    /// The branch view used to determine source and target files.
789    pub fn branch(mut self, v: impl Into<String>) -> Self {
790        self.mode.branch = v.into();
791        self
792    }
793}
794
795// ---------------------------------------------------------------------------
796// BranchOverride: branch + source accessors
797// ---------------------------------------------------------------------------
798
799impl Copy<BranchOverride> {
800    /// # Description
801    ///
802    /// -b branch
803    ///
804    /// The branch view used to determine source and target files.
805    pub fn get_branch(&self) -> &str {
806        &self.mode.branch
807    }
808
809    /// # Description
810    ///
811    /// -b branch
812    ///
813    /// The branch view used to determine source and target files.
814    pub fn set_branch(&mut self, v: impl Into<String>) -> &mut Self {
815        self.mode.branch = v.into();
816        self
817    }
818
819    /// # Description
820    ///
821    /// -b branch
822    ///
823    /// The branch view used to determine source and target files.
824    pub fn branch(mut self, v: impl Into<String>) -> Self {
825        self.mode.branch = v.into();
826        self
827    }
828
829    /// # Description
830    ///
831    /// -s `fromFile[rev]`
832    ///
833    /// Treat `fromFile` as the source and both sides of the branch view as the
834    /// target. To restrict the scope of the target further, specify the
835    /// optional `toFile` parameter.
836    #[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    /// # Description
849    ///
850    /// -s `fromFile[rev]`
851    ///
852    /// Treat `fromFile` as the source and both sides of the branch view as the
853    /// target. To restrict the scope of the target further, specify the
854    /// optional `toFile` parameter.
855    #[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    /// # Description
869    ///
870    /// -s `fromFile[rev]`
871    ///
872    /// Treat `fromFile` as the source and both sides of the branch view as the
873    /// target. To restrict the scope of the target further, specify the
874    /// optional `toFile` parameter.
875    #[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
889// ---------------------------------------------------------------------------
890// Stream<S>: shared stream accessors + strategy transitions
891// ---------------------------------------------------------------------------
892
893impl<S> Copy<Stream<S>> {
894    /// # Description
895    ///
896    /// -S stream
897    ///
898    #[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    /// # Description
924    ///
925    /// -S stream
926    ///
927    #[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    /// # Description
954    ///
955    /// -S stream
956    ///
957    #[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    /// # Description
984    ///
985    /// -P parent
986    ///
987    /// Specify a target stream other than the parent of the source stream.
988    /// Requires `-S`.
989    pub fn get_parent(&self) -> Option<&String> {
990        self.mode.parent.as_ref()
991    }
992
993    /// # Description
994    ///
995    /// -P parent
996    ///
997    /// Specify a target stream other than the parent of the source stream.
998    /// Requires `-S`.
999    pub fn set_parent(&mut self, v: impl Into<String>) -> &mut Self {
1000        self.mode.parent = Some(v.into());
1001        self
1002    }
1003
1004    /// # Description
1005    ///
1006    /// -P parent
1007    ///
1008    /// Specify a target stream other than the parent of the source stream.
1009    /// Requires `-S`.
1010    pub fn parent(mut self, v: impl Into<String>) -> Self {
1011        self.mode.parent = Some(v.into());
1012        self
1013    }
1014
1015    /// # Description
1016    ///
1017    /// -r
1018    ///
1019    /// Reverse the copy direction. With `-S`, copies from the parent stream to
1020    /// the source stream.
1021    pub fn get_reverse(&self) -> bool {
1022        self.mode.reverse
1023    }
1024
1025    /// # Description
1026    ///
1027    /// -r
1028    ///
1029    /// Reverse the copy direction. With `-S`, copies from the parent stream to
1030    /// the source stream.
1031    pub fn set_reverse(&mut self, v: bool) -> &mut Self {
1032        self.mode.reverse = v;
1033        self
1034    }
1035
1036    /// # Description
1037    ///
1038    /// -r
1039    ///
1040    /// Reverse the copy direction. With `-S`, copies from the parent stream to
1041    /// the source stream.
1042    pub fn reverse(mut self, v: bool) -> Self {
1043        self.mode.reverse = v;
1044        self
1045    }
1046
1047    /// # Description
1048    ///
1049    /// -F
1050    ///
1051    /// Force copy operation; perform the operation when the target stream is
1052    /// not configured to accept a copy of the source.
1053    pub fn get_force_stream_flow(&self) -> bool {
1054        self.mode.force_stream_flow
1055    }
1056
1057    /// # Description
1058    ///
1059    /// -F
1060    ///
1061    /// Force copy operation; perform the operation when the target stream is
1062    /// not configured to accept a copy of the source.
1063    pub fn set_force_stream_flow(&mut self, v: bool) -> &mut Self {
1064        self.mode.force_stream_flow = v;
1065        self
1066    }
1067
1068    /// # Description
1069    ///
1070    /// -F
1071    ///
1072    /// Force copy operation; perform the operation when the target stream is
1073    /// not configured to accept a copy of the source.
1074    pub fn force_stream_flow(mut self, v: bool) -> Self {
1075        self.mode.force_stream_flow = v;
1076        self
1077    }
1078}
1079
1080// Stream<Unselected>: strategy transitions
1081#[cfg(not(feature = "lt2020_2"))]
1082impl Copy<Stream<Unselected>> {
1083    /// # Description
1084    ///
1085    /// -Af
1086    ///
1087    /// Copy the files only (not the stream spec).
1088    ///
1089    /// Transitions this command to the [`CopyFilesOnly`] state.
1090    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    /// # Description
1113    ///
1114    /// -As
1115    ///
1116    /// Copy the stream spec only (not the files).
1117    ///
1118    /// Transitions this command to the [`CopyStreamSpecOnly`] state.
1119    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
1142// ---------------------------------------------------------------------------
1143// SubCommand
1144// ---------------------------------------------------------------------------
1145
1146impl<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
1189// ---------------------------------------------------------------------------
1190// Spawning
1191// ---------------------------------------------------------------------------
1192
1193// Direct form: exactly 2 args (fromFile, toFile).
1194impl<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    /// Spawns `p4 copy fromFile[rev] toFile` with the given source and target
1203    /// file arguments as a child process with piped standard output and error
1204    /// streams; use the returned [`Child`] handle to wait for it or interact
1205    /// with it.
1206    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
1216// Branch forms (forward + reverse): 0 or more toFile args.
1217impl<B> ParameterizedSpawn<()> for Copy<Branch<B>>
1218where
1219    Branch<B>: ExclusiveOption,
1220{
1221    type Output = Child;
1222    type Error = std::io::Error;
1223
1224    /// Spawns `p4 copy -b branch [-r]` without target file arguments as a
1225    /// child process with piped standard output and error streams; use the
1226    /// returned [`Child`] handle to wait for it or interact with it.
1227    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    /// Spawns `p4 copy -b branch [-r] [toFile[rev] ...]` for the given target
1245    /// files as a child process with piped standard output and error streams;
1246    /// use the returned [`Child`] handle to wait for it or interact with it.
1247    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
1256// BranchOverride form: 0 or more toFile args.
1257impl ParameterizedSpawn<()> for Copy<BranchOverride> {
1258    type Output = Child;
1259    type Error = std::io::Error;
1260
1261    /// Spawns `p4 copy -b branch -s fromFile[rev]` without target file
1262    /// arguments as a child process with piped standard output and error
1263    /// streams; use the returned [`Child`] handle to wait for it or interact
1264    /// with it.
1265    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    /// Spawns `p4 copy -b branch -s fromFile[rev] [toFile ...]` for the given
1282    /// target files as a child process with piped standard output and error
1283    /// streams; use the returned [`Child`] handle to wait for it or interact
1284    /// with it.
1285    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
1294// Stream forms: 0 or more toFile args.
1295impl<S> ParameterizedSpawn<()> for Copy<Stream<S>>
1296where
1297    Stream<S>: ExclusiveOption,
1298{
1299    type Output = Child;
1300    type Error = std::io::Error;
1301
1302    /// Spawns `p4 copy -S stream [...]` without target file arguments as a
1303    /// child process with piped standard output and error streams; use the
1304    /// returned [`Child`] handle to wait for it or interact with it.
1305    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    /// Spawns `p4 copy -S stream [...] [toFile[rev] ...]` for the given
1323    /// target files as a child process with piped standard output and error
1324    /// streams; use the returned [`Child`] handle to wait for it or interact
1325    /// with it.
1326    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(&copy.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(&copy.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(&copy.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(&copy.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(&copy.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(&copy.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(&copy.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(&copy.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(&copy.setup_command("p4")),
1542            ["-p", "localhost:1666", "copy"]
1543        );
1544    }
1545}