|
| 1 | +use crate::OutputFormat; |
| 2 | +use anyhow::{bail, Context}; |
| 3 | +use gix::bstr::{BStr, BString}; |
| 4 | +use gix::index::Entry; |
| 5 | +use gix::prelude::FindExt; |
| 6 | +use gix::Progress; |
| 7 | +use gix_status::index_as_worktree::content::FastEq; |
| 8 | +use gix_status::index_as_worktree::Change; |
| 9 | + |
| 10 | +pub enum Submodules { |
| 11 | + /// display all information about submodules, including ref changes, modifications and untracked files. |
| 12 | + All, |
| 13 | + /// Compare only the configuration of the superprojects commit with the actually checked out `HEAD` commit. |
| 14 | + RefChange, |
| 15 | + /// See if there are worktree modifications compared to the index, but do not check for untracked files. |
| 16 | + Modifications, |
| 17 | +} |
| 18 | + |
| 19 | +pub struct Options { |
| 20 | + pub format: OutputFormat, |
| 21 | + pub submodules: Submodules, |
| 22 | + pub thread_limit: Option<usize>, |
| 23 | +} |
| 24 | + |
| 25 | +pub fn show( |
| 26 | + repo: gix::Repository, |
| 27 | + pathspecs: Vec<BString>, |
| 28 | + out: impl std::io::Write, |
| 29 | + mut err: impl std::io::Write, |
| 30 | + mut progress: impl gix::NestedProgress, |
| 31 | + Options { |
| 32 | + format, |
| 33 | + // TODO: implement this |
| 34 | + submodules: _, |
| 35 | + thread_limit, |
| 36 | + }: Options, |
| 37 | +) -> anyhow::Result<()> { |
| 38 | + if format != OutputFormat::Human { |
| 39 | + bail!("Only human format is supported right now"); |
| 40 | + } |
| 41 | + let mut index = repo.index()?; |
| 42 | + let index = gix::threading::make_mut(&mut index); |
| 43 | + let pathspec = repo.pathspec( |
| 44 | + pathspecs, |
| 45 | + true, |
| 46 | + index, |
| 47 | + gix::worktree::stack::state::attributes::Source::WorktreeThenIdMapping, |
| 48 | + )?; |
| 49 | + let mut progress = progress.add_child("traverse index"); |
| 50 | + let start = std::time::Instant::now(); |
| 51 | + gix_status::index_as_worktree( |
| 52 | + index, |
| 53 | + repo.work_dir() |
| 54 | + .context("This operation cannot be run on a bare repository")?, |
| 55 | + &mut Printer(out), |
| 56 | + FastEq, |
| 57 | + { |
| 58 | + let odb = repo.objects.clone().into_arc()?; |
| 59 | + move |id, buf| odb.find_blob(id, buf) |
| 60 | + }, |
| 61 | + &mut progress, |
| 62 | + pathspec.detach()?, |
| 63 | + gix_status::index_as_worktree::Options { |
| 64 | + fs: repo.filesystem_options()?, |
| 65 | + thread_limit, |
| 66 | + stat: repo.stat_options()?, |
| 67 | + }, |
| 68 | + )?; |
| 69 | + |
| 70 | + writeln!(err, "\nhead -> index and untracked files aren't implemented yet")?; |
| 71 | + progress.show_throughput(start); |
| 72 | + Ok(()) |
| 73 | +} |
| 74 | + |
| 75 | +struct Printer<W>(W); |
| 76 | + |
| 77 | +impl<'index, W> gix_status::index_as_worktree::VisitEntry<'index> for Printer<W> |
| 78 | +where |
| 79 | + W: std::io::Write, |
| 80 | +{ |
| 81 | + type ContentChange = (); |
| 82 | + |
| 83 | + fn visit_entry( |
| 84 | + &mut self, |
| 85 | + entry: &'index Entry, |
| 86 | + rela_path: &'index BStr, |
| 87 | + change: Option<Change<Self::ContentChange>>, |
| 88 | + conflict: bool, |
| 89 | + ) { |
| 90 | + self.visit_inner(entry, rela_path, change, conflict).ok(); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<W: std::io::Write> Printer<W> { |
| 95 | + fn visit_inner( |
| 96 | + &mut self, |
| 97 | + _entry: &Entry, |
| 98 | + rela_path: &BStr, |
| 99 | + change: Option<Change<()>>, |
| 100 | + conflict: bool, |
| 101 | + ) -> anyhow::Result<()> { |
| 102 | + if let Some(change) = conflict |
| 103 | + .then_some('U') |
| 104 | + .or_else(|| change.as_ref().and_then(change_to_char)) |
| 105 | + { |
| 106 | + writeln!(&mut self.0, "{change} {rela_path}")?; |
| 107 | + } |
| 108 | + Ok(()) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +fn change_to_char(change: &Change<()>) -> Option<char> { |
| 113 | + // Known status letters: https://github.com/git/git/blob/6807fcfedab84bc8cd0fbf721bc13c4e68cda9ae/diff.h#L613 |
| 114 | + Some(match change { |
| 115 | + Change::Removed => 'D', |
| 116 | + Change::Type => 'T', |
| 117 | + Change::Modification { .. } => 'M', |
| 118 | + Change::IntentToAdd => return None, |
| 119 | + }) |
| 120 | +} |
0 commit comments