grit-lib 0.5.18

Core library for the grit Git implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//! Remove untracked (and optionally ignored) paths from the work tree.
//!
//! [`clean_untracked`] reuses the same untracked/ignored walk as [`super::status`]
//! ([`super::status::collect_untracked_and_ignored_with_rules`]) so results stay aligned with
//! `grit status` and system `git status`.

use std::collections::BTreeSet;
use std::fs;
use std::path::Path;

use crate::error::{Error, Result};
use crate::index::{Index, MODE_GITLINK};
use crate::porcelain::stash::remove_empty_dirs;
use crate::porcelain::status::{
    collect_untracked_and_ignored_inner, IgnoredMode, UntrackedCacheMissAction, UntrackedScan,
};
use crate::progress::ProgressSink;
use crate::repo::Repository;
use crate::worktree_rules::WorktreeRules;

/// Which untracked/ignored paths [`clean_untracked`] may remove.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum CleanScope {
    /// Untracked non-ignored paths only (default `git clean` without `-x`/`-X`).
    #[default]
    Untracked,
    /// Untracked and ignored paths (`git clean -x`).
    UntrackedAndIgnored,
    /// Ignored paths only (`git clean -X`).
    OnlyIgnored,
}

/// Inputs for [`clean_untracked`].
#[derive(Debug, Clone, Default)]
pub struct CleanOptions {
    /// Limit removal to paths matching these pathspecs (empty = entire tree).
    pub pathspecs: Vec<String>,
    /// When true, remove untracked directories as well as files (like `git clean -d`).
    pub directories: bool,
    /// When true, remove only files, not directories (like `git clean` without `-d`).
    pub files_only: bool,
    /// Which paths are eligible for removal.
    pub scope: CleanScope,
    /// Extra exclude patterns; matching paths are never removed (like `git clean -e`).
    pub excludes: Vec<String>,
    /// When true, report paths that would be removed without deleting anything.
    pub dry_run: bool,
}

fn clean_scope_includes_untracked(scope: CleanScope) -> bool {
    scope != CleanScope::OnlyIgnored
}

fn clean_scope_includes_ignored(scope: CleanScope) -> bool {
    matches!(
        scope,
        CleanScope::UntrackedAndIgnored | CleanScope::OnlyIgnored
    )
}

/// Result of [`clean_untracked`].
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CleanOutcome {
    /// Repository-relative paths removed (or that would be removed when `dry_run`).
    pub removed: Vec<String>,
}

/// Remove untracked paths from the work tree according to `opts`.
///
/// Nested repositories and submodule checkouts (directories containing `.git`) are
/// never entered or removed.
pub fn clean_untracked(
    repo: &Repository,
    opts: &CleanOptions,
    _progress: &mut dyn ProgressSink,
) -> Result<CleanOutcome> {
    let work_tree = repo
        .work_tree()
        .ok_or_else(|| Error::Message("clean requires a working tree".into()))?;

    let mut index = repo.load_index()?;
    let rules = WorktreeRules::from_repository(repo, &index)?;
    if opts.scope == CleanScope::OnlyIgnored && !opts.excludes.is_empty() {
        rules.ignore_mut().add_cli_excludes(&opts.excludes);
    }

    let remove_directories = opts.directories && !opts.files_only;
    let ignored_mode = if clean_scope_includes_ignored(opts.scope) {
        IgnoredMode::Traditional
    } else {
        IgnoredMode::No
    };
    // Match `git clean` without `-d`: never enumerate nested files inside untracked directories.
    let show_all = false;

    let scan = UntrackedScan {
        ignored_mode,
        show_all,
        sort_paths: true,
        use_untracked_cache: false,
        cache_miss: UntrackedCacheMissAction::RefreshFull,
    };

    let (untracked, ignored, _) = collect_untracked_and_ignored_inner(
        repo,
        &mut index,
        work_tree,
        &opts.pathspecs,
        scan,
        &rules,
    )?;

    let mut candidates = Vec::new();
    if clean_scope_includes_untracked(opts.scope) {
        candidates.extend(untracked);
    }
    if clean_scope_includes_ignored(opts.scope) {
        candidates.extend(ignored);
    }
    if remove_directories && opts.scope != CleanScope::OnlyIgnored {
        let empty_dirs = empty_untracked_directories(
            repo,
            &index,
            &opts.pathspecs,
            &rules,
            opts.scope,
            &candidates,
        )?;
        candidates.extend(empty_dirs);
    }
    let pathspec_ctx = repo.pathspec_match_context();
    candidates.sort();
    candidates.dedup();
    let mut removed = Vec::new();
    for rel in candidates {
        if should_skip_nested_git(work_tree, &rel) {
            continue;
        }
        if !remove_directories && rel.ends_with('/') {
            continue;
        }
        let paths_to_add = if rel.ends_with('/') {
            let trimmed = rel.trim_end_matches('/');
            let abs = work_tree.join(trimmed);
            if nested_git_worktree_descendant_under(&abs)
                || (!clean_scope_includes_ignored(opts.scope)
                    && dir_preserves_ignored_descendants(repo, &index, &rules, work_tree, &abs)?)
                || (!opts.excludes.is_empty()
                    && opts.scope != CleanScope::OnlyIgnored
                    && dir_has_excluded_descendant(work_tree, &abs, &opts.excludes, pathspec_ctx))
            {
                expand_collapsed_dir_paths(repo, &index, &rules, opts.scope, trimmed)?
            } else {
                vec![rel]
            }
        } else {
            vec![rel]
        };
        for path in paths_to_add {
            if should_skip_nested_git(work_tree, &path) {
                continue;
            }
            if !remove_directories && path.ends_with('/') {
                continue;
            }
            if !path_eligible_for_clean_removal(
                repo,
                &index,
                &rules,
                opts.scope,
                &path,
                &opts.excludes,
                pathspec_ctx,
            )? {
                continue;
            }
            if crate::worktree_cwd::cwd_would_be_removed_with_repo_path(
                work_tree,
                path.trim_end_matches('/'),
                repo.environment(),
            ) {
                return Err(Error::Message(format!(
                    "refusing to remove '{path}' because it contains the current directory"
                )));
            }
            removed.push(path);
        }
    }
    removed.sort();
    removed.dedup();

    if opts.dry_run {
        return Ok(CleanOutcome { removed });
    }

    let mut actually_removed = Vec::new();
    let environment = repo.environment();
    for rel in removal_order(&removed) {
        if remove_one_path(work_tree, &rel, remove_directories, environment)? {
            actually_removed.push(rel);
        }
    }
    actually_removed.sort();
    Ok(CleanOutcome {
        removed: actually_removed,
    })
}

/// Empty untracked directories are omitted by the status untracked walk but removed by `git clean -d`.
fn empty_untracked_directories(
    repo: &Repository,
    index: &Index,
    pathspecs: &[String],
    rules: &WorktreeRules,
    scope: CleanScope,
    already_listed: &[String],
) -> Result<Vec<String>> {
    let work_tree = repo
        .work_tree()
        .ok_or_else(|| Error::Message("clean requires a working tree".into()))?;
    let listed: BTreeSet<String> = already_listed
        .iter()
        .map(|p| p.trim_end_matches('/').to_owned())
        .collect();
    let mut found = Vec::new();
    let entries: Vec<_> = fs::read_dir(work_tree)
        .map_err(Error::Io)?
        .filter_map(|e| e.ok())
        .collect();
    for entry in entries {
        let name = entry.file_name().to_string_lossy().to_string();
        if name == ".git" {
            continue;
        }
        let child_abs = entry.path();
        if !child_abs.is_dir() {
            continue;
        }
        if let Some(path) = highest_empty_untracked_dir(
            repo, index, &child_abs, &name, pathspecs, rules, scope, &listed,
        )? {
            found.push(path);
        }
    }
    Ok(found)
}

fn path_matches_clean_exclude(
    path: &str,
    patterns: &[String],
    ctx: crate::pathspec::PathspecMatchContext,
) -> bool {
    if patterns.is_empty() {
        return false;
    }
    let trimmed = path.trim_end_matches('/');
    patterns.iter().any(|pat| {
        crate::pathspec::matches_pathspec_list_with_context(trimmed, std::slice::from_ref(pat), ctx)
            || crate::pathspec::matches_pathspec_list_with_context(
                &format!("{trimmed}/"),
                std::slice::from_ref(pat),
                ctx,
            )
    })
}

/// Whether `path` should be removed given scope and `-e` patterns (protection vs extra `-X` targets).
fn path_eligible_for_clean_removal(
    repo: &Repository,
    index: &Index,
    rules: &WorktreeRules,
    scope: CleanScope,
    path: &str,
    excludes: &[String],
    ctx: crate::pathspec::PathspecMatchContext,
) -> Result<bool> {
    let matches_exclude = path_matches_clean_exclude(path, excludes, ctx);
    match scope {
        CleanScope::OnlyIgnored => {
            if matches_exclude {
                return Ok(true);
            }
            let is_dir = path.ends_with('/');
            let check_path = path.trim_end_matches('/');
            let (ignored, _) =
                rules
                    .ignore_mut()
                    .check_path(repo, Some(index), check_path, is_dir)?;
            Ok(ignored)
        }
        _ => Ok(!matches_exclude),
    }
}

fn dir_has_excluded_descendant(
    work_tree: &Path,
    dir_abs: &Path,
    excludes: &[String],
    ctx: crate::pathspec::PathspecMatchContext,
) -> bool {
    walk_dir_has_excluded_descendant(work_tree, dir_abs, excludes, ctx)
}

fn walk_dir_has_excluded_descendant(
    work_tree: &Path,
    abs: &Path,
    excludes: &[String],
    ctx: crate::pathspec::PathspecMatchContext,
) -> bool {
    if is_nested_git_worktree(abs) {
        return false;
    }
    let entries: Vec<_> = match fs::read_dir(abs) {
        Ok(e) => e.filter_map(|e| e.ok()).collect(),
        Err(_) => return false,
    };
    for entry in entries {
        let name = entry.file_name().to_string_lossy().to_string();
        if name == ".git" {
            continue;
        }
        let path = entry.path();
        let rel = crate::git_path::strip_worktree_prefix(&path, work_tree).unwrap_or(name);
        if path.is_dir() {
            if path_matches_clean_exclude(&format!("{rel}/"), excludes, ctx)
                || path_matches_clean_exclude(&rel, excludes, ctx)
            {
                return true;
            }
            if walk_dir_has_excluded_descendant(work_tree, &path, excludes, ctx) {
                return true;
            }
        } else if path_matches_clean_exclude(&rel, excludes, ctx) {
            return true;
        }
    }
    false
}

/// Returns the highest empty untracked directory marker under `abs` (e.g. `outer/` not `outer/inner/`).
#[allow(clippy::too_many_arguments)]
fn highest_empty_untracked_dir(
    repo: &Repository,
    index: &Index,
    abs: &Path,
    rel: &str,
    pathspecs: &[String],
    rules: &WorktreeRules,
    scope: CleanScope,
    listed: &BTreeSet<String>,
) -> Result<Option<String>> {
    if !rel.is_empty() && is_nested_git_worktree(abs) {
        return Ok(None);
    }
    if !rel.is_empty() && has_tracked_under_index(index, rel) {
        return Ok(None);
    }
    if !rel.is_empty() && index_gitlink_at(index, rel) {
        return Ok(None);
    }

    let entries: Vec<_> = match fs::read_dir(abs) {
        Ok(e) => e.filter_map(|e| e.ok()).collect(),
        Err(_) => return Ok(None),
    };

    let mut child_dirs = Vec::new();
    for entry in entries {
        let name = entry.file_name().to_string_lossy().to_string();
        if name == ".git" {
            continue;
        }
        let child_abs = entry.path();
        if child_abs.is_dir() {
            let child_rel = if rel.is_empty() {
                name.clone()
            } else {
                format!("{rel}/{name}")
            };
            child_dirs.push((child_rel, child_abs));
        } else if !rel.is_empty() {
            return Ok(None);
        }
    }

    if listed.contains(rel.trim_end_matches('/')) {
        return Ok(None);
    }

    if !crate::porcelain::status::status_path_matches(rel, pathspecs)
        && !crate::porcelain::status::status_path_matches(&format!("{rel}/"), pathspecs)
    {
        return Ok(None);
    }

    let (ignored, _) = rules
        .ignore_mut()
        .check_path(repo, Some(index), rel, true)?;
    if ignored && !clean_scope_includes_ignored(scope) {
        return Ok(None);
    }

    if child_dirs.is_empty() {
        return Ok(Some(format!("{rel}/")));
    }

    for (child_rel, child_abs) in &child_dirs {
        if highest_empty_untracked_dir(
            repo, index, child_abs, child_rel, pathspecs, rules, scope, listed,
        )?
        .is_none()
        {
            return Ok(None);
        }
    }

    Ok(Some(format!("{rel}/")))
}

/// True when `dir_abs` contains ignored paths that `git clean` without `-x` would keep.
fn dir_preserves_ignored_descendants(
    repo: &Repository,
    index: &Index,
    rules: &WorktreeRules,
    work_tree: &Path,
    dir_abs: &Path,
) -> Result<bool> {
    walk_dir_preserves_ignored(repo, index, rules, work_tree, dir_abs)
}

fn walk_dir_preserves_ignored(
    repo: &Repository,
    index: &Index,
    rules: &WorktreeRules,
    work_tree: &Path,
    abs: &Path,
) -> Result<bool> {
    if is_nested_git_worktree(abs) {
        return Ok(false);
    }
    let entries: Vec<_> = match fs::read_dir(abs) {
        Ok(e) => e.filter_map(|e| e.ok()).collect(),
        Err(_) => return Ok(false),
    };
    for entry in entries {
        let name = entry.file_name().to_string_lossy().to_string();
        if name == ".git" {
            continue;
        }
        let path = entry.path();
        if path.is_dir() {
            if is_nested_git_worktree(&path) {
                continue;
            }
            let rel = crate::git_path::strip_worktree_prefix(&path, work_tree).unwrap_or(name);
            let (ignored, _) = rules
                .ignore_mut()
                .check_path(repo, Some(index), &rel, true)?;
            if ignored {
                return Ok(true);
            }
            if walk_dir_preserves_ignored(repo, index, rules, work_tree, &path)? {
                return Ok(true);
            }
        } else {
            let rel = crate::git_path::strip_worktree_prefix(&path, work_tree).unwrap_or(name);
            let (ignored, _) = rules
                .ignore_mut()
                .check_path(repo, Some(index), &rel, false)?;
            if ignored {
                return Ok(true);
            }
        }
    }
    Ok(false)
}

fn nested_git_worktree_descendant_under(dir_abs: &Path) -> bool {
    if !dir_abs.is_dir() {
        return false;
    }
    let entries = match fs::read_dir(dir_abs) {
        Ok(e) => e,
        Err(_) => return false,
    };
    for entry in entries.filter_map(|e| e.ok()) {
        let path = entry.path();
        if !path.is_dir() {
            continue;
        }
        if is_nested_git_worktree(&path) {
            return true;
        }
        if nested_git_worktree_descendant_under(&path) {
            return true;
        }
    }
    false
}

/// Expand a collapsed untracked directory into the paths `git clean` would remove,
/// skipping nested repositories and (unless `include_ignored`) ignored files.
fn expand_collapsed_dir_paths(
    repo: &Repository,
    index: &Index,
    rules: &WorktreeRules,
    scope: CleanScope,
    dir_rel: &str,
) -> Result<Vec<String>> {
    let work_tree = repo
        .work_tree()
        .ok_or_else(|| Error::Message("clean requires a working tree".into()))?;
    let mut paths = Vec::new();
    walk_expand_collapsed_dir(
        repo,
        index,
        rules,
        scope,
        &work_tree.join(dir_rel),
        dir_rel,
        &mut paths,
    )?;
    paths.sort();
    paths.dedup();
    Ok(paths)
}

#[allow(clippy::too_many_arguments)]
fn walk_expand_collapsed_dir(
    repo: &Repository,
    index: &Index,
    rules: &WorktreeRules,
    scope: CleanScope,
    abs: &Path,
    rel: &str,
    out: &mut Vec<String>,
) -> Result<()> {
    if is_nested_git_worktree(abs) {
        return Ok(());
    }
    let entries: Vec<_> = match fs::read_dir(abs) {
        Ok(e) => e.filter_map(|e| e.ok()).collect(),
        Err(_) => return Ok(()),
    };

    let mut child_dirs = Vec::new();
    for entry in entries {
        let name = entry.file_name().to_string_lossy().to_string();
        if name == ".git" {
            continue;
        }
        let child_abs = entry.path();
        let child_rel = if rel.is_empty() {
            name.clone()
        } else {
            format!("{rel}/{name}")
        };
        if child_abs.is_dir() {
            child_dirs.push((child_rel, child_abs));
        } else if removable_untracked_path(repo, index, rules, scope, &child_rel)? {
            out.push(child_rel);
        }
    }

    for (child_rel, child_abs) in child_dirs {
        if is_nested_git_worktree(&child_abs) {
            continue;
        }
        let before = out.len();
        walk_expand_collapsed_dir(repo, index, rules, scope, &child_abs, &child_rel, out)?;
        if out.len() == before
            && empty_untracked_directory(&child_abs)
            && removable_untracked_path(repo, index, rules, scope, &format!("{child_rel}/"))?
        {
            out.push(format!("{child_rel}/"));
        }
    }
    Ok(())
}

fn empty_untracked_directory(abs: &Path) -> bool {
    fs::read_dir(abs)
        .map(|d| d.filter_map(|e| e.ok()).all(|e| e.file_name() == ".git"))
        .unwrap_or(false)
}

fn removable_untracked_path(
    repo: &Repository,
    index: &Index,
    rules: &WorktreeRules,
    scope: CleanScope,
    rel: &str,
) -> Result<bool> {
    if clean_scope_includes_ignored(scope) && scope == CleanScope::UntrackedAndIgnored {
        return Ok(true);
    }
    if scope == CleanScope::OnlyIgnored {
        let is_dir = rel.ends_with('/');
        let check_path = rel.trim_end_matches('/');
        let (ignored, _) = rules
            .ignore_mut()
            .check_path(repo, Some(index), check_path, is_dir)?;
        return Ok(ignored);
    }
    let is_dir = rel.ends_with('/');
    let check_path = rel.trim_end_matches('/');
    let (ignored, _) = rules
        .ignore_mut()
        .check_path(repo, Some(index), check_path, is_dir)?;
    Ok(!ignored)
}

fn has_tracked_under_index(index: &Index, rel_dir: &str) -> bool {
    let prefix = format!("{rel_dir}/");
    index
        .entries
        .iter()
        .any(|e| e.stage() == 0 && String::from_utf8_lossy(&e.path).starts_with(&prefix))
}

fn index_gitlink_at(index: &Index, rel: &str) -> bool {
    index.entries.iter().any(|e| {
        e.stage() == 0 && e.mode == MODE_GITLINK && String::from_utf8_lossy(&e.path) == rel
    })
}

/// True when `abs` is a directory containing a nested `.git` (embedded repository).
pub(crate) fn is_nested_git_worktree(abs: &Path) -> bool {
    abs.is_dir() && abs.join(".git").exists()
}

/// True when `rel` names a nested repository root or a path inside one (matches `git clean` / `git stash -u`).
pub(crate) fn path_in_nested_git_worktree(work_tree: &Path, rel: &str) -> bool {
    let trimmed = rel.trim_end_matches('/');
    if is_nested_git_worktree(&work_tree.join(trimmed)) {
        return true;
    }
    let mut prefix = String::new();
    for part in trimmed.split('/') {
        if !prefix.is_empty() {
            prefix.push('/');
        }
        prefix.push_str(part);
        if is_nested_git_worktree(&work_tree.join(&prefix)) {
            return true;
        }
    }
    false
}

fn should_skip_nested_git(work_tree: &Path, rel: &str) -> bool {
    path_in_nested_git_worktree(work_tree, rel)
}

fn removal_order(paths: &[String]) -> Vec<String> {
    let mut ordered = paths.to_vec();
    ordered.sort_by(|a, b| {
        let depth_a = a.matches('/').count() + if a.ends_with('/') { 1 } else { 0 };
        let depth_b = b.matches('/').count() + if b.ends_with('/') { 1 } else { 0 };
        depth_b.cmp(&depth_a).then_with(|| a.cmp(b))
    });
    ordered
}

fn remove_one_path(
    work_tree: &Path,
    rel: &str,
    remove_empty_parent_dirs: bool,
    environment: &crate::environment::Environment,
) -> Result<bool> {
    let abs = work_tree.join(rel.trim_end_matches('/'));
    if !abs.exists() && abs.symlink_metadata().is_err() {
        return Ok(false);
    }
    if rel.ends_with('/') || abs.is_dir() {
        if is_nested_git_worktree(&abs) {
            return Ok(false);
        }
        fs::remove_dir_all(&abs).map_err(Error::Io)?;
        return Ok(true);
    }
    fs::remove_file(&abs).map_err(Error::Io)?;
    if remove_empty_parent_dirs {
        if let Some(parent) = abs.parent() {
            remove_empty_dirs(parent, work_tree, environment);
        }
    }
    Ok(true)
}