perforce-cli 0.1.0-alpha.2

A type-safe builder library for spawning Perforce (p4) commands, with compile-time option state isolation and multi-version support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};

use super::{ExclusiveOption, SubCommand};

use crate::global::GlobalOpts;
use crate::spawn::ParameterizedSpawn;

/// Archive files to an archive depot (default mode).
///
/// In this mode the revisions to archive can be selected with `-h` and `-z`;
/// see [`Archive::purge_physical_files`] for the alternative mode.
#[derive(Debug, Clone, Copy, Default)]
pub struct SafeArchive {
    skip_head_revisions: bool,

    #[cfg(not(feature = "lt2019_1"))]
    include_lazy_copies: bool,
}

impl ExclusiveOption for SafeArchive {
    fn inject_args(&self, command: &mut Command) {
        if self.skip_head_revisions {
            command.arg("-h");
        }

        #[cfg(not(feature = "lt2019_1"))]
        if self.include_lazy_copies {
            command.arg("-z");
        }
    }
}

/// Remove the physical file revisions of the specified files (`-p`).
///
/// Entered with [`Archive::purge_physical_files`]; `-h` and `-z` are not
/// available in this mode.
#[derive(Debug, Clone, Copy, Default)]
pub struct PhysicalPurge;

impl ExclusiveOption for PhysicalPurge {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-p");
    }
}

#[cfg_attr(
    feature = "lt2015_1",
    doc = "`p4 [g-opts] archive [-n -h -p -q -t] -D depot file[revRange] ...`: archive obsolete revisions to an archive depot."
)]
#[cfg_attr(
    all(feature = "lt2018_1", not(feature = "lt2015_1")),
    doc = "`p4 [g-opts] archive [-h -n -p -q -t] -D depot file[revRange] ...`: archive obsolete revisions to an archive depot."
)]
#[cfg_attr(
    all(feature = "lt2019_1", not(feature = "lt2018_1")),
    doc = "`p4 [g-opts] archive [-h -n -p -q -t] -D depot File Spec[revSpec]`: archive obsolete revisions to an archive depot."
)]
#[cfg_attr(
    not(feature = "lt2019_1"),
    doc = "`p4 [g-opts] archive [-n -h -p -q -t -z] -D depot File Spec[revSpec]`: archive obsolete revisions to an archive depot."
)]
///
/// The `S` type parameter tracks the operation mode at compile time; see
/// [`SafeArchive`], [`PhysicalPurge`], and [`Self::purge_physical_files`].
#[derive(Debug, Clone, Default)]
pub struct Archive<S = SafeArchive> {
    bin: PathBuf,

    global_opts: GlobalOpts,

    preview: bool,

    quiet_mode: bool,

    archive_delta: bool,

    depot: Option<String>,

    archive_mode: S,
}

impl<S: ExclusiveOption> SubCommand for Archive<S> {
    fn name(&self) -> &str {
        "archive"
    }

    fn inject_local_args(&self, command: &mut Command) {
        self.archive_mode.inject_args(command);

        if self.preview {
            command.arg("-n");
        }

        if self.quiet_mode {
            command.arg("-q");
        }

        if self.archive_delta {
            command.arg("-t");
        }

        if let Some(depot) = self.depot.as_ref() {
            command.arg("-D").arg(depot);
        }
    }

    fn global_opts(&self) -> Option<&GlobalOpts> {
        Some(&self.global_opts)
    }
}

impl Archive<SafeArchive> {
    /// Creates a new `p4 archive` command.
    ///
    /// `bin` is the path to the Perforce command-line executable.
    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
        Self {
            bin: bin.into(),
            global_opts,
            ..Default::default()
        }
    }

    /// # Description
    ///
    /// -p
    ///
    #[cfg_attr(
        feature = "lt2019_1",
        doc = "Purge any archives of the specified files named in the archive depot.",
        doc = "(The action for affected revisions is set to `purge` on completion. File",
        doc = "contents are no longer accessible from `p4 restore`.)"
    )]
    #[cfg_attr(
        all(feature = "lt2019_2", not(feature = "lt2019_1")),
        doc = "Purge any archives of the specified files named in the archive depot.",
        doc = "The action for affected revisions is set to `purge` on completion.",
        doc = "",
        doc = "**Warning:** File contents are no longer accessible from `p4 restore`."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2019_2")),
        doc = "Purge any archives of the specified files named in the archive depot.",
        doc = "The action for affected revisions is set to `purge` on completion.",
        doc = "",
        doc = "**Warning:** File contents are no longer accessible from `p4 restore`.",
        doc = "",
        doc = "**Tip:** If you want to retain the metadata of purged files, do one of the",
        doc = "following:",
        doc = "",
        doc = "- use the `-p` option of `p4 obliterate` (recommended)",
        doc = "- perform the two-step sequence of `p4 archive` followed by",
        doc = "  `p4 archive -p` (time-consuming)"
    )]
    #[cfg_attr(
        all(feature = "lt2024_2", not(feature = "lt2024_1")),
        doc = "For the specified files in the archive depot, remove the physical file",
        doc = "revisions but retain the revision history with the latest action being set",
        doc = "to `purge`.",
        doc = "",
        doc = "**Warning:** File contents are no longer accessible from `p4 restore`.",
        doc = "",
        doc = "**Tip:** If you want to retain the metadata of purged files, do one of the",
        doc = "following:",
        doc = "",
        doc = "- use the `-p` option of `p4 obliterate` (recommended)",
        doc = "- perform the two-step sequence of `p4 archive` followed by",
        doc = "  `p4 archive -p` (time-consuming)"
    )]
    #[cfg_attr(
        not(feature = "lt2024_2"),
        doc = "For the specified files in the archive depot, remove the physical file",
        doc = "revisions but retain the revision history with the latest action being set",
        doc = "to `purge`. File contents are no longer accessible from `p4 restore`.",
        doc = "",
        doc = "If you want to retain the metadata of purged files, do one of the following:",
        doc = "",
        doc = "- use the `-p` option of `p4 obliterate` (recommended)",
        doc = "- perform the two-step sequence of `p4 archive` followed by",
        doc = "  `p4 archive -p` (time-consuming)"
    )]
    ///
    /// Transitions this command to the [`PhysicalPurge`] state, in which
    /// the `-h` and `-z` options are not available; any `-h`/`-z` selection
    /// made in the [`SafeArchive`] state is discarded.
    pub fn purge_physical_files(self) -> Archive<PhysicalPurge> {
        Archive {
            bin: self.bin,
            global_opts: self.global_opts,
            preview: self.preview,
            quiet_mode: self.quiet_mode,
            archive_delta: self.archive_delta,
            depot: self.depot,
            archive_mode: PhysicalPurge,
        }
    }
}

impl<M> ParameterizedSpawn for Archive<M>
where
    Archive<M>: SubCommand,
{
    type Input<'a> = &'a [&'a OsStr];
    type Output<'a> = Child;
    type Error = std::io::Error;

    /// Spawns `p4 archive` for the given file specs as a child process with
    /// piped standard output and error streams; use the returned [`Child`]
    /// handle to wait for it or interact with it.
    fn spawn_with<'a>(&mut self, files: Self::Input<'a>) -> Result<Self::Output<'a>, Self::Error> {
        self.setup_command(&self.bin)
            .args(files)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl<M> Archive<M>
where
    Archive<M>: SubCommand,
{
    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "See the [Global Options](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "See the [“Global Options”](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2017_1", not(feature = "lt2015_1")),
        doc = "See [“Global Options”](GlobalOpts)."
    )]
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn get_global_opts(&self) -> &GlobalOpts {
        &self.global_opts
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "See the [Global Options](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "See the [“Global Options”](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2017_1", not(feature = "lt2015_1")),
        doc = "See [“Global Options”](GlobalOpts)."
    )]
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
        self.global_opts = v;
        self
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "See the [Global Options](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "See the [“Global Options”](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2017_1", not(feature = "lt2015_1")),
        doc = "See [“Global Options”](GlobalOpts)."
    )]
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
        self.global_opts = v;
        self
    }

    /// # Description
    ///
    /// -D depot
    ///
    /// Specify an archive depot to which files are to be archived.
    pub fn get_depot(&self) -> Option<&String> {
        self.depot.as_ref()
    }

    /// # Description
    ///
    /// -D depot
    ///
    /// Specify an archive depot to which files are to be archived.
    pub fn set_depot(&mut self, v: impl Into<String>) -> &mut Self {
        self.depot = Some(v.into());
        self
    }

    /// # Description
    ///
    /// -D depot
    ///
    /// Specify an archive depot to which files are to be archived.
    pub fn depot(mut self, v: impl Into<String>) -> Self {
        self.depot = Some(v.into());
        self
    }

    /// # Description
    ///
    /// -n
    ///
    #[cfg_attr(
        feature = "lt2018_1",
        doc = "Do not archive revisions; report on which revisions would have been archived."
    )]
    #[cfg_attr(
        not(feature = "lt2018_1"),
        doc = "Do not archive revisions. Instead, report on which revisions would have been",
        doc = "archived."
    )]
    pub fn get_preview(&self) -> bool {
        self.preview
    }

    /// # Description
    ///
    /// -n
    ///
    #[cfg_attr(
        feature = "lt2018_1",
        doc = "Do not archive revisions; report on which revisions would have been archived."
    )]
    #[cfg_attr(
        not(feature = "lt2018_1"),
        doc = "Do not archive revisions. Instead, report on which revisions would have been",
        doc = "archived."
    )]
    pub fn set_preview(&mut self, v: bool) -> &mut Self {
        self.preview = v;
        self
    }

    /// # Description
    ///
    /// -n
    ///
    #[cfg_attr(
        feature = "lt2018_1",
        doc = "Do not archive revisions; report on which revisions would have been archived."
    )]
    #[cfg_attr(
        not(feature = "lt2018_1"),
        doc = "Do not archive revisions. Instead, report on which revisions would have been",
        doc = "archived."
    )]
    pub fn preview(mut self, v: bool) -> Self {
        self.preview = v;
        self
    }

    /// # Description
    ///
    /// -q
    ///
    #[cfg_attr(
        feature = "lt2018_1",
        doc = "Quiet mode; suppress messages about skipped revisions."
    )]
    #[cfg_attr(
        not(feature = "lt2018_1"),
        doc = "Quiet mode, which suppresses messages about skipped revisions."
    )]
    pub fn get_quiet_mode(&self) -> bool {
        self.quiet_mode
    }

    /// # Description
    ///
    /// -q
    ///
    #[cfg_attr(
        feature = "lt2018_1",
        doc = "Quiet mode; suppress messages about skipped revisions."
    )]
    #[cfg_attr(
        not(feature = "lt2018_1"),
        doc = "Quiet mode, which suppresses messages about skipped revisions."
    )]
    pub fn set_quiet_mode(&mut self, v: bool) -> &mut Self {
        self.quiet_mode = v;
        self
    }

    /// # Description
    ///
    /// -q
    ///
    #[cfg_attr(
        feature = "lt2018_1",
        doc = "Quiet mode; suppress messages about skipped revisions."
    )]
    #[cfg_attr(
        not(feature = "lt2018_1"),
        doc = "Quiet mode, which suppresses messages about skipped revisions."
    )]
    pub fn quiet_mode(mut self, v: bool) -> Self {
        self.quiet_mode = v;
        self
    }

    /// # Description
    ///
    /// -t
    ///
    /// Archive text files (or other revisions stored in delta format, such as
    /// files of type `binary+D`)
    pub fn get_archive_delta(&self) -> bool {
        self.archive_delta
    }

    /// # Description
    ///
    /// -t
    ///
    /// Archive text files (or other revisions stored in delta format, such as
    /// files of type `binary+D`)
    pub fn set_archive_delta(&mut self, v: bool) -> &mut Self {
        self.archive_delta = v;
        self
    }

    /// # Description
    ///
    /// -t
    ///
    /// Archive text files (or other revisions stored in delta format, such as
    /// files of type `binary+D`)
    pub fn archive_delta(mut self, v: bool) -> Self {
        self.archive_delta = v;
        self
    }
}

impl Archive<SafeArchive> {
    /// # Description
    ///
    /// -h
    ///
    /// Do not archive head revisions.
    pub fn get_skip_head_revisions(&self) -> bool {
        self.archive_mode.skip_head_revisions
    }

    /// # Description
    ///
    /// -h
    ///
    /// Do not archive head revisions.
    pub fn set_skip_head_revisions(&mut self, v: bool) -> &mut Self {
        self.archive_mode.skip_head_revisions = v;
        self
    }

    /// # Description
    ///
    /// -h
    ///
    /// Do not archive head revisions.
    pub fn skip_head_revisions(mut self, v: bool) -> Self {
        self.archive_mode.skip_head_revisions = v;
        self
    }

    /// # Description
    ///
    /// -z
    ///
    #[cfg_attr(
        all(feature = "lt2021_1", not(feature = "lt2019_1")),
        doc = "Can reduce the use of disk space because it includes in the archive any files",
        doc = "that have lazy copies or are lazy copies. (A lazy copy is a reference to the",
        doc = "location of the full file.) With this flag, only Criteria 1 and 2 must be",
        doc = "met. Unless all copies are archived, the original file remains in the depot."
    )]
    #[cfg_attr(
        not(feature = "lt2021_1"),
        doc = "Includes any files that have lazy copies or are lazy copies. See Criteria",
        doc = "with `-z`."
    )]
    #[cfg(not(feature = "lt2019_1"))]
    pub fn get_include_lazy_copies(&self) -> bool {
        self.archive_mode.include_lazy_copies
    }

    /// # Description
    ///
    /// -z
    ///
    #[cfg_attr(
        all(feature = "lt2021_1", not(feature = "lt2019_1")),
        doc = "Can reduce the use of disk space because it includes in the archive any files",
        doc = "that have lazy copies or are lazy copies. (A lazy copy is a reference to the",
        doc = "location of the full file.) With this flag, only Criteria 1 and 2 must be",
        doc = "met. Unless all copies are archived, the original file remains in the depot."
    )]
    #[cfg_attr(
        not(feature = "lt2021_1"),
        doc = "Includes any files that have lazy copies or are lazy copies. See Criteria",
        doc = "with `-z`."
    )]
    #[cfg(not(feature = "lt2019_1"))]
    pub fn set_include_lazy_copies(&mut self, v: bool) -> &mut Self {
        self.archive_mode.include_lazy_copies = v;
        self
    }

    /// # Description
    ///
    /// -z
    ///
    #[cfg_attr(
        all(feature = "lt2021_1", not(feature = "lt2019_1")),
        doc = "Can reduce the use of disk space because it includes in the archive any files",
        doc = "that have lazy copies or are lazy copies. (A lazy copy is a reference to the",
        doc = "location of the full file.) With this flag, only Criteria 1 and 2 must be",
        doc = "met. Unless all copies are archived, the original file remains in the depot."
    )]
    #[cfg_attr(
        not(feature = "lt2021_1"),
        doc = "Includes any files that have lazy copies or are lazy copies. See Criteria",
        doc = "with `-z`."
    )]
    #[cfg(not(feature = "lt2019_1"))]
    pub fn include_lazy_copies(mut self, v: bool) -> Self {
        self.archive_mode.include_lazy_copies = v;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cmd::args_of;

    /// Dry-run checks of the assembled `p4 archive` command line; no process
    /// is spawned.
    #[test]
    fn without_options() {
        let archive = Archive::new("p4", GlobalOpts::new());

        assert_eq!(args_of(&archive.setup_command("p4")), ["archive"]);
    }

    #[test]
    fn all_safe_archive_options() {
        let mut archive = Archive::new("p4", GlobalOpts::new());
        archive
            .set_skip_head_revisions(true)
            .set_preview(true)
            .set_quiet_mode(true)
            .set_archive_delta(true)
            .set_depot("archives");
        #[cfg(not(feature = "lt2019_1"))]
        archive.set_include_lazy_copies(true);

        #[cfg_attr(feature = "lt2019_1", allow(unused_mut))]
        let mut expected = vec!["archive", "-h"];
        #[cfg(not(feature = "lt2019_1"))]
        expected.push("-z");
        expected.extend(["-n", "-q", "-t", "-D", "archives"]);

        assert_eq!(args_of(&archive.setup_command("p4")), expected);
    }

    #[test]
    fn physical_purge_mode() {
        let mut archive = Archive::new("p4", GlobalOpts::new());
        archive
            .set_preview(true)
            .set_quiet_mode(true)
            .set_archive_delta(true)
            .set_depot("archives");

        let archive = archive.purge_physical_files();

        assert_eq!(
            args_of(&archive.setup_command("p4")),
            ["archive", "-p", "-n", "-q", "-t", "-D", "archives"]
        );
    }

    /// `-h` and `-z` are not available in the physical purge mode; values set
    /// before the transition must not leak into the command line.
    #[test]
    fn archive_selection_flags_not_injected_after_purge() {
        let mut archive = Archive::new("p4", GlobalOpts::new());
        archive.set_skip_head_revisions(true);
        #[cfg(not(feature = "lt2019_1"))]
        archive.set_include_lazy_copies(true);

        let archive = archive.purge_physical_files();

        assert_eq!(args_of(&archive.setup_command("p4")), ["archive", "-p"]);
    }

    #[cfg(not(feature = "lt2019_1"))]
    #[test]
    fn lazy_copies_flag() {
        let archive = Archive::new("p4", GlobalOpts::new())
            .depot("archives")
            .include_lazy_copies(true);

        assert!(archive.get_include_lazy_copies());
        assert_eq!(
            args_of(&archive.setup_command("p4")),
            ["archive", "-z", "-D", "archives"]
        );
    }

    #[test]
    fn builder_style_with_str_and_global_opts() {
        let archive = Archive::new("p4", GlobalOpts::new().port("localhost:1666"))
            .depot("archives")
            .preview(true);

        assert_eq!(archive.get_depot(), Some(&"archives".to_string()));
        assert_eq!(
            args_of(&archive.setup_command("p4")),
            ["-p", "localhost:1666", "archive", "-n", "-D", "archives"]
        );
    }
}