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#[cfg_attr(feature = "lt2017_1", doc = "`archive` or `tangent`.")]
16#[cfg_attr(
17 not(feature = "lt2017_1"),
18 doc = "`archive`, or `tangent`. To support Git workflows, a special type is `graph`."
19)]
20#[cfg(not(feature = "lt2015_2"))]
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum DepotType {
23 Local,
25
26 Remote,
28
29 Spec,
31
32 Stream,
34
35 Unload,
37
38 Archive,
40
41 Tangent,
43
44 #[cfg(not(feature = "lt2017_1"))]
46 Graph,
47}
48
49#[cfg(not(feature = "lt2015_2"))]
50impl DepotType {
51 pub(crate) fn as_str(&self) -> &'static str {
52 match self {
53 Self::Local => "local",
54 Self::Remote => "remote",
55 Self::Spec => "spec",
56 Self::Stream => "stream",
57 Self::Unload => "unload",
58 Self::Archive => "archive",
59 Self::Tangent => "tangent",
60 #[cfg(not(feature = "lt2017_1"))]
61 Self::Graph => "graph",
62 }
63 }
64}
65
66#[derive(Debug, Clone, Copy, Default)]
70pub struct Delete {
71 force: bool,
72}
73
74impl ExclusiveOption for Delete {
75 fn inject_args(&self, command: &mut Command) {
76 command.arg("-d");
77 if self.force {
78 command.arg("-f");
79 }
80 }
81}
82
83#[derive(Debug, Clone, Default)]
89pub struct Stdout {
90 #[cfg(not(feature = "lt2015_2"))]
91 depot_type: Option<DepotType>,
92}
93
94impl ExclusiveOption for Stdout {
95 fn inject_args(&self, command: &mut Command) {
96 command.arg("-o");
97 #[cfg(not(feature = "lt2015_2"))]
98 if let Some(depot_type) = &self.depot_type {
99 command.arg("-t").arg(depot_type.as_str());
100 }
101 }
102}
103
104#[derive(Debug, Clone, Copy, Default)]
109pub struct Stdin {}
110
111impl ExclusiveOption for Stdin {
112 fn inject_args(&self, command: &mut Command) {
113 command.arg("-i");
114 }
115}
116
117#[cfg(not(feature = "lt2015_2"))]
123#[derive(Debug, Clone, Copy, Default)]
124pub struct Regular {
125 depot_type: Option<DepotType>,
126}
127
128#[cfg(not(feature = "lt2015_2"))]
129impl ExclusiveOption for Regular {
130 fn inject_args(&self, command: &mut Command) {
131 if let Some(depot_type) = &self.depot_type {
132 command.arg("-t").arg(depot_type.as_str());
133 }
134 }
135}
136
137#[cfg_attr(
138 feature = "lt2015_2",
139 doc = "`p4 [g-opts] depot depotname`\n\n\
140 `p4 [g-opts] depot -d [-f] depotname`\n\n\
141 `p4 [g-opts] depot -o depotname`\n\n\
142 `p4 [g-opts] depot -i`"
143)]
144#[cfg_attr(
145 not(feature = "lt2015_2"),
146 doc = "`p4 [g-opts] depot [-t type] depotname`\n\n\
147 `p4 [g-opts] depot -d [-f] depotname`\n\n\
148 `p4 [g-opts] depot -o [-t type] depotname`\n\n\
149 `p4 [g-opts] depot -i`"
150)]
151#[cfg_attr(feature = "lt2022_1", doc = "Create or edit a depot specification.")]
153#[cfg_attr(
154 all(feature = "lt2022_2", not(feature = "lt2022_1")),
155 doc = "Create, edit, or delete a depot specification.",
156 doc = "",
157 doc = "A branch, depot, label, and workspace cannot have the same name."
158)]
159#[cfg_attr(
160 all(feature = "lt2024_1", not(feature = "lt2022_2")),
161 doc = "Create, edit, or delete a depot specification."
162)]
163#[cfg_attr(
164 not(feature = "lt2024_1"),
165 doc = "Create, edit, or delete a depot specification.",
166 doc = "",
167 doc = "A branch, depot, label, and workspace cannot share the same name."
168)]
169#[cfg_attr(feature = "lt2015_2", doc = ".")]
175#[cfg_attr(
176 not(feature = "lt2015_2"),
177 doc = ", while [`Self::depot_type`] transitions to the [`Regular`] mode carrying `-t type`."
178)]
179#[derive(Debug, Clone, Default)]
180pub struct Depot<M = Unselected> {
181 bin: PathBuf,
182
183 global_opts: GlobalOpts,
184
185 mode: M,
186}
187
188impl Depot<Unselected> {
189 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
193 Self {
194 bin: bin.into(),
195 global_opts,
196 mode: Unselected,
197 }
198 }
199
200 #[cfg_attr(
207 feature = "lt2015_2",
208 doc = "The depot must not contain any files; the Perforce superuser can remove files with `p4 obliterate`.",
209 doc = "",
210 doc = "If the depot is `remote`, `p4 obliterate` must still be run: no files are deleted, but any outstanding client or label records referring to that depot are eliminated."
211 )]
212 #[cfg_attr(
213 all(feature = "lt2017_2", not(feature = "lt2015_2")),
214 doc = "The depot must not contain any files; the Perforce superuser can remove files with `p4 obliterate`.",
215 doc = "",
216 doc = "If the depot is `remote`, `p4 obliterate` must still be run: no files are deleted, but any outstanding client or label records referring to that depot are eliminated.",
217 doc = "",
218 doc = "The name of a depot may not be the same as the name of a branch, client workspace, or label."
219 )]
220 #[cfg_attr(
221 all(feature = "lt2022_1", not(feature = "lt2017_2")),
222 doc = "The depot must not contain any files; the Helix Server superuser can remove files with `p4 obliterate`.",
223 doc = "",
224 doc = "If the depot is `remote`, `p4 obliterate` must still be run: no files are deleted, but any outstanding client or label records referring to that depot are eliminated.",
225 doc = "",
226 doc = "The name of a depot may not be the same as the name of a branch, client workspace, or label."
227 )]
228 #[cfg_attr(
229 all(feature = "lt2022_2", not(feature = "lt2022_1")),
230 doc = "A classic depot must not contain any files. The Helix Core Server user with the `super` access level can remove files with `p4 obliterate`.",
231 doc = "",
232 doc = "A stream depot must not contain any stream specs. A Helix Core user with the `admin` access level can remove a stream with the `--obliterate` option of `p4 stream`.",
233 doc = "",
234 doc = "If the depot is `remote`, `p4 obliterate` must still be run. No files are deleted, but any outstanding client or label records referring to that depot are eliminated."
235 )]
236 #[cfg_attr(
237 all(feature = "lt2024_2", not(feature = "lt2022_2")),
238 doc = "A classic depot must not contain any files. The Helix Core Server user with the `super` access level can permanently remove files with `p4 obliterate`.",
239 doc = "",
240 doc = "A stream depot must not contain any streams. A Helix Core Server user with the `admin` access level can can use the `--obliterate` option of `p4 stream` to permanently remove a stream.",
241 doc = "",
242 doc = "If the depot is `remote`, `p4 obliterate` must still be run: no files are deleted, but any outstanding client or label records referring to that depot are eliminated.",
243 doc = "",
244 doc = "The name of a depot cannot be the same as the name of a branch, client workspace, or label."
245 )]
246 #[cfg_attr(
247 not(feature = "lt2024_2"),
248 doc = "A classic depot must not contain any files. The P4 Server user with the `super` access level can permanently remove files with `p4 obliterate`.",
249 doc = "",
250 doc = "A stream depot must not contain any streams. A P4 Server user with the `admin` access level can can use the `--obliterate` option of `p4 stream` to permanently remove a stream.",
251 doc = "",
252 doc = "If the depot is `remote`, `p4 obliterate` must still be run: no files are deleted, but any outstanding client or label records referring to that depot are eliminated.",
253 doc = "",
254 doc = "The name of a depot cannot be the same as the name of a branch, client workspace, or label."
255 )]
256 pub fn delete(self) -> Depot<Delete> {
259 Depot {
260 bin: self.bin,
261 global_opts: self.global_opts,
262 mode: Delete { force: false },
263 }
264 }
265
266 pub fn stdout(self) -> Depot<Stdout> {
274 Depot {
275 bin: self.bin,
276 global_opts: self.global_opts,
277 mode: Stdout {
278 #[cfg(not(feature = "lt2015_2"))]
279 depot_type: None,
280 },
281 }
282 }
283
284 #[cfg_attr(
289 feature = "lt2019_1",
290 doc = "Read a depot specification from standard input."
291 )]
292 #[cfg_attr(
293 not(feature = "lt2019_1"),
294 doc = "Read a depot specification from standard input, or from a file in the case of `p4 depot -o -t stream mystreamdepot2 > filename` where content of filename is the depot spec."
295 )]
296 pub fn stdin(self) -> Depot<Stdin> {
299 Depot {
300 bin: self.bin,
301 global_opts: self.global_opts,
302 mode: Stdin {},
303 }
304 }
305
306 #[cfg_attr(feature = "lt2017_1", doc = "`archive` or `tangent`.")]
312 #[cfg_attr(
313 not(feature = "lt2017_1"),
314 doc = "`archive`, or `tangent`. To support Git workflows, a special type is `graph`."
315 )]
316 #[cfg(not(feature = "lt2015_2"))]
320 pub fn depot_type(self, v: DepotType) -> Depot<Regular> {
321 Depot {
322 bin: self.bin,
323 global_opts: self.global_opts,
324 mode: Regular {
325 depot_type: Some(v),
326 },
327 }
328 }
329}
330
331impl<I: AsRef<OsStr>> ParameterizedSpawn<(I,)> for Depot<Unselected> {
332 type Output = Child;
333 type Error = std::io::Error;
334
335 fn spawn_with(&mut self, (depotname,): (I,)) -> Result<Self::Output, Self::Error> {
339 self.setup_command(&self.bin)
340 .arg(depotname)
341 .stdout(Stdio::piped())
342 .stderr(Stdio::piped())
343 .spawn()
344 }
345}
346
347impl<I: AsRef<OsStr>> ParameterizedSpawn<(I,)> for Depot<Delete> {
348 type Output = Child;
349 type Error = std::io::Error;
350
351 fn spawn_with(&mut self, (depotname,): (I,)) -> Result<Self::Output, Self::Error> {
355 self.setup_command(&self.bin)
356 .arg(depotname)
357 .stdout(Stdio::piped())
358 .stderr(Stdio::piped())
359 .spawn()
360 }
361}
362
363impl<I: AsRef<OsStr>> ParameterizedSpawn<(I,)> for Depot<Stdout> {
364 type Output = Child;
365 type Error = std::io::Error;
366
367 fn spawn_with(&mut self, (depotname,): (I,)) -> Result<Self::Output, Self::Error> {
371 self.setup_command(&self.bin)
372 .arg(depotname)
373 .stdout(Stdio::piped())
374 .stderr(Stdio::piped())
375 .spawn()
376 }
377}
378
379impl ParameterizedSpawn<()> for Depot<Stdin> {
380 type Output = Child;
381 type Error = std::io::Error;
382
383 fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
387 self.setup_command(&self.bin)
388 .stdin(Stdio::piped())
389 .stdout(Stdio::piped())
390 .stderr(Stdio::piped())
391 .spawn()
392 }
393}
394
395impl ParameterizedSpawn<(Stdio,)> for Depot<Stdin> {
396 type Output = Child;
397 type Error = std::io::Error;
398
399 fn spawn_with(&mut self, (stdio,): (Stdio,)) -> Result<Self::Output, Self::Error> {
407 self.setup_command(&self.bin)
408 .stdin(stdio)
409 .stdout(Stdio::piped())
410 .stderr(Stdio::piped())
411 .spawn()
412 }
413}
414
415#[cfg(not(feature = "lt2015_2"))]
416impl<I: AsRef<OsStr>> ParameterizedSpawn<(I,)> for Depot<Regular> {
417 type Output = Child;
418 type Error = std::io::Error;
419
420 fn spawn_with(&mut self, (depotname,): (I,)) -> Result<Self::Output, Self::Error> {
424 self.setup_command(&self.bin)
425 .arg(depotname)
426 .stdout(Stdio::piped())
427 .stderr(Stdio::piped())
428 .spawn()
429 }
430}
431
432impl Depot<Delete> {
433 #[cfg_attr(
439 feature = "lt2014_2",
440 doc = "`Map:` field (typically under `P4ROOT`) must be empty. Use the `-f` flag to remove all files even if the directory is not empty."
441 )]
442 #[cfg_attr(
443 all(feature = "lt2024_1", not(feature = "lt2014_2")),
444 doc = "`Map:` field (typically under `P4ROOT`) must be empty. Use the `-f` option to remove all files even if the directory is not empty."
445 )]
446 #[cfg_attr(
447 all(feature = "lt2025_2", not(feature = "lt2024_1")),
448 doc = "`Map` field (typically under `P4ROOT`) must be empty. Use the `-f` option to remove all files even if the directory is not empty."
449 )]
450 #[cfg_attr(
451 not(feature = "lt2025_2"),
452 doc = "`Map` field (typically under `P4ROOT`) must be empty. Use the `-f` option to remove all files even if the directory is not empty.",
453 doc = "",
454 doc = "The P4 Server will not delete files on disk if they are referenced elsewhere (due to being branched or integrated to another depot as a lazy copy). You should not remove referenced files from disk manually using operating system commands. For more information, see Deleting depots."
455 )]
456 pub fn get_force(&self) -> bool {
457 self.mode.force
458 }
459
460 #[cfg_attr(
466 feature = "lt2014_2",
467 doc = "`Map:` field (typically under `P4ROOT`) must be empty. Use the `-f` flag to remove all files even if the directory is not empty."
468 )]
469 #[cfg_attr(
470 all(feature = "lt2024_1", not(feature = "lt2014_2")),
471 doc = "`Map:` field (typically under `P4ROOT`) must be empty. Use the `-f` option to remove all files even if the directory is not empty."
472 )]
473 #[cfg_attr(
474 all(feature = "lt2025_2", not(feature = "lt2024_1")),
475 doc = "`Map` field (typically under `P4ROOT`) must be empty. Use the `-f` option to remove all files even if the directory is not empty."
476 )]
477 #[cfg_attr(
478 not(feature = "lt2025_2"),
479 doc = "`Map` field (typically under `P4ROOT`) must be empty. Use the `-f` option to remove all files even if the directory is not empty.",
480 doc = "",
481 doc = "The P4 Server will not delete files on disk if they are referenced elsewhere (due to being branched or integrated to another depot as a lazy copy). You should not remove referenced files from disk manually using operating system commands. For more information, see Deleting depots."
482 )]
483 pub fn set_force(&mut self, v: bool) -> &mut Self {
484 self.mode.force = v;
485 self
486 }
487
488 #[cfg_attr(
494 feature = "lt2014_2",
495 doc = "`Map:` field (typically under `P4ROOT`) must be empty. Use the `-f` flag to remove all files even if the directory is not empty."
496 )]
497 #[cfg_attr(
498 all(feature = "lt2024_1", not(feature = "lt2014_2")),
499 doc = "`Map:` field (typically under `P4ROOT`) must be empty. Use the `-f` option to remove all files even if the directory is not empty."
500 )]
501 #[cfg_attr(
502 all(feature = "lt2025_2", not(feature = "lt2024_1")),
503 doc = "`Map` field (typically under `P4ROOT`) must be empty. Use the `-f` option to remove all files even if the directory is not empty."
504 )]
505 #[cfg_attr(
506 not(feature = "lt2025_2"),
507 doc = "`Map` field (typically under `P4ROOT`) must be empty. Use the `-f` option to remove all files even if the directory is not empty.",
508 doc = "",
509 doc = "The P4 Server will not delete files on disk if they are referenced elsewhere (due to being branched or integrated to another depot as a lazy copy). You should not remove referenced files from disk manually using operating system commands. For more information, see Deleting depots."
510 )]
511 pub fn force(mut self, v: bool) -> Self {
512 self.mode.force = v;
513 self
514 }
515}
516
517#[cfg(not(feature = "lt2015_2"))]
518impl Depot<Regular> {
519 #[cfg_attr(feature = "lt2017_1", doc = "`archive` or `tangent`.")]
525 #[cfg_attr(
526 not(feature = "lt2017_1"),
527 doc = "`archive`, or `tangent`. To support Git workflows, a special type is `graph`."
528 )]
529 pub fn get_depot_type(&self) -> Option<DepotType> {
530 self.mode.depot_type
531 }
532
533 #[cfg_attr(feature = "lt2017_1", doc = "`archive` or `tangent`.")]
539 #[cfg_attr(
540 not(feature = "lt2017_1"),
541 doc = "`archive`, or `tangent`. To support Git workflows, a special type is `graph`."
542 )]
543 pub fn set_depot_type(&mut self, v: DepotType) -> &mut Self {
544 self.mode.depot_type = Some(v);
545 self
546 }
547
548 #[cfg_attr(feature = "lt2017_1", doc = "`archive` or `tangent`.")]
554 #[cfg_attr(
555 not(feature = "lt2017_1"),
556 doc = "`archive`, or `tangent`. To support Git workflows, a special type is `graph`."
557 )]
558 pub fn depot_type(mut self, v: DepotType) -> Self {
559 self.mode.depot_type = Some(v);
560 self
561 }
562
563 pub fn stdout(self) -> Depot<Stdout> {
572 Depot {
573 bin: self.bin,
574 global_opts: self.global_opts,
575 mode: Stdout {
576 depot_type: self.mode.depot_type,
577 },
578 }
579 }
580}
581
582impl Depot<Stdout> {
583 #[cfg_attr(feature = "lt2017_1", doc = "`archive` or `tangent`.")]
589 #[cfg_attr(
590 not(feature = "lt2017_1"),
591 doc = "`archive`, or `tangent`. To support Git workflows, a special type is `graph`."
592 )]
593 #[cfg(not(feature = "lt2015_2"))]
594 pub fn get_depot_type(&self) -> Option<DepotType> {
595 self.mode.depot_type
596 }
597
598 #[cfg_attr(feature = "lt2017_1", doc = "`archive` or `tangent`.")]
604 #[cfg_attr(
605 not(feature = "lt2017_1"),
606 doc = "`archive`, or `tangent`. To support Git workflows, a special type is `graph`."
607 )]
608 #[cfg(not(feature = "lt2015_2"))]
609 pub fn set_depot_type(&mut self, v: DepotType) -> &mut Self {
610 self.mode.depot_type = Some(v);
611 self
612 }
613
614 #[cfg_attr(feature = "lt2017_1", doc = "`archive` or `tangent`.")]
620 #[cfg_attr(
621 not(feature = "lt2017_1"),
622 doc = "`archive`, or `tangent`. To support Git workflows, a special type is `graph`."
623 )]
624 #[cfg(not(feature = "lt2015_2"))]
625 pub fn depot_type(mut self, v: DepotType) -> Self {
626 self.mode.depot_type = Some(v);
627 self
628 }
629}
630
631impl<M: ExclusiveOption> Depot<M> {
632 pub fn get_bin(&self) -> &Path {
634 &self.bin
635 }
636
637 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
639 self.bin = bin.into();
640 self
641 }
642
643 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
645 self.set_bin(bin);
646 self
647 }
648
649 #[cfg_attr(
654 feature = "lt2014_2",
655 doc = "See the [Global Options](GlobalOpts) section."
656 )]
657 #[cfg_attr(
658 all(feature = "lt2015_1", not(feature = "lt2014_2")),
659 doc = "See the [“Global Options”](GlobalOpts) section."
660 )]
661 #[cfg_attr(
662 all(feature = "lt2017_1", not(feature = "lt2015_1")),
663 doc = "See [“Global Options”](GlobalOpts)."
664 )]
665 #[cfg_attr(
666 all(feature = "lt2018_2", not(feature = "lt2017_1")),
667 doc = "See [Global Options](GlobalOpts)."
668 )]
669 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
670 pub fn get_global_opts(&self) -> &GlobalOpts {
671 &self.global_opts
672 }
673
674 #[cfg_attr(
679 feature = "lt2014_2",
680 doc = "See the [Global Options](GlobalOpts) section."
681 )]
682 #[cfg_attr(
683 all(feature = "lt2015_1", not(feature = "lt2014_2")),
684 doc = "See the [“Global Options”](GlobalOpts) section."
685 )]
686 #[cfg_attr(
687 all(feature = "lt2017_1", not(feature = "lt2015_1")),
688 doc = "See [“Global Options”](GlobalOpts)."
689 )]
690 #[cfg_attr(
691 all(feature = "lt2018_2", not(feature = "lt2017_1")),
692 doc = "See [Global Options](GlobalOpts)."
693 )]
694 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
695 pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
696 self.global_opts = v;
697 self
698 }
699
700 #[cfg_attr(
705 feature = "lt2014_2",
706 doc = "See the [Global Options](GlobalOpts) section."
707 )]
708 #[cfg_attr(
709 all(feature = "lt2015_1", not(feature = "lt2014_2")),
710 doc = "See the [“Global Options”](GlobalOpts) section."
711 )]
712 #[cfg_attr(
713 all(feature = "lt2017_1", not(feature = "lt2015_1")),
714 doc = "See [“Global Options”](GlobalOpts)."
715 )]
716 #[cfg_attr(
717 all(feature = "lt2018_2", not(feature = "lt2017_1")),
718 doc = "See [Global Options](GlobalOpts)."
719 )]
720 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
721 pub fn global_opts(mut self, v: GlobalOpts) -> Self {
722 self.global_opts = v;
723 self
724 }
725}
726
727impl<M: ExclusiveOption> SubCommand for Depot<M> {
728 fn name(&self) -> &str {
729 "depot"
730 }
731
732 fn inject_local_args(&self, command: &mut Command) {
733 self.mode.inject_args(command);
734 }
735
736 fn global_opts(&self) -> Option<&GlobalOpts> {
737 Some(&self.global_opts)
738 }
739}