Skip to main content

grit_lib/diff/
no_index.rs

1//! Compare arbitrary filesystem paths without a repository (`git diff --no-index`).
2
3use crate::error::ObjectError;
4use std::collections::{BTreeSet, HashSet};
5use std::fs;
6use std::path::{Path, PathBuf};
7
8use thiserror::Error;
9
10use crate::config::ConfigSet;
11use crate::hash::hash_object;
12use crate::objects::FileMode;
13use crate::objects::{HashAlgo, ObjectId, ObjectKind};
14use crate::odb::Odb;
15use crate::parallel::ThreadPolicy;
16
17use super::core::{
18    apply_rename_detection_to_diff_entries, mode_from_metadata, read_worktree_bytes_for_mode,
19    zero_oid, DiffEntry, DiffStatus,
20};
21use super::detail::line_stat_for_options;
22use super::options::DiffOptions;
23use super::patch_log::{write_patch, PatchTextOptions};
24use super::structured::{structured_file_diff, SideBytes};
25use super::targets::FileDiff;
26use crate::repo::{InitOptions, Repository};
27
28const S_IFMT: u32 = 0o170000;
29const S_IFREG: u32 = 0o100000;
30
31/// Failure modes specific to [`diff_paths`].
32#[derive(Debug, Error, Clone, PartialEq, Eq)]
33#[non_exhaustive]
34pub enum NoIndexError {
35    /// A path could not be read (missing or permission denied).
36    #[error("Could not access '{path}'")]
37    PathNotAccessible {
38        /// Filesystem path that could not be accessed.
39        path: String,
40    },
41    /// SHA-1 collision detection rejected blob content while hashing a path.
42    #[error("sha-1 hash collision while hashing no-index diff input")]
43    HashCollision,
44}
45
46impl From<NoIndexError> for crate::error::Error {
47    fn from(value: NoIndexError) -> Self {
48        Self::Diff(crate::error::DiffError::NoIndex(value))
49    }
50}
51
52/// Diff two filesystem paths (files, directories, or one of each).
53///
54/// Does not require a [`Repository`](crate::repo::Repository). Directory pairs are walked
55/// recursively with sorted names; a file paired with a directory compares against
56/// `directory/basename(file)` like Git. Symlink targets are diffed as text; executable
57/// bit differences appear as mode changes.
58///
59/// # Errors
60///
61/// Returns [`NoIndexError::PathNotAccessible`] when either top-level path cannot be accessed.
62pub fn diff_paths(a: &Path, b: &Path, opts: &DiffOptions) -> Result<Vec<FileDiff>, NoIndexError> {
63    let stat_only = opts.no_index_stat_only;
64    let temp = tempfile::tempdir().map_err(|e| NoIndexError::PathNotAccessible {
65        path: e.to_string(),
66    })?;
67    let odb = Odb::new(temp.path());
68    let entries = no_index_prepared_entries(a, b, opts, &odb)?;
69    if entries.is_empty() {
70        return Ok(Vec::new());
71    }
72    let diff_options = opts
73        .clone()
74        .cap_word_level_inline_for_changed_files(entries.len());
75    Ok(entries
76        .into_iter()
77        .filter_map(|entry| file_diff_from_entry(&odb, &entry, diff_options.clone(), stat_only))
78        .collect())
79}
80
81/// Write `git diff --no-index --binary`-style patch bytes for two filesystem paths.
82///
83/// Returns `Ok(true)` when the paths differ.
84///
85/// # Errors
86///
87/// Propagates [`NoIndexError`] and repository/patch I/O failures.
88pub fn write_paths_patch<W: std::io::Write>(
89    a: &Path,
90    b: &Path,
91    opts: &DiffOptions,
92    out: &mut W,
93) -> Result<bool, NoIndexError> {
94    let temp = tempfile::tempdir().map_err(|e| NoIndexError::PathNotAccessible {
95        path: e.to_string(),
96    })?;
97    let (repo, _) =
98        Repository::init(temp.path(), &InitOptions::new().with_bare(true)).map_err(|e| {
99            NoIndexError::PathNotAccessible {
100                path: e.to_string(),
101            }
102        })?;
103    let entries = no_index_prepared_entries(a, b, opts, repo.odb())?;
104    if entries.is_empty() {
105        return Ok(false);
106    }
107    let patch_opts = PatchTextOptions::from_repo(&repo, opts.clone()).map_err(|e| {
108        NoIndexError::PathNotAccessible {
109            path: e.to_string(),
110        }
111    })?;
112    write_patch(&repo, &entries, &patch_opts, out).map_err(|e| {
113        NoIndexError::PathNotAccessible {
114            path: e.to_string(),
115        }
116    })?;
117    Ok(true)
118}
119
120fn no_index_prepared_entries(
121    a: &Path,
122    b: &Path,
123    opts: &DiffOptions,
124    odb: &Odb,
125) -> Result<Vec<DiffEntry>, NoIndexError> {
126    let (left, right) = fixup_paths(a, b)?;
127    let mut entries = Vec::new();
128    queue_diff(left.as_deref(), right.as_deref(), false, &mut entries)?;
129    if entries.is_empty() {
130        return Ok(entries);
131    }
132
133    let config = ConfigSet::default();
134    let thread_policy = ThreadPolicy::default();
135    let rename_threshold = opts.renames.threshold_percent().unwrap_or(101);
136    store_changed_blobs(odb, &mut entries)?;
137    if rename_threshold < 100 {
138        apply_rename_detection_to_diff_entries(
139            odb,
140            entries,
141            None,
142            rename_threshold,
143            &config,
144            &thread_policy,
145        )
146        .map_err(|e| NoIndexError::PathNotAccessible {
147            path: e.to_string(),
148        })
149    } else {
150        Ok(entries)
151    }
152}
153
154fn file_diff_from_entry(
155    odb: &Odb,
156    entry: &DiffEntry,
157    opts: DiffOptions,
158    stat_only: bool,
159) -> Option<FileDiff> {
160    let path_unchanged = entry.old_path == entry.new_path;
161    if entry.old_oid == entry.new_oid
162        && entry.old_mode == entry.new_mode
163        && path_unchanged
164        && !matches!(entry.status, DiffStatus::Renamed | DiffStatus::Copied)
165    {
166        return None;
167    }
168    let (old_bytes, old_bin) = side_bytes_for_entry(odb, entry, Side::Old)?;
169    let (new_bytes, new_bin) = side_bytes_for_entry(odb, entry, Side::New)?;
170    let binary = old_bin || new_bin;
171    let path = entry
172        .new_path
173        .clone()
174        .or_else(|| entry.old_path.clone())
175        .unwrap_or_default();
176    let old_path = entry
177        .old_path
178        .clone()
179        .filter(|op| entry.new_path.as_ref() != Some(op));
180    let (old_encoding_lossy, new_encoding_lossy, hunks, line_stat) = if binary {
181        (
182            old_bytes.encoding_lossy,
183            new_bytes.encoding_lossy,
184            Vec::new(),
185            None,
186        )
187    } else if stat_only {
188        let old_text = String::from_utf8_lossy(&old_bytes.data);
189        let new_text = String::from_utf8_lossy(&new_bytes.data);
190        let stat = line_stat_for_options(old_text.as_ref(), new_text.as_ref(), &opts);
191        (
192            old_bytes.encoding_lossy,
193            new_bytes.encoding_lossy,
194            Vec::new(),
195            Some(stat),
196        )
197    } else {
198        let structured =
199            structured_file_diff(entry.old_mode, entry.new_mode, &old_bytes, &new_bytes, opts);
200        (
201            structured.old_encoding_lossy,
202            structured.new_encoding_lossy,
203            structured.hunks,
204            None,
205        )
206    };
207    Some(FileDiff {
208        path,
209        old_path,
210        status: entry.status,
211        old_mode: entry.old_mode.octal_if_present(),
212        new_mode: entry.new_mode.octal_if_present(),
213        old_oid: entry.old_oid,
214        new_oid: entry.new_oid,
215        binary,
216        old_size: old_bytes.byte_len(),
217        new_size: new_bytes.byte_len(),
218        content_changed: entry.old_oid != entry.new_oid,
219        old_encoding_lossy,
220        new_encoding_lossy,
221        hunks,
222        line_stat,
223    })
224}
225
226enum Side {
227    Old,
228    New,
229}
230
231fn side_bytes_for_entry(odb: &Odb, entry: &DiffEntry, side: Side) -> Option<(SideBytes, bool)> {
232    let (mode, oid) = match side {
233        Side::Old => (entry.old_mode, entry.old_oid),
234        Side::New => (entry.new_mode, entry.new_oid),
235    };
236    if mode.is_absent() {
237        return Some((SideBytes::from_bytes(Vec::new()), false));
238    }
239    let data = odb.read(&oid).ok()?.data;
240    Some(bytes_for_diff(&data))
241}
242
243fn bytes_for_diff(data: &[u8]) -> (SideBytes, bool) {
244    let binary = crate::merge_file::is_binary(data);
245    (SideBytes::from_bytes(data.to_vec()), binary)
246}
247
248/// Write blob objects for changed sides only (rename detection reads from the ODB).
249fn store_changed_blobs(odb: &Odb, entries: &mut [DiffEntry]) -> Result<(), NoIndexError> {
250    for entry in entries {
251        if entry.old_mode != FileMode::ABSENT && !entry.old_oid.is_zero() {
252            if let Some(path) = entry.old_path.as_deref() {
253                write_blob_if_missing(odb, path, entry.old_mode, &mut entry.old_oid)?;
254            }
255        }
256        if entry.new_mode != FileMode::ABSENT && !entry.new_oid.is_zero() {
257            if let Some(path) = entry.new_path.as_deref() {
258                write_blob_if_missing(odb, path, entry.new_mode, &mut entry.new_oid)?;
259            }
260        }
261    }
262    Ok(())
263}
264
265fn write_blob_if_missing(
266    odb: &Odb,
267    path: &str,
268    mode: FileMode,
269    oid: &mut ObjectId,
270) -> Result<(), NoIndexError> {
271    if !oid.is_zero() && odb.read(oid).is_ok() {
272        return Ok(());
273    }
274    *oid = hash_side_at_path(path, mode)?;
275    let data = read_worktree_bytes_for_mode(Path::new(path), mode.bits()).map_err(|_| {
276        NoIndexError::PathNotAccessible {
277            path: path.to_owned(),
278        }
279    })?;
280    let _ = odb.write(ObjectKind::Blob, &data);
281    Ok(())
282}
283
284fn hash_side_at_path(path: &str, mode: FileMode) -> Result<ObjectId, NoIndexError> {
285    let data = read_worktree_bytes_for_mode(Path::new(path), mode.bits()).map_err(|_| {
286        NoIndexError::PathNotAccessible {
287            path: path.to_owned(),
288        }
289    })?;
290    hash_blob(&data)
291}
292
293fn hash_blob(data: &[u8]) -> Result<ObjectId, NoIndexError> {
294    hash_object(HashAlgo::Sha1, ObjectKind::Blob, data).map_err(|e| match e {
295        crate::error::Error::Object(ObjectError::HashCollision { .. }) => {
296            NoIndexError::HashCollision
297        }
298        other => panic!("unexpected error hashing no-index blob: {other}"),
299    })
300}
301
302fn read_side_bytes(path: &Path, mode: u32) -> Result<Vec<u8>, NoIndexError> {
303    read_worktree_bytes_for_mode(path, mode).map_err(|_| NoIndexError::PathNotAccessible {
304        path: path.display().to_string(),
305    })
306}
307
308fn worktree_content_equal(
309    left: &Path,
310    right: &Path,
311    mode_left: u32,
312    mode_right: u32,
313) -> Result<bool, NoIndexError> {
314    let (ls, rs) = (
315        fs::metadata(left).map(|m| m.len()).ok(),
316        fs::metadata(right).map(|m| m.len()).ok(),
317    );
318    if ls != rs {
319        return Ok(false);
320    }
321    let old_bytes = read_side_bytes(left, mode_left)?;
322    let new_bytes = read_side_bytes(right, mode_right)?;
323    Ok(old_bytes == new_bytes)
324}
325
326/// Blob OIDs for a path pair, reading each present side at most once.
327fn oids_for_pair(
328    left: Option<&Path>,
329    right: Option<&Path>,
330    mode_left: u32,
331    mode_right: u32,
332) -> Result<(ObjectId, ObjectId), NoIndexError> {
333    match (left, right) {
334        (Some(l), Some(r)) if mode_left != 0 && mode_right != 0 => {
335            let old_bytes = read_side_bytes(l, mode_left)?;
336            let new_bytes = read_side_bytes(r, mode_right)?;
337            let old_oid = hash_blob(&old_bytes)?;
338            let new_oid = if old_bytes == new_bytes {
339                old_oid
340            } else {
341                hash_blob(&new_bytes)?
342            };
343            Ok((old_oid, new_oid))
344        }
345        (Some(l), _) if mode_left != 0 => {
346            let old_bytes = read_side_bytes(l, mode_left)?;
347            Ok((hash_blob(&old_bytes)?, zero_oid()))
348        }
349        (_, Some(r)) if mode_right != 0 => {
350            let new_bytes = read_side_bytes(r, mode_right)?;
351            Ok((zero_oid(), hash_blob(&new_bytes)?))
352        }
353        _ => Ok((zero_oid(), zero_oid())),
354    }
355}
356
357fn fixup_paths(a: &Path, b: &Path) -> Result<(Option<PathBuf>, Option<PathBuf>), NoIndexError> {
358    let meta_a = fs::symlink_metadata(a).ok();
359    let meta_b = fs::symlink_metadata(b).ok();
360    if meta_a.is_none() && meta_b.is_none() {
361        return Err(NoIndexError::PathNotAccessible {
362            path: a.display().to_string(),
363        });
364    }
365    let is_dir_a = meta_a.as_ref().is_some_and(|m| m.is_dir());
366    let is_dir_b = meta_b.as_ref().is_some_and(|m| m.is_dir());
367    if is_dir_a && is_dir_b {
368        return Ok((Some(a.to_path_buf()), Some(b.to_path_buf())));
369    }
370    if is_dir_a {
371        let mut joined = a.to_path_buf();
372        append_basename(&mut joined, b);
373        return Ok((Some(joined), Some(b.to_path_buf())));
374    }
375    if is_dir_b {
376        let mut joined = b.to_path_buf();
377        append_basename(&mut joined, a);
378        return Ok((Some(a.to_path_buf()), Some(joined)));
379    }
380    Ok((Some(a.to_path_buf()), Some(b.to_path_buf())))
381}
382
383fn append_basename(dir: &mut PathBuf, file: &Path) {
384    let name = file.file_name().unwrap_or_default();
385    if name.is_empty() {
386        return;
387    }
388    strip_trailing_slashes_preserve_root(dir);
389    dir.push(name);
390}
391
392/// Remove trailing `/` without turning `/` into an empty relative path.
393fn strip_trailing_slashes_preserve_root(path: &mut PathBuf) {
394    let raw = path.as_os_str().to_string_lossy();
395    let trimmed = raw.trim_end_matches('/');
396    if trimmed.is_empty() {
397        *path = PathBuf::from("/");
398    } else {
399        *path = PathBuf::from(trimmed);
400    }
401}
402
403fn queue_diff(
404    left: Option<&Path>,
405    right: Option<&Path>,
406    recursing: bool,
407    out: &mut Vec<DiffEntry>,
408) -> Result<(), NoIndexError> {
409    let (mode_left, mode_right) = (path_mode(left, recursing)?, path_mode(right, recursing)?);
410    let dir_left = path_is_dir(left);
411    let dir_right = path_is_dir(right);
412
413    if mode_left != 0 && mode_right != 0 && dir_left != dir_right {
414        if dir_left {
415            emit_pair(
416                out,
417                None,
418                right,
419                0,
420                mode_right,
421                right.map(path_display_string),
422            )?;
423            return queue_diff(left, None, true, out);
424        }
425        emit_pair(out, left, None, mode_left, 0, left.map(path_display_string))?;
426        return queue_diff(None, right, true, out);
427    }
428
429    if dir_left || dir_right {
430        diff_directories(left, right, out)
431    } else {
432        emit_pair(
433            out,
434            left,
435            right,
436            mode_left,
437            mode_right,
438            left.or(right).map(path_display_string),
439        )?;
440        Ok(())
441    }
442}
443
444fn path_is_dir(path: Option<&Path>) -> bool {
445    path.and_then(|p| fs::symlink_metadata(p).ok())
446        .is_some_and(|m| m.is_dir())
447}
448
449fn diff_directories(
450    left: Option<&Path>,
451    right: Option<&Path>,
452    out: &mut Vec<DiffEntry>,
453) -> Result<(), NoIndexError> {
454    let left_names = read_dir_names(left)?;
455    let right_names = read_dir_names(right)?;
456    let left_set: HashSet<_> = left_names.iter().cloned().collect();
457    let right_set: HashSet<_> = right_names.iter().cloned().collect();
458    let names: BTreeSet<_> = left_names
459        .iter()
460        .chain(right_names.iter())
461        .cloned()
462        .collect();
463    for name in names {
464        let l = left
465            .filter(|_| left_set.contains(&name))
466            .map(|p| p.join(&name));
467        let r = right
468            .filter(|_| right_set.contains(&name))
469            .map(|p| p.join(&name));
470        queue_diff(l.as_deref(), r.as_deref(), true, out)?;
471    }
472    Ok(())
473}
474
475fn read_dir_names(path: Option<&Path>) -> Result<Vec<String>, NoIndexError> {
476    let Some(path) = path else {
477        return Ok(Vec::new());
478    };
479    let mut names = Vec::new();
480    let read_dir = fs::read_dir(path).map_err(|_| NoIndexError::PathNotAccessible {
481        path: path.display().to_string(),
482    })?;
483    for entry in read_dir {
484        let entry = entry.map_err(|_| NoIndexError::PathNotAccessible {
485            path: path.display().to_string(),
486        })?;
487        let name = entry.file_name().to_string_lossy().into_owned();
488        if name == "." || name == ".." {
489            continue;
490        }
491        names.push(name);
492    }
493    names.sort();
494    Ok(names)
495}
496
497fn emit_pair(
498    out: &mut Vec<DiffEntry>,
499    left: Option<&Path>,
500    right: Option<&Path>,
501    mode_left: u32,
502    mode_right: u32,
503    display: Option<String>,
504) -> Result<(), NoIndexError> {
505    let (status, old_path, new_path) = match (left, right) {
506        (Some(_l), Some(_r)) if mode_left == 0 || mode_right == 0 => {
507            if mode_left == 0 {
508                (DiffStatus::Added, None, display.clone())
509            } else {
510                (DiffStatus::Deleted, display.clone(), None)
511            }
512        }
513        (Some(l), None) => (DiffStatus::Deleted, Some(path_display_string(l)), None),
514        (None, Some(r)) => (DiffStatus::Added, None, Some(path_display_string(r))),
515        (Some(l), Some(r)) => {
516            let lp = path_display_string(l);
517            let rp = path_display_string(r);
518            let status = if file_kind(mode_left) != file_kind(mode_right) {
519                DiffStatus::TypeChanged
520            } else {
521                DiffStatus::Modified
522            };
523            (status, Some(lp), Some(rp))
524        }
525        (None, None) => return Ok(()),
526    };
527
528    let old_mode = FileMode::new(mode_left);
529    let new_mode = FileMode::new(mode_right);
530
531    if let (Some(l), Some(r)) = (left, right) {
532        if mode_left != 0
533            && mode_right != 0
534            && old_mode == new_mode
535            && worktree_content_equal(l, r, mode_left, mode_right)?
536        {
537            return Ok(());
538        }
539    }
540
541    let (old_oid, new_oid) = oids_for_pair(left, right, mode_left, mode_right)?;
542
543    if old_oid == new_oid && old_mode == new_mode {
544        return Ok(());
545    }
546
547    out.push(DiffEntry {
548        status,
549        old_path,
550        new_path,
551        old_mode,
552        new_mode,
553        old_oid,
554        new_oid,
555        score: None,
556    });
557    Ok(())
558}
559
560fn file_kind(mode: u32) -> u32 {
561    if mode & S_IFMT != 0 {
562        mode & S_IFMT
563    } else {
564        S_IFREG
565    }
566}
567
568fn path_mode(path: Option<&Path>, recursing: bool) -> Result<u32, NoIndexError> {
569    let Some(path) = path else {
570        return Ok(0);
571    };
572    if !recursing && path.as_os_str().is_empty() {
573        return Ok(0);
574    }
575    match fs::symlink_metadata(path) {
576        Ok(meta) => Ok(mode_from_metadata(&meta)),
577        Err(_) if recursing => Ok(0),
578        Err(_) => Err(NoIndexError::PathNotAccessible {
579            path: path.display().to_string(),
580        }),
581    }
582}
583
584fn path_display_string(path: &Path) -> String {
585    path.to_string_lossy().into_owned()
586}