whogitit 0.2.0

Track AI-generated code at line-level granularity
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
use std::env;
use std::path::Path;

use anyhow::{Context, Result};
use git2::Repository;
use serde::{Deserialize, Serialize};

use crate::capture::pending::{PendingBuffer, PendingStore};
use crate::capture::threeway::ThreeWayAnalyzer;
use crate::core::attribution::{AIAttribution, PromptInfo, SessionMetadata};
use crate::privacy::{Redactor, WhogititConfig};
use crate::storage::notes::NotesStore;

/// Environment variable for session ID
const ENV_SESSION_ID: &str = "WHOGITIT_SESSION_ID";
/// Environment variable for model ID
const ENV_MODEL_ID: &str = "WHOGITIT_MODEL_ID";
/// Default model if not specified
const DEFAULT_MODEL: &str = "claude-opus-4-5-20251101";

/// Context from Claude Code transcript
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct HookContext {
    /// Whether the edit was made in plan mode
    #[serde(default)]
    pub plan_mode: bool,
    /// Whether this is from a subagent
    #[serde(default)]
    pub is_subagent: bool,
    /// Agent nesting depth (0=main, 1+=subagent)
    #[serde(default)]
    pub agent_depth: u8,
    /// Subagent ID if applicable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subagent_id: Option<String>,
}

/// Input from Claude Code hook for file changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HookInput {
    /// The tool being called (Edit, Write)
    pub tool: String,
    /// File path being modified
    pub file_path: String,
    /// The current user prompt/context
    pub prompt: String,
    /// Old file content (None for new files)
    pub old_content: Option<String>,
    /// New file content
    pub new_content: String,
    /// Context from transcript (plan mode, subagent, etc.)
    #[serde(default)]
    pub context: Option<HookContext>,
}

/// Claude Code hook handler
pub struct CaptureHook {
    /// Repository root path
    repo_root: std::path::PathBuf,
    /// Privacy redactor
    redactor: Redactor,
    /// Whether audit logging is enabled
    audit_enabled: bool,
}

impl CaptureHook {
    /// Create a new capture hook for a repository
    pub fn new(repo_path: &Path) -> Result<Self> {
        let repo_root = repo_path.to_path_buf();

        // Load config and build redactor
        let config = WhogititConfig::load(&repo_root).unwrap_or_default();
        let redactor = config.privacy.build_redactor();
        let audit_enabled = config.privacy.audit_log;

        Ok(Self {
            repo_root,
            redactor,
            audit_enabled,
        })
    }

    /// Get or create session ID
    fn get_session_id() -> String {
        env::var(ENV_SESSION_ID).unwrap_or_else(|_| uuid::Uuid::new_v4().to_string())
    }

    /// Get model ID from environment
    fn get_model_id() -> String {
        env::var(ENV_MODEL_ID).unwrap_or_else(|_| DEFAULT_MODEL.to_string())
    }

    /// Handle a file change from Claude Code
    pub fn on_file_change(&self, input: HookInput) -> Result<()> {
        let store = PendingStore::new(&self.repo_root);

        // Load or create pending buffer
        let mut buffer = match store.load()? {
            Some(b) => {
                // Check if we should start a new session
                // (different session ID in env means new session)
                let current_session = Self::get_session_id();
                if b.session.session_id != current_session && env::var(ENV_SESSION_ID).is_ok() {
                    // New session ID explicitly set, start fresh
                    // But first, warn about uncommitted changes
                    if b.has_changes() {
                        eprintln!(
                            "whogitit: Warning - discarding {} uncommitted edits from previous session",
                            b.total_edits()
                        );
                    }
                    PendingBuffer::new(&current_session, &Self::get_model_id())
                } else {
                    b
                }
            }
            None => {
                let mut buffer = PendingBuffer::new(&Self::get_session_id(), &Self::get_model_id());
                buffer.audit_logging_enabled = self.audit_enabled;
                buffer
            }
        };

        // Make path relative to repo root
        let relative_path = self.make_relative_path(&input.file_path)?;

        // Validate input
        if relative_path.is_empty() {
            anyhow::bail!("Empty file path");
        }

        // Check for path traversal attempts
        if relative_path.contains("..") {
            anyhow::bail!(
                "Path traversal detected in file path: '{}'. Paths containing '..' are not allowed.",
                relative_path
            );
        }

        // Check for absolute paths that escaped normalization
        if relative_path.starts_with('/') {
            anyhow::bail!(
                "Absolute path not allowed: '{}'. Paths must be relative to repository root.",
                relative_path
            );
        }

        if input.new_content.is_empty() && input.tool != "Delete" {
            eprintln!("whogitit: Warning - empty new_content for non-delete operation");
        }

        // Determine old content: use provided value, or fall back to git HEAD
        let old_content = match input.old_content {
            Some(content) => Some(content),
            None => {
                // Try to get content from git HEAD for existing files
                self.get_content_from_git_head(&relative_path)
            }
        };

        // Build edit context from hook input
        let edit_context =
            input
                .context
                .as_ref()
                .map(|ctx| crate::capture::snapshot::EditContext {
                    plan_mode: ctx.plan_mode,
                    subagent_id: ctx.subagent_id.clone(),
                    agent_depth: ctx.agent_depth,
                    plan_step: None,
                });

        // Record the edit with full content snapshots
        buffer.record_edit_with_context(
            &relative_path,
            old_content.as_deref(),
            &input.new_content,
            &input.tool,
            &input.prompt,
            Some(&self.redactor),
            edit_context,
        );

        // Save buffer with atomic write
        store.save(&buffer)?;

        Ok(())
    }

    /// Get file content from git HEAD (the last committed version)
    ///
    /// Returns None for new files or if git operations fail.
    /// Logs warnings for unexpected failures to aid debugging.
    fn get_content_from_git_head(&self, path: &str) -> Option<String> {
        let repo = match Repository::open(&self.repo_root) {
            Ok(r) => r,
            Err(e) => {
                eprintln!(
                    "whogitit: Warning - failed to open repository at '{}': {}",
                    self.repo_root.display(),
                    e
                );
                return None;
            }
        };

        let head = match repo.head() {
            Ok(h) => h,
            Err(e) => {
                // HEAD not existing is normal for new repos with no commits
                if e.code() != git2::ErrorCode::UnbornBranch {
                    eprintln!("whogitit: Warning - failed to get HEAD: {}", e);
                }
                return None;
            }
        };

        let commit = match head.peel_to_commit() {
            Ok(c) => c,
            Err(e) => {
                eprintln!("whogitit: Warning - failed to peel HEAD to commit: {}", e);
                return None;
            }
        };

        let tree = match commit.tree() {
            Ok(t) => t,
            Err(e) => {
                eprintln!("whogitit: Warning - failed to get commit tree: {}", e);
                return None;
            }
        };

        // File not existing in HEAD is normal for new files - don't warn
        let entry = match tree.get_path(std::path::Path::new(path)) {
            Ok(e) => e,
            Err(_) => return None, // New file, not in HEAD
        };

        let blob = match repo.find_blob(entry.id()) {
            Ok(b) => b,
            Err(e) => {
                eprintln!(
                    "whogitit: Warning - failed to read blob for '{}': {}",
                    path, e
                );
                return None;
            }
        };

        // Non-UTF8 content is valid for binary files - don't treat as error
        match std::str::from_utf8(blob.content()) {
            Ok(content) => Some(content.to_string()),
            Err(_) => None, // Binary file
        }
    }

    /// Handle post-commit: perform three-way analysis, attach notes, and clean up
    pub fn on_post_commit(&self) -> Result<Option<AIAttribution>> {
        let store = PendingStore::new(&self.repo_root);

        // Load pending buffer
        let buffer = match store.load()? {
            Some(b) if b.has_changes() => b,
            _ => return Ok(None),
        };

        // Open repo and get HEAD commit
        let repo = Repository::open(&self.repo_root).context("Failed to open repository")?;
        let head = repo
            .head()
            .context("Failed to get HEAD")?
            .peel_to_commit()
            .context("Failed to get HEAD commit")?;

        // Analyze each file with three-way diff against committed content
        let mut file_results = Vec::new();
        let tree = head.tree()?;

        for (path, history) in &buffer.file_histories {
            // Get the committed content for this file
            let committed_content = match tree.get_path(std::path::Path::new(path)) {
                Ok(entry) => {
                    let blob = repo.find_blob(entry.id())?;
                    String::from_utf8_lossy(blob.content()).to_string()
                }
                Err(_) => {
                    // File might have been deleted or not staged
                    continue;
                }
            };

            // Perform three-way analysis
            let result = ThreeWayAnalyzer::analyze_with_diff(history, &committed_content);
            file_results.push(result);
        }

        // Compute extended session metadata
        let used_plan_mode = buffer
            .file_histories
            .values()
            .flat_map(|h| h.edits.iter())
            .any(|e| e.context.plan_mode);

        let subagent_count = buffer
            .file_histories
            .values()
            .flat_map(|h| h.edits.iter())
            .filter(|e| e.context.agent_depth > 0)
            .count() as u32;

        // Create attribution with full analysis
        let attribution = AIAttribution {
            version: 3,
            session: SessionMetadata {
                session_id: buffer.session.session_id.clone(),
                model: buffer.session.model.clone(),
                started_at: buffer.session.started_at.clone(),
                prompt_count: buffer.session.prompt_count,
                used_plan_mode,
                subagent_count,
            },
            prompts: buffer
                .session
                .prompts
                .iter()
                .map(|p| PromptInfo {
                    index: p.index,
                    text: p.text.clone(),
                    timestamp: p.timestamp.clone(),
                    affected_files: p.affected_files.clone(),
                })
                .collect(),
            files: file_results,
        };

        // Store as git note
        let notes_store = NotesStore::new(&repo)?;
        notes_store.store_attribution(head.id(), &attribution)?;

        // Clean up pending file
        store.delete()?;

        // Log summary
        let total_ai = attribution
            .files
            .iter()
            .map(|f| f.summary.ai_lines + f.summary.ai_modified_lines)
            .sum::<usize>();
        let total_human = attribution
            .files
            .iter()
            .map(|f| f.summary.human_lines)
            .sum::<usize>();

        eprintln!(
            "whogitit: Attached attribution - {} AI lines, {} human lines across {} files",
            total_ai,
            total_human,
            attribution.files.len()
        );

        Ok(Some(attribution))
    }

    /// Make a path relative to the repo root
    fn make_relative_path(&self, path: &str) -> Result<String> {
        let abs_path = if Path::new(path).is_absolute() {
            Path::new(path).to_path_buf()
        } else {
            self.repo_root.join(path)
        };

        let relative = abs_path
            .strip_prefix(&self.repo_root)
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|_| path.to_string());

        Ok(relative)
    }

    /// Get current pending status
    pub fn status(&self) -> Result<PendingStatus> {
        let store = PendingStore::new(&self.repo_root);

        // Use quiet load to avoid spurious warnings during status check
        match store.load_quiet()? {
            Some(buffer) => {
                let session_id = buffer.session.session_id.clone();
                let file_count = buffer.file_count();
                let line_count = buffer.total_lines();
                let edit_count = buffer.total_edits();
                let prompt_count = buffer.session.prompt_count;
                let has_pending = buffer.has_changes();
                let is_stale = buffer.is_stale();
                let age = buffer.age_string();
                Ok(PendingStatus {
                    has_pending,
                    session_id: Some(session_id),
                    file_count,
                    line_count,
                    edit_count,
                    prompt_count,
                    is_stale,
                    age,
                })
            }
            None => Ok(PendingStatus {
                has_pending: false,
                session_id: None,
                file_count: 0,
                line_count: 0,
                edit_count: 0,
                prompt_count: 0,
                is_stale: false,
                age: String::new(),
            }),
        }
    }

    /// Clear pending changes without committing
    pub fn clear_pending(&self) -> Result<()> {
        let store = PendingStore::new(&self.repo_root);
        store.delete()
    }
}

/// Status of pending changes
#[derive(Debug)]
pub struct PendingStatus {
    pub has_pending: bool,
    pub session_id: Option<String>,
    pub file_count: usize,
    pub line_count: u32,
    pub edit_count: usize,
    pub prompt_count: u32,
    /// Whether the pending buffer is stale (older than 24 hours)
    pub is_stale: bool,
    /// Human-readable age of the pending buffer
    pub age: String,
}

/// Hook entry point for Claude Code integration
pub fn run_capture_hook() -> Result<()> {
    // Read input from stdin
    let input: HookInput = serde_json::from_reader(std::io::stdin())
        .context("Failed to read hook input from stdin")?;

    // Find repo root
    let repo_root = find_repo_root()?;

    // Only capture in repos that have been initialized with `whogitit init`
    if !is_repo_initialized(&repo_root) {
        return Ok(());
    }

    // Process the change
    let hook = CaptureHook::new(&repo_root)?;
    hook.on_file_change(input)?;

    Ok(())
}

/// Find the git repository root from current directory
fn find_repo_root() -> Result<std::path::PathBuf> {
    let current = env::current_dir()?;
    let repo = Repository::discover(&current).context("Not in a git repository")?;

    repo.workdir()
        .map(|p| p.to_path_buf())
        .context("Repository has no working directory")
}

/// Check if the repository has been initialized with `whogitit init`
/// by looking for the whogitit marker in the post-commit hook
fn is_repo_initialized(repo_root: &std::path::Path) -> bool {
    let post_commit = repo_root.join(".git/hooks/post-commit");
    if let Ok(content) = std::fs::read_to_string(&post_commit) {
        content.contains("whogitit")
    } else {
        false
    }
}

/// Git post-commit hook entry point
pub fn run_post_commit_hook() -> Result<()> {
    let repo_root = find_repo_root()?;
    let hook = CaptureHook::new(&repo_root)?;

    hook.on_post_commit()?;

    Ok(())
}

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

    fn create_test_repo() -> (TempDir, Repository) {
        let dir = TempDir::new().unwrap();
        let repo = Repository::init(dir.path()).unwrap();

        // Create initial commit
        {
            let sig = Signature::now("Test", "test@test.com").unwrap();
            let tree_id = repo.index().unwrap().write_tree().unwrap();
            let tree = repo.find_tree(tree_id).unwrap();
            repo.commit(Some("HEAD"), &sig, &sig, "Initial", &tree, &[])
                .unwrap();
        }

        (dir, repo)
    }

    #[test]
    fn test_capture_hook_on_file_change() {
        let (dir, _repo) = create_test_repo();
        let hook = CaptureHook::new(dir.path()).unwrap();

        let input = HookInput {
            tool: "Write".to_string(),
            file_path: "test.rs".to_string(),
            prompt: "Create a test file".to_string(),
            old_content: None,
            new_content: "fn test() {}\n".to_string(),
            context: None,
        };

        hook.on_file_change(input).unwrap();

        let status = hook.status().unwrap();
        assert!(status.has_pending);
        assert_eq!(status.file_count, 1);
        assert_eq!(status.edit_count, 1);
        assert_eq!(status.prompt_count, 1);
    }

    #[test]
    fn test_capture_hook_multiple_edits() {
        let (dir, _repo) = create_test_repo();
        let hook = CaptureHook::new(dir.path()).unwrap();

        // First edit
        hook.on_file_change(HookInput {
            tool: "Write".to_string(),
            file_path: "test.rs".to_string(),
            prompt: "Create file".to_string(),
            old_content: None,
            new_content: "line1\n".to_string(),
            context: None,
        })
        .unwrap();

        // Second edit to same file
        hook.on_file_change(HookInput {
            tool: "Edit".to_string(),
            file_path: "test.rs".to_string(),
            prompt: "Add line".to_string(),
            old_content: Some("line1\n".to_string()),
            new_content: "line1\nline2\n".to_string(),
            context: None,
        })
        .unwrap();

        let status = hook.status().unwrap();
        assert_eq!(status.file_count, 1);
        assert_eq!(status.edit_count, 2);
        assert_eq!(status.prompt_count, 2);
    }

    #[test]
    fn test_capture_hook_status_empty() {
        let (dir, _repo) = create_test_repo();
        let hook = CaptureHook::new(dir.path()).unwrap();

        let status = hook.status().unwrap();
        assert!(!status.has_pending);
        assert_eq!(status.file_count, 0);
    }

    #[test]
    fn test_capture_hook_clear() {
        let (dir, _repo) = create_test_repo();
        let hook = CaptureHook::new(dir.path()).unwrap();

        // Add a change
        hook.on_file_change(HookInput {
            tool: "Write".to_string(),
            file_path: "test.rs".to_string(),
            prompt: "test".to_string(),
            old_content: None,
            new_content: "content\n".to_string(),
            context: None,
        })
        .unwrap();

        assert!(hook.status().unwrap().has_pending);

        // Clear
        hook.clear_pending().unwrap();
        assert!(!hook.status().unwrap().has_pending);
    }

    #[test]
    fn test_is_repo_initialized() {
        let dir = TempDir::new().unwrap();
        let hooks_dir = dir.path().join(".git/hooks");
        std::fs::create_dir_all(&hooks_dir).unwrap();

        // Not initialized - no hook file
        assert!(!is_repo_initialized(dir.path()));

        // Not initialized - hook exists but no whogitit marker
        std::fs::write(hooks_dir.join("post-commit"), "#!/bin/bash\necho hello").unwrap();
        assert!(!is_repo_initialized(dir.path()));

        // Initialized - hook contains whogitit
        std::fs::write(
            hooks_dir.join("post-commit"),
            "#!/bin/bash\nwhogitit commit",
        )
        .unwrap();
        assert!(is_repo_initialized(dir.path()));
    }
}