gity 0.1.2

Make large Git repositories feel instant - a lightweight daemon that accelerates Git operations
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
use mimalloc::MiMalloc;

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

mod daemon_launcher;
mod paths;
mod status_cache;

use clap::Parser;
use gity_cli::{format_repo_status, format_response, Cli, CliAction, CliError};
use gity_daemon::{
    NngClient, NngServer, NotificationServer, NotificationSubscriber, Runtime, ServerError,
};
use gity_ipc::{
    DaemonCommand, DaemonError, DaemonNotification, DaemonResponse, DaemonService, JobEventKind,
    RepoStatusDetail, ValidatedPath, WatchEventKind,
};
use gity_storage::{StorageContext, StorageError};
use std::{
    env,
    fs::OpenOptions,
    io::{self, Write},
    path::{Path, PathBuf},
    time::{SystemTime, UNIX_EPOCH},
};
use tokio::{
    pin, signal,
    sync::mpsc,
    time::{sleep, Duration},
};

use crate::{daemon_launcher::spawn_daemon, paths::GityPaths, status_cache::StatusCache};

/// Converts a PathBuf to a ValidatedPath.
fn validated_path(path: PathBuf) -> Result<ValidatedPath, CliError> {
    ValidatedPath::new(path).map_err(|e| CliError::Message(e.to_string()))
}

const DEFAULT_ADDR: &str = "tcp://127.0.0.1:7557";
const DEFAULT_EVENTS_ADDR: &str = "tcp://127.0.0.1:7558";

#[tokio::main]
async fn main() {
    if let Err(err) = try_main().await {
        eprintln!("{err}");
        std::process::exit(1);
    }
}

async fn try_main() -> Result<(), CliError> {
    let cli = Cli::parse();
    let address = default_address();
    let events_address = default_events_address();
    match cli.into_action()? {
        CliAction::RunDaemon => run_daemon(address, events_address).await,
        action => run_client_action(action, &address, &events_address).await,
    }
}

async fn run_client_action(
    action: CliAction,
    address: &str,
    events_address: &str,
) -> Result<(), CliError> {
    match action {
        CliAction::Rpc(command) => run_command_with_auto_start(command, address).await,
        CliAction::List { stats } => run_list_command(address, stats).await,
        CliAction::Logs {
            repo_path,
            follow,
            limit,
        } => run_logs_command(address, repo_path, follow, limit).await,
        CliAction::FsMonitorHelper {
            version,
            token,
            repo,
        } => run_fsmonitor_helper(address, version, token, repo).await,
        CliAction::StreamEvents => {
            ensure_daemon_running(address).await?;
            run_event_stream(events_address).await
        }
        CliAction::RunDaemon => unreachable!("handled earlier"),
        CliAction::StartDaemon => run_start_daemon(address).await,
        CliAction::StopDaemon => run_stop_daemon(address).await,
        CliAction::OneshotDaemon { repo_path } => {
            run_oneshot_daemon(address, events_address, repo_path).await
        }
        CliAction::RunTray => run_tray(address).await,
        CliAction::DbStats => run_db_stats().await,
        CliAction::DbCompact => run_db_compact().await,
        CliAction::DbPruneLogs { older_than_days } => run_db_prune_logs(older_than_days).await,
    }
}

#[cfg(feature = "tray")]
async fn run_tray(address: &str) -> Result<(), CliError> {
    use gity_tray::{run_tray_loop, GityTray, TrayConfig};

    // Make sure daemon is running first
    ensure_daemon_running(address).await?;

    let config = TrayConfig {
        daemon_address: address.to_string(),
    };

    let tray = GityTray::new(config)
        .map_err(|e| CliError::Message(format!("failed to create tray: {}", e)))?;

    println!("System tray started. Right-click the icon for options.");
    run_tray_loop(&tray);

    Ok(())
}

#[cfg(not(feature = "tray"))]
async fn run_tray(_address: &str) -> Result<(), CliError> {
    Err(CliError::Message(
        "tray feature not enabled - rebuild with --features tray".into(),
    ))
}

async fn run_command_with_auto_start(
    command: DaemonCommand,
    address: &str,
) -> Result<(), CliError> {
    let client = NngClient::new(address.to_string());
    match command {
        DaemonCommand::Status { repo_path, .. } => {
            match run_status_command(&client, repo_path.clone(), address).await {
                Err(CliError::Daemon(DaemonError::Transport(_))) => {
                    ensure_daemon_running(address).await?;
                    run_status_command(&client, repo_path, address).await
                }
                result => result,
            }
        }
        other => run_generic_command(&client, other, address).await,
    }
}

async fn run_generic_command(
    client: &NngClient,
    command: DaemonCommand,
    address: &str,
) -> Result<(), CliError> {
    // Warn about problematic filesystem configurations before registration
    if let DaemonCommand::RegisterRepo { ref repo_path } = command {
        warn_if_problematic_filesystem(repo_path);
    }
    let response = request_with_restart(client, address, command).await?;
    println!("{}", format_response(&response));
    Ok(())
}

/// Warn users if the repository is on a filesystem where file watching may not work.
#[allow(unused_variables)]
fn warn_if_problematic_filesystem(repo_path: &Path) {
    // Check for WSL2 + Windows filesystem (9P/drvfs)
    #[cfg(target_os = "linux")]
    {
        if is_wsl() && is_windows_filesystem(repo_path) {
            eprintln!("⚠️  Warning: Repository is on a Windows filesystem (/mnt/...)");
            eprintln!(
                "   File watching via inotify does NOT work across the WSL2/Windows boundary."
            );
            eprintln!("   For best results, move the repository to the Linux filesystem:");
            eprintln!("     git clone <url> ~/code/repo");
            eprintln!("     gity register ~/code/repo");
            eprintln!();
            eprintln!("   See: docs/fsmonitor.md#wsl2-windows-subsystem-for-linux");
            eprintln!();
        }
    }

    // Check for network filesystems
    #[cfg(target_os = "linux")]
    {
        if is_network_filesystem(repo_path) {
            eprintln!("⚠️  Warning: Repository appears to be on a network filesystem.");
            eprintln!("   File watching may be unreliable. Consider disabling fsmonitor:");
            eprintln!("     git config core.fsmonitor false");
            eprintln!();
        }
    }
}

#[cfg(target_os = "linux")]
fn is_wsl() -> bool {
    std::fs::read_to_string("/proc/version")
        .map(|v| v.to_lowercase().contains("microsoft"))
        .unwrap_or(false)
}

#[cfg(target_os = "linux")]
fn is_windows_filesystem(path: &Path) -> bool {
    // Check if path starts with /mnt/ (typical WSL mount point for Windows drives)
    let path_str = path.to_string_lossy();
    if path_str.starts_with("/mnt/") && path_str.len() > 5 {
        // /mnt/c, /mnt/d, etc.
        let after_mnt = &path_str[5..];
        if let Some(first_char) = after_mnt.chars().next() {
            if first_char.is_ascii_alphabetic() {
                return true;
            }
        }
    }
    false
}

#[cfg(target_os = "linux")]
fn is_network_filesystem(path: &Path) -> bool {
    use std::process::Command;
    // Use df to check filesystem type
    if let Ok(output) = Command::new("df").arg("-T").arg(path).output() {
        let stdout = String::from_utf8_lossy(&output.stdout).to_lowercase();
        // Common network filesystem types
        return stdout.contains("nfs")
            || stdout.contains("cifs")
            || stdout.contains("smb")
            || stdout.contains("sshfs")
            || stdout.contains("fuse.sshfs");
    }
    false
}

async fn run_list_command(address: &str, stats: bool) -> Result<(), CliError> {
    let client = NngClient::new(address.to_string());
    run_generic_command(&client, DaemonCommand::ListRepos, address).await?;
    if stats {
        let metrics = request_with_restart(&client, address, DaemonCommand::Metrics).await?;
        println!("{}", format_response(&metrics));
    }
    Ok(())
}

async fn run_fsmonitor_helper(
    address: &str,
    version: u8,
    token: Option<String>,
    repo_override: Option<PathBuf>,
) -> Result<(), CliError> {
    if version != 2 {
        return Err(CliError::Message(format!(
            "unsupported fsmonitor protocol version {version}"
        )));
    }
    let repo_path = resolve_repo_path(repo_override)?;
    let known_generation = token.as_deref().and_then(|value| value.parse::<u64>().ok());

    // Try reading from mmap cache first (fast path)
    if let Some(known_gen) = known_generation {
        if let Ok(paths) = GityPaths::discover() {
            let cache_dir = paths.data_dir().join("fsmonitor_cache");
            if let Ok(cache) = gity_daemon::FsMonitorCache::new(cache_dir) {
                // Check if cached generation matches
                if let Ok(Some(cached_gen)) = cache.read_generation(&repo_path) {
                    if cached_gen == known_gen {
                        // Generation unchanged, return cached snapshot (empty dirty paths)
                        if let Ok(Some(snapshot)) = cache.read(&repo_path) {
                            return emit_fsmonitor_payload(version, &snapshot).map_err(map_io_error);
                        }
                    }
                }
            }
        }
    }

    // Cache miss or stale - fall through to IPC
    let client = NngClient::new(address.to_string());
    let validated = validated_path(repo_path.clone())?;
    let response = request_with_restart(
        &client,
        address,
        DaemonCommand::FsMonitorSnapshot {
            repo_path: validated,
            last_seen_generation: known_generation,
        },
    )
    .await?;
    let snapshot = match response {
        DaemonResponse::FsMonitorSnapshot(snapshot) => snapshot,
        other => {
            return Err(CliError::Message(format!(
                "unexpected fsmonitor response: {other:?}"
            )));
        }
    };
    emit_fsmonitor_payload(version, &snapshot).map_err(map_io_error)
}

async fn run_logs_command(
    address: &str,
    repo_path: PathBuf,
    follow: bool,
    limit: usize,
) -> Result<(), CliError> {
    let client = NngClient::new(address.to_string());
    let validated = validated_path(repo_path.clone())?;
    let response = request_with_restart(
        &client,
        address,
        DaemonCommand::FetchLogs {
            repo_path: validated,
            limit,
        },
    )
    .await?;
    match response {
        DaemonResponse::Logs(entries) => {
            if entries.is_empty() {
                println!("no log entries for {}", repo_path.display());
            } else {
                for entry in entries {
                    print_log_entry(&entry);
                }
            }
        }
        other => {
            println!("{}", format_response(&other));
        }
    }
    if follow {
        ensure_daemon_running(address).await?;
        follow_log_stream(address, repo_path).await?;
    }
    Ok(())
}

async fn run_event_stream(address: &str) -> Result<(), CliError> {
    let subscriber = NotificationSubscriber::new(address.to_string());
    let mut stream = subscriber.connect().await.map_err(CliError::Daemon)?;
    println!("listening for daemon notifications on {address} (Ctrl+C to exit)...");
    loop {
        tokio::select! {
            _ = signal::ctrl_c() => break,
            notification = stream.next() => match notification {
                Ok(notification) => print_notification(&notification),
                Err(err) => return Err(CliError::Daemon(err)),
            }
        }
    }
    Ok(())
}

async fn follow_log_stream(address: &str, repo_path: PathBuf) -> Result<(), CliError> {
    let subscriber = NotificationSubscriber::new(address.to_string());
    let mut stream = subscriber.connect().await.map_err(CliError::Daemon)?;
    println!(
        "following logs for {} (Ctrl+C to exit)...",
        repo_path.display()
    );
    loop {
        tokio::select! {
            _ = signal::ctrl_c() => break,
            notification = stream.next() => match notification {
                Ok(DaemonNotification::Log(entry)) => {
                    if entry.repo_path == repo_path {
                        print_log_entry(&entry);
                    }
                }
                Ok(_) => {}
                Err(err) => return Err(CliError::Daemon(err)),
            }
        }
    }
    Ok(())
}

async fn run_start_daemon(address: &str) -> Result<(), CliError> {
    let client = NngClient::new(address.to_string());
    if client.execute(DaemonCommand::HealthCheck).await.is_ok() {
        println!("daemon already running on {address}");
        return Ok(());
    }
    spawn_daemon(address)
        .map_err(|err| CliError::Message(format!("failed to start daemon: {err}")))?;
    let mut attempts = 0;
    let max_attempts = 25;
    while attempts < max_attempts {
        match client.execute(DaemonCommand::HealthCheck).await {
            Ok(_) => {
                println!("daemon started on {address}");
                return Ok(());
            }
            Err(_) => {
                attempts += 1;
                sleep(Duration::from_millis(200)).await;
            }
        }
    }
    Err(CliError::Message(
        "timed out waiting for daemon to start".into(),
    ))
}

async fn run_stop_daemon(address: &str) -> Result<(), CliError> {
    let client = NngClient::new(address.to_string());
    match client.execute(DaemonCommand::Shutdown).await {
        Ok(response) => {
            println!("{}", format_response(&response));
            Ok(())
        }
        Err(DaemonError::Transport(_)) => {
            println!("daemon not running");
            Ok(())
        }
        Err(err) => Err(CliError::Daemon(err)),
    }
}

async fn run_db_stats() -> Result<(), CliError> {
    let paths = GityPaths::discover().map_err(map_io_error)?;
    let storage = StorageContext::new(paths.data_dir()).map_err(map_storage_error)?;
    let stats = storage.stats().map_err(map_storage_error)?;

    println!("Database Statistics:");
    println!("  Data directory: {}", paths.data_dir().display());
    println!("  Metadata size: {}", format_bytes(stats.metadata_size_bytes));
    println!("  Logs size: {}", format_bytes(stats.logs_size_bytes));
    println!(
        "  Total size: {}",
        format_bytes(stats.metadata_size_bytes + stats.logs_size_bytes)
    );
    println!("  Registered repos: {}", stats.repo_count);
    println!("  Log entries: {}", stats.log_entry_count);
    Ok(())
}

async fn run_db_compact() -> Result<(), CliError> {
    let paths = GityPaths::discover().map_err(map_io_error)?;
    let storage = StorageContext::new(paths.data_dir()).map_err(map_storage_error)?;

    let before = storage.stats().map_err(map_storage_error)?;
    let before_size = before.metadata_size_bytes + before.logs_size_bytes;

    println!("Compacting databases...");
    storage.compact_all().map_err(map_storage_error)?;

    let after = storage.stats().map_err(map_storage_error)?;
    let after_size = after.metadata_size_bytes + after.logs_size_bytes;

    println!("Compaction complete.");
    println!(
        "  Before: {}",
        format_bytes(before_size)
    );
    println!(
        "  After: {}",
        format_bytes(after_size)
    );
    if before_size > after_size {
        println!(
            "  Reclaimed: {}",
            format_bytes(before_size - after_size)
        );
    }
    Ok(())
}

async fn run_db_prune_logs(older_than_days: u64) -> Result<(), CliError> {
    let paths = GityPaths::discover().map_err(map_io_error)?;
    let storage = StorageContext::new(paths.data_dir()).map_err(map_storage_error)?;

    let max_age = Duration::from_secs(older_than_days * 24 * 60 * 60);
    println!(
        "Pruning log entries older than {} days...",
        older_than_days
    );

    let pruned = storage
        .prune_old_log_entries(max_age)
        .map_err(map_storage_error)?;

    println!("Pruned {} log entries.", pruned);
    Ok(())
}

fn format_bytes(bytes: u64) -> String {
    const UNITS: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"];
    let mut value = bytes as f64;
    let mut unit = 0;
    while value >= 1024.0 && unit < UNITS.len() - 1 {
        value /= 1024.0;
        unit += 1;
    }
    if unit == 0 {
        format!("{bytes} {}", UNITS[unit])
    } else {
        format!("{value:.1} {}", UNITS[unit])
    }
}

async fn run_oneshot_daemon(
    address: &str,
    events_address: &str,
    repo_path: PathBuf,
) -> Result<(), CliError> {
    let paths = GityPaths::discover().map_err(map_io_error)?;
    let storage = StorageContext::new(paths.data_dir()).map_err(map_storage_error)?;
    let store = storage.metadata_store().map_err(map_storage_error)?;
    let log_tree = storage.log_tree().map_err(map_storage_error)?;
    let helper_command = fsmonitor_helper_command();

    let (notification_tx, notification_rx) = mpsc::unbounded_channel();
    let runtime =
        Runtime::with_notifications(store, Some(notification_tx), helper_command, Some(log_tree));
    let shutdown = runtime.shutdown_signal();
    let shared = runtime.shared();
    let service = runtime.service_handle();

    // Register the repo if not already registered
    let validated = validated_path(repo_path.clone())?;
    let _ = service
        .execute(DaemonCommand::RegisterRepo {
            repo_path: validated,
        })
        .await;

    let runtime_fut = runtime.run();
    let server = NngServer::new(address.to_string(), shared.clone(), shutdown.clone());
    let server_fut = server.run();
    let notification_server = NotificationServer::new(events_address.to_string(), notification_rx);
    let notification_fut = notification_server.run(shutdown.clone());

    pin!(runtime_fut);
    pin!(server_fut);
    pin!(notification_fut);

    println!("oneshot daemon for {} on {address}", repo_path.display());

    // Run until jobs are complete or timeout
    let timeout = sleep(Duration::from_secs(30));
    pin!(timeout);

    loop {
        let pending_jobs = match service.execute(DaemonCommand::HealthCheck).await {
            Ok(DaemonResponse::Health(health)) => health.pending_jobs,
            _ => 0,
        };

        if pending_jobs == 0 {
            println!("all jobs complete, shutting down");
            break;
        }

        tokio::select! {
            _ = &mut timeout => {
                println!("timeout reached, shutting down");
                break;
            }
            _ = sleep(Duration::from_millis(500)) => continue,
        }
    }

    shutdown.shutdown();
    let _ = runtime_fut.await;
    let _ = server_fut.await;
    let _ = notification_fut.await;

    Ok(())
}

async fn run_status_command(
    client: &NngClient,
    repo_path: ValidatedPath,
    address: &str,
) -> Result<(), CliError> {
    let paths = GityPaths::discover().map_err(map_io_error)?;
    let cache = StatusCache::new(paths.data_dir()).map_err(map_io_error)?;
    let cached = cache.load(repo_path.as_path()).map_err(map_io_error)?;
    let cached_detail = cached.clone();
    let known_generation = cached.as_ref().map(|detail| detail.generation);
    let response = request_with_restart(
        client,
        address,
        DaemonCommand::Status {
            repo_path: repo_path.clone(),
            known_generation,
        },
    )
    .await?;
    let decision = resolve_status_decision(response, cached_detail);
    if let Some(detail) = decision.to_cache.as_ref() {
        cache.store(detail).map_err(map_io_error)?;
    }
    if let Some(err) = decision.stderr {
        eprintln!("{err}");
    }
    println!("{}", decision.stdout);
    Ok(())
}

fn print_notification(notification: &DaemonNotification) {
    match notification {
        DaemonNotification::WatchEvent(event) => println!(
            "[watch] {} -> {} ({})",
            event.repo_path.display(),
            event.path.display(),
            format_watch_kind(event.kind)
        ),
        DaemonNotification::JobEvent(event) => println!(
            "[job] {:?} for {} ({})",
            event.job,
            event.repo_path.display(),
            format_job_event_kind(event.kind)
        ),
        DaemonNotification::RepoStatus(detail) => println!(
            "[status] {} dirty paths={}, generation={}",
            detail.repo_path.display(),
            detail.dirty_paths.len(),
            detail.generation
        ),
        DaemonNotification::Log(entry) => print_log_entry(entry),
    }
}

fn print_log_entry(entry: &gity_ipc::LogEntry) {
    let timestamp = entry
        .timestamp
        .duration_since(UNIX_EPOCH)
        .map(|dur| dur.as_secs())
        .unwrap_or_default();
    println!(
        "[log {}] {}: {}",
        timestamp,
        entry.repo_path.display(),
        entry.message
    );
}

fn format_watch_kind(kind: WatchEventKind) -> &'static str {
    match kind {
        WatchEventKind::Created => "created",
        WatchEventKind::Modified => "modified",
        WatchEventKind::Deleted => "deleted",
    }
}

fn format_job_event_kind(kind: JobEventKind) -> &'static str {
    match kind {
        JobEventKind::Queued => "queued",
        JobEventKind::Started => "started",
        JobEventKind::Completed => "completed",
        JobEventKind::Failed => "failed",
    }
}

struct StatusDecision {
    stdout: String,
    stderr: Option<String>,
    to_cache: Option<RepoStatusDetail>,
}

fn resolve_status_decision(
    response: DaemonResponse,
    cached: Option<RepoStatusDetail>,
) -> StatusDecision {
    match response {
        DaemonResponse::RepoStatus(detail) => StatusDecision {
            stdout: format_repo_status(&detail),
            stderr: None,
            to_cache: Some(detail),
        },
        DaemonResponse::RepoStatusUnchanged {
            repo_path,
            generation,
        } => {
            if let Some(detail) = cached {
                StatusDecision {
                    stdout: format_repo_status(&detail),
                    stderr: None,
                    to_cache: None,
                }
            } else {
                StatusDecision {
                    stdout: format_response(&DaemonResponse::RepoStatusUnchanged {
                        repo_path,
                        generation,
                    }),
                    stderr: None,
                    to_cache: None,
                }
            }
        }
        DaemonResponse::Error(message) => {
            if let Some(detail) = cached {
                StatusDecision {
                    stdout: format_repo_status(&detail),
                    stderr: Some(format!("daemon error: {message}; showing cached status")),
                    to_cache: None,
                }
            } else {
                StatusDecision {
                    stdout: message,
                    stderr: None,
                    to_cache: None,
                }
            }
        }
        other => StatusDecision {
            stdout: format_response(&other),
            stderr: None,
            to_cache: None,
        },
    }
}

async fn request_with_restart(
    client: &NngClient,
    address: &str,
    command: DaemonCommand,
) -> Result<DaemonResponse, CliError> {
    match client.execute(command.clone()).await {
        Ok(response) => Ok(response),
        Err(DaemonError::Transport(_)) => {
            ensure_daemon_running(address).await?;
            client.execute(command).await.map_err(CliError::Daemon)
        }
        Err(err) => Err(CliError::Daemon(err)),
    }
}

async fn run_daemon(address: String, events_address: String) -> Result<(), CliError> {
    let paths = GityPaths::discover().map_err(map_io_error)?;
    let storage = StorageContext::new(paths.data_dir()).map_err(map_storage_error)?;
    append_daemon_log(
        paths.daemon_log_path(),
        &format!("Starting daemon on {address}"),
    )?;
    let store = storage.metadata_store().map_err(map_storage_error)?;

    let helper_command = fsmonitor_helper_command();
    let (notification_tx, notification_rx) = mpsc::unbounded_channel();
    let log_tree = storage.log_tree().map_err(map_storage_error)?;
    let runtime = Runtime::with_notifications_and_data_dir(
        store,
        Some(notification_tx),
        helper_command.clone(),
        Some(log_tree),
        paths.data_dir().to_path_buf(),
    );
    let shutdown = runtime.shutdown_signal();
    let shared = runtime.shared();
    let runtime_fut = runtime.run();
    let server = NngServer::new(address.clone(), shared, shutdown.clone());
    let server_fut = server.run();
    let notification_server = NotificationServer::new(events_address.clone(), notification_rx);
    let notification_fut = notification_server.run(shutdown.clone());

    pin!(runtime_fut);
    pin!(server_fut);
    pin!(notification_fut);

    println!("Daemon listening on {address}");

    let mut server_result: Option<Result<(), ServerError>> = None;
    let mut notification_result: Option<Result<(), ServerError>> = None;
    tokio::select! {
        _ = &mut runtime_fut => (),
        result = &mut server_fut => {
            server_result = Some(result);
        }
        result = &mut notification_fut => {
            notification_result = Some(result);
        }
        _ = signal::ctrl_c() => {
            println!("Ctrl+C received, shutting down...");
        }
    }

    shutdown.shutdown();
    let _ = runtime_fut.await;
    if server_result.is_none() {
        server_result = Some(server_fut.await);
    }
    if notification_result.is_none() {
        notification_result = Some(notification_fut.await);
    }

    if let Some(Err(err)) = server_result {
        return Err(CliError::Message(format!("daemon server error: {err}")));
    }
    if let Some(Err(err)) = notification_result {
        return Err(CliError::Message(format!(
            "notification server error: {err}"
        )));
    }
    append_daemon_log(paths.daemon_log_path(), "Daemon shutdown complete")?;
    Ok(())
}

fn default_address() -> String {
    std::env::var("GITY_DAEMON_ADDR").unwrap_or_else(|_| DEFAULT_ADDR.to_string())
}

fn default_events_address() -> String {
    std::env::var("GITY_EVENTS_ADDR").unwrap_or_else(|_| DEFAULT_EVENTS_ADDR.to_string())
}

async fn ensure_daemon_running(address: &str) -> Result<(), CliError> {
    let paths = GityPaths::discover().map_err(map_io_error)?;
    let _ = StorageContext::new(paths.data_dir()).map_err(map_storage_error)?;
    spawn_daemon(address)
        .map_err(|err| CliError::Message(format!("failed to start daemon: {err}")))?;
    let client = NngClient::new(address.to_string());
    let mut attempts = 0;
    let max_attempts = 25;
    while attempts < max_attempts {
        match client.execute(DaemonCommand::HealthCheck).await {
            Ok(_) => return Ok(()),
            Err(DaemonError::Transport(_)) => {
                attempts += 1;
                sleep(Duration::from_millis(200)).await;
            }
            Err(err) => return Err(CliError::Daemon(err)),
        }
    }
    Err(CliError::Message(
        "timed out waiting for daemon to start".into(),
    ))
}

fn append_daemon_log(path: impl AsRef<Path>, message: &str) -> Result<(), CliError> {
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let mut file = OpenOptions::new()
        .create(true)
        .append(true)
        .open(path.as_ref())
        .map_err(map_io_error)?;
    writeln!(file, "[{timestamp}] {message}").map_err(map_io_error)?;
    Ok(())
}

fn map_io_error(err: io::Error) -> CliError {
    CliError::Message(err.to_string())
}

fn map_storage_error(err: StorageError) -> CliError {
    CliError::Message(err.to_string())
}

fn fsmonitor_helper_command() -> Option<String> {
    if let Ok(value) = env::var("GITY_FSMONITOR_HELPER") {
        let trimmed = value.trim();
        if trimmed.is_empty() {
            return None;
        }
        return Some(value);
    }
    env::current_exe()
        .ok()
        .map(|path| format!("\"{}\" fsmonitor-helper", path.display()))
}

fn resolve_repo_path(repo_override: Option<PathBuf>) -> Result<PathBuf, CliError> {
    if let Some(path) = repo_override {
        return Ok(path);
    }
    if let Ok(git_dir) = env::var("GIT_DIR") {
        let cwd = env::current_dir().map_err(map_io_error)?;
        let candidate = if Path::new(&git_dir).is_absolute() {
            PathBuf::from(git_dir)
        } else {
            cwd.join(git_dir)
        };
        if candidate.file_name().and_then(|name| name.to_str()) == Some(".git") {
            return candidate
                .parent()
                .map(|parent| parent.to_path_buf())
                .ok_or_else(|| CliError::Message("unable to resolve repo root".into()));
        } else {
            return Ok(candidate);
        }
    }
    env::current_dir().map_err(map_io_error)
}

fn emit_fsmonitor_payload(_version: u8, snapshot: &gity_ipc::FsMonitorSnapshot) -> io::Result<()> {
    let mut stdout = io::stdout().lock();
    stdout.write_all(snapshot.generation.to_string().as_bytes())?;
    stdout.write_all(b"\0")?;
    for path in &snapshot.dirty_paths {
        let path_str = path.to_string_lossy();
        stdout.write_all(path_str.as_bytes())?;
        stdout.write_all(b"\0")?;
    }
    stdout.flush()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_detail() -> RepoStatusDetail {
        RepoStatusDetail {
            repo_path: PathBuf::from("/repo"),
            dirty_paths: vec![PathBuf::from("file.txt")],
            generation: 5,
        }
    }

    #[test]
    fn resolve_status_decision_uses_cached_when_unchanged() {
        let cached = sample_detail();
        let response = DaemonResponse::RepoStatusUnchanged {
            repo_path: PathBuf::from("/repo"),
            generation: cached.generation,
        };
        let decision = resolve_status_decision(response, Some(cached.clone()));
        assert!(decision.to_cache.is_none());
        assert!(decision.stderr.is_none());
        assert_eq!(decision.stdout, format_repo_status(&cached));
    }

    #[test]
    fn resolve_status_decision_warns_and_uses_cache_on_error() {
        let cached = sample_detail();
        let response = DaemonResponse::Error("boom".into());
        let decision = resolve_status_decision(response, Some(cached.clone()));
        assert!(decision.to_cache.is_none());
        assert!(decision
            .stderr
            .as_deref()
            .is_some_and(|msg| msg.contains("showing cached status")));
        assert_eq!(decision.stdout, format_repo_status(&cached));
    }

    #[test]
    fn resolve_status_decision_surfaces_error_without_cache() {
        let response = DaemonResponse::Error("boom".into());
        let decision = resolve_status_decision(response, None);
        assert_eq!(decision.stdout, "boom");
        assert!(decision.stderr.is_none());
        assert!(decision.to_cache.is_none());
    }
}