perforce-cli 0.1.0-alpha.4

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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
use std::{
    ffi::OsStr,
    path::PathBuf,
    process::{Child, Command, Stdio},
};

use super::{DiffOptions, ExclusiveOption, SubCommand, Unselected};

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

/// The `-b branch` sub-mode of the [`DepotContent`] state: diff files in
/// two branched codelines through a branch mapping.
///
/// Entered with [`Diff2::branch`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BranchMode {
    branch: String,
}

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

/// The `-S stream` sub-mode of the [`DepotContent`] state: diff a stream
/// with its parent.
///
/// Entered with [`Diff2::stream`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamMode {
    stream: String,

    parent: Option<String>,
}

impl ExclusiveOption for StreamMode {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-S").arg(&self.stream);

        if let Some(parent) = &self.parent {
            command.arg("-P").arg(parent);
        }
    }
}

/// Stream spec mode of `p4 diff2` (`-As`): diff two arbitrary stream specs
/// against each other.
///
/// Entered with [`Diff2::stream_spec_mode`]; the two stream specs to compare
/// are passed as the arguments of the spawned command. As the `-As` form
/// accepts only `-doptions` besides g-opts, this mode offers neither the
/// depot content options nor a sub-mode.
#[cfg(not(feature = "lt2019_1"))]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct StreamSpecMode;

#[cfg(not(feature = "lt2019_1"))]
impl ExclusiveOption for StreamSpecMode {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-As");
    }
}

/// The depot content modes of `p4 diff2`: the default form comparing two
/// depot paths, or the `-b branch` / `-S stream` sub-modes.
///
/// Entered with [`Diff2::differing_only`], [`Diff2::quiet_mode`],
/// [`Diff2::diff_nontext`], [`Diff2::unified_patch`], [`Diff2::branch`], or
/// [`Diff2::stream`].
///
/// The `M` type parameter tracks the sub-mode at compile time. The default
/// [`Unselected`] state diffs the file pair given as spawn arguments;
/// [`Diff2::branch`] transitions to the [`BranchMode`] sub-mode, and
/// [`Diff2::stream`] transitions to the [`StreamMode`] sub-mode. The
/// `[-Od -q -t -u]` options are shared by all sub-modes and therefore
/// stored here.
#[derive(Debug, Clone, Default)]
pub struct DepotContent<M = Unselected> {
    differing_only: bool,

    quiet_mode: bool,

    diff_nontext: bool,

    unified_patch: bool,

    mode: M,
}

impl<M: ExclusiveOption> ExclusiveOption for DepotContent<M> {
    fn inject_args(&self, command: &mut Command) {
        if self.differing_only {
            command.arg("-Od");
        }

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

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

        if self.unified_patch {
            command.arg("-u");
        }

        self.mode.inject_args(command);
    }
}

#[cfg_attr(
    feature = "lt2014_2",
    doc = "`p4 [g-opts] diff2 [-dflags -Od -q -t -u] file1[rev] file2[rev]`",
    doc = "",
    doc = "`p4 [g-opts] diff2 [-dflags -Od -q -t -u] -b branch [[fromfile[rev]] tofile[rev]]`",
    doc = "",
    doc = "`p4 [g-opts] diff2 [-dflags -Od -q -t -u] -S stream [-P parent] [[fromfile[rev]] tofile[rev]]`"
)]
#[cfg_attr(
    all(feature = "lt2015_1", not(feature = "lt2014_2")),
    doc = "`p4 [g-opts] diff2 [-doptions -Od -q -t -u] file1[rev] file2[rev]`",
    doc = "",
    doc = "`p4 [g-opts] diff2 [-doptions -Od -q -t -u] -b branch [[fromfile[rev]] tofile[rev]]`",
    doc = "",
    doc = "`p4 [g-opts] diff2 [-doptions -Od -q -t -u] -S stream [-P parent] [[fromfile[rev]] tofile[rev]]`"
)]
#[cfg_attr(
    all(feature = "lt2019_1", not(feature = "lt2015_1")),
    doc = "`p4 [g-opts] diff2 [-doptions] [-Od -q -t -u] file1[rev] file2[rev]`",
    doc = "",
    doc = "`p4 [g-opts] diff2 [-doptions] [-Od -q -t -u] -b branch [[fromfile[rev]] tofile[rev]]`",
    doc = "",
    doc = "`p4 [g-opts] diff2 [-doptions] [-Od -q -t -u] [-S stream] [-P parent] [[fromfile[rev]] tofile[rev]]`"
)]
#[cfg_attr(
    not(feature = "lt2019_1"),
    doc = "`p4 [g-opts] diff2 [-doptions] [-Od -q -t -u] file1[rev] file2[rev]`",
    doc = "",
    doc = "`p4 [g-opts] diff2 [-doptions] [-Od -q -t -u] -b branch [[fromfile[rev]] tofile[rev]]`",
    doc = "",
    doc = "`p4 [g-opts] diff2 [-doptions] [-Od -q -t -u] [-S stream] [-P parent] [[fromfile[rev]] tofile[rev]]`",
    doc = "",
    doc = "`p4 [g-opts] diff2 [-doptions] -As streamname1[@change1] streamname2[@change2]`"
)]
///
/// Diff utility for comparing the content at two depot paths. (For
/// comparing workspace content to depot content, see `p4 diff`.)
///
#[cfg_attr(
    not(feature = "lt2019_1"),
    doc = "Also compares two arbitrary stream specs with the -As option."
)]
///
#[cfg_attr(
    feature = "lt2019_1",
    doc = "The `M` type parameter tracks the command mode at compile time. The default [`Unselected`] state diffs the two depot paths given as spawn arguments; [`Self::differing_only`], [`Self::quiet_mode`], [`Self::diff_nontext`], and [`Self::unified_patch`] transition to the [`DepotContent`] state, and [`Self::branch`] and [`Self::stream`] transition to the depot content [`BranchMode`] and [`StreamMode`] sub-modes."
)]
#[cfg_attr(
    not(feature = "lt2019_1"),
    doc = "The `M` type parameter tracks the command mode at compile time. The default [`Unselected`] state diffs the two depot paths given as spawn arguments; [`Self::differing_only`], [`Self::quiet_mode`], [`Self::diff_nontext`], and [`Self::unified_patch`] transition to the [`DepotContent`] state, [`Self::branch`] and [`Self::stream`] transition to the depot content [`BranchMode`] and [`StreamMode`] sub-modes, and [`Self::stream_spec_mode`] transitions to the [`StreamSpecMode`] state."
)]
#[derive(Debug, Clone, Default)]
pub struct Diff2<M = Unselected> {
    bin: PathBuf,

    global_opts: GlobalOpts,

    diff_opts: Option<DiffOptions>,

    mode: M,
}

impl Diff2<Unselected> {
    /// Creates a new `p4 diff2` 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,
            diff_opts: None,
            mode: Unselected,
        }
    }

    /// # Description
    ///
    /// -Od
    ///
    /// Limit output to only those files that differ.
    ///
    /// Transitions this command to the [`DepotContent`] state with `-Od`
    /// set according to `v`.
    pub fn differing_only(self, v: bool) -> Diff2<DepotContent<Unselected>> {
        Diff2 {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: DepotContent {
                differing_only: v,
                quiet_mode: false,
                diff_nontext: false,
                unified_patch: false,
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// -q
    ///
    /// Quiet diff. Display only the header; if `file1` and `file2` are
    /// identical, display only "`file1` - no differing files" as the output.
    ///
    /// Transitions this command to the [`DepotContent`] state with `-q` set
    /// according to `v`.
    pub fn quiet_mode(self, v: bool) -> Diff2<DepotContent<Unselected>> {
        Diff2 {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: DepotContent {
                differing_only: false,
                quiet_mode: v,
                diff_nontext: false,
                unified_patch: false,
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// -t
    ///
    /// Diff the file revisions even if the file(s) are not of type `text`.
    ///
    /// Transitions this command to the [`DepotContent`] state with `-t` set
    /// according to `v`.
    pub fn diff_nontext(self, v: bool) -> Diff2<DepotContent<Unselected>> {
        Diff2 {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: DepotContent {
                differing_only: false,
                quiet_mode: false,
                diff_nontext: v,
                unified_patch: false,
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// -u
    ///
    /// Generate unified output format, showing added and deleted lines with
    /// sufficient context for compatibility with the `patch(1)` utility.
    /// Only those files that differ are included. File names and dates
    #[cfg_attr(feature = "lt2017_2", doc = "remain in Perforce syntax.")]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2017_2")),
        doc = "remain in Helix Server syntax."
    )]
    #[cfg_attr(
        all(feature = "lt2024_2", not(feature = "lt2024_1")),
        doc = "remain in Helix Core Server syntax."
    )]
    #[cfg_attr(not(feature = "lt2024_2"), doc = "remain in P4 Server syntax.")]
    ///
    /// Transitions this command to the [`DepotContent`] state with `-u` set
    /// according to `v`.
    pub fn unified_patch(self, v: bool) -> Diff2<DepotContent<Unselected>> {
        Diff2 {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: DepotContent {
                differing_only: false,
                quiet_mode: false,
                diff_nontext: false,
                unified_patch: v,
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// -b branch
    ///
    /// Use a branch mapping to diff files in two branched codelines. The
    /// files that are compared can be limited by file patterns in either
    /// the `from` or `to` file specifications.
    ///
    /// Transitions this command to the [`DepotContent`] state with the
    /// [`BranchMode`] sub-mode selected and the branch mapping set to
    /// `name`.
    pub fn branch(self, name: impl Into<String>) -> Diff2<DepotContent<BranchMode>> {
        Diff2 {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: DepotContent {
                differing_only: false,
                quiet_mode: false,
                diff_nontext: false,
                unified_patch: false,
                mode: BranchMode {
                    branch: name.into(),
                },
            },
        }
    }

    /// # Description
    ///
    /// -S stream
    ///
    /// Diff a stream with its parent. To diff the stream with a stream
    /// other than its configured parent, specify [`Diff2::parent`] or
    /// [`Diff2::set_parent`].
    ///
    /// Transitions this command to the [`DepotContent`] state with the
    /// [`StreamMode`] sub-mode selected and the stream set to `name`.
    pub fn stream(self, name: impl Into<String>) -> Diff2<DepotContent<StreamMode>> {
        Diff2 {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: DepotContent {
                differing_only: false,
                quiet_mode: false,
                diff_nontext: false,
                unified_patch: false,
                mode: StreamMode {
                    stream: name.into(),
                    parent: None,
                },
            },
        }
    }

    /// # Description
    ///
    /// -As
    ///
    /// Allows two arbitrary stream specs to be diffed against each other.
    /// Can be used with a streamname, or with a streamname at a specific
    /// changelist number.
    ///
    /// Although this option requires the user have at least the `list`
    /// access to the stream path, it ignores any other entry in the
    /// protections table, including any minus sign (`-`) that would
    /// otherwise block the operation.
    ///
    /// Transitions this command to the [`StreamSpecMode`] state. The two
    /// stream specs to compare are passed as the arguments of the spawned
    /// command ([`ParameterizedSpawn::spawn_with`]). As the `-As` form
    /// accepts only `-doptions` besides g-opts, this transition is
    /// unavailable once the command has entered the [`DepotContent`] state.
    #[cfg(not(feature = "lt2019_1"))]
    pub fn stream_spec_mode(self) -> Diff2<StreamSpecMode> {
        Diff2 {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: StreamSpecMode,
        }
    }
}

impl<M> Diff2<M> {
    /// # Description
    ///
    /// -doptions
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff flags. See the Usage Notes below for a listing of these flags."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff options. See the Usage Notes below for a listing of these options."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2015_1")),
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff options. See Usage Notes for a listing of these options."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff options. See Usage notes for a listing of these options."
    )]
    pub fn get_diff_options(&self) -> Option<&DiffOptions> {
        self.diff_opts.as_ref()
    }

    /// # Description
    ///
    /// -doptions
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff flags. See the Usage Notes below for a listing of these flags."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff options. See the Usage Notes below for a listing of these options."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2015_1")),
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff options. See Usage Notes for a listing of these options."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff options. See Usage notes for a listing of these options."
    )]
    pub fn set_diff_options(&mut self, v: impl Into<DiffOptions>) -> &mut Self {
        self.diff_opts = Some(v.into());
        self
    }

    /// # Description
    ///
    /// -doptions
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff flags. See the Usage Notes below for a listing of these flags."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff options. See the Usage Notes below for a listing of these options."
    )]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2015_1")),
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff options. See Usage Notes for a listing of these options."
    )]
    #[cfg_attr(
        not(feature = "lt2024_1"),
        doc = "Runs the diff routine with one of a subset of the standard UNIX diff options. See Usage notes for a listing of these options."
    )]
    pub fn diff_options(mut self, v: impl Into<DiffOptions>) -> Self {
        self.diff_opts = Some(v.into());
        self
    }
}

impl<M: ExclusiveOption> Diff2<M> {
    /// # 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
    }
}

impl<M> Diff2<DepotContent<M>> {
    /// # Description
    ///
    /// -Od
    ///
    /// Limit output to only those files that differ.
    pub fn get_differing_only(&self) -> bool {
        self.mode.differing_only
    }

    /// # Description
    ///
    /// -Od
    ///
    /// Limit output to only those files that differ.
    pub fn set_differing_only(&mut self, v: bool) -> &mut Self {
        self.mode.differing_only = v;
        self
    }

    /// # Description
    ///
    /// -Od
    ///
    /// Limit output to only those files that differ.
    pub fn differing_only(mut self, v: bool) -> Self {
        self.mode.differing_only = v;
        self
    }

    /// # Description
    ///
    /// -q
    ///
    /// Quiet diff. Display only the header; if `file1` and `file2` are
    /// identical, display only "`file1` - no differing files" as the output.
    pub fn get_quiet_mode(&self) -> bool {
        self.mode.quiet_mode
    }

    /// # Description
    ///
    /// -q
    ///
    /// Quiet diff. Display only the header; if `file1` and `file2` are
    /// identical, display only "`file1` - no differing files" as the output.
    pub fn set_quiet_mode(&mut self, v: bool) -> &mut Self {
        self.mode.quiet_mode = v;
        self
    }

    /// # Description
    ///
    /// -q
    ///
    /// Quiet diff. Display only the header; if `file1` and `file2` are
    /// identical, display only "`file1` - no differing files" as the output.
    pub fn quiet_mode(mut self, v: bool) -> Self {
        self.mode.quiet_mode = v;
        self
    }

    /// # Description
    ///
    /// -t
    ///
    /// Diff the file revisions even if the file(s) are not of type `text`.
    pub fn get_diff_nontext(&self) -> bool {
        self.mode.diff_nontext
    }

    /// # Description
    ///
    /// -t
    ///
    /// Diff the file revisions even if the file(s) are not of type `text`.
    pub fn set_diff_nontext(&mut self, v: bool) -> &mut Self {
        self.mode.diff_nontext = v;
        self
    }

    /// # Description
    ///
    /// -t
    ///
    /// Diff the file revisions even if the file(s) are not of type `text`.
    pub fn diff_nontext(mut self, v: bool) -> Self {
        self.mode.diff_nontext = v;
        self
    }

    /// # Description
    ///
    /// -u
    ///
    /// Generate unified output format, showing added and deleted lines with
    /// sufficient context for compatibility with the `patch(1)` utility.
    /// Only those files that differ are included. File names and dates
    #[cfg_attr(feature = "lt2017_2", doc = "remain in Perforce syntax.")]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2017_2")),
        doc = "remain in Helix Server syntax."
    )]
    #[cfg_attr(
        all(feature = "lt2024_2", not(feature = "lt2024_1")),
        doc = "remain in Helix Core Server syntax."
    )]
    #[cfg_attr(not(feature = "lt2024_2"), doc = "remain in P4 Server syntax.")]
    pub fn get_unified_patch(&self) -> bool {
        self.mode.unified_patch
    }

    /// # Description
    ///
    /// -u
    ///
    /// Generate unified output format, showing added and deleted lines with
    /// sufficient context for compatibility with the `patch(1)` utility.
    /// Only those files that differ are included. File names and dates
    #[cfg_attr(feature = "lt2017_2", doc = "remain in Perforce syntax.")]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2017_2")),
        doc = "remain in Helix Server syntax."
    )]
    #[cfg_attr(
        all(feature = "lt2024_2", not(feature = "lt2024_1")),
        doc = "remain in Helix Core Server syntax."
    )]
    #[cfg_attr(not(feature = "lt2024_2"), doc = "remain in P4 Server syntax.")]
    pub fn set_unified_patch(&mut self, v: bool) -> &mut Self {
        self.mode.unified_patch = v;
        self
    }

    /// # Description
    ///
    /// -u
    ///
    /// Generate unified output format, showing added and deleted lines with
    /// sufficient context for compatibility with the `patch(1)` utility.
    /// Only those files that differ are included. File names and dates
    #[cfg_attr(feature = "lt2017_2", doc = "remain in Perforce syntax.")]
    #[cfg_attr(
        all(feature = "lt2024_1", not(feature = "lt2017_2")),
        doc = "remain in Helix Server syntax."
    )]
    #[cfg_attr(
        all(feature = "lt2024_2", not(feature = "lt2024_1")),
        doc = "remain in Helix Core Server syntax."
    )]
    #[cfg_attr(not(feature = "lt2024_2"), doc = "remain in P4 Server syntax.")]
    pub fn unified_patch(mut self, v: bool) -> Self {
        self.mode.unified_patch = v;
        self
    }
}

impl Diff2<DepotContent<Unselected>> {
    /// # Description
    ///
    /// -b branch
    ///
    /// Use a branch mapping to diff files in two branched codelines. The
    /// files that are compared can be limited by file patterns in either
    /// the `from` or `to` file specifications.
    ///
    /// Transitions this command to the [`DepotContent`] state with the
    /// [`BranchMode`] sub-mode selected and the branch mapping set to
    /// `name`.
    pub fn branch(self, name: impl Into<String>) -> Diff2<DepotContent<BranchMode>> {
        Diff2 {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: DepotContent {
                differing_only: self.mode.differing_only,
                quiet_mode: self.mode.quiet_mode,
                diff_nontext: self.mode.diff_nontext,
                unified_patch: self.mode.unified_patch,
                mode: BranchMode {
                    branch: name.into(),
                },
            },
        }
    }

    /// # Description
    ///
    /// -S stream
    ///
    /// Diff a stream with its parent. To diff the stream with a stream
    /// other than its configured parent, specify [`Diff2::parent`] or
    /// [`Diff2::set_parent`].
    ///
    /// Transitions this command to the [`DepotContent`] state with the
    /// [`StreamMode`] sub-mode selected and the stream set to `name`.
    pub fn stream(self, name: impl Into<String>) -> Diff2<DepotContent<StreamMode>> {
        Diff2 {
            bin: self.bin,
            global_opts: self.global_opts,
            diff_opts: self.diff_opts,
            mode: DepotContent {
                differing_only: self.mode.differing_only,
                quiet_mode: self.mode.quiet_mode,
                diff_nontext: self.mode.diff_nontext,
                unified_patch: self.mode.unified_patch,
                mode: StreamMode {
                    stream: name.into(),
                    parent: None,
                },
            },
        }
    }
}

impl Diff2<DepotContent<BranchMode>> {
    /// # Description
    ///
    /// -b branch
    ///
    /// Use a branch mapping to diff files in two branched codelines. The
    /// files that are compared can be limited by file patterns in either
    /// the `from` or `to` file specifications.
    pub fn get_branch(&self) -> &str {
        &self.mode.mode.branch
    }

    /// # Description
    ///
    /// -b branch
    ///
    /// Use a branch mapping to diff files in two branched codelines. The
    /// files that are compared can be limited by file patterns in either
    /// the `from` or `to` file specifications.
    pub fn set_branch(&mut self, v: impl Into<String>) -> &mut Self {
        self.mode.mode.branch = v.into();
        self
    }

    /// # Description
    ///
    /// -b branch
    ///
    /// Use a branch mapping to diff files in two branched codelines. The
    /// files that are compared can be limited by file patterns in either
    /// the `from` or `to` file specifications.
    pub fn branch(mut self, v: impl Into<String>) -> Self {
        self.mode.mode.branch = v.into();
        self
    }
}

impl Diff2<DepotContent<StreamMode>> {
    /// # Description
    ///
    /// -S stream
    ///
    /// Diff a stream with its parent. To diff the stream with a stream
    /// other than its configured parent, specify [`Diff2::parent`] or
    /// [`Diff2::set_parent`].
    pub fn get_stream(&self) -> &str {
        &self.mode.mode.stream
    }

    /// # Description
    ///
    /// -S stream
    ///
    /// Diff a stream with its parent. To diff the stream with a stream
    /// other than its configured parent, specify [`Diff2::parent`] or
    /// [`Diff2::set_parent`].
    pub fn set_stream(&mut self, v: impl Into<String>) -> &mut Self {
        self.mode.mode.stream = v.into();
        self
    }

    /// # Description
    ///
    /// -S stream
    ///
    /// Diff a stream with its parent. To diff the stream with a stream
    /// other than its configured parent, specify [`Diff2::parent`] or
    /// [`Diff2::set_parent`].
    pub fn stream(mut self, v: impl Into<String>) -> Self {
        self.mode.mode.stream = v.into();
        self
    }

    /// # Description
    ///
    /// -P parent
    ///
    /// Diff the stream with a stream other than its configured parent.
    pub fn get_parent(&self) -> Option<&str> {
        self.mode.mode.parent.as_deref()
    }

    /// # Description
    ///
    /// -P parent
    ///
    /// Diff the stream with a stream other than its configured parent.
    pub fn set_parent(&mut self, v: impl Into<String>) -> &mut Self {
        self.mode.mode.parent = Some(v.into());
        self
    }

    /// # Description
    ///
    /// -P parent
    ///
    /// Diff the stream with a stream other than its configured parent.
    pub fn parent(mut self, v: impl Into<String>) -> Self {
        self.mode.mode.parent = Some(v.into());
        self
    }
}

impl<M: ExclusiveOption> SubCommand for Diff2<M> {
    fn name(&self) -> &str {
        "diff2"
    }

    fn inject_local_args(&self, command: &mut Command) {
        if let Some(diff_opts) = &self.diff_opts {
            diff_opts.inject_arg(command);
        }

        self.mode.inject_args(command);
    }

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

impl<A, B> ParameterizedSpawn<(A, B)> for Diff2<Unselected>
where
    A: AsRef<OsStr>,
    B: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff2` for the given pair of file arguments as a child
    /// process with piped standard output and error streams; use the
    /// returned [`Child`] handle to wait for it or interact with it.
    ///
    /// Each file argument is a file name, optionally with a revision
    /// specifier (for example `file#2` or `file@34`).
    fn spawn_with(&mut self, (file1, file2): (A, B)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .arg(file1)
            .arg(file2)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl<A, B> ParameterizedSpawn<(A, B)> for Diff2<DepotContent<Unselected>>
where
    A: AsRef<OsStr>,
    B: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff2` for the given pair of file arguments as a child
    /// process with piped standard output and error streams; use the
    /// returned [`Child`] handle to wait for it or interact with it.
    ///
    /// Each file argument is a file name, optionally with a revision
    /// specifier (for example `file#2` or `file@34`).
    fn spawn_with(&mut self, (file1, file2): (A, B)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .arg(file1)
            .arg(file2)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl ParameterizedSpawn<()> for Diff2<DepotContent<BranchMode>> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff2 -b branch` without file arguments as a child
    /// process with piped standard output and error streams; the whole
    /// branch mapping is diffed. Use the returned [`Child`] handle to wait
    /// for it or interact with it.
    fn spawn_with(&mut self, _: ()) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl<I> ParameterizedSpawn<(I,)> for Diff2<DepotContent<BranchMode>>
where
    I: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff2 -b branch [tofile[rev]]` as a child process with
    /// piped standard output and error streams; use the returned [`Child`]
    /// handle to wait for it or interact with it.
    ///
    /// Pass `Some(tofile)` to limit the target side of the branch mapping
    /// to the given file, or `None` to diff the whole branch mapping.
    fn spawn_with(&mut self, (tofile,): (I,)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .arg(tofile)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl<A, B> ParameterizedSpawn<(A, B)> for Diff2<DepotContent<BranchMode>>
where
    A: AsRef<OsStr>,
    B: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff2 -b branch fromfile[rev] tofile[rev]` as a child
    /// process with piped standard output and error streams; the branch
    /// mapping is diffed between the given files. Use the returned
    /// [`Child`] handle to wait for it or interact with it.
    fn spawn_with(&mut self, (fromfile, tofile): (A, B)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .arg(fromfile)
            .arg(tofile)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl ParameterizedSpawn<()> for Diff2<DepotContent<StreamMode>> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff2 -S stream` without file arguments as a child
    /// process with piped standard output and error streams; the whole
    /// stream is diffed with its parent. Use the returned [`Child`] handle
    /// to wait for it or interact with it.
    fn spawn_with(&mut self, _: ()) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl<I> ParameterizedSpawn<(I,)> for Diff2<DepotContent<StreamMode>>
where
    I: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff2 -S stream [tofile[rev]]` as a child process with
    /// piped standard output and error streams; use the returned [`Child`]
    /// handle to wait for it or interact with it.
    ///
    /// Pass `Some(tofile)` to limit the target side of the stream diff to
    /// the given file, or `None` to diff the whole stream with its parent.
    fn spawn_with(&mut self, (tofile,): (I,)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .arg(tofile)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl<A, B> ParameterizedSpawn<(A, B)> for Diff2<DepotContent<StreamMode>>
where
    A: AsRef<OsStr>,
    B: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff2 -S stream fromfile[rev] tofile[rev]` as a child
    /// process with piped standard output and error streams; the stream is
    /// diffed with its parent between the given files. Use the returned
    /// [`Child`] handle to wait for it or interact with it.
    fn spawn_with(&mut self, (fromfile, tofile): (A, B)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .arg(fromfile)
            .arg(tofile)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

#[cfg(not(feature = "lt2019_1"))]
impl<A, B> ParameterizedSpawn<(A, B)> for Diff2<StreamSpecMode>
where
    A: AsRef<OsStr>,
    B: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diff2 -As` for the given pair of stream 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.
    ///
    /// Each stream spec is a streamname, optionally at a specific changelist
    /// number: `@head` selects the head version, `@change` the version at a
    /// specific change, and `@=change` the shelved version at a specific
    /// change.
    fn spawn_with(&mut self, (spec1, spec2): (A, B)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .arg(spec1)
            .arg(spec2)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

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

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

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

    #[test]
    fn depot_content_flags_are_injected() {
        let diff2 = Diff2::new("p4", GlobalOpts::new())
            .differing_only(true)
            .quiet_mode(true)
            .diff_nontext(true)
            .unified_patch(true);

        assert_eq!(
            args_of(&diff2.setup_command("p4")),
            ["diff2", "-Od", "-q", "-t", "-u"]
        );
    }

    #[test]
    fn depot_content_flags_apply_to_branch_mode() {
        let diff2 = Diff2::new("p4", GlobalOpts::new())
            .quiet_mode(true)
            .branch("branch2");

        assert!(diff2.get_quiet_mode());
        assert_eq!(
            args_of(&diff2.setup_command("p4")),
            ["diff2", "-q", "-b", "branch2"]
        );
    }

    #[test]
    fn depot_content_flags_apply_after_branch_transition() {
        let diff2 = Diff2::new("p4", GlobalOpts::new())
            .branch("branch2")
            .quiet_mode(true);

        assert!(diff2.get_quiet_mode());
        assert_eq!(
            args_of(&diff2.setup_command("p4")),
            ["diff2", "-q", "-b", "branch2"]
        );
    }

    #[test]
    fn diff_options_are_injected() {
        let mut diff2 =
            Diff2::new("p4", GlobalOpts::new()).diff_options(DiffOptionsBuilder::unified(None));
        diff2.set_diff_options(DiffOptionsBuilder::summary());

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

    #[test]
    fn branch_mode_injects_branch() {
        let diff2 = Diff2::new("p4", GlobalOpts::new()).branch("branch2");

        assert_eq!(diff2.get_branch(), "branch2");
        assert_eq!(
            args_of(&diff2.setup_command("p4")),
            ["diff2", "-b", "branch2"]
        );
    }

    #[test]
    fn branch_mode_with_files() {
        let diff2 = Diff2::new("p4", GlobalOpts::new()).branch("branch2");

        // Mirrors `spawn_with`, which appends the file arguments after the
        // assembled command.
        let mut command = diff2.setup_command("p4");
        command.arg(OsStr::new("//depot/rel1/..."));
        command.arg(OsStr::new("//depot/rel2/...#4"));

        assert_eq!(
            args_of(&command),
            [
                "diff2",
                "-b",
                "branch2",
                "//depot/rel1/...",
                "//depot/rel2/...#4"
            ]
        );
    }

    #[test]
    fn branch_mode_with_tofile() {
        let diff2 = Diff2::new("p4", GlobalOpts::new()).branch("branch2");

        // Mirrors `spawn_with` for the single-file input, which appends the
        // target file after the assembled command.
        let mut command = diff2.setup_command("p4");
        command.arg(OsStr::new("//depot/rel2/...#4"));

        assert_eq!(
            args_of(&command),
            ["diff2", "-b", "branch2", "//depot/rel2/...#4"]
        );
    }

    #[test]
    fn stream_mode_injects_stream() {
        let diff2 = Diff2::new("p4", GlobalOpts::new()).stream("myStream");

        assert_eq!(diff2.get_stream(), "myStream");
        assert_eq!(
            args_of(&diff2.setup_command("p4")),
            ["diff2", "-S", "myStream"]
        );
    }

    #[test]
    fn stream_mode_with_parent() {
        let mut diff2 = Diff2::new("p4", GlobalOpts::new()).stream("myStream");
        diff2.set_parent("mainStream");

        assert_eq!(diff2.get_parent(), Some("mainStream"));
        assert_eq!(
            args_of(&diff2.setup_command("p4")),
            ["diff2", "-S", "myStream", "-P", "mainStream"]
        );
    }

    #[test]
    fn stream_mode_with_tofile() {
        let diff2 = Diff2::new("p4", GlobalOpts::new()).stream("myStream");

        // Mirrors `spawn_with` for the single-file input, which appends the
        // target file after the assembled command.
        let mut command = diff2.setup_command("p4");
        command.arg(OsStr::new("//depot/rel2/...#4"));

        assert_eq!(
            args_of(&command),
            ["diff2", "-S", "myStream", "//depot/rel2/...#4"]
        );
    }

    #[test]
    fn stream_mode_preserves_diff_options() {
        let diff2 = Diff2::new("p4", GlobalOpts::new())
            .diff_options(DiffOptionsBuilder::summary())
            .stream("myStream");

        assert_eq!(
            args_of(&diff2.setup_command("p4")),
            ["diff2", "-ds", "-S", "myStream"]
        );
    }

    #[cfg(not(feature = "lt2019_1"))]
    #[test]
    fn stream_spec_mode_bare() {
        let diff2 = Diff2::new("p4", GlobalOpts::new()).stream_spec_mode();

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

    #[cfg(not(feature = "lt2019_1"))]
    #[test]
    fn stream_spec_mode_with_specs() {
        let diff2 = Diff2::new("p4", GlobalOpts::new()).stream_spec_mode();

        // Mirrors `spawn_with`, which appends the two stream specs after
        // the assembled command.
        let mut command = diff2.setup_command("p4");
        command.arg("myStream@=1");
        command.arg("yourStream@2");

        assert_eq!(
            args_of(&command),
            ["diff2", "-As", "myStream@=1", "yourStream@2"]
        );
    }
}