coderlib 0.1.0

A Rust library for AI-powered code assistance and agentic system
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
//! Git integration tools
//!
//! This module provides Git repository analysis and operations including:
//! - Repository status and diff analysis
//! - Commit history and blame information
//! - Branch analysis and merge conflict detection
//! - Change impact analysis

use async_trait::async_trait;
use git2::{Repository, Status, StatusOptions, BlameOptions, DiffOptions};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::collections::HashMap;

use crate::integration::HostIntegration;
use crate::tools::{Tool, ToolResponse, ToolError, Permission, validation};

/// Git repository analysis tool
pub struct GitTool {
    repo_cache: std::sync::Arc<std::sync::Mutex<HashMap<PathBuf, Repository>>>,
}

/// Git repository status information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitStatus {
    pub branch: String,
    pub ahead: usize,
    pub behind: usize,
    pub modified_files: Vec<String>,
    pub added_files: Vec<String>,
    pub deleted_files: Vec<String>,
    pub untracked_files: Vec<String>,
    pub conflicted_files: Vec<String>,
    pub is_clean: bool,
}

/// Git diff information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitDiffInfo {
    pub file_path: String,
    pub additions: usize,
    pub deletions: usize,
    pub hunks: Vec<DiffHunk>,
}

/// Individual diff hunk
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffHunk {
    pub old_start: u32,
    pub old_lines: u32,
    pub new_start: u32,
    pub new_lines: u32,
    pub header: String,
    pub lines: Vec<DiffLine>,
}

/// Individual diff line
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffLine {
    pub line_type: String, // "added", "deleted", "context"
    pub content: String,
    pub line_number: Option<u32>,
}

/// Git commit information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommitInfo {
    pub id: String,
    pub message: String,
    pub author: String,
    pub email: String,
    pub timestamp: i64,
    pub files_changed: Vec<String>,
    pub additions: usize,
    pub deletions: usize,
}

/// Git blame information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlameInfo {
    pub file_path: String,
    pub lines: Vec<BlameLine>,
}

/// Individual blame line
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlameLine {
    pub line_number: usize,
    pub content: String,
    pub commit_id: String,
    pub author: String,
    pub timestamp: i64,
}

/// Branch information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BranchInfo {
    pub name: String,
    pub is_current: bool,
    pub is_remote: bool,
    pub last_commit: String,
    pub ahead: usize,
    pub behind: usize,
}

impl GitTool {
    /// Create a new Git tool
    pub fn new() -> Self {
        Self {
            repo_cache: std::sync::Arc::new(std::sync::Mutex::new(HashMap::new())),
        }
    }

    /// Get or create a repository instance
    fn get_repository(&self, repo_path: &Path) -> Result<Repository, ToolError> {
        let mut cache = self.repo_cache.lock().unwrap();
        
        if let Some(repo) = cache.get(repo_path) {
            // Try to use cached repository
            if repo.path().exists() {
                return Ok(Repository::open(repo.path())?);
            }
        }

        // Find repository root
        let repo = Repository::discover(repo_path)
            .map_err(|e| ToolError::ExecutionFailed(format!("Not a git repository: {}", e)))?;
        
        let repo_root = repo.workdir()
            .ok_or_else(|| ToolError::ExecutionFailed("Bare repository not supported".to_string()))?
            .to_path_buf();
        
        cache.insert(repo_root, Repository::open(repo.path())?);
        Ok(repo)
    }

    /// Get repository status
    async fn get_status(&self, repo_path: &Path) -> Result<GitStatus, ToolError> {
        let repo = self.get_repository(repo_path)?;
        
        // Get current branch
        let head = repo.head()?;
        let branch = if head.is_branch() {
            head.shorthand().unwrap_or("HEAD").to_string()
        } else {
            "HEAD".to_string()
        };

        // Get status
        let mut status_opts = StatusOptions::new();
        status_opts.include_untracked(true);
        status_opts.include_ignored(false);
        
        let statuses = repo.statuses(Some(&mut status_opts))?;
        
        let mut modified_files = Vec::new();
        let mut added_files = Vec::new();
        let mut deleted_files = Vec::new();
        let mut untracked_files = Vec::new();
        let mut conflicted_files = Vec::new();

        for entry in statuses.iter() {
            let path = entry.path().unwrap_or("").to_string();
            let status = entry.status();

            if status.contains(Status::CONFLICTED) {
                conflicted_files.push(path.clone());
            }
            if status.contains(Status::WT_MODIFIED) || status.contains(Status::INDEX_MODIFIED) {
                modified_files.push(path.clone());
            }
            if status.contains(Status::WT_NEW) || status.contains(Status::INDEX_NEW) {
                if status.contains(Status::WT_NEW) {
                    untracked_files.push(path.clone());
                } else {
                    added_files.push(path.clone());
                }
            }
            if status.contains(Status::WT_DELETED) || status.contains(Status::INDEX_DELETED) {
                deleted_files.push(path.clone());
            }
        }

        let is_clean = modified_files.is_empty() && added_files.is_empty() && 
                      deleted_files.is_empty() && untracked_files.is_empty() && 
                      conflicted_files.is_empty();

        // Get ahead/behind info
        let (ahead, behind) = self.get_ahead_behind(&repo, &branch)?;

        Ok(GitStatus {
            branch,
            ahead,
            behind,
            modified_files,
            added_files,
            deleted_files,
            untracked_files,
            conflicted_files,
            is_clean,
        })
    }

    /// Get ahead/behind commit count
    fn get_ahead_behind(&self, repo: &Repository, branch: &str) -> Result<(usize, usize), ToolError> {
        let local_ref = repo.find_reference(&format!("refs/heads/{}", branch));
        let remote_ref = repo.find_reference(&format!("refs/remotes/origin/{}", branch));

        match (local_ref, remote_ref) {
            (Ok(local), Ok(remote)) => {
                let local_oid = local.target().ok_or_else(|| ToolError::ExecutionFailed("Invalid local ref".to_string()))?;
                let remote_oid = remote.target().ok_or_else(|| ToolError::ExecutionFailed("Invalid remote ref".to_string()))?;
                
                let (ahead, behind) = repo.graph_ahead_behind(local_oid, remote_oid)?;
                Ok((ahead, behind))
            }
            _ => Ok((0, 0))
        }
    }

    /// Get diff information
    async fn get_diff(&self, repo_path: &Path, params: &serde_json::Value) -> Result<Vec<GitDiffInfo>, ToolError> {
        let repo = self.get_repository(repo_path)?;
        
        let staged = validation::optional_string(params, "staged").unwrap_or_else(|| "false".to_string()) == "true";
        let file_path = validation::optional_string(params, "file_path");

        let diff = if staged {
            // Diff between index and HEAD
            let tree = repo.head()?.peel_to_tree()?;
            let mut diff_opts = DiffOptions::new();
            if let Some(path) = &file_path {
                diff_opts.pathspec(path);
            }
            repo.diff_tree_to_index(Some(&tree), None, Some(&mut diff_opts))?
        } else {
            // Diff between working directory and index
            let mut diff_opts = DiffOptions::new();
            if let Some(path) = &file_path {
                diff_opts.pathspec(path);
            }
            repo.diff_index_to_workdir(None, Some(&mut diff_opts))?
        };

        let mut diff_infos = Vec::new();
        
        diff.foreach(
            &mut |delta, _progress| {
                if let Some(path) = delta.new_file().path() {
                    diff_infos.push(GitDiffInfo {
                        file_path: path.to_string_lossy().to_string(),
                        additions: 0,
                        deletions: 0,
                        hunks: Vec::new(),
                    });
                }
                true
            },
            None,
            None,
            None,
        )?;

        // TODO: Implement detailed hunk parsing
        // This would require more complex diff parsing

        Ok(diff_infos)
    }

    /// Get commit log
    async fn get_log(&self, repo_path: &Path, params: &serde_json::Value) -> Result<Vec<CommitInfo>, ToolError> {
        let repo = self.get_repository(repo_path)?;
        
        let limit = validation::optional_string(params, "limit")
            .and_then(|s| s.parse::<usize>().ok())
            .unwrap_or(10);
        
        let file_path = validation::optional_string(params, "file_path");

        let mut revwalk = repo.revwalk()?;
        revwalk.push_head()?;
        revwalk.set_sorting(git2::Sort::TIME)?;

        let mut commits = Vec::new();
        let mut count = 0;

        for oid in revwalk {
            if count >= limit {
                break;
            }

            let oid = oid?;
            let commit = repo.find_commit(oid)?;
            
            // If file_path is specified, check if this commit affects that file
            if let Some(path) = &file_path {
                let tree = commit.tree()?;
                if tree.get_path(Path::new(path)).is_err() {
                    continue; // Skip commits that don't affect this file
                }
            }

            let author = commit.author();
            let message = commit.message().unwrap_or("").to_string();
            
            commits.push(CommitInfo {
                id: oid.to_string(),
                message,
                author: author.name().unwrap_or("").to_string(),
                email: author.email().unwrap_or("").to_string(),
                timestamp: author.when().seconds(),
                files_changed: Vec::new(), // TODO: Calculate changed files
                additions: 0, // TODO: Calculate stats
                deletions: 0, // TODO: Calculate stats
            });

            count += 1;
        }

        Ok(commits)
    }

    /// Get blame information
    async fn get_blame(&self, repo_path: &Path, params: &serde_json::Value) -> Result<BlameInfo, ToolError> {
        let repo = self.get_repository(repo_path)?;
        let file_path = validation::require_string(params, "file_path")?;

        let mut blame_opts = BlameOptions::new();
        let blame = repo.blame_file(Path::new(&file_path), Some(&mut blame_opts))?;

        // Read file content to get line content
        let full_path = repo.workdir()
            .ok_or_else(|| ToolError::ExecutionFailed("Bare repository not supported".to_string()))?
            .join(&file_path);
        
        let content = std::fs::read_to_string(&full_path)
            .map_err(|e| ToolError::ExecutionFailed(format!("Failed to read file: {}", e)))?;
        
        let lines: Vec<&str> = content.lines().collect();
        let mut blame_lines = Vec::new();

        for (line_num, line_content) in lines.iter().enumerate() {
            if let Some(hunk) = blame.get_line(line_num + 1) {
                let commit = repo.find_commit(hunk.final_commit_id())?;
                let author = commit.author();

                blame_lines.push(BlameLine {
                    line_number: line_num + 1,
                    content: line_content.to_string(),
                    commit_id: hunk.final_commit_id().to_string(),
                    author: author.name().unwrap_or("").to_string(),
                    timestamp: author.when().seconds(),
                });
            }
        }

        Ok(BlameInfo {
            file_path,
            lines: blame_lines,
        })
    }

    /// Get branch information
    async fn get_branches(&self, repo_path: &Path) -> Result<Vec<BranchInfo>, ToolError> {
        let repo = self.get_repository(repo_path)?;
        
        let branches = repo.branches(Some(git2::BranchType::Local))?;
        let mut branch_infos = Vec::new();

        let current_branch = repo.head()?.shorthand().unwrap_or("").to_string();

        for branch_result in branches {
            let (branch, _branch_type) = branch_result?;
            if let Some(name) = branch.name()? {
                let is_current = name == current_branch;
                let last_commit = if let Some(oid) = branch.get().target() {
                    oid.to_string()
                } else {
                    "".to_string()
                };

                let (ahead, behind) = if is_current {
                    self.get_ahead_behind(&repo, name)?
                } else {
                    (0, 0)
                };

                branch_infos.push(BranchInfo {
                    name: name.to_string(),
                    is_current,
                    is_remote: false,
                    last_commit,
                    ahead,
                    behind,
                });
            }
        }

        Ok(branch_infos)
    }
}

#[async_trait]
impl Tool for GitTool {
    async fn execute(
        &self,
        parameters: serde_json::Value,
        _host: &dyn HostIntegration,
    ) -> Result<ToolResponse, ToolError> {
        let action = validation::require_string(&parameters, "action")?;
        let repo_path = validation::optional_path(&parameters, "repo_path")
            .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));

        match action.as_str() {
            "status" => {
                let status = self.get_status(&repo_path).await?;
                let content = format!(
                    "Git Status\n\
                    Branch: {}\n\
                    Ahead: {} commits\n\
                    Behind: {} commits\n\
                    Modified: {} files\n\
                    Added: {} files\n\
                    Deleted: {} files\n\
                    Untracked: {} files\n\
                    Conflicted: {} files\n\
                    Clean: {}",
                    status.branch,
                    status.ahead,
                    status.behind,
                    status.modified_files.len(),
                    status.added_files.len(),
                    status.deleted_files.len(),
                    status.untracked_files.len(),
                    status.conflicted_files.len(),
                    status.is_clean
                );
                Ok(ToolResponse::with_metadata(content, serde_json::to_value(status)?))
            }
            "diff" => {
                let diff_info = self.get_diff(&repo_path, &parameters).await?;
                let content = format!(
                    "Git Diff\n\
                    Files changed: {}\n\
                    Total changes: {} files",
                    diff_info.len(),
                    diff_info.len()
                );
                Ok(ToolResponse::with_metadata(content, serde_json::to_value(diff_info)?))
            }
            "log" => {
                let commits = self.get_log(&repo_path, &parameters).await?;
                let content = format!(
                    "Git Log\n\
                    Showing {} commits\n\
                    Latest: {}",
                    commits.len(),
                    commits.first().map(|c| c.message.as_str()).unwrap_or("No commits")
                );
                Ok(ToolResponse::with_metadata(content, serde_json::to_value(commits)?))
            }
            "blame" => {
                let blame_info = self.get_blame(&repo_path, &parameters).await?;
                let content = format!(
                    "Git Blame for {}\n\
                    Lines: {}",
                    blame_info.file_path,
                    blame_info.lines.len()
                );
                Ok(ToolResponse::with_metadata(content, serde_json::to_value(blame_info)?))
            }
            "branches" => {
                let branches = self.get_branches(&repo_path).await?;
                let current = branches.iter().find(|b| b.is_current);
                let content = format!(
                    "Git Branches\n\
                    Total: {}\n\
                    Current: {}",
                    branches.len(),
                    current.map(|b| b.name.as_str()).unwrap_or("None")
                );
                Ok(ToolResponse::with_metadata(content, serde_json::to_value(branches)?))
            }
            _ => Err(ToolError::InvalidParameters(format!("Unknown git action: {}", action)))
        }
    }

    fn requires_permission(&self) -> Permission {
        Permission::None // Git operations are generally safe for analysis
    }

    fn description(&self) -> &str {
        "Analyze Git repository status, history, and changes"
    }

    fn name(&self) -> &str {
        "git"
    }

    fn parameter_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "description": "Git action to perform",
                    "enum": ["status", "diff", "log", "blame", "branches"]
                },
                "repo_path": {
                    "type": "string",
                    "description": "Path to git repository (optional, defaults to current directory)"
                },
                "file_path": {
                    "type": "string",
                    "description": "Specific file path for blame, log, or diff operations"
                },
                "limit": {
                    "type": "string",
                    "description": "Number of commits to show in log (default: 10)"
                },
                "staged": {
                    "type": "string",
                    "description": "Show staged changes in diff (true/false, default: false)"
                }
            },
            "required": ["action"]
        })
    }

    fn clone_box(&self) -> Box<dyn Tool> {
        Box::new(Self::new())
    }
}

impl Default for GitTool {
    fn default() -> Self {
        Self::new()
    }
}