Debian Codegraph @

src/subcommands/diff.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#;
2
3 use craterust-analyzer cargo git-delta 0.19.2 crate/::configrust-analyzer cargo git-delta 0.19.2 config/::{selfrust-analyzer cargo git-delta 0.19.2 config/};
4
5 use craterust-analyzer cargo git-delta 0.19.2 crate/::utilsrust-analyzer cargo git-delta 0.19.2 utils/::gitrust-analyzer cargo git-delta 0.19.2 utils/git/::retrieve_git_versionrust-analyzer cargo git-delta 0.19.2 utils/git/retrieve_git_version().;
6
7 use craterust-analyzer cargo git-delta 0.19.2 crate/::subcommandsrust-analyzer cargo git-delta 0.19.2 subcommands/::{SubCmdKindrust-analyzer cargo git-delta 0.19.2 subcommands/external/SubCmdKind#, SubCommandrust-analyzer cargo git-delta 0.19.2 subcommands/external/SubCommand#};
8 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#;
9
10 /// Build `git diff` command for the files provided on the command line. Fall back to
11 /// `diff` if the supplied "files" use process substitution.
12 pub fn build_diff_cmdrust-analyzer cargo git-delta 0.19.2 subcommands/diff/build_diff_cmd().(
13 minus_file: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#,
14 plus_file: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#,
15 config: &configrust-analyzer cargo git-delta 0.19.2 config/::Configrust-analyzer cargo git-delta 0.19.2 config/Config#,
16 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<SubCommandrust-analyzer cargo git-delta 0.19.2 subcommands/external/SubCommand#, i32> {
17 // suppress `dead_code` warning, values are accessed via `get_one::<PathBuf>("plus/minus_file")`
18 debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(config.minus_filerust-analyzer cargo git-delta 0.19.2 config/Config#minus_file..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() == minus_file);
19 debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(config.plus_filerust-analyzer cargo git-delta 0.19.2 config/Config#plus_file..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() == plus_file);
20
21 let mut diff_args = match shell_wordsrust-analyzer cargo shell-words 1.1.0 crate/::splitrust-analyzer cargo shell-words 1.1.0 split().(config.diff_argsrust-analyzer cargo git-delta 0.19.2 config/Config#diff_args..trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().()) {
22 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(words) => words,
23 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
24 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("Failed to parse diff args: {}: {err}", config.diff_argsrust-analyzer cargo git-delta 0.19.2 config/Config#diff_args.);
25 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(config.error_exit_coderust-analyzer cargo git-delta 0.19.2 config/Config#error_exit_code.);
26 }
27 };
28 // Permit e.g. -@U1
29 if diff_args
30 .firstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]first().()
31 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|arg| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().arg.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().arg.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('-'))
32 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(false)
33 {
34 diff_args[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut(). = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("-{}", diff_args[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().)
35 }
36
37 let via_process_substitution =
38 |f: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#| f.starts_withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]starts_with().("/proc/self/fd/") || f.starts_withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]starts_with().("/dev/fd/");
39
40 // https://stackoverflow.com/questions/22706714/why-does-git-diff-not-work-with-process-substitution
41 // git <2.42 does not support process substitution
42 let (differ, mut diff_cmd) = match retrieve_git_versionrust-analyzer cargo git-delta 0.19.2 utils/git/retrieve_git_version().() {
43 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(version)
44 if version rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialOrd<Self>`]ge().(2, 42)
45 || !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().(via_process_substitution(minus_file)
46 || via_process_substitution(plus_file)) =>
47 {
48 (
49 SubCmdKindrust-analyzer cargo git-delta 0.19.2 subcommands/external/SubCmdKind#::GitDiffrust-analyzer cargo git-delta 0.19.2 subcommands/external/SubCmdKind#GitDiff#,
50 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["git", "diff", "--no-index", "--color"],
51 )
52 }
53 _ => (
54 SubCmdKindrust-analyzer cargo git-delta 0.19.2 subcommands/external/SubCmdKind#::Diffrust-analyzer cargo git-delta 0.19.2 subcommands/external/SubCmdKind#Diff#,
55 if diff_args_set_unified_contextrust-analyzer cargo git-delta 0.19.2 subcommands/diff/diff_args_set_unified_context().(&diff_args) {
56 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["diff"]
57 } else {
58 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["diff", "-U3"]
59 },
60 ),
61 };
62
63 diff_cmd.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(
64 diff_args
65 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
66 .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|s| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().s.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().())
67 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().),
68 );
69 diff_cmd.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().("--");
70 let mut diff_cmd = diff_cmd.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`From<&T>`]from().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>();
71 diff_cmd.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(minus_file.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
72 diff_cmd.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(plus_file.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
73 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(SubCommandrust-analyzer cargo git-delta 0.19.2 subcommands/external/SubCommand#::newrust-analyzer cargo git-delta 0.19.2 subcommands/external/impl#[SubCommand]new().(differ, diff_cmd))
74 }
75
76 /// Do the user-supplied `diff` args set the unified context?
77 fn diff_args_set_unified_contextrust-analyzer cargo git-delta 0.19.2 subcommands/diff/diff_args_set_unified_context().<I, S>(args: I) -> bool
78 where
79 I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = S>,
80 S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str>,
81 {
82 // This function is applied to `diff` args; not `git diff`.
83 for arg in args {
84 let arg = arg.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().();
85 if arg rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."-u" || arg rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."-U" {
86 // diff allows a space after -U (git diff does not)
87 return true;
88 }
89 if (arg.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("-U") || arg.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("-u"))
90 && arg.split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_at().(2).1.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u32>().is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()
91 {
92 return true;
93 }
94 }
95 false
96 }
97
98 #[cfg(test)]
99 mod main_testsrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ {
100 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#;
101 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#;
102
103 use superrust-analyzer cargo git-delta 0.19.2 subcommands/diff/::diff_args_set_unified_contextrust-analyzer cargo git-delta 0.19.2 subcommands/diff/diff_args_set_unified_context().;
104
105 use rstestrust-analyzer cargo rstest 0.21.0 crate/::rstestrust-analyzer cargo rstest_macros 0.21.0 rstest:;
106
107 #[rstestrust-analyzer cargo rstest_macros 0.21.0 rstest:]
108 #[case(&["-u"], true)]
109 #[case(&["-u7"], true)]
110 #[case(&["-u77"], true)]
111 #[case(&["-ux"], false)]
112 #[case(&["-U"], true)]
113 #[case(&["-U7"], true)]
114 #[case(&["-U77"], true)]
115 #[case(&["-Ux"], false)]
116 fn test_unified_diff_arg_is_detected_in_diff_argsrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/test_unified_diff_arg_is_detected_in_diff_args().(
117 #[case] diff_args: &[&str],
118 #[case] expected: bool,
119 ) {
120 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(diff_args_set_unified_contextrust-analyzer cargo git-delta 0.19.2 subcommands/diff/diff_args_set_unified_context().(diff_args), expected)
121 }
122
123 enum ExpectDiffrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff# {
124 Yesrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#Yes#,
125 Norust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#No#,
126 }
127
128 #[cfg(not(target_os = "windows"))]
129 #[rstestrust-analyzer cargo rstest_macros 0.21.0 rstest:]
130 // #[case("/dev/null", "/dev/null", ExpectDiff::No)] https://github.com/dandavison/delta/pull/546#issuecomment-835852373
131 #[case("/etc/group", "/etc/passwd", ExpectDiffrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#::Yesrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#Yes#)]
132 #[case("/dev/null", "/etc/passwd", ExpectDiffrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#::Yesrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#Yes#)]
133 #[case("/etc/passwd", "/etc/passwd", ExpectDiffrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#::Norust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#No#)]
134 fn test_diff_real_filesrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/test_diff_real_files().(
135 #[case] file_a: &str,
136 #[case] file_b: &str,
137 #[case] expect_diff: ExpectDiffrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#,
138 #[values(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![], vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["-@''"], vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["-@-u"], vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["-@-U99"], vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["-@-U0"])] args: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<
139 &str,
140 >,
141 ) {
142 let mut writer = Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/impl#[`Cursor<T>`]new().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]);
143 let mut runargs = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`From<&T>`]from().(file_a), OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`From<&T>`]from().(file_b)];
144 runargs.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(args.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`From<&T>`]from().));
145 let exit_code = craterust-analyzer cargo git-delta 0.19.2 crate/::run_apprust-analyzer cargo git-delta 0.19.2 run_app().(runargs, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&mut writer));
146
147 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
148 exit_code.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
149 match expect_diff {
150 ExpectDiffrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#::Yesrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#Yes# => 1,
151 ExpectDiffrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#::Norust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#No# => 0,
152 }
153 );
154 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
155 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(writer.get_refrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/impl#[`Cursor<T>`]get_ref().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() != "",
156 match expect_diff {
157 ExpectDiffrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#::Yesrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#Yes# => true,
158 ExpectDiffrust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#::Norust-analyzer cargo git-delta 0.19.2 subcommands/diff/main_tests/ExpectDiff#No# => false,
159 }
160 );
161 }
162 }