chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
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
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! Git integration commands

use anyhow::{Context, Result};
use colored::*;
use std::path::Path;
use std::process::Command;

use crate::workspace::get_workspace_by_path;

/// Configure git settings for chat sessions
pub fn git_config(name: Option<&str>, email: Option<&str>, path: Option<&str>) -> Result<()> {
    let project_dir = path.map(Path::new).unwrap_or_else(|| Path::new("."));

    // Check if git repo exists
    if !project_dir.join(".git").exists() {
        anyhow::bail!("Not a git repository: {}", project_dir.display());
    }

    // If no options provided, show current config
    if name.is_none() && email.is_none() {
        println!("Git configuration for: {}", project_dir.display());

        let output = Command::new("git")
            .current_dir(project_dir)
            .args(["config", "--local", "user.name"])
            .output()?;
        let current_name = String::from_utf8_lossy(&output.stdout).trim().to_string();

        let output = Command::new("git")
            .current_dir(project_dir)
            .args(["config", "--local", "user.email"])
            .output()?;
        let current_email = String::from_utf8_lossy(&output.stdout).trim().to_string();

        println!(
            "  Name:  {}",
            if current_name.is_empty() {
                "(not set)".to_string()
            } else {
                current_name
            }
        );
        println!(
            "  Email: {}",
            if current_email.is_empty() {
                "(not set)".to_string()
            } else {
                current_email
            }
        );
        return Ok(());
    }

    // Set name if provided
    if let Some(n) = name {
        let output = Command::new("git")
            .current_dir(project_dir)
            .args(["config", "--local", "user.name", n])
            .output()?;

        if !output.status.success() {
            anyhow::bail!(
                "Failed to set git user.name: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }
        println!("{} Set git user.name = {}", "[OK]".green(), n);
    }

    // Set email if provided
    if let Some(e) = email {
        let output = Command::new("git")
            .current_dir(project_dir)
            .args(["config", "--local", "user.email", e])
            .output()?;

        if !output.status.success() {
            anyhow::bail!(
                "Failed to set git user.email: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }
        println!("{} Set git user.email = {}", "[OK]".green(), e);
    }

    Ok(())
}

/// Initialize git versioning for chat sessions
pub fn git_init(project_path: &str) -> Result<()> {
    let workspace = get_workspace_by_path(project_path)?
        .context(format!("Workspace not found for path: {}", project_path))?;

    let project_dir = Path::new(project_path);
    let vscode_dir = project_dir.join(".vscode");
    let symlink_path = vscode_dir.join("chat-sessions");

    // Create .vscode directory if needed
    std::fs::create_dir_all(&vscode_dir)?;

    // Create symlink to chat sessions
    if symlink_path.exists() {
        println!("{} Chat versioning already initialized", "[!]".yellow());
        println!("   Symlink: {}", symlink_path.display());
        return Ok(());
    }

    #[cfg(unix)]
    std::os::unix::fs::symlink(&workspace.chat_sessions_path, &symlink_path)?;

    #[cfg(windows)]
    std::os::windows::fs::symlink_dir(&workspace.chat_sessions_path, &symlink_path)?;

    println!(
        "{} Initialized git versioning for chat sessions",
        "[OK]".green()
    );
    println!("   Symlink: {}", symlink_path.display());
    println!("   Target: {}", workspace.chat_sessions_path.display());
    println!("\nNext steps:");
    println!("  1. Add .vscode/chat-sessions to your .gitignore if you want to exclude them");
    println!(
        "  2. Or commit them: csm add {} --commit -m 'Add chat sessions'",
        project_path
    );

    Ok(())
}

/// Add chat sessions to git
pub fn git_add(project_path: &str, commit: bool, message: Option<&str>) -> Result<()> {
    let project_dir = Path::new(project_path);
    let chat_sessions_path = project_dir.join(".vscode").join("chat-sessions");

    if !chat_sessions_path.exists() {
        anyhow::bail!(
            "Chat versioning not initialized. Run 'csm init {}' first",
            project_path
        );
    }

    // Stage files
    let output = Command::new("git")
        .current_dir(project_dir)
        .args(["add", ".vscode/chat-sessions"])
        .output()?;

    if !output.status.success() {
        anyhow::bail!(
            "Failed to stage chat sessions: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    println!("{} Staged chat sessions for commit", "[OK]".green());

    // Commit if requested
    if commit {
        let msg = message.unwrap_or("Update chat sessions");

        let output = Command::new("git")
            .current_dir(project_dir)
            .args(["commit", "-m", msg])
            .output()?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            if stderr.contains("nothing to commit") {
                println!("{} Nothing to commit", "[i]".blue());
            } else {
                anyhow::bail!("Failed to commit: {}", stderr);
            }
        } else {
            // Get commit hash
            let output = Command::new("git")
                .current_dir(project_dir)
                .args(["rev-parse", "--short", "HEAD"])
                .output()?;

            let hash = String::from_utf8_lossy(&output.stdout).trim().to_string();
            println!("{} Committed: {}", "[OK]".green(), hash);
        }
    }

    Ok(())
}

/// Show git status of chat sessions
pub fn git_status(project_path: &str) -> Result<()> {
    let project_dir = Path::new(project_path);

    // Check if it's a git repo
    let is_git_repo = project_dir.join(".git").exists();

    // Check if versioning is enabled
    let chat_sessions_path = project_dir.join(".vscode").join("chat-sessions");
    let versioning_enabled = chat_sessions_path.exists();

    // Get workspace info
    let workspace = get_workspace_by_path(project_path)?;
    let session_count = workspace.map(|w| w.chat_session_count).unwrap_or(0);

    println!("Project: {}", project_path);
    println!("Git repository: {}", if is_git_repo { "Yes" } else { "No" });
    println!(
        "Chat versioning: {}",
        if versioning_enabled {
            "Enabled"
        } else {
            "Disabled"
        }
    );
    println!("Total sessions: {}", session_count);

    if versioning_enabled && is_git_repo {
        // Get git status for chat sessions
        let output = Command::new("git")
            .current_dir(project_dir)
            .args(["status", "--porcelain", ".vscode/chat-sessions"])
            .output()?;

        let status = String::from_utf8_lossy(&output.stdout);
        let lines: Vec<&str> = status.lines().collect();

        let modified: Vec<_> = lines
            .iter()
            .filter(|l| l.starts_with(" M") || l.starts_with("M "))
            .collect();
        let untracked: Vec<_> = lines.iter().filter(|l| l.starts_with("??")).collect();
        let staged: Vec<_> = lines
            .iter()
            .filter(|l| l.starts_with("A ") || l.starts_with("M "))
            .collect();

        println!("\nGit status:");
        println!("  Modified: {}", modified.len());
        println!("  Untracked: {}", untracked.len());
        println!("  Staged: {}", staged.len());

        if !modified.is_empty() {
            println!("\n  Modified files:");
            for f in modified.iter().take(5) {
                println!(
                    "    - {}",
                    f.trim_start_matches(|c: char| c.is_whitespace() || c == 'M')
                );
            }
            if modified.len() > 5 {
                println!("    ... and {} more", modified.len() - 5);
            }
        }
    }

    Ok(())
}

/// Create a git tag snapshot of chat sessions
pub fn git_snapshot(project_path: &str, tag: Option<&str>, message: Option<&str>) -> Result<()> {
    let project_dir = Path::new(project_path);
    let chat_sessions_path = project_dir.join(".vscode").join("chat-sessions");

    if !chat_sessions_path.exists() {
        anyhow::bail!(
            "Chat versioning not initialized. Run 'csm init {}' first",
            project_path
        );
    }

    // Generate tag name if not provided
    let timestamp = chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string();
    let tag_name = tag
        .map(|t| t.to_string())
        .unwrap_or_else(|| format!("chat-snapshot-{}", timestamp));

    let msg = message.unwrap_or("Chat session snapshot");

    // Stage and commit
    let _ = Command::new("git")
        .current_dir(project_dir)
        .args(["add", ".vscode/chat-sessions"])
        .output()?;

    let _ = Command::new("git")
        .current_dir(project_dir)
        .args(["commit", "-m", &format!("Snapshot: {}", msg)])
        .output()?;

    // Create tag
    let output = Command::new("git")
        .current_dir(project_dir)
        .args(["tag", "-a", &tag_name, "-m", msg])
        .output()?;

    if !output.status.success() {
        anyhow::bail!(
            "Failed to create tag: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    // Get commit hash
    let output = Command::new("git")
        .current_dir(project_dir)
        .args(["rev-parse", "--short", "HEAD"])
        .output()?;

    let hash = String::from_utf8_lossy(&output.stdout).trim().to_string();

    println!("{} Created snapshot", "[OK]".green());
    println!("   Tag: {}", tag_name);
    println!("   Commit: {}", hash);

    Ok(())
}

/// Track chat sessions together with associated file changes
pub fn git_track(
    project_path: &str,
    message: Option<&str>,
    all: bool,
    files: Option<&[String]>,
    tag: Option<&str>,
) -> Result<()> {
    let project_dir = Path::new(project_path);
    let chat_sessions_path = project_dir.join(".vscode").join("chat-sessions");

    if !chat_sessions_path.exists() {
        anyhow::bail!(
            "Chat versioning not initialized. Run 'csm git init {}' first",
            project_path
        );
    }

    println!(
        "{} Tracking chat sessions with file changes",
        "[*]".blue().bold()
    );
    println!("{}", "=".repeat(60));

    // Stage chat sessions
    let output = Command::new("git")
        .current_dir(project_dir)
        .args(["add", ".vscode/chat-sessions"])
        .output()?;

    if !output.status.success() {
        anyhow::bail!(
            "Failed to stage chat sessions: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    println!("{} Staged chat sessions", "[OK]".green());

    // Stage additional files if requested
    if all {
        let output = Command::new("git")
            .current_dir(project_dir)
            .args(["add", "-A"])
            .output()?;

        if output.status.success() {
            println!("{} Staged all changes", "[OK]".green());
        }
    } else if let Some(file_list) = files {
        for file in file_list {
            let output = Command::new("git")
                .current_dir(project_dir)
                .args(["add", file])
                .output()?;

            if output.status.success() {
                println!("{} Staged: {}", "[OK]".green(), file);
            } else {
                println!("{} Failed to stage: {}", "[!]".yellow(), file);
            }
        }
    }

    // Get status summary
    let output = Command::new("git")
        .current_dir(project_dir)
        .args(["diff", "--cached", "--stat"])
        .output()?;

    let stat = String::from_utf8_lossy(&output.stdout);
    if !stat.is_empty() {
        println!("\n{} Changes to be committed:", "[*]".blue());
        for line in stat.lines().take(10) {
            println!("   {}", line);
        }
        if stat.lines().count() > 10 {
            println!("   ... and {} more files", stat.lines().count() - 10);
        }
    }

    // Generate commit message
    let timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M").to_string();
    let default_msg = format!("Track chat sessions with changes ({})", timestamp);
    let commit_msg = message.unwrap_or(&default_msg);

    // Create commit
    let output = Command::new("git")
        .current_dir(project_dir)
        .args(["commit", "-m", commit_msg])
        .output()?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        if stderr.contains("nothing to commit") {
            println!("\n{} Nothing to commit", "[i]".blue());
            return Ok(());
        }
        anyhow::bail!("Failed to commit: {}", stderr);
    }

    // Get commit hash
    let output = Command::new("git")
        .current_dir(project_dir)
        .args(["rev-parse", "--short", "HEAD"])
        .output()?;

    let hash = String::from_utf8_lossy(&output.stdout).trim().to_string();
    println!("\n{} Committed: {}", "[OK]".green(), hash);

    // Create tag if requested
    if let Some(tag_name) = tag {
        let output = Command::new("git")
            .current_dir(project_dir)
            .args(["tag", "-a", tag_name, "-m", commit_msg])
            .output()?;

        if output.status.success() {
            println!("{} Tagged: {}", "[OK]".green(), tag_name);
        }
    }

    Ok(())
}

/// Show history of chat session commits with associated file changes
pub fn git_log(project_path: &str, count: usize, sessions_only: bool) -> Result<()> {
    let project_dir = Path::new(project_path);

    println!("{} Chat Session History", "[*]".blue().bold());
    println!("{}", "=".repeat(60));

    // Build log command
    let mut args = vec![
        "log".to_string(),
        format!("-{}", count),
        "--pretty=format:%h|%ad|%s".to_string(),
        "--date=short".to_string(),
    ];

    if sessions_only {
        args.push("--".to_string());
        args.push(".vscode/chat-sessions".to_string());
    }

    let output = Command::new("git")
        .current_dir(project_dir)
        .args(&args)
        .output()?;

    if !output.status.success() {
        anyhow::bail!(
            "Failed to get git log: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    let log = String::from_utf8_lossy(&output.stdout);

    if log.is_empty() {
        println!("\n{} No commits found", "[i]".blue());
        return Ok(());
    }

    println!();
    for line in log.lines() {
        let parts: Vec<&str> = line.split('|').collect();
        if parts.len() >= 3 {
            let hash = parts[0];
            let date = parts[1];
            let message = parts[2];

            // Check if this commit touched chat sessions
            let output = Command::new("git")
                .current_dir(project_dir)
                .args(["diff-tree", "--no-commit-id", "--name-only", "-r", hash])
                .output()?;

            let files = String::from_utf8_lossy(&output.stdout);
            let has_chat = files.contains("chat-sessions");
            let file_count = files.lines().count();

            let chat_marker = if has_chat {
                "[chat]".cyan()
            } else {
                "      ".normal()
            };

            println!(
                "{} {} {} {} ({})",
                hash.yellow(),
                chat_marker,
                date.dimmed(),
                message,
                format!("{} files", file_count).dimmed()
            );
        }
    }

    println!();
    println!(
        "{} Use 'csm git diff --from <hash>' to see changes",
        "[i]".cyan()
    );

    Ok(())
}

/// Diff chat sessions between commits or current state
pub fn git_diff(
    project_path: &str,
    from: Option<&str>,
    to: Option<&str>,
    with_files: bool,
) -> Result<()> {
    let project_dir = Path::new(project_path);

    let from_ref = from.unwrap_or("HEAD");
    let to_ref = to.unwrap_or("");

    println!("{} Chat Session Diff", "[*]".blue().bold());
    println!("{}", "=".repeat(60));

    if to_ref.is_empty() {
        println!("{} {}..working directory", "[>]".blue(), from_ref);
    } else {
        println!("{} {}..{}", "[>]".blue(), from_ref, to_ref);
    }

    // Get diff for chat sessions
    let mut diff_args = vec!["diff".to_string()];
    if to_ref.is_empty() {
        diff_args.push(from_ref.to_string());
    } else {
        diff_args.push(format!("{}..{}", from_ref, to_ref));
    }
    diff_args.push("--stat".to_string());
    diff_args.push("--".to_string());
    diff_args.push(".vscode/chat-sessions".to_string());

    let output = Command::new("git")
        .current_dir(project_dir)
        .args(&diff_args)
        .output()?;

    let chat_diff = String::from_utf8_lossy(&output.stdout);

    if chat_diff.is_empty() {
        println!("\n{} No changes to chat sessions", "[i]".blue());
    } else {
        println!("\n{} Chat session changes:", "[*]".cyan());
        for line in chat_diff.lines() {
            println!("   {}", line);
        }
    }

    // Show associated file changes if requested
    if with_files {
        let mut file_diff_args = vec!["diff".to_string()];
        if to_ref.is_empty() {
            file_diff_args.push(from_ref.to_string());
        } else {
            file_diff_args.push(format!("{}..{}", from_ref, to_ref));
        }
        file_diff_args.push("--stat".to_string());

        let output = Command::new("git")
            .current_dir(project_dir)
            .args(&file_diff_args)
            .output()?;

        let file_diff = String::from_utf8_lossy(&output.stdout);

        if !file_diff.is_empty() {
            println!("\n{} All file changes:", "[*]".cyan());
            for line in file_diff.lines().take(20) {
                println!("   {}", line);
            }
            if file_diff.lines().count() > 20 {
                println!("   ... and {} more", file_diff.lines().count() - 20);
            }
        }
    }

    Ok(())
}

/// Restore chat sessions from a specific commit
pub fn git_restore(project_path: &str, commit: &str, with_files: bool, backup: bool) -> Result<()> {
    let project_dir = Path::new(project_path);
    let chat_sessions_path = project_dir.join(".vscode").join("chat-sessions");

    println!("{} Restoring Chat Sessions", "[*]".blue().bold());
    println!("{}", "=".repeat(60));
    println!("{} From commit: {}", "[>]".blue(), commit);

    // Verify commit exists
    let output = Command::new("git")
        .current_dir(project_dir)
        .args(["rev-parse", "--verify", commit])
        .output()?;

    if !output.status.success() {
        anyhow::bail!("Commit not found: {}", commit);
    }

    // Create backup if requested
    if backup && chat_sessions_path.exists() {
        let backup_name = format!("chat-sessions-backup-{}", chrono::Utc::now().timestamp());
        let backup_path = project_dir.join(".vscode").join(&backup_name);

        if let Err(e) = std::fs::rename(&chat_sessions_path, &backup_path) {
            println!("{} Failed to create backup: {}", "[!]".yellow(), e);
        } else {
            println!("{} Created backup: {}", "[OK]".green(), backup_name);
        }
    }

    // Restore chat sessions
    let output = Command::new("git")
        .current_dir(project_dir)
        .args(["checkout", commit, "--", ".vscode/chat-sessions"])
        .output()?;

    if !output.status.success() {
        anyhow::bail!(
            "Failed to restore chat sessions: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    println!("{} Restored chat sessions from {}", "[OK]".green(), commit);

    // Restore associated files if requested
    if with_files {
        println!(
            "\n{} This will restore ALL files from commit {}!",
            "[!]".yellow().bold(),
            commit
        );
        println!(
            "{} Are you sure? Use git checkout directly for selective restore.",
            "[i]".cyan()
        );
        // We don't actually restore all files - too dangerous
        // Instead, show what would be restored

        let output = Command::new("git")
            .current_dir(project_dir)
            .args(["diff", "--name-only", commit])
            .output()?;

        let files = String::from_utf8_lossy(&output.stdout);
        let file_count = files.lines().count();

        if file_count > 0 {
            println!("\n{} Files that differ from {}:", "[*]".cyan(), commit);
            for line in files.lines().take(10) {
                println!("   {}", line);
            }
            if file_count > 10 {
                println!("   ... and {} more", file_count - 10);
            }
            println!(
                "\n{} To restore all: git checkout {} -- .",
                "[i]".cyan(),
                commit
            );
        }
    }

    Ok(())
}