lore-cli 0.1.13

Capture AI coding sessions and link them to git commits
Documentation
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
//! Git hooks management command.
//!
//! Provides functionality to install, uninstall, and check the status of
//! git hooks that integrate Lore with the git workflow. The hooks enable
//! automatic session linking after commits and session references in commit
//! messages.

use anyhow::{Context, Result};
use clap::Subcommand;
use colored::Colorize;
use std::fs;
use std::path::{Path, PathBuf};

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

/// Marker comment to identify Lore-managed hooks.
const LORE_HOOK_MARKER: &str = "# Lore hook - managed by lore hooks install";

/// Post-commit hook script content.
///
/// This hook runs after each commit and links any currently active AI
/// development sessions to the commit. This enables forward auto-linking
/// where sessions are linked as commits happen, rather than retroactively.
const POST_COMMIT_HOOK: &str = r#"#!/bin/sh
# Lore post-commit hook
# Lore hook - managed by lore hooks install

# Link any active sessions to this commit
if command -v lore >/dev/null 2>&1; then
    lore link --current --commit HEAD 2>/dev/null || true
fi
"#;

/// Prepare-commit-msg hook script content.
const PREPARE_COMMIT_MSG_HOOK: &str = r#"#!/bin/sh
# Lore prepare-commit-msg hook - add session references
# Lore hook - managed by lore hooks install

COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2

# Only run for regular commits (not merge, squash, etc.)
if [ "$COMMIT_SOURCE" = "" ] || [ "$COMMIT_SOURCE" = "message" ]; then
    if command -v lore >/dev/null 2>&1; then
        # Get active sessions that might be related to this commit
        # This is a placeholder - full implementation would query lore
        :
    fi
fi
"#;

/// Hook types that Lore manages.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HookType {
    /// Runs after each commit to auto-link sessions.
    PostCommit,
    /// Runs before commit message editor to add session references.
    PrepareCommitMsg,
}

impl HookType {
    /// Returns the filename for this hook type.
    fn filename(&self) -> &'static str {
        match self {
            HookType::PostCommit => "post-commit",
            HookType::PrepareCommitMsg => "prepare-commit-msg",
        }
    }

    /// Returns the script content for this hook type.
    fn content(&self) -> &'static str {
        match self {
            HookType::PostCommit => POST_COMMIT_HOOK,
            HookType::PrepareCommitMsg => PREPARE_COMMIT_MSG_HOOK,
        }
    }

    /// Returns all managed hook types.
    fn all() -> &'static [HookType] {
        &[HookType::PostCommit, HookType::PrepareCommitMsg]
    }
}

/// Subcommands for the hooks command.
#[derive(Subcommand)]
pub enum HooksCommand {
    /// Install git hooks in the current repository
    #[command(long_about = "Installs Lore's git hooks in the current repository's\n\
        .git/hooks directory. The post-commit hook automatically\n\
        links sessions to commits using time and file overlap.\n\
        Existing hooks are backed up before being replaced.")]
    Install {
        /// Overwrite existing hooks (backs up originals)
        #[arg(long)]
        #[arg(long_help = "Replace existing hooks that are not managed by Lore.\n\
            The original hooks are saved as <hook>.backup and can\n\
            be restored with 'lore hooks uninstall'.")]
        force: bool,
    },

    /// Uninstall git hooks from the current repository
    #[command(long_about = "Removes Lore's git hooks from the current repository.\n\
        Only removes hooks that Lore installed (identified by marker).\n\
        Restores backed-up hooks if they exist.")]
    Uninstall,

    /// Show status of installed hooks
    #[command(long_about = "Shows which git hooks are currently installed and\n\
        whether they are managed by Lore or are third-party hooks.")]
    Status,
}

/// Arguments for the hooks command.
#[derive(clap::Args)]
#[command(after_help = "EXAMPLES:\n    \
    lore hooks install         Install hooks (skips existing)\n    \
    lore hooks install --force Replace existing hooks\n    \
    lore hooks uninstall       Remove Lore hooks\n    \
    lore hooks status          Check installed hooks")]
pub struct Args {
    /// Hooks subcommand to run
    #[command(subcommand)]
    pub command: HooksCommand,
}

/// Executes the hooks command.
///
/// Dispatches to the appropriate subcommand handler.
pub fn run(args: Args) -> Result<()> {
    match args.command {
        HooksCommand::Install { force } => run_install(force),
        HooksCommand::Uninstall => run_uninstall(),
        HooksCommand::Status => run_status(),
    }
}

/// Installs Lore git hooks in the current repository.
///
/// Creates hook scripts in `.git/hooks/` that integrate with Lore.
/// Existing hooks are backed up before being replaced when using --force.
fn run_install(force: bool) -> Result<()> {
    let hooks_dir = get_hooks_dir()?;
    println!("Installing Lore hooks in {}", hooks_dir.display());
    println!();

    let mut installed_count = 0;
    let mut skipped_count = 0;

    for hook_type in HookType::all() {
        let hook_path = hooks_dir.join(hook_type.filename());
        let status = install_hook(&hook_path, *hook_type, force)?;

        match status {
            InstallStatus::Installed => {
                println!("  {} {}", "Installed".green(), hook_type.filename());
                installed_count += 1;
            }
            InstallStatus::Replaced => {
                println!(
                    "  {} {} (backed up existing to {}.backup)",
                    "Replaced".yellow(),
                    hook_type.filename(),
                    hook_type.filename()
                );
                installed_count += 1;
            }
            InstallStatus::Skipped => {
                println!(
                    "  {} {} (use --force to overwrite)",
                    "Skipped".yellow(),
                    hook_type.filename()
                );
                skipped_count += 1;
            }
            InstallStatus::AlreadyInstalled => {
                println!(
                    "  {} {} (already a Lore hook)",
                    "Skipped".dimmed(),
                    hook_type.filename()
                );
                skipped_count += 1;
            }
        }
    }

    println!();
    if installed_count > 0 {
        println!(
            "Successfully installed {} hook(s).",
            installed_count.to_string().green()
        );
    }
    if skipped_count > 0 && !force {
        println!("{}", "Use --force to overwrite existing hooks.".dimmed());
    }

    Ok(())
}

/// Status of a hook installation attempt.
enum InstallStatus {
    /// Hook was freshly installed.
    Installed,
    /// Existing hook was backed up and replaced.
    Replaced,
    /// Hook already exists and was not replaced.
    Skipped,
    /// Hook is already a Lore-managed hook.
    AlreadyInstalled,
}

/// Installs a single hook.
///
/// Returns the status of the installation attempt.
fn install_hook(hook_path: &Path, hook_type: HookType, force: bool) -> Result<InstallStatus> {
    if hook_path.exists() {
        // Check if it's already a Lore hook
        let existing_content = fs::read_to_string(hook_path)
            .with_context(|| format!("Failed to read existing hook: {}", hook_path.display()))?;

        if existing_content.contains(LORE_HOOK_MARKER) {
            // Already a Lore hook, update it
            write_hook(hook_path, hook_type)?;
            return Ok(InstallStatus::AlreadyInstalled);
        }

        if !force {
            return Ok(InstallStatus::Skipped);
        }

        // Backup existing hook
        let backup_path = hook_path.with_extension("backup");
        fs::rename(hook_path, &backup_path)
            .with_context(|| format!("Failed to backup hook to {}", backup_path.display()))?;

        write_hook(hook_path, hook_type)?;
        Ok(InstallStatus::Replaced)
    } else {
        write_hook(hook_path, hook_type)?;
        Ok(InstallStatus::Installed)
    }
}

/// Writes a hook script to the specified path.
///
/// Sets the executable bit on Unix systems.
fn write_hook(hook_path: &Path, hook_type: HookType) -> Result<()> {
    fs::write(hook_path, hook_type.content())
        .with_context(|| format!("Failed to write hook: {}", hook_path.display()))?;

    #[cfg(unix)]
    {
        let mut perms = fs::metadata(hook_path)?.permissions();
        perms.set_mode(0o755);
        fs::set_permissions(hook_path, perms)
            .with_context(|| format!("Failed to set permissions on {}", hook_path.display()))?;
    }

    Ok(())
}

/// Uninstalls Lore git hooks from the current repository.
///
/// Only removes hooks that were installed by Lore (identified by marker comment).
/// Restores backup hooks if they exist.
fn run_uninstall() -> Result<()> {
    let hooks_dir = get_hooks_dir()?;
    println!("Uninstalling Lore hooks from {}", hooks_dir.display());
    println!();

    let mut removed_count = 0;
    let mut restored_count = 0;
    let mut not_found_count = 0;

    for hook_type in HookType::all() {
        let hook_path = hooks_dir.join(hook_type.filename());

        if !hook_path.exists() {
            println!(
                "  {} {} (not installed)",
                "Skipped".dimmed(),
                hook_type.filename()
            );
            not_found_count += 1;
            continue;
        }

        // Check if it's a Lore hook
        let content = fs::read_to_string(&hook_path)
            .with_context(|| format!("Failed to read hook: {}", hook_path.display()))?;

        if !content.contains(LORE_HOOK_MARKER) {
            println!(
                "  {} {} (not a Lore hook)",
                "Skipped".yellow(),
                hook_type.filename()
            );
            continue;
        }

        // Remove the hook
        fs::remove_file(&hook_path)
            .with_context(|| format!("Failed to remove hook: {}", hook_path.display()))?;
        removed_count += 1;

        // Check for backup to restore
        let backup_path = hook_path.with_extension("backup");
        if backup_path.exists() {
            fs::rename(&backup_path, &hook_path)
                .with_context(|| format!("Failed to restore backup: {}", backup_path.display()))?;
            println!(
                "  {} {} (restored from backup)",
                "Removed".green(),
                hook_type.filename()
            );
            restored_count += 1;
        } else {
            println!("  {} {}", "Removed".green(), hook_type.filename());
        }
    }

    println!();
    if removed_count > 0 {
        println!("Removed {} hook(s).", removed_count.to_string().green());
        if restored_count > 0 {
            println!(
                "Restored {} original hook(s) from backup.",
                restored_count.to_string().green()
            );
        }
    } else if not_found_count == HookType::all().len() {
        println!("{}", "No Lore hooks were installed.".yellow());
    }

    Ok(())
}

/// Shows the status of Lore git hooks.
///
/// Reports which hooks are installed and whether they are Lore-managed.
fn run_status() -> Result<()> {
    let hooks_dir = get_hooks_dir()?;

    println!("Git hooks status:");
    println!();

    for hook_type in HookType::all() {
        let hook_path = hooks_dir.join(hook_type.filename());
        let status = get_hook_status(&hook_path)?;

        let status_str = match status {
            HookStatus::Lore => "installed".green().to_string(),
            HookStatus::Other => "other hook installed".yellow().to_string(),
            HookStatus::None => "not installed".dimmed().to_string(),
        };

        println!(
            "  {:<20} {}",
            format!("{}:", hook_type.filename()),
            status_str
        );
    }

    Ok(())
}

/// Status of a hook file.
enum HookStatus {
    /// Lore hook is installed.
    Lore,
    /// Another (non-Lore) hook is installed.
    Other,
    /// No hook is installed.
    None,
}

/// Gets the status of a hook file.
fn get_hook_status(hook_path: &Path) -> Result<HookStatus> {
    if !hook_path.exists() {
        return Ok(HookStatus::None);
    }

    let content = fs::read_to_string(hook_path)
        .with_context(|| format!("Failed to read hook: {}", hook_path.display()))?;

    if content.contains(LORE_HOOK_MARKER) {
        Ok(HookStatus::Lore)
    } else {
        Ok(HookStatus::Other)
    }
}

/// Gets the path to the git hooks directory.
///
/// Discovers the git repository and returns the path to `.git/hooks/`.
fn get_hooks_dir() -> Result<PathBuf> {
    let repo = git2::Repository::discover(".")
        .context("Not in a git repository. Run this command from within a git repository.")?;

    let git_dir = repo.path();
    let hooks_dir = git_dir.join("hooks");

    // Create hooks directory if it doesn't exist
    if !hooks_dir.exists() {
        fs::create_dir_all(&hooks_dir).with_context(|| {
            format!("Failed to create hooks directory: {}", hooks_dir.display())
        })?;
    }

    Ok(hooks_dir)
}

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

    /// Creates a temporary git repository for testing.
    fn create_test_repo() -> Result<(TempDir, PathBuf)> {
        let temp_dir = TempDir::new()?;
        let repo_path = temp_dir.path();

        // Initialize git repository
        git2::Repository::init(repo_path)?;

        let hooks_dir = repo_path.join(".git").join("hooks");
        fs::create_dir_all(&hooks_dir)?;

        Ok((temp_dir, hooks_dir))
    }

    #[test]
    fn test_hook_type_filename() {
        assert_eq!(HookType::PostCommit.filename(), "post-commit");
        assert_eq!(HookType::PrepareCommitMsg.filename(), "prepare-commit-msg");
    }

    #[test]
    fn test_hook_type_content_contains_marker() {
        for hook_type in HookType::all() {
            assert!(
                hook_type.content().contains(LORE_HOOK_MARKER),
                "Hook {} should contain the Lore marker",
                hook_type.filename()
            );
        }
    }

    #[test]
    fn test_hook_type_content_is_valid_shell_script() {
        for hook_type in HookType::all() {
            assert!(
                hook_type.content().starts_with("#!/bin/sh"),
                "Hook {} should start with shebang",
                hook_type.filename()
            );
        }
    }

    #[test]
    fn test_post_commit_hook_calls_link_current() {
        let content = HookType::PostCommit.content();
        assert!(
            content.contains("lore link --current --commit HEAD"),
            "Post-commit hook should call 'lore link --current --commit HEAD'"
        );
    }

    #[test]
    fn test_install_hook_fresh() -> Result<()> {
        let (_temp_dir, hooks_dir) = create_test_repo()?;
        let hook_path = hooks_dir.join("post-commit");

        let status = install_hook(&hook_path, HookType::PostCommit, false)?;

        assert!(matches!(status, InstallStatus::Installed));
        assert!(hook_path.exists());

        let content = fs::read_to_string(&hook_path)?;
        assert!(content.contains(LORE_HOOK_MARKER));

        Ok(())
    }

    #[test]
    fn test_install_hook_skips_existing() -> Result<()> {
        let (_temp_dir, hooks_dir) = create_test_repo()?;
        let hook_path = hooks_dir.join("post-commit");

        // Create existing hook
        fs::write(&hook_path, "#!/bin/sh\necho 'existing hook'")?;

        let status = install_hook(&hook_path, HookType::PostCommit, false)?;

        assert!(matches!(status, InstallStatus::Skipped));

        // Original content should be preserved
        let content = fs::read_to_string(&hook_path)?;
        assert!(content.contains("existing hook"));

        Ok(())
    }

    #[test]
    fn test_install_hook_force_creates_backup() -> Result<()> {
        let (_temp_dir, hooks_dir) = create_test_repo()?;
        let hook_path = hooks_dir.join("post-commit");
        let backup_path = hooks_dir.join("post-commit.backup");

        // Create existing hook
        fs::write(&hook_path, "#!/bin/sh\necho 'existing hook'")?;

        let status = install_hook(&hook_path, HookType::PostCommit, true)?;

        assert!(matches!(status, InstallStatus::Replaced));
        assert!(backup_path.exists());

        // Backup should contain original content
        let backup_content = fs::read_to_string(&backup_path)?;
        assert!(backup_content.contains("existing hook"));

        // New hook should be Lore hook
        let new_content = fs::read_to_string(&hook_path)?;
        assert!(new_content.contains(LORE_HOOK_MARKER));

        Ok(())
    }

    #[test]
    fn test_install_hook_updates_existing_lore_hook() -> Result<()> {
        let (_temp_dir, hooks_dir) = create_test_repo()?;
        let hook_path = hooks_dir.join("post-commit");

        // Create existing Lore hook
        let old_content = format!("#!/bin/sh\n{LORE_HOOK_MARKER}\nold version");
        fs::write(&hook_path, &old_content)?;

        let status = install_hook(&hook_path, HookType::PostCommit, false)?;

        assert!(matches!(status, InstallStatus::AlreadyInstalled));

        // Should be updated to current content
        let content = fs::read_to_string(&hook_path)?;
        assert_eq!(content, POST_COMMIT_HOOK);

        Ok(())
    }

    #[test]
    fn test_get_hook_status_not_installed() -> Result<()> {
        let (_temp_dir, hooks_dir) = create_test_repo()?;
        let hook_path = hooks_dir.join("post-commit");

        let status = get_hook_status(&hook_path)?;

        assert!(matches!(status, HookStatus::None));

        Ok(())
    }

    #[test]
    fn test_get_hook_status_lore_installed() -> Result<()> {
        let (_temp_dir, hooks_dir) = create_test_repo()?;
        let hook_path = hooks_dir.join("post-commit");

        fs::write(&hook_path, POST_COMMIT_HOOK)?;

        let status = get_hook_status(&hook_path)?;

        assert!(matches!(status, HookStatus::Lore));

        Ok(())
    }

    #[test]
    fn test_get_hook_status_other_installed() -> Result<()> {
        let (_temp_dir, hooks_dir) = create_test_repo()?;
        let hook_path = hooks_dir.join("post-commit");

        fs::write(&hook_path, "#!/bin/sh\necho 'other hook'")?;

        let status = get_hook_status(&hook_path)?;

        assert!(matches!(status, HookStatus::Other));

        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn test_write_hook_sets_executable() -> Result<()> {
        let (_temp_dir, hooks_dir) = create_test_repo()?;
        let hook_path = hooks_dir.join("post-commit");

        write_hook(&hook_path, HookType::PostCommit)?;

        let metadata = fs::metadata(&hook_path)?;
        let mode = metadata.permissions().mode();

        // Check that owner execute bit is set
        assert!(mode & 0o100 != 0, "Hook should be executable");

        Ok(())
    }
}