gn-cli 0.1.11

git-notes CLI — add, list, show, sync, resolve notes
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
use anyhow::{Context, Result};
use clap::Args;
use gn_core::{Namespace, Note, NotesEngine};
use std::process::Command;

#[derive(Args, Debug)]
pub struct RebaseHealArgs {
    /// Namespace to check notes from (default: all standard namespaces)
    #[arg(short, long)]
    pub namespace: Option<String>,

    /// Dry run: detect and report re-anchoring candidates without applying changes
    #[arg(long)]
    pub dry_run: bool,

    /// Verbose logging of commit matching heuristics
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Debug, Clone)]
struct CommitMetadata {
    sha: String,
    subject: String,
    patch_id: Option<String>,
    changed_files: Vec<String>,
}

/// Execute git command and return trimmed stdout if successful
fn git_output(repo_path: &std::path::Path, args: &[&str]) -> Option<String> {
    let output = Command::new("git")
        .args(args)
        .current_dir(repo_path)
        .output()
        .ok()?;
    if output.status.success() {
        Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
    } else {
        None
    }
}

/// Check if a commit is an ancestor of HEAD
fn is_ancestor_of_head(repo_path: &std::path::Path, commit: &str) -> bool {
    let status = Command::new("git")
        .args(["merge-base", "--is-ancestor", commit, "HEAD"])
        .current_dir(repo_path)
        .status();
    match status {
        Ok(s) => s.success(),
        Err(_) => false,
    }
}

/// Verify if a commit object exists in git repo
fn verify_commit_exists(repo_path: &std::path::Path, commit: &str) -> bool {
    let arg = format!("{}^{{commit}}", commit);
    let status = Command::new("git")
        .args(["rev-parse", "--verify", "-q", &arg])
        .current_dir(repo_path)
        .status();
    match status {
        Ok(s) => s.success(),
        Err(_) => false,
    }
}

/// Get git patch-id for a given commit
fn get_patch_id(repo_path: &std::path::Path, commit: &str) -> Option<String> {
    use std::io::Write;
    use std::process::Stdio;

    let diff_output = Command::new("git")
        .args(["diff-tree", "-p", "--root", commit])
        .current_dir(repo_path)
        .output()
        .ok()?;

    if !diff_output.status.success() || diff_output.stdout.is_empty() {
        return None;
    }

    let mut patch_id_cmd = Command::new("git")
        .args(["patch-id", "--stable"])
        .current_dir(repo_path)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .ok()?;

    if let Some(mut stdin) = patch_id_cmd.stdin.take() {
        let _ = stdin.write_all(&diff_output.stdout);
    }

    let output = patch_id_cmd.wait_with_output().ok()?;
    if output.status.success() {
        let out_str = String::from_utf8_lossy(&output.stdout);
        // git patch-id prints: "<patch_id> <commit_sha>"
        let parts: Vec<&str> = out_str.split_whitespace().collect();
        if !parts.is_empty() {
            return Some(parts[0].to_string());
        }
    }
    None
}

/// Retrieve the commit subject for a given commit SHA
fn get_commit_subject(repo_path: &std::path::Path, commit: &str) -> Option<String> {
    git_output(repo_path, &["log", "-1", "--format=%s", commit])
}

/// Retrieve changed files for a commit
fn get_commit_files(repo_path: &std::path::Path, commit: &str) -> Vec<String> {
    if let Some(out) = git_output(repo_path, &["diff-tree", "--no-commit-id", "--name-only", "-r", "--root", commit]) {
        out.lines()
            .map(|l| l.trim().to_string())
            .filter(|l| !l.is_empty())
            .collect()
    } else {
        Vec::new()
    }
}

/// Load recent commits on current HEAD branch (up to 100)
fn load_head_history(repo_path: &std::path::Path) -> Vec<CommitMetadata> {
    let log_out = match git_output(repo_path, &["log", "-n", "100", "--format=%H%x09%s"]) {
        Some(o) => o,
        None => return Vec::new(),
    };

    let mut commits = Vec::new();
    for line in log_out.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        let parts: Vec<&str> = line.splitn(2, '\t').collect();
        if parts.is_empty() {
            continue;
        }
        let sha = parts[0].to_string();
        let subject = if parts.len() > 1 {
            parts[1].to_string()
        } else {
            String::new()
        };
        commits.push(CommitMetadata {
            sha,
            subject,
            patch_id: None,
            changed_files: Vec::new(),
        });
    }
    commits
}

/// Find the new commit corresponding to an old commit SHA
fn find_new_commit(
    repo_path: &std::path::Path,
    old_sha: &str,
    note: &Note,
    head_commits: &mut [CommitMetadata],
    verbose: bool,
) -> Option<String> {
    // 1. Try patch-id matching if old commit exists in repo objects
    if verify_commit_exists(repo_path, old_sha) {
        if let Some(old_patch_id) = get_patch_id(repo_path, old_sha) {
            if verbose {
                println!(
                    "  [heuristic:patch-id] old commit {} has patch-id {}",
                    &old_sha[..old_sha.len().min(8)],
                    &old_patch_id[..old_patch_id.len().min(8)]
                );
            }
            for candidate in head_commits.iter_mut() {
                if candidate.patch_id.is_none() {
                    candidate.patch_id = get_patch_id(repo_path, &candidate.sha);
                }
                if let Some(ref cand_patch_id) = candidate.patch_id {
                    if cand_patch_id == &old_patch_id {
                        if verbose {
                            println!(
                                "  [heuristic:patch-id] match found: candidate commit {}",
                                &candidate.sha[..candidate.sha.len().min(8)]
                            );
                        }
                        return Some(candidate.sha.clone());
                    }
                }
            }
        }
    }

    // 2. Try commit subject match if available
    let old_subject = get_commit_subject(repo_path, old_sha);
    if let Some(ref subj) = old_subject {
        let trimmed_subj = subj.trim();
        if !trimmed_subj.is_empty() {
            // Check exact subject match
            let matches: Vec<&CommitMetadata> = head_commits
                .iter()
                .filter(|c| c.subject.trim() == trimmed_subj)
                .collect();

            if matches.len() == 1 {
                if verbose {
                    println!(
                        "  [heuristic:subject] unique subject match: '{}' -> {}",
                        trimmed_subj,
                        &matches[0].sha[..matches[0].sha.len().min(8)]
                    );
                }
                return Some(matches[0].sha.clone());
            } else if matches.len() > 1 {
                // If multiple commits have the same subject, disambiguate using note.file
                if let Some(ref note_file) = note.file {
                    for m in &matches {
                        let files = get_commit_files(repo_path, &m.sha);
                        if files.iter().any(|f| f == note_file) {
                            if verbose {
                                println!(
                                    "  [heuristic:subject+file] disambiguated '{}' with file '{}' -> {}",
                                    trimmed_subj,
                                    note_file,
                                    &m.sha[..m.sha.len().min(8)]
                                );
                            }
                            return Some(m.sha.clone());
                        }
                    }
                }
            }
        }
    }

    // 3. Fallback: if note targets a specific file, check commits touching that file
    if let Some(ref note_file) = note.file {
        for candidate in head_commits.iter_mut() {
            if candidate.changed_files.is_empty() {
                candidate.changed_files = get_commit_files(repo_path, &candidate.sha);
            }
            if candidate.changed_files.iter().any(|f| f == note_file) {
                // If old subject was non-empty and starts with candidate subject or vice-versa
                if let Some(ref subj) = old_subject {
                    if !subj.is_empty()
                        && (subj.starts_with(&candidate.subject)
                            || candidate.subject.starts_with(subj))
                    {
                        if verbose {
                            println!(
                                "  [heuristic:file+partial_subject] matched file '{}' and subject prefix -> {}",
                                note_file,
                                &candidate.sha[..candidate.sha.len().min(8)]
                            );
                        }
                        return Some(candidate.sha.clone());
                    }
                }
            }
        }
    }

    None
}

pub fn run(args: &RebaseHealArgs) -> Result<()> {
    run_in_repo(std::path::Path::new("."), args)
}

pub fn run_in_repo(repo_path: &std::path::Path, args: &RebaseHealArgs) -> Result<()> {
    let engine = NotesEngine::new(repo_path);

    let namespaces = match &args.namespace {
        Some(ns) => vec![Namespace::from_str(ns)],
        None => vec![
            Namespace::Comments,
            Namespace::Review,
            Namespace::Todos,
        ],
    };

    let mut head_commits = load_head_history(repo_path);

    let mut healed_count = 0;
    let mut valid_count = 0;
    let mut unresolved_count = 0;

    for ns in &namespaces {
        let notes = match engine.read_notes(ns) {
            Ok(n) => n,
            Err(_) => continue,
        };

        for mut note in notes {
            let old_sha = &note.commit;
            let note_short_id = &note.id.to_string()[..8];
            let location = match (&note.file, note.line_start) {
                (Some(f), Some(l)) => format!("{}:{}", f, l),
                (Some(f), None) => f.clone(),
                (None, _) => "<no file>".to_string(),
            };

            // Check if note's commit is already on the current branch (HEAD history)
            if verify_commit_exists(repo_path, old_sha) && is_ancestor_of_head(repo_path, old_sha) {
                valid_count += 1;
                if args.verbose {
                    println!(
                        "Note {} anchor {} is already valid on HEAD.",
                        note_short_id,
                        &old_sha[..old_sha.len().min(8)]
                    );
                }
                continue;
            }

            // Note is anchored to an unreachable or rewritten commit
            if args.verbose {
                println!(
                    "Note {} anchor {} is not on HEAD. Attempting to heal...",
                    note_short_id,
                    &old_sha[..old_sha.len().min(8)]
                );
            }

            let found_commit = find_new_commit(repo_path, old_sha, &note, &mut head_commits, args.verbose);

            match found_commit {
                Some(new_sha) => {
                    let old_short = &old_sha[..old_sha.len().min(8)];
                    let new_short = &new_sha[..new_sha.len().min(8)];

                    if args.dry_run {
                        println!(
                            "[dry-run] Would re-anchor note {} from {} to {} ({})",
                            note_short_id, old_short, new_short, location
                        );
                    } else {
                        note.commit = new_sha.clone();
                        engine
                            .write_note(&note)
                            .with_context(|| format!("Failed to update note {}", note.id))?;
                        println!(
                            "✓ Re-anchored note {} to {} (file: {})",
                            note_short_id,
                            new_short,
                            note.file.as_deref().unwrap_or("<no file>")
                        );
                    }
                    healed_count += 1;
                }
                None => {
                    unresolved_count += 1;
                    if args.verbose {
                        println!(
                            "✗ Could not resolve new commit for note {} (old anchor: {})",
                            note_short_id,
                            &old_sha[..old_sha.len().min(8)]
                        );
                    }
                }
            }
        }
    }

    println!(
        "✨ Rebase healing complete: {} healed, {} already valid, {} unresolved.",
        healed_count, valid_count, unresolved_count
    );

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use tempfile::TempDir;

    fn run_git(dir: &std::path::Path, args: &[&str]) -> String {
        let out = Command::new("git")
            .args(args)
            .current_dir(dir)
            .output()
            .expect("git cmd failed");
        String::from_utf8_lossy(&out.stdout).trim().to_string()
    }

    #[test]
    fn test_rebase_heal_amended_commit() {
        let temp_dir = TempDir::new().unwrap();
        let repo_path = temp_dir.path();

        run_git(repo_path, &["init"]);
        run_git(repo_path, &["config", "user.email", "test@test.com"]);
        run_git(repo_path, &["config", "user.name", "Test User"]);

        // Commit 1: base
        let file_path = repo_path.join("file.txt");
        let mut f = File::create(&file_path).unwrap();
        writeln!(f, "Initial content").unwrap();
        run_git(repo_path, &["add", "file.txt"]);
        run_git(repo_path, &["commit", "-m", "Initial commit"]);

        // Commit 2: feature
        let mut f = File::create(&file_path).unwrap();
        writeln!(f, "Feature content line 1\nFeature content line 2").unwrap();
        run_git(repo_path, &["add", "file.txt"]);
        run_git(repo_path, &["commit", "-m", "Add feature"]);
        let old_feature_sha = run_git(repo_path, &["rev-parse", "HEAD"]);

        // Create a note anchored to Commit 2
        let engine = NotesEngine::new(repo_path);
        let note = Note::new(
            old_feature_sha.clone(),
            Some("file.txt".to_string()),
            Some(1),
            Some(2),
            "Check this feature".to_string(),
            "Reviewer <rev@test.com>".to_string(),
            Namespace::Comments,
        );
        engine.write_note(&note).unwrap();

        // Now amend Commit 2 (changes SHA)
        let mut f = File::create(&file_path).unwrap();
        writeln!(f, "Feature content line 1\nFeature content line 2 modified").unwrap();
        run_git(repo_path, &["add", "file.txt"]);
        run_git(repo_path, &["commit", "--amend", "-m", "Add feature"]);
        let new_feature_sha = run_git(repo_path, &["rev-parse", "HEAD"]);

        assert_ne!(old_feature_sha, new_feature_sha);

        // Run rebase heal on test repo
        let args = RebaseHealArgs {
            namespace: Some("comments".to_string()),
            dry_run: false,
            verbose: true,
        };

        let res = run_in_repo(repo_path, &args);
        assert!(res.is_ok());

        // Verify note was re-anchored to new_feature_sha
        let notes = engine.read_notes(&Namespace::Comments).unwrap();
        assert_eq!(notes.len(), 1);
        assert_eq!(notes[0].commit, new_feature_sha);
    }

    #[test]
    fn test_rebase_heal_dry_run() {
        let temp_dir = TempDir::new().unwrap();
        let repo_path = temp_dir.path();

        run_git(repo_path, &["init"]);
        run_git(repo_path, &["config", "user.email", "test@test.com"]);
        run_git(repo_path, &["config", "user.name", "Test User"]);

        let file_path = repo_path.join("file.txt");
        let mut f = File::create(&file_path).unwrap();
        writeln!(f, "Initial").unwrap();
        run_git(repo_path, &["add", "file.txt"]);
        run_git(repo_path, &["commit", "-m", "Initial commit"]);

        let mut f = File::create(&file_path).unwrap();
        writeln!(f, "Feature 1").unwrap();
        run_git(repo_path, &["add", "file.txt"]);
        run_git(repo_path, &["commit", "-m", "Feature 1"]);
        let old_feature_sha = run_git(repo_path, &["rev-parse", "HEAD"]);

        let engine = NotesEngine::new(repo_path);
        let note = Note::new(
            old_feature_sha.clone(),
            Some("file.txt".to_string()),
            Some(1),
            Some(1),
            "Needs check".to_string(),
            "Reviewer <rev@test.com>".to_string(),
            Namespace::Comments,
        );
        engine.write_note(&note).unwrap();

        // Amend commit
        let mut f = File::create(&file_path).unwrap();
        writeln!(f, "Feature 1 amended").unwrap();
        run_git(repo_path, &["add", "file.txt"]);
        run_git(repo_path, &["commit", "--amend", "-m", "Feature 1"]);

        let dry_args = RebaseHealArgs {
            namespace: Some("comments".to_string()),
            dry_run: true,
            verbose: false,
        };

        let res = run_in_repo(repo_path, &dry_args);
        assert!(res.is_ok());

        // In dry run, note should still have old commit SHA
        let notes = engine.read_notes(&Namespace::Comments).unwrap();
        assert_eq!(notes.len(), 1);
        assert_eq!(notes[0].commit, old_feature_sha);
    }
}