Skip to main content

grit_lib/diff/
no_index.rs

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