Skip to main content

diff/
diff.rs

1//! Example of the `p4 diff` command using the builder pattern.
2//!
3//! `p4 diff` has three mutually exclusive modes, tracked at compile time by
4//! the command's type parameter:
5//!
6//! - [`Diff<Unselected>`]: no mode selected yet — enter workspace mode with
7//!   [`Diff::force`], [`Diff::differing_only`], or [`Diff::diff_nontext`]
8#![cfg_attr(feature = "lt2019_1", doc = ".")]
9#![cfg_attr(
10    not(feature = "lt2019_1"),
11    doc = ", or stream-spec mode with [`Diff::stream_spec_mode`]."
12)]
13//! - [`Diff<WorkspaceMode<M>>`]: diff workspace files against the depot. The
14//!   inner `M` parameter isolates `-m max` ([`WorkspaceRegularMode`]) from
15//!   `-soptions` ([`WorkspaceDisplayMode`]).
16#![cfg_attr(
17    not(feature = "lt2019_1"),
18    doc = "- [`Diff<StreamSpecMode>`]: diff stream specs via `-As`."
19)]
20//!
21//! Run with:
22//!
23//! ```text
24//! cargo run --example diff
25//! ```
26
27use std::ffi::OsStr;
28
29use perforce_cli::P4Cli;
30use perforce_cli::cmd::DiffOptionsBuilder;
31use perforce_cli::cmd::diff::DisplayOptions;
32use perforce_cli::spawn::ParameterizedOutput;
33#[cfg(not(feature = "lt2019_1"))]
34use perforce_cli::spawn::ParameterizedSpawn;
35
36fn main() -> std::io::Result<()> {
37    let p4 = P4Cli::default();
38
39    // Workspace mode: force a diff against head, use the unified format with
40    // whitespace-insensitive comparison, and limit output to the first 10
41    // files. `force(true)` transitions the command into `WorkspaceMode`, and
42    // `limit(10)` further transitions it into `WorkspaceRegularMode` (where
43    // `-soptions` is unavailable).
44    let mut diff = p4
45        .diff()
46        .force(true)
47        .diff_options(DiffOptionsBuilder::unified(None).ignore_all_whitespace())
48        .limit(10);
49
50    let files = [OsStr::new("//depot/project/src/...")];
51    let output = diff.output_with((&files,))?;
52    println!("{}", String::from_utf8_lossy(&output.stdout));
53
54    // Display mode: instead of full diffs, list only the unopened files that
55    // differ from the depot. `display_options` transitions `WorkspaceMode`
56    // into `WorkspaceDisplayMode`, where `-m max` is unavailable.
57    let mut list_changed = p4
58        .diff()
59        .force(true)
60        .display_options(DisplayOptions::UnopenedChanged);
61
62    let output = list_changed.output_with((&files,))?;
63    println!("{}", String::from_utf8_lossy(&output.stdout));
64
65    // Stream-spec mode: diff a privately edited stream spec against the head
66    // version of another stream. `stream_spec_mode` transitions the command
67    // into `StreamSpecMode`; the stream spec is passed to `spawn_with`.
68    #[cfg(not(feature = "lt2019_1"))]
69    {
70        let mut stream_diff = p4.diff().stream_spec_mode();
71        let mut child = stream_diff.spawn_with(("//streams/main@head",))?;
72        child.wait()?;
73    }
74
75    Ok(())
76}