cargo-port 0.1.0

A TUI for inspecting and managing Rust projects
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
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use std::sync::mpsc::Sender as StdSender;
use std::time::Instant;

use tokio::runtime::Handle;
use tokio::sync::Semaphore;

use super::ProjectEntry;
use super::WatchState;
use super::events::EventContext;
use crate::channel::Sender;
use crate::constants::CARGO_TOML;
use crate::constants::DEBOUNCE_DURATION;
use crate::constants::GIT_DIR;
use crate::constants::TARGET_DIR;
use crate::lint;
use crate::project::AbsolutePath;
use crate::project::CheckoutInfo;
use crate::project::RepoInfo;
use crate::scan;
use crate::scan::BackgroundMsg;
use crate::scan::MetadataDispatchContext;

pub(super) fn schedule_disk_refresh(
    pending_disk: &mut HashMap<String, WatchState>,
    project_label: &str,
    now: Instant,
) {
    match pending_disk
        .entry(project_label.to_string())
        .or_insert(WatchState::Idle)
    {
        state @ WatchState::Idle => {
            *state = WatchState::pending(now, false);
        },
        WatchState::Pending {
            debounce_deadline, ..
        } => {
            *debounce_deadline = now + DEBOUNCE_DURATION;
        },
        state @ WatchState::Running => *state = WatchState::RunningDirty,
        WatchState::RunningDirty => {},
    }
}

pub(super) fn handle_disk_completion(
    pending_disk: &mut HashMap<String, WatchState>,
    project_label: &str,
) {
    let now = Instant::now();
    let Some(state) = pending_disk.get_mut(project_label) else {
        return;
    };
    *state = match *state {
        WatchState::Running => WatchState::Idle,
        WatchState::RunningDirty => WatchState::pending(now, false),
        WatchState::Idle | WatchState::Pending { .. } => return,
    };
}

pub(super) fn handle_git_completion(
    pending_git: &mut HashMap<AbsolutePath, WatchState>,
    repo_root: &AbsolutePath,
) {
    let now = Instant::now();
    let Some(state) = pending_git.get_mut(repo_root) else {
        return;
    };
    *state = match *state {
        WatchState::Running => WatchState::Idle,
        WatchState::RunningDirty => WatchState::pending(now, false),
        WatchState::Idle | WatchState::Pending { .. } => return,
    };
}

pub(super) fn is_fast_git_refresh_event(event_path: &Path, entry: &ProjectEntry) -> bool {
    let Some(repo_root) = entry.repo_root.as_deref() else {
        return false;
    };
    let Some(git_dir) = entry.git_dir.as_deref() else {
        return false;
    };
    let Some(common_git_dir) = entry.common_git_dir.as_deref() else {
        return false;
    };
    event_path == repo_root.join(".gitignore")
        || event_path == git_dir.join("index")
        || event_path == git_dir.join("info").join("exclude")
        || event_path == git_dir.join("HEAD")
        || event_path == common_git_dir.join("packed-refs")
        || event_path.starts_with(common_git_dir.join("refs").join("heads"))
        || event_path.starts_with(common_git_dir.join("refs").join("remotes"))
        || is_worktree_git_fallback_event(event_path, git_dir)
}

pub(super) fn is_internal_git_refresh_event(event_path: &Path, entry: &ProjectEntry) -> bool {
    let Some(git_dir) = entry.git_dir.as_deref() else {
        return false;
    };
    let Some(common_git_dir) = entry.common_git_dir.as_deref() else {
        return false;
    };
    let Some(repo_root) = entry.repo_root.as_deref() else {
        return false;
    };
    event_path == repo_root.join(".gitignore")
        || event_path == git_dir.join("index")
        || event_path == git_dir.join("index.lock")
        || event_path == git_dir.join("info").join("exclude")
        || event_path == git_dir.join("HEAD")
        || event_path == git_dir.join("FETCH_HEAD")
        || event_path == git_dir.join("ORIG_HEAD")
        || event_path == git_dir.join("config")
        || event_path == git_dir.join("packed-refs")
        || event_path.starts_with(git_dir.join("refs").join("heads"))
        || event_path.starts_with(git_dir.join("refs").join("remotes"))
        || event_path == common_git_dir.join("packed-refs")
        || event_path.starts_with(common_git_dir.join("refs").join("heads"))
        || event_path.starts_with(common_git_dir.join("refs").join("remotes"))
        || is_worktree_git_fallback_event(event_path, git_dir)
}

pub(super) fn is_worktree_git_fallback_event(event_path: &Path, git_dir: &Path) -> bool {
    let logs_dir = git_dir.join("logs");
    event_path == git_dir || event_path == logs_dir || event_path.starts_with(&logs_dir)
}

/// Key used to dedup git refreshes in `pending_git`. Prefers the
/// shared `common_git_dir` so primary + linked worktrees of the same
/// repo collapse into a single pending refresh; falls back to the
/// per-entry `repo_root` when the common-git-dir lookup is missing
/// (degenerate case — e.g. a worktree whose `.git` file points at a
/// path we couldn't resolve).
pub(super) fn git_refresh_key(entry: &ProjectEntry) -> Option<AbsolutePath> {
    entry
        .common_git_dir
        .clone()
        .or_else(|| entry.repo_root.clone())
}

pub(super) fn enqueue_git_refresh(
    pending_git: &mut HashMap<AbsolutePath, WatchState>,
    repo_root: AbsolutePath,
    now: Instant,
    immediate: bool,
    cause: &str,
) {
    let pending_count = pending_git
        .iter()
        .filter(|(path, _)| path.as_path() != repo_root.as_path())
        .filter(|(_, state)| matches!(state, WatchState::Pending { .. }))
        .count()
        + usize::from(!matches!(
            pending_git.get(&repo_root),
            Some(WatchState::Pending { .. })
        ));
    tracing::info!(
        repo_root = %repo_root.display(),
        immediate,
        cause,
        pending_git = pending_count,
        "watcher_enqueue_git_refresh"
    );
    match pending_git.entry(repo_root).or_insert(WatchState::Idle) {
        state @ WatchState::Idle => *state = WatchState::pending(now, immediate),
        WatchState::Pending {
            debounce_deadline, ..
        } => {
            *debounce_deadline = if immediate {
                now
            } else {
                now + DEBOUNCE_DURATION
            };
        },
        state @ WatchState::Running => *state = WatchState::RunningDirty,
        WatchState::RunningDirty => {},
    }
}

fn is_ready_to_launch(state: &WatchState, now: Instant) -> bool {
    matches!(
        state,
        WatchState::Pending {
            debounce_deadline,
            max_deadline,
        } if now >= *debounce_deadline || now >= *max_deadline
    )
}

const fn mark_running(state: &mut WatchState) {
    if matches!(state, WatchState::Pending { .. }) {
        *state = WatchState::Running;
    }
}

pub(super) fn is_internal_git_path(event_path: &Path, entry: &ProjectEntry) -> bool {
    let repo_root = entry.repo_root.as_deref();
    let git_dir = entry.git_dir.as_deref();
    let common_git_dir = entry.common_git_dir.as_deref();
    // Match events under the resolved git dir (handles worktrees) or
    // under repo_root/.git (handles normal repos where git_dir ==
    // repo_root/.git, but also catches events like refs/heads updates
    // that live in the common git dir rather than the worktree git dir).
    git_dir.is_some_and(|d| event_path.starts_with(d))
        || common_git_dir.is_some_and(|d| event_path.starts_with(d))
        || repo_root.is_some_and(|r| event_path.starts_with(r.join(GIT_DIR)))
}

/// Cargo's `target-directory` may be redirected by an out-of-tree
/// `<dir>/.cargo/config[.toml]` (typically `~/.cargo/config.toml`,
/// the cargo home). Edits to such a config affect every project
/// nested under `<dir>`, none of which contains the event path —
/// so the per-project `classify_cargo_metadata_event_path` gate at
/// the bottom of `handle_notify_event` will not fire for them.
///
/// When the event basename matches a cargo-metadata trigger AND the
/// path looks like `<dir>/.cargo/config[.toml]`, fan a metadata
/// refresh out to every project whose root is descendant of `<dir>`.
pub(super) fn try_dispatch_out_of_tree_cargo_config_refresh(
    event_path: &Path,
    ctx: &EventContext<'_>,
    metadata_dispatch: Option<&MetadataDispatchContext>,
) {
    let Some(dispatch) = metadata_dispatch else {
        return;
    };
    if !matches!(
        lint::classify_cargo_metadata_basename(event_path),
        Some(lint::CargoMetadataTriggerKind::CargoConfig)
    ) {
        return;
    }
    let Some(cargo_dir) = event_path.parent() else {
        return;
    };
    let Some(host_dir) = cargo_dir.parent() else {
        return;
    };
    for project_root in ctx.projects.keys() {
        if project_root.as_path().starts_with(host_dir) {
            scan::spawn_cargo_metadata_refresh(dispatch.clone(), project_root.clone());
        }
    }
}

/// Does `event_path` sit under the workspace's resolved target
/// directory? `resolved_target = None` means we don't yet have
/// workspace metadata — fall back to `<project_root>/target`, which is
/// what cargo uses by default.
///
/// When the metadata *is* available (e.g. target is redirected via
/// `CARGO_TARGET_DIR` or `.cargo/config.toml`), events under the real
/// target dir are suppressed and events under the in-tree `target/`
/// decoy are treated as ordinary project events. The design doc
/// (call-site migrations → step 2) calls this out explicitly.
pub(super) fn is_target_event_for(
    event_path: &Path,
    project_root: &Path,
    resolved_target: Option<&Path>,
) -> bool {
    let fallback = project_root.join(TARGET_DIR);
    let dir = resolved_target.unwrap_or(fallback.as_path());
    event_path.starts_with(dir)
}

pub(super) fn is_target_metadata_event(event_path: &Path, project_root: &Path) -> bool {
    let cargo_toml = project_root.join(CARGO_TOML);
    let build_rs = project_root.join("build.rs");
    let src_main = project_root.join("src").join("main.rs");
    let src_bin = project_root.join("src").join("bin");
    let examples = project_root.join("examples");
    let benches = project_root.join("benches");
    let tests = project_root.join("tests");

    event_path == cargo_toml
        || event_path == build_rs
        || event_path == src_main
        || event_path.starts_with(src_bin)
        || event_path.starts_with(examples)
        || event_path.starts_with(benches)
        || event_path.starts_with(tests)
}

pub(super) fn emit_root_git_info_refresh(
    background_tx: &Sender<BackgroundMsg>,
    entry: &ProjectEntry,
) {
    let started = Instant::now();
    let Some(repo) = RepoInfo::get(entry.abs_path.as_path()) else {
        return;
    };
    let checkout = CheckoutInfo::get(entry.abs_path.as_path(), repo.local_main_branch.as_deref());
    tracing::info!(
        elapsed_ms = tui_pane::perf_log_ms(started.elapsed().as_millis()),
        path = %entry.project_label,
        git_status = checkout.as_ref().map_or("unknown", |c| c.status.label()),
        "watcher_root_git_info_refresh"
    );
    let _ = background_tx.send(BackgroundMsg::RepoInfo {
        path: entry.abs_path.clone(),
        info: repo,
    });
    if let Some(checkout) = checkout {
        let _ = background_tx.send(BackgroundMsg::CheckoutInfo {
            path: entry.abs_path.clone(),
            info: checkout,
        });
    }
}

pub(super) fn fire_git_updates(
    handle: &Handle,
    git_limit: &Arc<Semaphore>,
    git_done_tx: &StdSender<AbsolutePath>,
    background_tx: &Sender<BackgroundMsg>,
    projects: &HashMap<AbsolutePath, ProjectEntry>,
    pending_git: &mut HashMap<AbsolutePath, WatchState>,
) {
    let now = Instant::now();
    let ready: Vec<AbsolutePath> = pending_git
        .iter()
        .filter(|(_, state)| is_ready_to_launch(state, now))
        .map(|(refresh_key, _)| refresh_key.clone())
        .collect();

    for refresh_key in ready {
        // Affected = every entry whose refresh-key matches; for the
        // common-git-dir case that's primary + all linked siblings of
        // the same repo. Each entry needs its own `CheckoutInfo`
        // probe because branch/HEAD/status differ per worktree.
        let affected: Vec<AbsolutePath> = projects
            .values()
            .filter(|entry| git_refresh_key(entry).as_ref() == Some(&refresh_key))
            .map(|entry| entry.abs_path.clone())
            .collect();
        if affected.is_empty() {
            if let Some(state) = pending_git.get_mut(&refresh_key) {
                *state = WatchState::Idle;
            }
            continue;
        }
        // Identify the primary checkout: the one whose own `.git` is
        // the common git dir (i.e., its working tree is `<git_dir>/..`).
        // Falls back to the first affected entry when no clear primary
        // is visible (e.g., entry registered without `common_git_dir`).
        let primary_path = projects
            .values()
            .find(|entry| {
                entry.git_dir.as_deref() == Some(refresh_key.as_path())
                    || entry.common_git_dir.as_deref() == Some(refresh_key.as_path())
                        && entry.abs_path.as_path().join(GIT_DIR).is_dir()
            })
            .map_or_else(|| affected[0].clone(), |entry| entry.abs_path.clone());
        if let Some(state) = pending_git.get_mut(&refresh_key) {
            mark_running(state);
        }
        spawn_git_refresh(
            handle,
            git_limit,
            git_done_tx.clone(),
            background_tx.clone(),
            refresh_key,
            primary_path,
            affected,
        );
    }
}

pub(super) fn spawn_git_refresh(
    handle: &Handle,
    git_limit: &Arc<Semaphore>,
    git_done_tx: StdSender<AbsolutePath>,
    background_tx: Sender<BackgroundMsg>,
    refresh_key: AbsolutePath,
    primary_path: AbsolutePath,
    affected: Vec<AbsolutePath>,
) {
    let handle = handle.clone();
    let git_limit = Arc::clone(git_limit);
    handle.spawn(async move {
        let queue_started = Instant::now();
        let Ok(_permit) = git_limit.acquire_owned().await else {
            return;
        };
        tracing::info!(
            elapsed_ms = tui_pane::perf_log_ms(queue_started.elapsed().as_millis()),
            refresh_key = %refresh_key.display(),
            affected_rows = affected.len(),
            "watcher_git_queue_wait"
        );

        // Probe the per-repo half once at the primary's path. Linked
        // siblings reuse this RepoInfo via the entry's `git_repo` slot
        // (the primary-only write policy in `handle_repo_info` keeps
        // just this copy).
        let started = Instant::now();
        let primary_for_repo = primary_path.clone();
        let repo_info =
            tokio::task::spawn_blocking(move || RepoInfo::get(primary_for_repo.as_path()))
                .await
                .ok()
                .flatten();
        let local_main_branch = repo_info.as_ref().and_then(|r| r.local_main_branch.clone());
        if let Some(repo_info) = repo_info {
            let _ = background_tx.send(BackgroundMsg::RepoInfo {
                path: primary_path.clone(),
                info: repo_info,
            });
        }

        // Probe the per-checkout half for each affected path. These are
        // cheap (no per-remote loop); each yields the worktree's own
        // branch/HEAD/status.
        for checkout_path in affected {
            let probe_path = checkout_path.clone();
            let lmb = local_main_branch.clone();
            let checkout = tokio::task::spawn_blocking(move || {
                CheckoutInfo::get(probe_path.as_path(), lmb.as_deref())
            })
            .await
            .ok()
            .flatten();
            if let Some(checkout) = checkout {
                let _ = background_tx.send(BackgroundMsg::CheckoutInfo {
                    path: checkout_path,
                    info: checkout,
                });
            }
        }

        tracing::info!(
            elapsed_ms = tui_pane::perf_log_ms(started.elapsed().as_millis()),
            refresh_key = %refresh_key.display(),
            "watcher_git_refresh"
        );
        let _ = git_done_tx.send(refresh_key);
    });
}

pub(super) fn fire_disk_updates(
    handle: &Handle,
    disk_limit: &Arc<Semaphore>,
    disk_done_tx: &StdSender<String>,
    background_tx: &Sender<BackgroundMsg>,
    projects: &HashMap<AbsolutePath, ProjectEntry>,
    pending_disk: &mut HashMap<String, WatchState>,
) {
    let now = Instant::now();
    let ready: Vec<String> = pending_disk
        .iter()
        .filter(|(_, state)| is_ready_to_launch(state, now))
        .map(|(key, _)| key.clone())
        .collect();

    for project_label in ready {
        let Some(entry) = projects.values().find(|e| e.project_label == project_label) else {
            if let Some(state) = pending_disk.get_mut(&project_label) {
                *state = WatchState::Idle;
            }
            continue;
        };
        if let Some(state) = pending_disk.get_mut(&project_label) {
            mark_running(state);
        }
        spawn_disk_update(
            handle,
            disk_limit,
            disk_done_tx.clone(),
            background_tx.clone(),
            project_label.clone(),
            entry.abs_path.clone(),
        );
    }
}

pub(super) fn spawn_disk_update(
    handle: &Handle,
    disk_limit: &Arc<Semaphore>,
    disk_done_tx: StdSender<String>,
    background_tx: Sender<BackgroundMsg>,
    project_label: String,
    abs_path: AbsolutePath,
) {
    let handle = handle.clone();
    let disk_limit = Arc::clone(disk_limit);
    handle.spawn(async move {
        let queue_started = Instant::now();
        let Ok(_permit) = disk_limit.acquire_owned().await else {
            return;
        };
        tracing::info!(
            elapsed_ms = tui_pane::perf_log_ms(queue_started.elapsed().as_millis()),
            path = %project_label,
            abs_path = %abs_path.display(),
            "watcher_disk_queue_wait"
        );

        let started = Instant::now();
        let abs_for_msg = abs_path.clone();
        let bytes = tokio::task::spawn_blocking(move || scan::dir_size(&abs_path))
            .await
            .ok()
            .unwrap_or(0);
        tracing::info!(
            elapsed_ms = tui_pane::perf_log_ms(started.elapsed().as_millis()),
            path = %project_label,
            bytes,
            "watcher_disk_usage"
        );
        let _ = background_tx.send(BackgroundMsg::DiskUsage {
            path: abs_for_msg,
            bytes,
        });
        let _ = disk_done_tx.send(project_label);
    });
}