Skip to main content

jj_cli/
diff_util.rs

1// Copyright 2020-2022 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::borrow::Cow;
16use std::cmp::max;
17use std::future;
18use std::io;
19use std::iter;
20use std::ops::Range;
21use std::path::Path;
22use std::path::PathBuf;
23
24use bstr::BStr;
25use bstr::BString;
26use clap_complete::ArgValueCandidates;
27use futures::StreamExt as _;
28use futures::TryStreamExt as _;
29use futures::stream::BoxStream;
30use itertools::Itertools as _;
31use jj_lib::backend::BackendError;
32use jj_lib::backend::BackendResult;
33use jj_lib::backend::CommitId;
34use jj_lib::backend::CopyRecord;
35use jj_lib::backend::TreeValue;
36use jj_lib::commit::Commit;
37use jj_lib::config::ConfigGetError;
38use jj_lib::conflict_labels::ConflictLabels;
39use jj_lib::conflicts::ConflictMarkerStyle;
40use jj_lib::conflicts::ConflictMaterializeOptions;
41use jj_lib::conflicts::MaterializedTreeDiffEntry;
42use jj_lib::conflicts::MaterializedTreeValue;
43use jj_lib::conflicts::materialize_merge_result_to_bytes;
44use jj_lib::conflicts::materialized_diff_stream;
45use jj_lib::copies::CopiesTreeDiffEntry;
46use jj_lib::copies::CopiesTreeDiffEntryPath;
47use jj_lib::copies::CopyOperation;
48use jj_lib::copies::CopyRecords;
49use jj_lib::diff::ContentDiff;
50use jj_lib::diff::DiffHunk;
51use jj_lib::diff::DiffHunkKind;
52use jj_lib::diff_presentation::DiffTokenType;
53use jj_lib::diff_presentation::FileContent;
54use jj_lib::diff_presentation::LineCompareMode;
55use jj_lib::diff_presentation::diff_by_line;
56use jj_lib::diff_presentation::file_content_for_diff;
57use jj_lib::diff_presentation::unified::DiffLineType;
58use jj_lib::diff_presentation::unified::UnifiedDiffError;
59use jj_lib::diff_presentation::unified::git_diff_part;
60use jj_lib::diff_presentation::unified::unified_diff_hunks;
61use jj_lib::diff_presentation::unzip_diff_hunks_to_lines;
62use jj_lib::files;
63use jj_lib::files::ConflictDiffHunk;
64use jj_lib::files::DiffLineHunkSide;
65use jj_lib::files::DiffLineIterator;
66use jj_lib::files::DiffLineNumber;
67use jj_lib::matchers::Matcher;
68use jj_lib::merge::Diff;
69use jj_lib::merge::Merge;
70use jj_lib::merge::MergeBuilder;
71use jj_lib::merge::MergedTreeValue;
72use jj_lib::merged_tree::MergedTree;
73use jj_lib::repo::Repo;
74use jj_lib::repo_path::InvalidRepoPathError;
75use jj_lib::repo_path::RepoPath;
76use jj_lib::repo_path::RepoPathUiConverter;
77use jj_lib::rewrite::rebase_to_dest_parent;
78use jj_lib::settings::UserSettings;
79use jj_lib::store::Store;
80use thiserror::Error;
81use tracing::instrument;
82use unicode_width::UnicodeWidthStr as _;
83
84use crate::command_error::CommandError;
85use crate::command_error::cli_error;
86use crate::commit_templater;
87use crate::config::CommandNameAndArgs;
88use crate::formatter::Formatter;
89use crate::formatter::FormatterExt as _;
90use crate::merge_tools;
91use crate::merge_tools::DiffGenerateError;
92use crate::merge_tools::DiffToolMode;
93use crate::merge_tools::ExternalMergeTool;
94use crate::merge_tools::generate_diff;
95use crate::merge_tools::invoke_external_diff;
96use crate::merge_tools::new_utf8_temp_dir;
97use crate::templater::TemplateRenderer;
98use crate::text_util;
99use crate::ui::Ui;
100
101#[derive(clap::Args, Clone, Debug)]
102#[command(next_help_heading = "Diff Formatting Options")]
103#[command(group(clap::ArgGroup::new("short-format").args(&["summary", "stat", "types", "name_only"])))]
104#[command(group(clap::ArgGroup::new("long-format").args(&["git", "color_words"])))]
105pub struct DiffFormatArgs {
106    /// For each path, show only whether it was modified, added, or deleted
107    #[arg(long, short)]
108    pub summary: bool,
109
110    /// Show a histogram of the changes
111    #[arg(long)]
112    pub stat: bool,
113
114    /// For each path, show only its type before and after
115    ///
116    /// The diff is shown as two letters. The first letter indicates the type
117    /// before and the second letter indicates the type after. '-' indicates
118    /// that the path was not present, 'F' represents a regular file, `L'
119    /// represents a symlink, 'C' represents a conflict, and 'G' represents a
120    /// Git submodule.
121    #[arg(long)]
122    pub types: bool,
123
124    /// For each path, show only its path
125    ///
126    /// Typically useful for shell commands like:
127    ///    `jj diff -r @- --name-only | xargs perl -pi -e's/OLD/NEW/g`
128    #[arg(long)]
129    pub name_only: bool,
130
131    /// Show a Git-format diff
132    #[arg(long)]
133    pub git: bool,
134
135    /// Show a word-level diff with changes indicated only by color
136    #[arg(long)]
137    pub color_words: bool,
138
139    /// Generate diff by external command
140    ///
141    /// A builtin format can also be specified as `:<name>`. For example,
142    /// `--tool=:git` is equivalent to `--git`.
143    #[arg(long)]
144    #[arg(add = ArgValueCandidates::new(crate::complete::diff_formatters))]
145    pub tool: Option<String>,
146
147    /// Number of lines of context to show
148    #[arg(long)]
149    context: Option<usize>,
150
151    // Short flags are set by command to avoid future conflicts.
152    /// Ignore whitespace when comparing lines.
153    #[arg(long)] // short = 'w'
154    ignore_all_space: bool,
155
156    /// Ignore changes in amount of whitespace when comparing lines.
157    #[arg(long, conflicts_with = "ignore_all_space")] // short = 'b'
158    ignore_space_change: bool,
159}
160
161#[derive(Clone, Debug, Eq, PartialEq)]
162pub enum DiffFormat {
163    // Non-trivial parameters are boxed in order to keep the variants small
164    Summary,
165    Stat(Box<DiffStatOptions>),
166    Types,
167    NameOnly,
168    Git(Box<UnifiedDiffOptions>),
169    ColorWords(Box<ColorWordsDiffOptions>),
170    Tool(Box<ExternalMergeTool>),
171}
172
173#[derive(Clone, Copy, Debug, Eq, PartialEq)]
174enum BuiltinFormatKind {
175    Summary,
176    Stat,
177    Types,
178    NameOnly,
179    Git,
180    ColorWords,
181}
182
183impl BuiltinFormatKind {
184    // Alternatively, we could use or vendor one of the crates `strum`,
185    // `enum-iterator`, or `variant_count` (for a check that the length of the array
186    // is correct). The latter is very simple and is also a nightly feature.
187    const ALL_VARIANTS: &[Self] = &[
188        Self::Summary,
189        Self::Stat,
190        Self::Types,
191        Self::NameOnly,
192        Self::Git,
193        Self::ColorWords,
194    ];
195
196    fn from_name(name: &str) -> Result<Self, String> {
197        match name {
198            "summary" => Ok(Self::Summary),
199            "stat" => Ok(Self::Stat),
200            "types" => Ok(Self::Types),
201            "name-only" => Ok(Self::NameOnly),
202            "git" => Ok(Self::Git),
203            "color-words" => Ok(Self::ColorWords),
204            _ => Err(format!("Invalid builtin diff format: {name}")),
205        }
206    }
207
208    fn short_from_args(args: &DiffFormatArgs) -> Option<Self> {
209        if args.summary {
210            Some(Self::Summary)
211        } else if args.stat {
212            Some(Self::Stat)
213        } else if args.types {
214            Some(Self::Types)
215        } else if args.name_only {
216            Some(Self::NameOnly)
217        } else {
218            None
219        }
220    }
221
222    fn long_from_args(args: &DiffFormatArgs) -> Option<Self> {
223        if args.git {
224            Some(Self::Git)
225        } else if args.color_words {
226            Some(Self::ColorWords)
227        } else {
228            None
229        }
230    }
231
232    fn is_short(self) -> bool {
233        match self {
234            Self::Summary | Self::Stat | Self::Types | Self::NameOnly => true,
235            Self::Git | Self::ColorWords => false,
236        }
237    }
238
239    fn to_arg_name(self) -> &'static str {
240        match self {
241            Self::Summary => "summary",
242            Self::Stat => "stat",
243            Self::Types => "types",
244            Self::NameOnly => "name-only",
245            Self::Git => "git",
246            Self::ColorWords => "color-words",
247        }
248    }
249
250    fn to_format(
251        self,
252        settings: &UserSettings,
253        args: &DiffFormatArgs,
254    ) -> Result<DiffFormat, ConfigGetError> {
255        match self {
256            Self::Summary => Ok(DiffFormat::Summary),
257            Self::Stat => {
258                let mut options = DiffStatOptions::default();
259                options.merge_args(args);
260                Ok(DiffFormat::Stat(Box::new(options)))
261            }
262            Self::Types => Ok(DiffFormat::Types),
263            Self::NameOnly => Ok(DiffFormat::NameOnly),
264            Self::Git => {
265                let mut options = UnifiedDiffOptions::from_settings(settings)?;
266                options.merge_args(args);
267                Ok(DiffFormat::Git(Box::new(options)))
268            }
269            Self::ColorWords => {
270                let mut options = ColorWordsDiffOptions::from_settings(settings)?;
271                options.merge_args(args);
272                Ok(DiffFormat::ColorWords(Box::new(options)))
273            }
274        }
275    }
276}
277
278/// Returns the list of builtin diff format names such as `:git`
279pub fn all_builtin_diff_format_names() -> Vec<String> {
280    BuiltinFormatKind::ALL_VARIANTS
281        .iter()
282        .map(|kind| format!(":{}", kind.to_arg_name()))
283        .collect()
284}
285
286fn diff_formatter_tool(
287    settings: &UserSettings,
288    name: &str,
289) -> Result<Option<ExternalMergeTool>, CommandError> {
290    let maybe_tool = merge_tools::get_external_tool_config(settings, name)?;
291    if let Some(tool) = &maybe_tool
292        && tool.diff_args.is_empty()
293    {
294        return Err(cli_error(format!(
295            "The tool `{name}` cannot be used for diff formatting"
296        )));
297    }
298    Ok(maybe_tool)
299}
300
301/// Returns a list of requested diff formats, which will never be empty.
302pub fn diff_formats_for(
303    settings: &UserSettings,
304    args: &DiffFormatArgs,
305) -> Result<Vec<DiffFormat>, CommandError> {
306    let formats = diff_formats_from_args(settings, args)?;
307    if formats.iter().all(|f| f.is_none()) {
308        Ok(vec![default_diff_format(settings, args)?])
309    } else {
310        Ok(formats.into_iter().flatten().collect())
311    }
312}
313
314/// Returns a list of requested diff formats for log-like commands, which may be
315/// empty.
316pub fn diff_formats_for_log(
317    settings: &UserSettings,
318    args: &DiffFormatArgs,
319    patch: bool,
320) -> Result<Vec<DiffFormat>, CommandError> {
321    let [short_format, mut long_format] = diff_formats_from_args(settings, args)?;
322    // --patch implies default if no "long" format is specified
323    if patch && long_format.is_none() {
324        // TODO: maybe better to error out if the configured default isn't a
325        // "long" format?
326        let default_format = default_diff_format(settings, args)?;
327        if short_format.as_ref() != Some(&default_format) {
328            long_format = Some(default_format);
329        }
330    }
331    Ok([short_format, long_format].into_iter().flatten().collect())
332}
333
334fn diff_formats_from_args(
335    settings: &UserSettings,
336    args: &DiffFormatArgs,
337) -> Result<[Option<DiffFormat>; 2], CommandError> {
338    let short_kind = BuiltinFormatKind::short_from_args(args);
339    let long_kind = BuiltinFormatKind::long_from_args(args);
340    let mut short_format = short_kind
341        .map(|kind| kind.to_format(settings, args))
342        .transpose()?;
343    let mut long_format = long_kind
344        .map(|kind| kind.to_format(settings, args))
345        .transpose()?;
346    if let Some(name) = &args.tool {
347        let ensure_new = |old_kind: Option<BuiltinFormatKind>| match old_kind {
348            Some(old) => Err(cli_error(format!(
349                "--tool={name} cannot be used with --{old}",
350                old = old.to_arg_name()
351            ))),
352            None => Ok(()),
353        };
354        if let Some(name) = name.strip_prefix(':') {
355            let kind = BuiltinFormatKind::from_name(name).map_err(cli_error)?;
356            let format = kind.to_format(settings, args)?;
357            if kind.is_short() {
358                ensure_new(short_kind)?;
359                short_format = Some(format);
360            } else {
361                ensure_new(long_kind)?;
362                long_format = Some(format);
363            }
364        } else {
365            ensure_new(long_kind)?;
366            let tool = diff_formatter_tool(settings, name)?
367                .unwrap_or_else(|| ExternalMergeTool::with_program(name));
368            long_format = Some(DiffFormat::Tool(Box::new(tool)));
369        }
370    }
371    Ok([short_format, long_format])
372}
373
374fn default_diff_format(
375    settings: &UserSettings,
376    args: &DiffFormatArgs,
377) -> Result<DiffFormat, CommandError> {
378    let tool_args: CommandNameAndArgs = settings.get("ui.diff-formatter")?;
379    if let Some(name) = tool_args.as_str().and_then(|s| s.strip_prefix(':')) {
380        Ok(BuiltinFormatKind::from_name(name)
381            .map_err(|err| ConfigGetError::Type {
382                name: "ui.diff-formatter".to_owned(),
383                error: err.into(),
384                source_path: None,
385            })?
386            .to_format(settings, args)?)
387    } else {
388        let tool = if let Some(name) = tool_args.as_str() {
389            diff_formatter_tool(settings, name)?
390        } else {
391            None
392        }
393        .unwrap_or_else(|| ExternalMergeTool::with_diff_args(&tool_args));
394        Ok(DiffFormat::Tool(Box::new(tool)))
395    }
396}
397
398#[derive(Debug, Error)]
399pub enum DiffRenderError {
400    #[error("Failed to generate diff")]
401    DiffGenerate(#[source] DiffGenerateError),
402    #[error(transparent)]
403    Backend(#[from] BackendError),
404    #[error("Access denied to {path}")]
405    AccessDenied {
406        path: String,
407        source: Box<dyn std::error::Error + Send + Sync>,
408    },
409    #[error(transparent)]
410    InvalidRepoPath(#[from] InvalidRepoPathError),
411    #[error(transparent)]
412    Io(#[from] io::Error),
413}
414
415impl From<UnifiedDiffError> for DiffRenderError {
416    fn from(value: UnifiedDiffError) -> Self {
417        match value {
418            UnifiedDiffError::Backend(error) => Self::Backend(error),
419            UnifiedDiffError::AccessDenied { path, source } => Self::AccessDenied { path, source },
420        }
421    }
422}
423
424/// Configuration and environment to render textual diff.
425pub struct DiffRenderer<'a> {
426    repo: &'a dyn Repo,
427    path_converter: &'a RepoPathUiConverter,
428    conflict_marker_style: ConflictMarkerStyle,
429    formats: Vec<DiffFormat>,
430}
431
432impl<'a> DiffRenderer<'a> {
433    pub fn new(
434        repo: &'a dyn Repo,
435        path_converter: &'a RepoPathUiConverter,
436        conflict_marker_style: ConflictMarkerStyle,
437        formats: Vec<DiffFormat>,
438    ) -> Self {
439        Self {
440            repo,
441            path_converter,
442            conflict_marker_style,
443            formats,
444        }
445    }
446
447    /// Generates diff between `trees`.
448    pub async fn show_diff(
449        &self,
450        ui: &Ui, // TODO: remove Ui dependency if possible
451        formatter: &mut dyn Formatter,
452        trees: Diff<&MergedTree>,
453        matcher: &dyn Matcher,
454        copy_records: &CopyRecords,
455        width: usize,
456    ) -> Result<(), DiffRenderError> {
457        let mut formatter = formatter.labeled("diff");
458        self.show_diff_trees(ui, *formatter, trees, matcher, copy_records, width)
459            .await
460    }
461
462    async fn show_diff_trees(
463        &self,
464        ui: &Ui,
465        formatter: &mut dyn Formatter,
466        trees: Diff<&MergedTree>,
467        matcher: &dyn Matcher,
468        copy_records: &CopyRecords,
469        width: usize,
470    ) -> Result<(), DiffRenderError> {
471        let diff_stream = || {
472            trees
473                .before
474                .diff_stream_with_copies(trees.after, matcher, copy_records)
475        };
476        let conflict_labels = trees.map(|tree| tree.labels());
477
478        let store = self.repo.store();
479        let path_converter = self.path_converter;
480        for format in &self.formats {
481            match format {
482                DiffFormat::Summary => {
483                    let tree_diff = diff_stream();
484                    show_diff_summary(*formatter.labeled("summary"), tree_diff, path_converter)
485                        .await?;
486                }
487                DiffFormat::Stat(options) => {
488                    let tree_diff = diff_stream();
489                    let stats =
490                        DiffStats::calculate(store, tree_diff, options, self.conflict_marker_style)
491                            .await?;
492                    show_diff_stats(*formatter.labeled("stat"), &stats, path_converter, width)?;
493                }
494                DiffFormat::Types => {
495                    let tree_diff = diff_stream();
496                    show_types(*formatter.labeled("types"), tree_diff, path_converter).await?;
497                }
498                DiffFormat::NameOnly => {
499                    let tree_diff = diff_stream();
500                    show_names(*formatter.labeled("name_only"), tree_diff, path_converter).await?;
501                }
502                DiffFormat::Git(options) => {
503                    let tree_diff = diff_stream();
504                    show_git_diff(
505                        *formatter.labeled("git"),
506                        store,
507                        tree_diff,
508                        conflict_labels,
509                        options,
510                        self.conflict_marker_style,
511                    )
512                    .await?;
513                }
514                DiffFormat::ColorWords(options) => {
515                    let tree_diff = diff_stream();
516                    show_color_words_diff(
517                        *formatter.labeled("color_words"),
518                        store,
519                        tree_diff,
520                        conflict_labels,
521                        path_converter,
522                        options,
523                        self.conflict_marker_style,
524                    )
525                    .await?;
526                }
527                DiffFormat::Tool(tool) => {
528                    match tool.diff_invocation_mode {
529                        DiffToolMode::FileByFile => {
530                            let tree_diff = diff_stream();
531                            show_file_by_file_diff(
532                                ui,
533                                formatter,
534                                store,
535                                tree_diff,
536                                conflict_labels,
537                                path_converter,
538                                tool,
539                                self.conflict_marker_style,
540                                width,
541                            )
542                            .await
543                        }
544                        DiffToolMode::Dir => {
545                            let mut writer = formatter.raw()?;
546                            generate_diff(
547                                ui,
548                                writer.as_mut(),
549                                trees,
550                                matcher,
551                                tool,
552                                self.conflict_marker_style,
553                                width,
554                            )
555                            .await
556                            .map_err(DiffRenderError::DiffGenerate)
557                        }
558                    }?;
559                }
560            }
561        }
562        Ok(())
563    }
564
565    fn show_diff_commit_descriptions(
566        &self,
567        formatter: &mut dyn Formatter,
568        descriptions: Diff<&Merge<&str>>,
569    ) -> Result<(), DiffRenderError> {
570        if !descriptions.is_changed() {
571            return Ok(());
572        }
573        const DUMMY_PATH: &str = "JJ-COMMIT-DESCRIPTION";
574        let materialize_options = ConflictMaterializeOptions {
575            marker_style: self.conflict_marker_style,
576            marker_len: None,
577            merge: self.repo.store().merge_options().clone(),
578        };
579        for format in &self.formats {
580            match format {
581                // Omit diff from "short" formats. Printing dummy file path
582                // wouldn't be useful.
583                DiffFormat::Summary
584                | DiffFormat::Stat(_)
585                | DiffFormat::Types
586                | DiffFormat::NameOnly => {}
587                DiffFormat::Git(options) => {
588                    // Git format must be parsable, so use dummy file path.
589                    show_git_diff_texts(
590                        formatter,
591                        Diff::new(DUMMY_PATH, DUMMY_PATH),
592                        descriptions,
593                        options,
594                        &materialize_options,
595                    )?;
596                }
597                DiffFormat::ColorWords(options) => {
598                    writeln!(formatter.labeled("header"), "Modified commit description:")?;
599                    show_color_words_diff_hunks(
600                        formatter,
601                        descriptions,
602                        Diff::new(&ConflictLabels::unlabeled(), &ConflictLabels::unlabeled()),
603                        options,
604                        &materialize_options,
605                    )?;
606                }
607                DiffFormat::Tool(_) => {
608                    // TODO: materialize commit description as file?
609                }
610            }
611        }
612        Ok(())
613    }
614
615    /// Generates diff between `from_commits` and `to_commit` based off their
616    /// parents. The `from_commits` will temporarily be rebased onto the
617    /// `to_commit` parents to exclude unrelated changes.
618    pub async fn show_inter_diff(
619        &self,
620        ui: &Ui,
621        formatter: &mut dyn Formatter,
622        from_commits: &[Commit],
623        to_commit: &Commit,
624        matcher: &dyn Matcher,
625        width: usize,
626    ) -> Result<(), DiffRenderError> {
627        let mut formatter = formatter.labeled("diff");
628        let from_description = if from_commits.is_empty() {
629            Merge::resolved("")
630        } else {
631            // TODO: use common predecessor as the base description?
632            MergeBuilder::from_iter(itertools::intersperse(
633                from_commits.iter().map(|c| c.description()),
634                "",
635            ))
636            .build()
637            .simplify()
638        };
639        let to_description = Merge::resolved(to_commit.description());
640        let from_tree = rebase_to_dest_parent(self.repo, from_commits, to_commit).await?;
641        let to_tree = to_commit.tree();
642        let copy_records = CopyRecords::default(); // TODO
643        self.show_diff_commit_descriptions(
644            *formatter,
645            Diff::new(&from_description, &to_description),
646        )?;
647        self.show_diff_trees(
648            ui,
649            *formatter,
650            Diff::new(&from_tree, &to_tree),
651            matcher,
652            &copy_records,
653            width,
654        )
655        .await
656    }
657
658    /// Generates diff of the given `commit` compared to its parents.
659    pub async fn show_patch(
660        &self,
661        ui: &Ui,
662        formatter: &mut dyn Formatter,
663        commit: &Commit,
664        matcher: &dyn Matcher,
665        width: usize,
666    ) -> Result<(), DiffRenderError> {
667        let from_tree = commit.parent_tree(self.repo).await?;
668        let to_tree = commit.tree();
669        let mut copy_records = CopyRecords::default();
670        for parent_id in commit.parent_ids() {
671            let records =
672                get_copy_records(self.repo.store(), parent_id, commit.id(), matcher).await?;
673            copy_records.add_records(records);
674        }
675        self.show_diff(
676            ui,
677            formatter,
678            Diff::new(&from_tree, &to_tree),
679            matcher,
680            &copy_records,
681            width,
682        )
683        .await
684    }
685}
686
687pub async fn get_copy_records(
688    store: &Store,
689    root: &CommitId,
690    head: &CommitId,
691    matcher: &dyn Matcher,
692) -> BackendResult<Vec<CopyRecord>> {
693    // TODO: teach backend about matching path prefixes?
694    let stream = store.get_copy_records(None, root, head)?;
695    // TODO: test record.source as well? should be AND-ed or OR-ed?
696    stream
697        .try_filter(|record| future::ready(matcher.matches(&record.target)))
698        .try_collect()
699        .await
700}
701
702/// How conflicts are processed and rendered in diffs.
703#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Deserialize)]
704#[serde(rename_all = "kebab-case")]
705pub enum ConflictDiffMethod {
706    /// Compares materialized contents.
707    #[default]
708    Materialize,
709    /// Compares individual pairs of left and right contents.
710    Pair,
711}
712
713#[derive(Clone, Debug, Default, Eq, PartialEq)]
714pub struct LineDiffOptions {
715    /// How equivalence of lines is tested.
716    pub compare_mode: LineCompareMode,
717    // TODO: add --ignore-blank-lines, etc. which aren't mutually exclusive.
718}
719
720impl LineDiffOptions {
721    fn merge_args(&mut self, args: &DiffFormatArgs) {
722        self.compare_mode = if args.ignore_all_space {
723            LineCompareMode::IgnoreAllSpace
724        } else if args.ignore_space_change {
725            LineCompareMode::IgnoreSpaceChange
726        } else {
727            LineCompareMode::Exact
728        };
729    }
730}
731
732#[derive(Clone, Debug, Eq, PartialEq)]
733pub struct ColorWordsDiffOptions {
734    /// How conflicts are processed and rendered.
735    pub conflict: ConflictDiffMethod,
736    /// Number of context lines to show.
737    pub context: usize,
738    /// How lines are tokenized and compared.
739    pub line_diff: LineDiffOptions,
740    /// Maximum number of removed/added word alternation to inline.
741    pub max_inline_alternation: Option<usize>,
742}
743
744impl ColorWordsDiffOptions {
745    pub fn from_settings(settings: &UserSettings) -> Result<Self, ConfigGetError> {
746        let max_inline_alternation = {
747            let name = "diff.color-words.max-inline-alternation";
748            match settings.get_int(name)? {
749                -1 => None, // unlimited
750                n => Some(usize::try_from(n).map_err(|err| ConfigGetError::Type {
751                    name: name.to_owned(),
752                    error: err.into(),
753                    source_path: None,
754                })?),
755            }
756        };
757        Ok(Self {
758            conflict: settings.get("diff.color-words.conflict")?,
759            context: settings.get("diff.color-words.context")?,
760            line_diff: LineDiffOptions::default(),
761            max_inline_alternation,
762        })
763    }
764
765    fn merge_args(&mut self, args: &DiffFormatArgs) {
766        if let Some(context) = args.context {
767            self.context = context;
768        }
769        self.line_diff.merge_args(args);
770    }
771}
772
773fn show_color_words_diff_hunks<T: AsRef<[u8]>>(
774    formatter: &mut dyn Formatter,
775    contents: Diff<&Merge<T>>,
776    conflict_labels: Diff<&ConflictLabels>,
777    options: &ColorWordsDiffOptions,
778    materialize_options: &ConflictMaterializeOptions,
779) -> io::Result<()> {
780    let line_number = DiffLineNumber { left: 1, right: 1 };
781    let labels = Diff::new("removed", "added");
782    if let (Some(left), Some(right)) = (contents.before.as_resolved(), contents.after.as_resolved())
783    {
784        let contents = Diff::new(left, right).map(BStr::new);
785        show_color_words_resolved_hunks(formatter, contents, line_number, labels, options)?;
786        return Ok(());
787    }
788    match options.conflict {
789        ConflictDiffMethod::Materialize => {
790            let contents = contents.zip(conflict_labels).map(|(side, labels)| {
791                materialize_merge_result_to_bytes(side, labels, materialize_options)
792            });
793            show_color_words_resolved_hunks(
794                formatter,
795                contents.as_ref().map(BStr::new),
796                line_number,
797                labels,
798                options,
799            )?;
800        }
801        ConflictDiffMethod::Pair => {
802            let contents = contents.map(|side| files::merge(side, &materialize_options.merge));
803            show_color_words_conflict_hunks(
804                formatter,
805                contents.as_ref(),
806                line_number,
807                labels,
808                options,
809            )?;
810        }
811    }
812    Ok(())
813}
814
815fn show_color_words_conflict_hunks(
816    formatter: &mut dyn Formatter,
817    contents: Diff<&Merge<BString>>,
818    mut line_number: DiffLineNumber,
819    labels: Diff<&str>,
820    options: &ColorWordsDiffOptions,
821) -> io::Result<DiffLineNumber> {
822    let num_lefts = contents.before.as_slice().len();
823    let line_diff = diff_by_line(
824        itertools::chain(contents.before, contents.after),
825        &options.line_diff.compare_mode,
826    );
827    // Matching entries shouldn't appear consecutively in diff of two inputs.
828    // However, if the inputs have conflicts, there may be a hunk that can be
829    // resolved, resulting [matching, resolved, matching] sequence.
830    let mut contexts: Vec<Diff<&BStr>> = Vec::new();
831    let mut emitted = false;
832
833    for hunk in files::conflict_diff_hunks(line_diff.hunks(), num_lefts) {
834        match hunk.kind {
835            // There may be conflicts in matching hunk, but just pick one. It
836            // would be too verbose to show all conflict pairs as context.
837            DiffHunkKind::Matching => {
838                contexts.push(Diff::new(hunk.lefts.first(), hunk.rights.first()));
839            }
840            DiffHunkKind::Different => {
841                let num_after = if emitted { options.context } else { 0 };
842                let num_before = options.context;
843                line_number = show_color_words_context_lines(
844                    formatter,
845                    &contexts,
846                    line_number,
847                    labels,
848                    options,
849                    num_after,
850                    num_before,
851                )?;
852                contexts.clear();
853                emitted = true;
854                line_number = if let (Some(&left), Some(&right)) =
855                    (hunk.lefts.as_resolved(), hunk.rights.as_resolved())
856                {
857                    show_color_words_diff_lines(
858                        formatter,
859                        Diff::new(left, right),
860                        line_number,
861                        labels,
862                        options,
863                    )?
864                } else {
865                    show_color_words_unresolved_hunk(
866                        formatter,
867                        &hunk,
868                        line_number,
869                        labels,
870                        options,
871                    )?
872                }
873            }
874        }
875    }
876
877    let num_after = if emitted { options.context } else { 0 };
878    let num_before = 0;
879    show_color_words_context_lines(
880        formatter,
881        &contexts,
882        line_number,
883        labels,
884        options,
885        num_after,
886        num_before,
887    )
888}
889
890fn show_color_words_unresolved_hunk(
891    formatter: &mut dyn Formatter,
892    hunk: &ConflictDiffHunk,
893    line_number: DiffLineNumber,
894    labels: Diff<&str>,
895    options: &ColorWordsDiffOptions,
896) -> io::Result<DiffLineNumber> {
897    let hunk_desc = if hunk.lefts.is_resolved() {
898        "Created conflict"
899    } else if hunk.rights.is_resolved() {
900        "Resolved conflict"
901    } else {
902        "Modified conflict"
903    };
904    writeln!(formatter.labeled("hunk_header"), "<<<<<<< {hunk_desc}")?;
905
906    // Pad with identical (negative, positive) terms. It's common that one of
907    // the sides is resolved, or both sides have the same numbers of terms. If
908    // both sides are conflicts, and the numbers of terms are different, the
909    // choice of padding terms is arbitrary.
910    let num_terms = max(hunk.lefts.as_slice().len(), hunk.rights.as_slice().len());
911    let lefts = hunk.lefts.iter().enumerate();
912    let rights = hunk.rights.iter().enumerate();
913    let padded = iter::zip(
914        lefts.chain(iter::repeat((0, hunk.lefts.first()))),
915        rights.chain(iter::repeat((0, hunk.rights.first()))),
916    )
917    .take(num_terms);
918    let mut max_line_number = line_number;
919    for (i, ((left_index, &left_content), (right_index, &right_content))) in padded.enumerate() {
920        let positive = i % 2 == 0;
921        writeln!(
922            formatter.labeled("hunk_header"),
923            "{sep} left {left_name} #{left_index} to right {right_name} #{right_index}",
924            sep = if positive { "+++++++" } else { "-------" },
925            // these numbers should be compatible with the "tree-set" language #5307
926            left_name = if left_index % 2 == 0 { "side" } else { "base" },
927            left_index = left_index / 2 + 1,
928            right_name = if right_index % 2 == 0 { "side" } else { "base" },
929            right_index = right_index / 2 + 1,
930        )?;
931        let contents = Diff::new(left_content, right_content);
932        let labels = match positive {
933            true => labels,
934            false => labels.invert(),
935        };
936        // Individual hunk pair may be largely the same, so diff it again.
937        let new_line_number =
938            show_color_words_resolved_hunks(formatter, contents, line_number, labels, options)?;
939        // Take max to assign unique line numbers to trailing hunks. The line
940        // numbers can't be real anyway because preceding conflict hunks might
941        // have been resolved.
942        max_line_number.left = max(max_line_number.left, new_line_number.left);
943        max_line_number.right = max(max_line_number.right, new_line_number.right);
944    }
945
946    writeln!(formatter.labeled("hunk_header"), ">>>>>>> Conflict ends")?;
947    Ok(max_line_number)
948}
949
950fn show_color_words_resolved_hunks(
951    formatter: &mut dyn Formatter,
952    contents: Diff<&BStr>,
953    mut line_number: DiffLineNumber,
954    labels: Diff<&str>,
955    options: &ColorWordsDiffOptions,
956) -> io::Result<DiffLineNumber> {
957    let line_diff = diff_by_line(contents.into_array(), &options.line_diff.compare_mode);
958    // Matching entries shouldn't appear consecutively in diff of two inputs.
959    let mut context: Option<Diff<&BStr>> = None;
960    let mut emitted = false;
961
962    for hunk in line_diff.hunks() {
963        let &[left, right] = hunk.contents.as_slice() else {
964            panic!("hunk contents should have two sides")
965        };
966        let hunk_contents = Diff::new(left, right);
967        match hunk.kind {
968            DiffHunkKind::Matching => {
969                context = Some(hunk_contents);
970            }
971            DiffHunkKind::Different => {
972                let num_after = if emitted { options.context } else { 0 };
973                let num_before = options.context;
974                line_number = show_color_words_context_lines(
975                    formatter,
976                    context.as_slice(),
977                    line_number,
978                    labels,
979                    options,
980                    num_after,
981                    num_before,
982                )?;
983                context = None;
984                emitted = true;
985                line_number = show_color_words_diff_lines(
986                    formatter,
987                    hunk_contents,
988                    line_number,
989                    labels,
990                    options,
991                )?;
992            }
993        }
994    }
995
996    let num_after = if emitted { options.context } else { 0 };
997    let num_before = 0;
998    show_color_words_context_lines(
999        formatter,
1000        context.as_slice(),
1001        line_number,
1002        labels,
1003        options,
1004        num_after,
1005        num_before,
1006    )
1007}
1008
1009/// Prints `num_after` lines, ellipsis, and `num_before` lines.
1010fn show_color_words_context_lines(
1011    formatter: &mut dyn Formatter,
1012    contexts: &[Diff<&BStr>],
1013    mut line_number: DiffLineNumber,
1014    labels: Diff<&str>,
1015    options: &ColorWordsDiffOptions,
1016    num_after: usize,
1017    num_before: usize,
1018) -> io::Result<DiffLineNumber> {
1019    const SKIPPED_CONTEXT_LINE: &str = "    ...\n";
1020    let extract = |after: bool| -> (Vec<&[u8]>, Vec<&[u8]>, u32) {
1021        let mut lines = contexts
1022            .iter()
1023            .map(|contents| {
1024                if after {
1025                    contents.after
1026                } else {
1027                    contents.before
1028                }
1029            })
1030            .flat_map(|side| side.split_inclusive(|b| *b == b'\n'))
1031            .fuse();
1032        let after_lines = lines.by_ref().take(num_after).collect();
1033        let before_lines = lines.by_ref().rev().take(num_before + 1).collect();
1034        let num_skipped: u32 = lines.count().try_into().unwrap();
1035        (after_lines, before_lines, num_skipped)
1036    };
1037    let show = |formatter: &mut dyn Formatter,
1038                [left_lines, right_lines]: [&[&[u8]]; 2],
1039                mut line_number: DiffLineNumber| {
1040        // Whether or not left/right lines differ in whitespace, they are
1041        // "context" lines.
1042        let mut formatter = formatter.labeled("context");
1043        if left_lines == right_lines {
1044            for line in left_lines {
1045                show_color_words_line_number(
1046                    *formatter,
1047                    Diff::new(Some(line_number.left), Some(line_number.right)),
1048                    labels,
1049                )?;
1050                show_color_words_inline_hunks(
1051                    *formatter,
1052                    &[(DiffLineHunkSide::Both, line.as_ref())],
1053                    labels,
1054                )?;
1055                line_number.left += 1;
1056                line_number.right += 1;
1057            }
1058            Ok(line_number)
1059        } else {
1060            let left = left_lines.concat();
1061            let right = right_lines.concat();
1062            show_color_words_diff_lines(
1063                *formatter,
1064                Diff::new(&left, &right).map(BStr::new),
1065                line_number,
1066                labels,
1067                options,
1068            )
1069        }
1070    };
1071
1072    let (left_after, mut left_before, num_left_skipped) = extract(false);
1073    let (right_after, mut right_before, num_right_skipped) = extract(true);
1074    line_number = show(formatter, [&left_after, &right_after], line_number)?;
1075    if num_left_skipped > 0 || num_right_skipped > 0 {
1076        write!(formatter, "{SKIPPED_CONTEXT_LINE}")?;
1077        line_number.left += num_left_skipped;
1078        line_number.right += num_right_skipped;
1079        if left_before.len() > num_before {
1080            left_before.pop();
1081            line_number.left += 1;
1082        }
1083        if right_before.len() > num_before {
1084            right_before.pop();
1085            line_number.right += 1;
1086        }
1087    }
1088    left_before.reverse();
1089    right_before.reverse();
1090    line_number = show(formatter, [&left_before, &right_before], line_number)?;
1091    Ok(line_number)
1092}
1093
1094fn show_color_words_diff_lines(
1095    formatter: &mut dyn Formatter,
1096    contents: Diff<&BStr>,
1097    mut line_number: DiffLineNumber,
1098    labels: Diff<&str>,
1099    options: &ColorWordsDiffOptions,
1100) -> io::Result<DiffLineNumber> {
1101    let word_diff_hunks = ContentDiff::by_word(contents.into_array())
1102        .hunks()
1103        .collect_vec();
1104    let can_inline = match options.max_inline_alternation {
1105        None => true,     // unlimited
1106        Some(0) => false, // no need to count alternation
1107        Some(max_num) => {
1108            let groups = split_diff_hunks_by_matching_newline(&word_diff_hunks);
1109            groups.map(count_diff_alternation).max().unwrap_or(0) <= max_num
1110        }
1111    };
1112    if can_inline {
1113        let mut diff_line_iter =
1114            DiffLineIterator::with_line_number(word_diff_hunks.iter(), line_number);
1115        for diff_line in diff_line_iter.by_ref() {
1116            show_color_words_line_number(
1117                formatter,
1118                Diff::new(
1119                    diff_line
1120                        .has_left_content()
1121                        .then_some(diff_line.line_number.left),
1122                    diff_line
1123                        .has_right_content()
1124                        .then_some(diff_line.line_number.right),
1125                ),
1126                labels,
1127            )?;
1128            show_color_words_inline_hunks(formatter, &diff_line.hunks, labels)?;
1129        }
1130        line_number = diff_line_iter.next_line_number();
1131    } else {
1132        let lines = unzip_diff_hunks_to_lines(&word_diff_hunks);
1133        for tokens in &lines.before {
1134            show_color_words_line_number(
1135                formatter,
1136                Diff::new(Some(line_number.left), None),
1137                labels,
1138            )?;
1139            show_color_words_single_sided_line(formatter, tokens, labels.before)?;
1140            line_number.left += 1;
1141        }
1142        for tokens in &lines.after {
1143            show_color_words_line_number(
1144                formatter,
1145                Diff::new(None, Some(line_number.right)),
1146                labels,
1147            )?;
1148            show_color_words_single_sided_line(formatter, tokens, labels.after)?;
1149            line_number.right += 1;
1150        }
1151    }
1152    Ok(line_number)
1153}
1154
1155fn show_color_words_line_number(
1156    formatter: &mut dyn Formatter,
1157    line_numbers: Diff<Option<u32>>,
1158    labels: Diff<&str>,
1159) -> io::Result<()> {
1160    if let Some(line_number) = line_numbers.before {
1161        write!(
1162            formatter.labeled(labels.before).labeled("line_number"),
1163            "{line_number:>4}"
1164        )?;
1165        write!(formatter, " ")?;
1166    } else {
1167        write!(formatter, "     ")?;
1168    }
1169    if let Some(line_number) = line_numbers.after {
1170        write!(
1171            formatter.labeled(labels.after).labeled("line_number"),
1172            "{line_number:>4}"
1173        )?;
1174        write!(formatter, ": ")?;
1175    } else {
1176        write!(formatter, "    : ")?;
1177    }
1178    Ok(())
1179}
1180
1181/// Prints line hunks which may contain tokens originating from both sides.
1182fn show_color_words_inline_hunks(
1183    formatter: &mut dyn Formatter,
1184    line_hunks: &[(DiffLineHunkSide, &BStr)],
1185    labels: Diff<&str>,
1186) -> io::Result<()> {
1187    for (side, data) in line_hunks {
1188        let label = match side {
1189            DiffLineHunkSide::Both => None,
1190            DiffLineHunkSide::Left => Some(labels.before),
1191            DiffLineHunkSide::Right => Some(labels.after),
1192        };
1193        if let Some(label) = label {
1194            formatter.labeled(label).labeled("token").write_all(data)?;
1195        } else {
1196            formatter.write_all(data)?;
1197        }
1198    }
1199    let (_, data) = line_hunks.last().expect("diff line must not be empty");
1200    if !data.ends_with(b"\n") {
1201        writeln!(formatter)?;
1202    }
1203    Ok(())
1204}
1205
1206/// Prints left/right-only line tokens with the given label.
1207fn show_color_words_single_sided_line(
1208    formatter: &mut dyn Formatter,
1209    tokens: &[(DiffTokenType, &[u8])],
1210    label: &str,
1211) -> io::Result<()> {
1212    show_diff_line_tokens(*formatter.labeled(label), tokens)?;
1213    let (_, data) = tokens.last().expect("diff line must not be empty");
1214    if !data.ends_with(b"\n") {
1215        writeln!(formatter)?;
1216    }
1217    Ok(())
1218}
1219
1220/// Counts number of diff-side alternation, ignoring matching hunks.
1221///
1222/// This function is meant to measure visual complexity of diff hunks. It's easy
1223/// to read hunks containing some removed or added words, but is getting harder
1224/// as more removes and adds interleaved.
1225///
1226/// For example,
1227/// - `[matching]` -> 0
1228/// - `[left]` -> 1
1229/// - `[left, matching, left]` -> 1
1230/// - `[matching, left, right, matching, right]` -> 2
1231/// - `[left, right, matching, right, left]` -> 3
1232fn count_diff_alternation(diff_hunks: &[DiffHunk]) -> usize {
1233    diff_hunks
1234        .iter()
1235        .filter_map(|hunk| match hunk.kind {
1236            DiffHunkKind::Matching => None,
1237            DiffHunkKind::Different => Some(&hunk.contents),
1238        })
1239        // Map non-empty diff side to index (0: left, 1: right)
1240        .flat_map(|contents| contents.iter().positions(|content| !content.is_empty()))
1241        // Omit e.g. left->(matching->)*left
1242        .dedup()
1243        .count()
1244}
1245
1246/// Splits hunks into slices of contiguous changed lines.
1247fn split_diff_hunks_by_matching_newline<'a, 'b>(
1248    diff_hunks: &'a [DiffHunk<'b>],
1249) -> impl Iterator<Item = &'a [DiffHunk<'b>]> {
1250    diff_hunks.split_inclusive(|hunk| match hunk.kind {
1251        DiffHunkKind::Matching => hunk.contents.iter().all(|content| content.contains(&b'\n')),
1252        DiffHunkKind::Different => false,
1253    })
1254}
1255
1256async fn diff_content(
1257    path: &RepoPath,
1258    value: MaterializedTreeValue,
1259    materialize_options: &ConflictMaterializeOptions,
1260) -> BackendResult<FileContent<BString>> {
1261    diff_content_with(
1262        path,
1263        value,
1264        |content| content,
1265        |contents, labels| {
1266            materialize_merge_result_to_bytes(&contents, &labels, materialize_options)
1267        },
1268    )
1269    .await
1270}
1271
1272#[derive(PartialEq, Eq, Debug)]
1273struct DiffContentAsMerge {
1274    file_content: Merge<BString>,
1275    conflict_labels: ConflictLabels,
1276}
1277
1278impl DiffContentAsMerge {
1279    pub fn is_empty(&self) -> bool {
1280        self.file_content
1281            .as_resolved()
1282            .is_some_and(|c| c.is_empty())
1283    }
1284}
1285
1286async fn diff_content_as_merge(
1287    path: &RepoPath,
1288    value: MaterializedTreeValue,
1289) -> BackendResult<FileContent<DiffContentAsMerge>> {
1290    diff_content_with(
1291        path,
1292        value,
1293        |contents| DiffContentAsMerge {
1294            file_content: Merge::resolved(contents),
1295            conflict_labels: ConflictLabels::unlabeled(),
1296        },
1297        |contents, labels| DiffContentAsMerge {
1298            file_content: contents,
1299            conflict_labels: labels,
1300        },
1301    )
1302    .await
1303}
1304
1305async fn diff_content_with<T>(
1306    path: &RepoPath,
1307    value: MaterializedTreeValue,
1308    map_resolved: impl FnOnce(BString) -> T,
1309    map_conflict: impl FnOnce(Merge<BString>, ConflictLabels) -> T,
1310) -> BackendResult<FileContent<T>> {
1311    match value {
1312        MaterializedTreeValue::Absent => Ok(FileContent {
1313            is_binary: false,
1314            contents: map_resolved(BString::default()),
1315        }),
1316        MaterializedTreeValue::AccessDenied(err) => Ok(FileContent {
1317            is_binary: false,
1318            contents: map_resolved(format!("Access denied: {err}").into()),
1319        }),
1320        MaterializedTreeValue::File(mut file) => {
1321            file_content_for_diff(path, &mut file, map_resolved).await
1322        }
1323        MaterializedTreeValue::Symlink { id: _, target } => Ok(FileContent {
1324            // Unix file paths can't contain null bytes.
1325            is_binary: false,
1326            contents: map_resolved(target.into()),
1327        }),
1328        MaterializedTreeValue::GitSubmodule(id) => Ok(FileContent {
1329            is_binary: false,
1330            contents: map_resolved(format!("Git submodule checked out at {id}").into()),
1331        }),
1332        // TODO: are we sure this is never binary?
1333        MaterializedTreeValue::FileConflict(file) => Ok(FileContent {
1334            is_binary: false,
1335            contents: map_conflict(file.contents, file.labels),
1336        }),
1337        MaterializedTreeValue::OtherConflict { id, labels } => Ok(FileContent {
1338            is_binary: false,
1339            contents: map_resolved(id.describe(&labels).into()),
1340        }),
1341        MaterializedTreeValue::Tree(id) => {
1342            panic!("Unexpected tree with id {id:?} in diff at path {path:?}");
1343        }
1344    }
1345}
1346
1347fn basic_diff_file_type(value: &MaterializedTreeValue) -> &'static str {
1348    match value {
1349        MaterializedTreeValue::Absent => {
1350            panic!("absent path in diff");
1351        }
1352        MaterializedTreeValue::AccessDenied(_) => "access denied",
1353        MaterializedTreeValue::File(file) => {
1354            if file.executable {
1355                "executable file"
1356            } else {
1357                "regular file"
1358            }
1359        }
1360        MaterializedTreeValue::Symlink { .. } => "symlink",
1361        MaterializedTreeValue::Tree(_) => "tree",
1362        MaterializedTreeValue::GitSubmodule(_) => "Git submodule",
1363        MaterializedTreeValue::FileConflict(_) | MaterializedTreeValue::OtherConflict { .. } => {
1364            "conflict"
1365        }
1366    }
1367}
1368
1369pub async fn show_color_words_diff(
1370    formatter: &mut dyn Formatter,
1371    store: &Store,
1372    tree_diff: BoxStream<'_, CopiesTreeDiffEntry>,
1373    conflict_labels: Diff<&ConflictLabels>,
1374    path_converter: &RepoPathUiConverter,
1375    options: &ColorWordsDiffOptions,
1376    marker_style: ConflictMarkerStyle,
1377) -> Result<(), DiffRenderError> {
1378    let materialize_options = ConflictMaterializeOptions {
1379        marker_style,
1380        marker_len: None,
1381        merge: store.merge_options().clone(),
1382    };
1383    let empty_content = || Merge::resolved(BString::default());
1384    let mut diff_stream = materialized_diff_stream(store, tree_diff, conflict_labels);
1385    while let Some(MaterializedTreeDiffEntry { path, values }) = diff_stream.next().await {
1386        let left_path = path.source();
1387        let right_path = path.target();
1388        let left_ui_path = path_converter.format_file_path(left_path);
1389        let right_ui_path = path_converter.format_file_path(right_path);
1390        let Diff {
1391            before: left_value,
1392            after: right_value,
1393        } = values?;
1394
1395        match (&left_value, &right_value) {
1396            (MaterializedTreeValue::AccessDenied(source), _) => {
1397                write!(
1398                    formatter.labeled("access-denied"),
1399                    "Access denied to {left_ui_path}:"
1400                )?;
1401                writeln!(formatter, " {source}")?;
1402                continue;
1403            }
1404            (_, MaterializedTreeValue::AccessDenied(source)) => {
1405                write!(
1406                    formatter.labeled("access-denied"),
1407                    "Access denied to {right_ui_path}:"
1408                )?;
1409                writeln!(formatter, " {source}")?;
1410                continue;
1411            }
1412            _ => {}
1413        }
1414        if left_value.is_absent() {
1415            let description = basic_diff_file_type(&right_value);
1416            writeln!(
1417                formatter.labeled("header"),
1418                "Added {description} {right_ui_path}:"
1419            )?;
1420            let right_content = diff_content_as_merge(right_path, right_value).await?;
1421            if right_content.contents.is_empty() {
1422                writeln!(formatter.labeled("empty"), "    (empty)")?;
1423            } else if right_content.is_binary {
1424                writeln!(formatter.labeled("binary"), "    (binary)")?;
1425            } else {
1426                show_color_words_diff_hunks(
1427                    formatter,
1428                    Diff::new(&empty_content(), &right_content.contents.file_content),
1429                    Diff::new(
1430                        &ConflictLabels::unlabeled(),
1431                        &right_content.contents.conflict_labels,
1432                    ),
1433                    options,
1434                    &materialize_options,
1435                )?;
1436            }
1437        } else if right_value.is_present() {
1438            let description = match (&left_value, &right_value) {
1439                (MaterializedTreeValue::File(left), MaterializedTreeValue::File(right)) => {
1440                    if left.executable && right.executable {
1441                        "Modified executable file".to_string()
1442                    } else if left.executable {
1443                        "Executable file became non-executable at".to_string()
1444                    } else if right.executable {
1445                        "Non-executable file became executable at".to_string()
1446                    } else {
1447                        "Modified regular file".to_string()
1448                    }
1449                }
1450                (
1451                    MaterializedTreeValue::FileConflict(_)
1452                    | MaterializedTreeValue::OtherConflict { .. },
1453                    MaterializedTreeValue::FileConflict(_)
1454                    | MaterializedTreeValue::OtherConflict { .. },
1455                ) => "Modified conflict in".to_string(),
1456                (
1457                    MaterializedTreeValue::FileConflict(_)
1458                    | MaterializedTreeValue::OtherConflict { .. },
1459                    _,
1460                ) => "Resolved conflict in".to_string(),
1461                (
1462                    _,
1463                    MaterializedTreeValue::FileConflict(_)
1464                    | MaterializedTreeValue::OtherConflict { .. },
1465                ) => "Created conflict in".to_string(),
1466                (MaterializedTreeValue::Symlink { .. }, MaterializedTreeValue::Symlink { .. }) => {
1467                    "Symlink target changed at".to_string()
1468                }
1469                (_, _) => {
1470                    let left_type = basic_diff_file_type(&left_value);
1471                    let right_type = basic_diff_file_type(&right_value);
1472                    let (first, rest) = left_type.split_at(1);
1473                    format!(
1474                        "{}{} became {} at",
1475                        first.to_ascii_uppercase(),
1476                        rest,
1477                        right_type
1478                    )
1479                }
1480            };
1481            let left_content = diff_content_as_merge(left_path, left_value).await?;
1482            let right_content = diff_content_as_merge(right_path, right_value).await?;
1483            if left_path == right_path {
1484                writeln!(
1485                    formatter.labeled("header"),
1486                    "{description} {right_ui_path}:"
1487                )?;
1488            } else {
1489                writeln!(
1490                    formatter.labeled("header"),
1491                    "{description} {right_ui_path} ({left_ui_path} => {right_ui_path}):"
1492                )?;
1493            }
1494            if left_content.is_binary || right_content.is_binary {
1495                writeln!(formatter.labeled("binary"), "    (binary)")?;
1496            } else if left_content.contents != right_content.contents {
1497                show_color_words_diff_hunks(
1498                    formatter,
1499                    Diff::new(
1500                        &left_content.contents.file_content,
1501                        &right_content.contents.file_content,
1502                    ),
1503                    Diff::new(
1504                        &left_content.contents.conflict_labels,
1505                        &right_content.contents.conflict_labels,
1506                    ),
1507                    options,
1508                    &materialize_options,
1509                )?;
1510            }
1511        } else {
1512            let description = basic_diff_file_type(&left_value);
1513            writeln!(
1514                formatter.labeled("header"),
1515                "Removed {description} {right_ui_path}:"
1516            )?;
1517            let left_content = diff_content_as_merge(left_path, left_value).await?;
1518            if left_content.contents.is_empty() {
1519                writeln!(formatter.labeled("empty"), "    (empty)")?;
1520            } else if left_content.is_binary {
1521                writeln!(formatter.labeled("binary"), "    (binary)")?;
1522            } else {
1523                show_color_words_diff_hunks(
1524                    formatter,
1525                    Diff::new(&left_content.contents.file_content, &empty_content()),
1526                    Diff::new(
1527                        &left_content.contents.conflict_labels,
1528                        &ConflictLabels::unlabeled(),
1529                    ),
1530                    options,
1531                    &materialize_options,
1532                )?;
1533            }
1534        }
1535    }
1536    Ok(())
1537}
1538
1539#[expect(clippy::too_many_arguments)]
1540pub async fn show_file_by_file_diff(
1541    ui: &Ui,
1542    formatter: &mut dyn Formatter,
1543    store: &Store,
1544    tree_diff: BoxStream<'_, CopiesTreeDiffEntry>,
1545    conflict_labels: Diff<&ConflictLabels>,
1546    path_converter: &RepoPathUiConverter,
1547    tool: &ExternalMergeTool,
1548    marker_style: ConflictMarkerStyle,
1549    width: usize,
1550) -> Result<(), DiffRenderError> {
1551    let materialize_options = ConflictMaterializeOptions {
1552        marker_style,
1553        marker_len: None,
1554        merge: store.merge_options().clone(),
1555    };
1556    let create_file = async |path: &RepoPath,
1557                             wc_dir: &Path,
1558                             value: MaterializedTreeValue|
1559           -> Result<PathBuf, DiffRenderError> {
1560        let fs_path = path.to_fs_path(wc_dir)?;
1561        std::fs::create_dir_all(fs_path.parent().unwrap())?;
1562        let content = diff_content(path, value, &materialize_options).await?;
1563        std::fs::write(&fs_path, content.contents)?;
1564        Ok(fs_path)
1565    };
1566
1567    let temp_dir = new_utf8_temp_dir("jj-diff-")?;
1568    let left_wc_dir = temp_dir.path().join("left");
1569    let right_wc_dir = temp_dir.path().join("right");
1570    let mut diff_stream = materialized_diff_stream(store, tree_diff, conflict_labels);
1571    while let Some(MaterializedTreeDiffEntry { path, values }) = diff_stream.next().await {
1572        let Diff {
1573            before: left_value,
1574            after: right_value,
1575        } = values?;
1576        let left_path = path.source();
1577        let right_path = path.target();
1578        let left_ui_path = path_converter.format_file_path(left_path);
1579        let right_ui_path = path_converter.format_file_path(right_path);
1580
1581        match (&left_value, &right_value) {
1582            (_, MaterializedTreeValue::AccessDenied(source)) => {
1583                write!(
1584                    formatter.labeled("access-denied"),
1585                    "Access denied to {right_ui_path}:"
1586                )?;
1587                writeln!(formatter, " {source}")?;
1588                continue;
1589            }
1590            (MaterializedTreeValue::AccessDenied(source), _) => {
1591                write!(
1592                    formatter.labeled("access-denied"),
1593                    "Access denied to {left_ui_path}:"
1594                )?;
1595                writeln!(formatter, " {source}")?;
1596                continue;
1597            }
1598            _ => {}
1599        }
1600        let left_path = create_file(left_path, &left_wc_dir, left_value).await?;
1601        let right_path = create_file(right_path, &right_wc_dir, right_value).await?;
1602        let patterns = &maplit::hashmap! {
1603            "left" => left_path
1604                .strip_prefix(temp_dir.path())
1605                .expect("path should be relative to temp_dir")
1606                .to_str()
1607                .expect("temp_dir should be valid utf-8")
1608                .to_owned(),
1609            "right" => right_path
1610                .strip_prefix(temp_dir.path())
1611                .expect("path should be relative to temp_dir")
1612                .to_str()
1613                .expect("temp_dir should be valid utf-8")
1614                .to_owned(),
1615            "width" => width.to_string(),
1616        };
1617
1618        let mut writer = formatter.raw()?;
1619        invoke_external_diff(ui, writer.as_mut(), tool, temp_dir.path(), patterns)
1620            .map_err(DiffRenderError::DiffGenerate)?;
1621    }
1622    Ok::<(), DiffRenderError>(())
1623}
1624
1625#[derive(Clone, Debug, Eq, PartialEq)]
1626pub struct UnifiedDiffOptions {
1627    /// Number of context lines to show.
1628    pub context: usize,
1629    /// How lines are tokenized and compared.
1630    pub line_diff: LineDiffOptions,
1631}
1632
1633impl UnifiedDiffOptions {
1634    pub fn from_settings(settings: &UserSettings) -> Result<Self, ConfigGetError> {
1635        Ok(Self {
1636            context: settings.get("diff.git.context")?,
1637            line_diff: LineDiffOptions::default(),
1638        })
1639    }
1640
1641    fn merge_args(&mut self, args: &DiffFormatArgs) {
1642        if let Some(context) = args.context {
1643            self.context = context;
1644        }
1645        self.line_diff.merge_args(args);
1646    }
1647}
1648
1649fn show_unified_diff_hunks(
1650    formatter: &mut dyn Formatter,
1651    contents: Diff<&BStr>,
1652    options: &UnifiedDiffOptions,
1653) -> io::Result<()> {
1654    // "If the chunk size is 0, the first number is one lower than one would
1655    // expect." - https://www.artima.com/weblogs/viewpost.jsp?thread=164293
1656    //
1657    // The POSIX spec also states that "the ending line number of an empty range
1658    // shall be the number of the preceding line, or 0 if the range is at the
1659    // start of the file."
1660    // - https://pubs.opengroup.org/onlinepubs/9799919799/utilities/diff.html
1661    fn to_line_number(range: Range<usize>) -> usize {
1662        if range.is_empty() {
1663            range.start
1664        } else {
1665            range.start + 1
1666        }
1667    }
1668
1669    for hunk in unified_diff_hunks(contents, options.context, options.line_diff.compare_mode) {
1670        writeln!(
1671            formatter.labeled("hunk_header"),
1672            "@@ -{},{} +{},{} @@",
1673            to_line_number(hunk.left_line_range.clone()),
1674            hunk.left_line_range.len(),
1675            to_line_number(hunk.right_line_range.clone()),
1676            hunk.right_line_range.len()
1677        )?;
1678        for (line_type, tokens) in &hunk.lines {
1679            let (label, sigil) = match line_type {
1680                DiffLineType::Context => ("context", " "),
1681                DiffLineType::Removed => ("removed", "-"),
1682                DiffLineType::Added => ("added", "+"),
1683            };
1684            write!(formatter.labeled(label), "{sigil}")?;
1685            show_diff_line_tokens(*formatter.labeled(label), tokens)?;
1686            let (_, content) = tokens.last().expect("hunk line must not be empty");
1687            if !content.ends_with(b"\n") {
1688                write!(formatter, "\n\\ No newline at end of file\n")?;
1689            }
1690        }
1691    }
1692    Ok(())
1693}
1694
1695fn show_diff_line_tokens(
1696    formatter: &mut dyn Formatter,
1697    tokens: &[(DiffTokenType, &[u8])],
1698) -> io::Result<()> {
1699    for (token_type, content) in tokens {
1700        match token_type {
1701            DiffTokenType::Matching => formatter.write_all(content)?,
1702            DiffTokenType::Different => formatter.labeled("token").write_all(content)?,
1703        }
1704    }
1705    Ok(())
1706}
1707
1708pub async fn show_git_diff(
1709    formatter: &mut dyn Formatter,
1710    store: &Store,
1711    tree_diff: BoxStream<'_, CopiesTreeDiffEntry>,
1712    conflict_labels: Diff<&ConflictLabels>,
1713    options: &UnifiedDiffOptions,
1714    marker_style: ConflictMarkerStyle,
1715) -> Result<(), DiffRenderError> {
1716    let materialize_options = ConflictMaterializeOptions {
1717        marker_style,
1718        marker_len: None,
1719        merge: store.merge_options().clone(),
1720    };
1721    let mut diff_stream = materialized_diff_stream(store, tree_diff, conflict_labels);
1722    while let Some(MaterializedTreeDiffEntry { path, values }) = diff_stream.next().await {
1723        let left_path = path.source();
1724        let right_path = path.target();
1725        let left_path_string = left_path.as_internal_file_string();
1726        let right_path_string = right_path.as_internal_file_string();
1727        let values = values?;
1728
1729        let left_part = git_diff_part(left_path, values.before, &materialize_options).await?;
1730        let right_part = git_diff_part(right_path, values.after, &materialize_options).await?;
1731
1732        {
1733            let mut formatter = formatter.labeled("file_header");
1734            writeln!(
1735                formatter,
1736                "diff --git a/{left_path_string} b/{right_path_string}"
1737            )?;
1738            let left_hash = &left_part.hash;
1739            let right_hash = &right_part.hash;
1740            match (left_part.mode, right_part.mode) {
1741                (None, Some(right_mode)) => {
1742                    writeln!(formatter, "new file mode {right_mode}")?;
1743                    writeln!(formatter, "index {left_hash}..{right_hash}")?;
1744                }
1745                (Some(left_mode), None) => {
1746                    writeln!(formatter, "deleted file mode {left_mode}")?;
1747                    writeln!(formatter, "index {left_hash}..{right_hash}")?;
1748                }
1749                (Some(left_mode), Some(right_mode)) => {
1750                    if let Some(op) = path.copy_operation() {
1751                        let operation = match op {
1752                            CopyOperation::Copy => "copy",
1753                            CopyOperation::Rename => "rename",
1754                        };
1755                        // TODO: include similarity index?
1756                        writeln!(formatter, "{operation} from {left_path_string}")?;
1757                        writeln!(formatter, "{operation} to {right_path_string}")?;
1758                    }
1759                    if left_mode != right_mode {
1760                        writeln!(formatter, "old mode {left_mode}")?;
1761                        writeln!(formatter, "new mode {right_mode}")?;
1762                        if left_hash != right_hash {
1763                            writeln!(formatter, "index {left_hash}..{right_hash}")?;
1764                        }
1765                    } else if left_hash != right_hash {
1766                        writeln!(formatter, "index {left_hash}..{right_hash} {left_mode}")?;
1767                    }
1768                }
1769                (None, None) => panic!("either left or right part should be present"),
1770            }
1771        }
1772
1773        if left_part.content.contents == right_part.content.contents {
1774            continue; // no content hunks
1775        }
1776
1777        let left_path = match left_part.mode {
1778            Some(_) => format!("a/{left_path_string}"),
1779            None => "/dev/null".to_owned(),
1780        };
1781        let right_path = match right_part.mode {
1782            Some(_) => format!("b/{right_path_string}"),
1783            None => "/dev/null".to_owned(),
1784        };
1785        if left_part.content.is_binary || right_part.content.is_binary {
1786            // TODO: add option to emit Git binary diff
1787            writeln!(
1788                formatter,
1789                "Binary files {left_path} and {right_path} differ"
1790            )?;
1791        } else {
1792            writeln!(formatter.labeled("file_header"), "--- {left_path}")?;
1793            writeln!(formatter.labeled("file_header"), "+++ {right_path}")?;
1794            show_unified_diff_hunks(
1795                formatter,
1796                Diff::new(&left_part.content.contents, &right_part.content.contents).map(BStr::new),
1797                options,
1798            )?;
1799        }
1800    }
1801    Ok(())
1802}
1803
1804/// Generates diff of non-binary contents in Git format.
1805fn show_git_diff_texts<T: AsRef<[u8]>>(
1806    formatter: &mut dyn Formatter,
1807    paths: Diff<&str>,
1808    contents: Diff<&Merge<T>>,
1809    options: &UnifiedDiffOptions,
1810    materialize_options: &ConflictMaterializeOptions,
1811) -> io::Result<()> {
1812    let Diff {
1813        before: left_path,
1814        after: right_path,
1815    } = paths;
1816    {
1817        let mut formatter = formatter.labeled("file_header");
1818        writeln!(formatter, "diff --git a/{left_path} b/{right_path}")?;
1819        writeln!(formatter, "--- {left_path}")?;
1820        writeln!(formatter, "+++ {right_path}")?;
1821    }
1822    let contents = contents.map(|content| match content.as_resolved() {
1823        Some(text) => Cow::Borrowed(BStr::new(text)),
1824        None => Cow::Owned(materialize_merge_result_to_bytes(
1825            content,
1826            &ConflictLabels::unlabeled(),
1827            materialize_options,
1828        )),
1829    });
1830    show_unified_diff_hunks(formatter, contents.as_ref().map(Cow::as_ref), options)
1831}
1832
1833#[instrument(skip_all)]
1834pub async fn show_diff_summary(
1835    formatter: &mut dyn Formatter,
1836    mut tree_diff: BoxStream<'_, CopiesTreeDiffEntry>,
1837    path_converter: &RepoPathUiConverter,
1838) -> Result<(), DiffRenderError> {
1839    while let Some(CopiesTreeDiffEntry { path, values }) = tree_diff.next().await {
1840        let values = values?;
1841        let status = diff_status(&path, &values);
1842        let (label, sigil) = (status.label(), status.char());
1843        let ui_path = match path.to_diff() {
1844            Some(paths) => path_converter.format_copied_path(paths),
1845            None => path_converter.format_file_path(path.target()),
1846        };
1847        writeln!(formatter.labeled(label), "{sigil} {ui_path}")?;
1848    }
1849    Ok(())
1850}
1851
1852fn diff_status_inner(
1853    path: &CopiesTreeDiffEntryPath,
1854    is_present_before: bool,
1855    is_present_after: bool,
1856) -> DiffEntryStatus {
1857    if let Some(op) = path.copy_operation() {
1858        match op {
1859            CopyOperation::Copy => DiffEntryStatus::Copied,
1860            CopyOperation::Rename => DiffEntryStatus::Renamed,
1861        }
1862    } else {
1863        match (is_present_before, is_present_after) {
1864            (true, true) => DiffEntryStatus::Modified,
1865            (false, true) => DiffEntryStatus::Added,
1866            (true, false) => DiffEntryStatus::Removed,
1867            (false, false) => panic!("values pair must differ"),
1868        }
1869    }
1870}
1871
1872pub fn diff_status(
1873    path: &CopiesTreeDiffEntryPath,
1874    values: &Diff<MergedTreeValue>,
1875) -> DiffEntryStatus {
1876    diff_status_inner(path, values.before.is_present(), values.after.is_present())
1877}
1878
1879#[derive(Clone, Debug, Default, Eq, PartialEq)]
1880pub struct DiffStatOptions {
1881    /// How lines are tokenized and compared.
1882    pub line_diff: LineDiffOptions,
1883}
1884
1885impl DiffStatOptions {
1886    fn merge_args(&mut self, args: &DiffFormatArgs) {
1887        self.line_diff.merge_args(args);
1888    }
1889}
1890
1891#[derive(Clone, Debug)]
1892pub struct DiffStats {
1893    entries: Vec<DiffStatEntry>,
1894}
1895
1896impl DiffStats {
1897    /// Calculates stats of changed lines per file.
1898    pub async fn calculate(
1899        store: &Store,
1900        tree_diff: BoxStream<'_, CopiesTreeDiffEntry>,
1901        options: &DiffStatOptions,
1902        marker_style: ConflictMarkerStyle,
1903    ) -> BackendResult<Self> {
1904        let materialize_options = ConflictMaterializeOptions {
1905            marker_style,
1906            marker_len: None,
1907            merge: store.merge_options().clone(),
1908        };
1909        let conflict_labels = ConflictLabels::unlabeled();
1910        let entries = materialized_diff_stream(
1911            store,
1912            tree_diff,
1913            Diff::new(&conflict_labels, &conflict_labels),
1914        )
1915        .then(async |MaterializedTreeDiffEntry { path, values }| {
1916            let values = values?;
1917            let status =
1918                diff_status_inner(&path, values.before.is_present(), values.after.is_present());
1919            let left_content =
1920                diff_content(path.source(), values.before, &materialize_options).await?;
1921            let right_content =
1922                diff_content(path.target(), values.after, &materialize_options).await?;
1923            let stat = get_diff_stat_entry(
1924                path,
1925                status,
1926                Diff::new(&left_content, &right_content),
1927                options,
1928            );
1929            BackendResult::Ok(stat)
1930        })
1931        .try_collect()
1932        .await?;
1933        Ok(Self { entries })
1934    }
1935
1936    /// List of stats per file.
1937    pub fn entries(&self) -> &[DiffStatEntry] {
1938        &self.entries
1939    }
1940
1941    /// Total number of inserted lines.
1942    pub fn count_total_added(&self) -> usize {
1943        self.entries
1944            .iter()
1945            .filter_map(|stat| stat.added_removed.map(|(added, _)| added))
1946            .sum()
1947    }
1948
1949    /// Total number of deleted lines.
1950    pub fn count_total_removed(&self) -> usize {
1951        self.entries
1952            .iter()
1953            .filter_map(|stat| stat.added_removed.map(|(_, removed)| removed))
1954            .sum()
1955    }
1956}
1957
1958#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1959pub enum DiffEntryStatus {
1960    Added,
1961    Removed,
1962    Modified,
1963    Copied,
1964    Renamed,
1965}
1966
1967impl DiffEntryStatus {
1968    pub fn label(&self) -> &'static str {
1969        match self {
1970            Self::Added => "added",
1971            Self::Removed => "removed",
1972            Self::Modified => "modified",
1973            Self::Copied => "copied",
1974            Self::Renamed => "renamed",
1975        }
1976    }
1977
1978    pub fn char(&self) -> char {
1979        match self {
1980            Self::Added => 'A',
1981            Self::Removed => 'D',
1982            Self::Modified => 'M',
1983            Self::Copied => 'C',
1984            Self::Renamed => 'R',
1985        }
1986    }
1987}
1988
1989#[derive(Clone, Debug)]
1990pub struct DiffStatEntry {
1991    pub path: CopiesTreeDiffEntryPath,
1992    /// Lines added and removed; None for binary files.
1993    pub added_removed: Option<(usize, usize)>,
1994    /// Change in file size in bytes.
1995    pub bytes_delta: isize,
1996    pub status: DiffEntryStatus,
1997}
1998
1999fn get_diff_stat_entry(
2000    path: CopiesTreeDiffEntryPath,
2001    status: DiffEntryStatus,
2002    contents: Diff<&FileContent<BString>>,
2003    options: &DiffStatOptions,
2004) -> DiffStatEntry {
2005    let added_removed = if contents.before.is_binary || contents.after.is_binary {
2006        None
2007    } else {
2008        let diff = diff_by_line(
2009            contents.map(|content| &content.contents).into_array(),
2010            &options.line_diff.compare_mode,
2011        );
2012        let mut added = 0;
2013        let mut removed = 0;
2014        for hunk in diff.hunks() {
2015            match hunk.kind {
2016                DiffHunkKind::Matching => {}
2017                DiffHunkKind::Different => {
2018                    let [left, right] = hunk.contents[..].try_into().unwrap();
2019                    removed += left.split_inclusive(|b| *b == b'\n').count();
2020                    added += right.split_inclusive(|b| *b == b'\n').count();
2021                }
2022            }
2023        }
2024        Some((added, removed))
2025    };
2026
2027    DiffStatEntry {
2028        path,
2029        added_removed,
2030        bytes_delta: contents.after.contents.len() as isize
2031            - contents.before.contents.len() as isize,
2032        status,
2033    }
2034}
2035
2036pub fn show_diff_stats(
2037    formatter: &mut dyn Formatter,
2038    stats: &DiffStats,
2039    path_converter: &RepoPathUiConverter,
2040    display_width: usize,
2041) -> io::Result<()> {
2042    let ui_paths = stats
2043        .entries()
2044        .iter()
2045        .map(|stat| match stat.path.to_diff() {
2046            Some(paths) => path_converter.format_copied_path(paths),
2047            None => path_converter.format_file_path(stat.path.target()),
2048        })
2049        .collect_vec();
2050
2051    // Entries format like:
2052    //   path/to/file | 123 ++--
2053    // or, for binary files:
2054    //   path/to/file | (binary) +1234 bytes
2055    //
2056    // Depending on display widths, we can elide part of the path,
2057    // and the ++-- bar will adjust its scale to fill the rest.
2058
2059    // Choose how many columns to use for the path.  The right side will use the
2060    // rest. Start with the longest path.  The code below might shorten it.
2061    let mut max_path_width = ui_paths.iter().map(|s| s.width()).max().unwrap_or(0);
2062
2063    // Fit to the available display width, but always assume at least a tiny bit of
2064    // room.
2065    let available_width = max(display_width.saturating_sub(" | ".len()), 8);
2066
2067    // Measure the widest right side for line diffs and reduce max_path_width if
2068    // needed.
2069    let max_diffs = stats
2070        .entries()
2071        .iter()
2072        .filter_map(|stat| {
2073            let (added, removed) = stat.added_removed?;
2074            Some(added + removed)
2075        })
2076        .max();
2077    let diff_number_width = max_diffs.map_or(0, |n| n.to_string().len());
2078    if max_diffs.is_some() {
2079        let width = diff_number_width + " ".len();
2080        // Ensure 30% of the space for displaying the ++-- graph:
2081        max_path_width =
2082            max_path_width.min((0.7 * available_width.saturating_sub(width) as f64) as usize);
2083    }
2084
2085    // Measure widest right side for binary diffs and reduce max_path_width if
2086    // needed.
2087    let max_bytes = stats
2088        .entries
2089        .iter()
2090        .filter(|stat| stat.added_removed.is_none())
2091        .map(|stat| stat.bytes_delta.abs())
2092        .max();
2093    if let Some(max) = max_bytes {
2094        let width = if max > 0 {
2095            format!("(binary) {max:+} bytes").len()
2096        } else {
2097            "(binary)".len()
2098        };
2099        max_path_width = max_path_width.min(available_width.saturating_sub(width));
2100    }
2101
2102    // Now that we've chosen the path width, use the rest of the space for the ++--
2103    // bar.
2104    let max_bar_width =
2105        available_width.saturating_sub(max_path_width + diff_number_width + " ".len());
2106    let factor = match max_diffs {
2107        Some(max) if max > max_bar_width => max_bar_width as f64 / max as f64,
2108        _ => 1.0,
2109    };
2110
2111    for (stat, ui_path) in iter::zip(stats.entries(), &ui_paths) {
2112        // replace start of path with ellipsis if the path is too long
2113        let (path, path_width) = text_util::elide_start(ui_path, "...", max_path_width);
2114        let path_pad_width = max_path_width - path_width;
2115        write!(
2116            formatter,
2117            "{path}{:path_pad_width$} | ",
2118            "", // pad to max_path_width
2119        )?;
2120        if let Some((added, removed)) = stat.added_removed {
2121            let bar_length = ((added + removed) as f64 * factor) as usize;
2122            // If neither adds nor removes are present, bar length should be zero.
2123            // If only one is present, bar length should be at least 1.
2124            // If both are present, bar length should be at least 2.
2125            //
2126            // Fractional space after scaling is given to whichever of adds/removes is
2127            // smaller, to show at least one tick for small (but nonzero) counts.
2128            let bar_length = bar_length.max(usize::from(added > 0) + usize::from(removed > 0));
2129            let (bar_added, bar_removed) = if added < removed {
2130                let len = (added as f64 * factor).ceil() as usize;
2131                (len, bar_length - len)
2132            } else {
2133                let len = (removed as f64 * factor).ceil() as usize;
2134                (bar_length - len, len)
2135            };
2136            write!(
2137                formatter,
2138                "{:>diff_number_width$}{}",
2139                added + removed,
2140                if bar_added + bar_removed > 0 { " " } else { "" },
2141            )?;
2142            write!(formatter.labeled("added"), "{}", "+".repeat(bar_added))?;
2143            writeln!(formatter.labeled("removed"), "{}", "-".repeat(bar_removed))?;
2144        } else {
2145            write!(formatter.labeled("binary"), "(binary)")?;
2146            if stat.bytes_delta != 0 {
2147                let label = if stat.bytes_delta < 0 {
2148                    "removed"
2149                } else {
2150                    "added"
2151                };
2152                write!(formatter.labeled(label), " {:+}", stat.bytes_delta)?;
2153                write!(formatter, " bytes")?;
2154            }
2155            writeln!(formatter)?;
2156        }
2157    }
2158
2159    let total_added = stats.count_total_added();
2160    let total_removed = stats.count_total_removed();
2161    let total_files = stats.entries().len();
2162    writeln!(
2163        formatter.labeled("stat-summary"),
2164        "{} file{} changed, {} insertion{}(+), {} deletion{}(-)",
2165        total_files,
2166        if total_files == 1 { "" } else { "s" },
2167        total_added,
2168        if total_added == 1 { "" } else { "s" },
2169        total_removed,
2170        if total_removed == 1 { "" } else { "s" },
2171    )?;
2172    Ok(())
2173}
2174
2175pub async fn show_types(
2176    formatter: &mut dyn Formatter,
2177    mut tree_diff: BoxStream<'_, CopiesTreeDiffEntry>,
2178    path_converter: &RepoPathUiConverter,
2179) -> Result<(), DiffRenderError> {
2180    while let Some(CopiesTreeDiffEntry { path, values }) = tree_diff.next().await {
2181        let values = values?;
2182        let ui_path = match path.to_diff() {
2183            Some(paths) => path_converter.format_copied_path(paths),
2184            None => path_converter.format_file_path(path.target()),
2185        };
2186        writeln!(
2187            formatter.labeled("modified"),
2188            "{before}{after} {ui_path}",
2189            before = diff_summary_char(&values.before),
2190            after = diff_summary_char(&values.after)
2191        )?;
2192    }
2193    Ok(())
2194}
2195
2196fn diff_summary_char(value: &MergedTreeValue) -> char {
2197    match value.as_resolved() {
2198        Some(None) => '-',
2199        Some(Some(TreeValue::File { .. })) => 'F',
2200        Some(Some(TreeValue::Symlink(_))) => 'L',
2201        Some(Some(TreeValue::GitSubmodule(_))) => 'G',
2202        None => 'C',
2203        Some(Some(TreeValue::Tree(_))) => {
2204            panic!("Unexpected {value:?} in diff")
2205        }
2206    }
2207}
2208
2209pub async fn show_names(
2210    formatter: &mut dyn Formatter,
2211    mut tree_diff: BoxStream<'_, CopiesTreeDiffEntry>,
2212    path_converter: &RepoPathUiConverter,
2213) -> io::Result<()> {
2214    while let Some(CopiesTreeDiffEntry { path, .. }) = tree_diff.next().await {
2215        writeln!(
2216            formatter,
2217            "{}",
2218            path_converter.format_file_path(path.target())
2219        )?;
2220    }
2221    Ok(())
2222}
2223
2224pub async fn show_templated(
2225    formatter: &mut dyn Formatter,
2226    mut tree_diff: BoxStream<'_, CopiesTreeDiffEntry>,
2227    template: &TemplateRenderer<'_, commit_templater::TreeDiffEntry>,
2228) -> Result<(), DiffRenderError> {
2229    while let Some(entry) = tree_diff.next().await {
2230        let entry = commit_templater::TreeDiffEntry::from_backend_entry_with_copies(entry)?;
2231        template.format(&entry, formatter)?;
2232    }
2233    Ok(())
2234}