git-iris 2.0.8

AI-powered Git workflow assistant for smart commits, code reviews, changelogs, and release 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
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
//! Git operations tools for Rig-based agents
//!
//! This module provides Git operations using Rig's tool system.

use anyhow::Result;
use rig::completion::ToolDefinition;
use rig::tool::Tool;
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;

use crate::context::{ChangeType, RecentCommit};
use crate::define_tool_error;
use crate::git::StagedFile;

use super::common::{get_current_repo, parameters_schema};

define_tool_error!(GitError);

/// Helper to add a change type if not already present
fn add_change(changes: &mut Vec<&'static str>, change: &'static str) {
    if !changes.contains(&change) {
        changes.push(change);
    }
}

/// Check for function definitions in a line based on language
fn is_function_def(line: &str, ext: &str) -> bool {
    match ext {
        "rs" => {
            line.starts_with("pub fn ")
                || line.starts_with("fn ")
                || line.starts_with("pub async fn ")
                || line.starts_with("async fn ")
        }
        "ts" | "tsx" | "js" | "jsx" => {
            line.starts_with("function ")
                || line.starts_with("async function ")
                || line.contains(" = () =>")
                || line.contains(" = async () =>")
        }
        "py" => line.starts_with("def ") || line.starts_with("async def "),
        "go" => line.starts_with("func "),
        _ => false,
    }
}

/// Check for import statements based on language
fn is_import(line: &str, ext: &str) -> bool {
    match ext {
        "rs" => line.starts_with("use ") || line.starts_with("pub use "),
        "ts" | "tsx" | "js" | "jsx" => line.starts_with("import ") || line.starts_with("export "),
        "py" => line.starts_with("import ") || line.starts_with("from "),
        "go" => line.starts_with("import "),
        _ => false,
    }
}

/// Check for type definitions based on language
fn is_type_def(line: &str, ext: &str) -> bool {
    match ext {
        "rs" => {
            line.starts_with("pub struct ")
                || line.starts_with("struct ")
                || line.starts_with("pub enum ")
                || line.starts_with("enum ")
        }
        "ts" | "tsx" | "js" | "jsx" => {
            line.starts_with("interface ")
                || line.starts_with("type ")
                || line.starts_with("class ")
        }
        "py" => line.starts_with("class "),
        "go" => line.starts_with("type "),
        _ => false,
    }
}

/// Detect semantic change types from diff content
#[allow(clippy::cognitive_complexity)]
fn detect_semantic_changes(diff: &str, path: &str) -> Vec<&'static str> {
    use std::path::Path;

    let mut changes = Vec::new();

    // Get file extension
    let ext = Path::new(path)
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_lowercase)
        .unwrap_or_default();

    // Only analyze supported languages
    let supported = matches!(
        ext.as_str(),
        "rs" | "ts" | "tsx" | "js" | "jsx" | "py" | "go"
    );

    if supported {
        // Analyze added lines for patterns
        for line in diff
            .lines()
            .filter(|l| l.starts_with('+') && !l.starts_with("+++"))
        {
            let line = line.trim_start_matches('+').trim();

            if is_function_def(line, &ext) {
                add_change(&mut changes, "adds function");
            }
            if is_import(line, &ext) {
                add_change(&mut changes, "modifies imports");
            }
            if is_type_def(line, &ext) {
                add_change(&mut changes, "adds type");
            }
            // Rust-specific: impl blocks
            if ext == "rs" && line.starts_with("impl ") {
                add_change(&mut changes, "adds impl");
            }
        }
    }

    // Check for general change patterns
    let has_deletions = diff
        .lines()
        .any(|l| l.starts_with('-') && !l.starts_with("---"));
    let has_additions = diff
        .lines()
        .any(|l| l.starts_with('+') && !l.starts_with("+++"));

    if has_deletions && has_additions && changes.is_empty() {
        changes.push("refactors code");
    } else if has_deletions && !has_additions {
        changes.push("removes code");
    }

    changes
}

/// Calculate relevance score for a file (0.0 - 1.0)
/// Higher score = more important for commit message
#[allow(clippy::case_sensitive_file_extension_comparisons)]
fn calculate_relevance_score(file: &StagedFile) -> (f32, Vec<&'static str>) {
    let mut score: f32 = 0.5; // Base score
    let mut reasons = Vec::new();
    let path = file.path.to_lowercase();

    // Change type scoring
    match file.change_type {
        ChangeType::Added => {
            score += 0.15;
            reasons.push("new file");
        }
        ChangeType::Modified => {
            score += 0.1;
        }
        ChangeType::Deleted => {
            score += 0.05;
            reasons.push("deleted");
        }
    }

    // File type scoring - source code is most important
    if path.ends_with(".rs")
        || path.ends_with(".py")
        || path.ends_with(".ts")
        || path.ends_with(".tsx")
        || path.ends_with(".js")
        || path.ends_with(".jsx")
        || path.ends_with(".go")
        || path.ends_with(".java")
        || path.ends_with(".kt")
        || path.ends_with(".swift")
        || path.ends_with(".c")
        || path.ends_with(".cpp")
        || path.ends_with(".h")
    {
        score += 0.15;
        reasons.push("source code");
    } else if path.ends_with(".toml")
        || path.ends_with(".json")
        || path.ends_with(".yaml")
        || path.ends_with(".yml")
    {
        score += 0.1;
        reasons.push("config");
    } else if path.ends_with(".md") || path.ends_with(".txt") || path.ends_with(".rst") {
        score += 0.02;
        reasons.push("docs");
    }

    // Path-based scoring
    if path.contains("/src/") || path.starts_with("src/") {
        score += 0.1;
        reasons.push("core source");
    }
    if path.contains("/test") || path.contains("_test.") || path.contains(".test.") {
        score -= 0.1;
        reasons.push("test file");
    }
    if path.contains("generated") || path.contains(".lock") || path.contains("package-lock") {
        score -= 0.2;
        reasons.push("generated/lock");
    }
    if path.contains("/vendor/") || path.contains("/node_modules/") {
        score -= 0.3;
        reasons.push("vendored");
    }

    // Diff size scoring (estimate from diff length)
    let diff_lines = file.diff.lines().count();
    if diff_lines > 10 && diff_lines < 200 {
        score += 0.1;
        reasons.push("substantive changes");
    } else if diff_lines >= 200 {
        score += 0.05;
        reasons.push("large diff");
    }

    // Add semantic change detection
    let semantic_changes = detect_semantic_changes(&file.diff, &file.path);
    for change in semantic_changes {
        if !reasons.contains(&change) {
            // Boost score for structural changes
            if change == "adds function" || change == "adds type" || change == "adds impl" {
                score += 0.1;
            }
            reasons.push(change);
        }
    }

    // Clamp to 0.0-1.0
    score = score.clamp(0.0, 1.0);

    (score, reasons)
}

/// Scored file for output
struct ScoredFile<'a> {
    file: &'a StagedFile,
    score: f32,
    reasons: Vec<&'static str>,
}

/// Build the diff output string from scored files
fn format_diff_output(
    scored_files: &[ScoredFile],
    total_files: usize,
    is_filtered: bool,
    include_diffs: bool,
) -> String {
    let mut output = String::new();
    let showing = scored_files.len();

    // Calculate stats
    let additions: usize = scored_files
        .iter()
        .map(|sf| sf.file.diff.lines().filter(|l| l.starts_with('+')).count())
        .sum();
    let deletions: usize = scored_files
        .iter()
        .map(|sf| sf.file.diff.lines().filter(|l| l.starts_with('-')).count())
        .sum();
    let total_lines = additions + deletions;

    // Categorize size
    let (size, guidance) = if is_filtered {
        ("Filtered", "Showing requested files only.")
    } else if total_files <= 3 && total_lines < 100 {
        ("Small", "Focus on all files equally.")
    } else if total_files <= 10 && total_lines < 500 {
        ("Medium", "Prioritize files with >60% relevance.")
    } else {
        (
            "Large",
            "Use files=['path1','path2'] with detail='standard' to analyze specific files.",
        )
    };

    // Header
    let files_info = if is_filtered {
        format!("{showing} of {total_files} files")
    } else {
        format!("{total_files} files")
    };
    output.push_str(&format!(
        "=== CHANGES SUMMARY ===\n{files_info} | +{additions} -{deletions} | Size: {size} ({total_lines} lines)\nGuidance: {guidance}\n\n"
    ));

    // File list
    output.push_str("Files by importance:\n");
    for sf in scored_files {
        let reasons = if sf.reasons.is_empty() {
            String::new()
        } else {
            format!(" ({})", sf.reasons.join(", "))
        };
        output.push_str(&format!(
            "  [{:.0}%] {:?} {}{reasons}\n",
            sf.score * 100.0,
            sf.file.change_type,
            sf.file.path
        ));
    }
    output.push('\n');

    // Diffs or hint
    if include_diffs {
        output.push_str("=== DIFFS ===\n");
        for sf in scored_files {
            output.push_str(&format!(
                "--- {} [{:.0}% relevance]\n",
                sf.file.path,
                sf.score * 100.0
            ));
            output.push_str(&sf.file.diff);
            output.push('\n');
        }
    } else if is_filtered {
        output.push_str("(Use detail='standard' to see full diffs for these files)\n");
    } else {
        output.push_str(
            "(Use detail='standard' with files=['file1','file2'] to see specific diffs)\n",
        );
    }

    output
}

// Git status tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitStatus;

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct GitStatusArgs {
    #[serde(default)]
    pub include_unstaged: bool,
}

impl Tool for GitStatus {
    const NAME: &'static str = "git_status";
    type Error = GitError;
    type Args = GitStatusArgs;
    type Output = String;

    async fn definition(&self, _: String) -> ToolDefinition {
        ToolDefinition {
            name: "git_status".to_string(),
            description: "Get current Git repository status including staged and unstaged files"
                .to_string(),
            parameters: parameters_schema::<GitStatusArgs>(),
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        let repo = get_current_repo().map_err(GitError::from)?;

        let files_info = repo
            .extract_files_info(args.include_unstaged)
            .map_err(GitError::from)?;

        let mut output = String::new();
        output.push_str(&format!("Branch: {}\n", files_info.branch));
        output.push_str(&format!(
            "Files changed: {}\n",
            files_info.staged_files.len()
        ));

        for file in &files_info.staged_files {
            output.push_str(&format!("  {}: {:?}\n", file.path, file.change_type));
        }

        Ok(output)
    }
}

// Git diff tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitDiff;

/// Detail level for diff output
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema, Default)]
#[serde(rename_all = "lowercase")]
pub enum DetailLevel {
    /// Summary only: file list with stats and relevance scores, no diffs (default)
    #[default]
    Summary,
    /// Standard: includes full diffs (use with `files` filter for large changesets)
    Standard,
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct GitDiffArgs {
    /// Use "staged" or omit for staged changes, or specify commit/branch
    #[serde(default)]
    pub from: Option<String>,
    /// Target commit/branch (use with from)
    #[serde(default)]
    pub to: Option<String>,
    /// Detail level: "summary" (default) for overview, "standard" for full diffs
    #[serde(default)]
    pub detail: DetailLevel,
    /// Filter to specific files (use with detail="standard" for targeted analysis)
    #[serde(default)]
    pub files: Option<Vec<String>>,
}

impl Tool for GitDiff {
    const NAME: &'static str = "git_diff";
    type Error = GitError;
    type Args = GitDiffArgs;
    type Output = String;

    async fn definition(&self, _: String) -> ToolDefinition {
        ToolDefinition {
            name: "git_diff".to_string(),
            description: "Get Git diff for file changes. Returns summary by default (file list with relevance scores). Use detail='standard' with files=['path1','path2'] to get full diffs for specific files. Progressive approach: call once for summary, then again with files filter for important ones.".to_string(),
            parameters: parameters_schema::<GitDiffArgs>(),
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        let repo = get_current_repo().map_err(GitError::from)?;

        // Normalize empty strings to None (LLMs often send "" instead of null)
        let from = args.from.filter(|s| !s.is_empty());
        let to = args.to.filter(|s| !s.is_empty());

        // Handle the case where we want staged changes
        // - No args: get staged changes
        // - from="staged": get staged changes
        // - Otherwise: get commit range
        let files = match (from.as_deref(), to.as_deref()) {
            (None | Some("staged"), None) | (Some("staged"), Some("HEAD")) => {
                // Get staged changes
                let files_info = repo.extract_files_info(false).map_err(GitError::from)?;
                files_info.staged_files
            }
            (Some(from), Some(to)) => {
                // Get changes between two commits/branches
                repo.get_commit_range_files(from, to)
                    .map_err(GitError::from)?
            }
            (None, Some(_)) => {
                // Invalid: to without from
                return Err(GitError(
                    "Cannot specify 'to' without 'from'. Use both or neither.".to_string(),
                ));
            }
            (Some(from), None) => {
                // Get changes from a specific commit to HEAD (already handled "staged" above)
                repo.get_commit_range_files(from, "HEAD")
                    .map_err(GitError::from)?
            }
        };

        // Score and sort files by relevance
        let mut scored_files: Vec<ScoredFile> = files
            .iter()
            .map(|file| {
                let (score, reasons) = calculate_relevance_score(file);
                ScoredFile {
                    file,
                    score,
                    reasons,
                }
            })
            .collect();

        // Sort by score descending (most important first)
        scored_files.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Track total before filtering
        let total_files = scored_files.len();

        // Filter to specific files if requested
        let is_filtered = args.files.is_some();
        if let Some(ref filter) = args.files {
            scored_files.retain(|sf| filter.iter().any(|f| sf.file.path.contains(f)));
        }

        // Build output
        let include_diffs = matches!(args.detail, DetailLevel::Standard);
        Ok(format_diff_output(
            &scored_files,
            total_files,
            is_filtered,
            include_diffs,
        ))
    }
}

// Git log tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitLog;

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct GitLogArgs {
    #[serde(default)]
    pub count: Option<usize>,
    #[serde(default)]
    pub from: Option<String>,
    #[serde(default)]
    pub to: Option<String>,
}

impl Tool for GitLog {
    const NAME: &'static str = "git_log";
    type Error = GitError;
    type Args = GitLogArgs;
    type Output = String;

    async fn definition(&self, _: String) -> ToolDefinition {
        ToolDefinition {
            name: "git_log".to_string(),
            description: "Get Git commit history".to_string(),
            parameters: parameters_schema::<GitLogArgs>(),
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        let repo = get_current_repo().map_err(GitError::from)?;

        if let Some(from) = args.from {
            let to = args.to.unwrap_or_else(|| "HEAD".to_string());
            let commits = repo
                .get_commits_in_range(&from, &to)
                .map_err(GitError::from)?;
            return Ok(format_git_log_output(
                &format!("Commits from {from} to {to}:"),
                &commits,
                true,
            ));
        }

        if args.to.is_some() {
            return Err(GitError::from(anyhow::anyhow!(
                "git_log requires `from` when `to` is provided"
            )));
        }

        let commits = repo
            .get_recent_commits(args.count.unwrap_or(10))
            .map_err(GitError::from)?;

        Ok(format_git_log_output("Recent commits:", &commits, false))
    }
}

fn format_git_log_output(
    header: &str,
    commits: &[RecentCommit],
    include_contributors: bool,
) -> String {
    let mut output = String::new();
    output.push_str(header);
    output.push('\n');

    for commit in commits {
        let title = commit.message.lines().next().unwrap_or_default().trim();
        output.push_str(&format!("{}: {} ({})\n", commit.hash, title, commit.author));
    }

    if include_contributors {
        let contributors: BTreeSet<String> = commits
            .iter()
            .map(|commit| commit.author.trim())
            .filter(|author| !author.is_empty() && !is_bot_author(author))
            .map(ToOwned::to_owned)
            .collect();

        if !contributors.is_empty() {
            output.push_str("\nContributors (excluding bots):\n");
            for contributor in contributors {
                output.push_str(&format!("- {contributor}\n"));
            }
        }
    }

    output
}

fn is_bot_author(author: &str) -> bool {
    let normalized = author.trim().to_ascii_lowercase();

    normalized.contains("[bot]")
        || normalized.contains("dependabot")
        || normalized.contains("renovate")
        || normalized.contains("github-actions")
        || normalized.ends_with(" bot")
        || normalized.ends_with("-bot")
        || normalized == "bot"
}

// Git repository info tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitRepoInfo;

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct GitRepoInfoArgs {}

impl Tool for GitRepoInfo {
    const NAME: &'static str = "git_repo_info";
    type Error = GitError;
    type Args = GitRepoInfoArgs;
    type Output = String;

    async fn definition(&self, _: String) -> ToolDefinition {
        ToolDefinition {
            name: "git_repo_info".to_string(),
            description: "Get general information about the Git repository".to_string(),
            parameters: parameters_schema::<GitRepoInfoArgs>(),
        }
    }

    async fn call(&self, _args: Self::Args) -> Result<Self::Output, Self::Error> {
        let repo = get_current_repo().map_err(GitError::from)?;

        let branch = repo.get_current_branch().map_err(GitError::from)?;
        let remote_url = repo.get_remote_url().unwrap_or("None").to_string();

        let mut output = String::new();
        output.push_str("Repository Information:\n");
        output.push_str(&format!("Current Branch: {branch}\n"));
        output.push_str(&format!("Remote URL: {remote_url}\n"));
        output.push_str(&format!(
            "Repository Path: {}\n",
            repo.repo_path().display()
        ));

        Ok(output)
    }
}

// Git changed files tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitChangedFiles;

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct GitChangedFilesArgs {
    #[serde(default)]
    pub from: Option<String>,
    #[serde(default)]
    pub to: Option<String>,
}

impl Tool for GitChangedFiles {
    const NAME: &'static str = "git_changed_files";
    type Error = GitError;
    type Args = GitChangedFilesArgs;
    type Output = String;

    async fn definition(&self, _: String) -> ToolDefinition {
        ToolDefinition {
            name: "git_changed_files".to_string(),
            description: "Get list of files that have changed between commits or branches"
                .to_string(),
            parameters: parameters_schema::<GitChangedFilesArgs>(),
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        let repo = get_current_repo().map_err(GitError::from)?;

        // Normalize empty strings to None (LLMs often send "" instead of null)
        let from = args.from.filter(|s| !s.is_empty());
        let mut to = args.to.filter(|s| !s.is_empty());

        // Default to HEAD when the caller provides only a starting point.
        if from.is_some() && to.is_none() {
            to = Some("HEAD".to_string());
        }

        let files = match (from, to) {
            (Some(from), Some(to)) => {
                // When both from and to are provided, get files changed between commits/branches
                let range_files = repo
                    .get_commit_range_files(&from, &to)
                    .map_err(GitError::from)?;
                range_files.iter().map(|f| f.path.clone()).collect()
            }
            (None, Some(to)) => {
                // When only to is provided, get files changed in that single commit
                repo.get_file_paths_for_commit(&to)
                    .map_err(GitError::from)?
            }
            (Some(_from), None) => {
                // Invalid: from without to doesn't make sense for file listing
                return Err(GitError(
                    "Cannot specify 'from' without 'to' for file listing".to_string(),
                ));
            }
            (None, None) => {
                // When neither are provided, get staged files
                let files_info = repo.extract_files_info(false).map_err(GitError::from)?;
                files_info.file_paths
            }
        };

        let mut output = String::new();
        output.push_str("Changed files:\n");

        for file in files {
            output.push_str(&format!("  {file}\n"));
        }

        Ok(output)
    }
}