codanna 0.9.19

Code Intelligence for Large Language Models
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
//! Git operations using system git binary
//!
//! Replaces direct libgit2 usage to honor ~/.ssh/config, credential helpers,
//! and other git configuration that libgit2 does not support.

use std::path::Path;
use std::process::Command;
use thiserror::Error;

/// Git process exit code with user-friendly Display.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GitExitCode(pub Option<i32>);

impl std::fmt::Display for GitExitCode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.0 {
            Some(c) => write!(f, "{c}"),
            None => write!(f, "unknown"),
        }
    }
}

#[derive(Error, Debug)]
pub enum GitError {
    #[error(
        "git is not installed or not found on PATH\nSuggestion: Install git and ensure it is in your PATH"
    )]
    GitNotFound,

    #[error("repository not found: {url}\nSuggestion: Check the URL and your access permissions")]
    RepositoryNotFound { url: String },

    #[error(
        "reference '{ref_name}' not found in {url}\nSuggestion: Use a valid branch name or tag"
    )]
    ReferenceNotFound { url: String, ref_name: String },

    #[error(
        "git {command} failed (exit {exit_code}): {stderr}\nSuggestion: Check network connection and repository permissions"
    )]
    CommandFailed {
        command: String,
        stderr: String,
        exit_code: GitExitCode,
    },

    #[error("IO error: {0}\nSuggestion: Check file permissions and disk space")]
    Io(#[from] std::io::Error),
}

impl GitError {
    /// Error detail without the suggestion line.
    /// Use when embedding in a wrapper error that provides its own suggestion.
    pub fn message(&self) -> String {
        let full = self.to_string();
        match full.find("\nSuggestion:") {
            Some(pos) => full[..pos].to_string(),
            None => full,
        }
    }
}

pub type GitResult<T> = Result<T, GitError>;

/// Run a git command and return stdout on success, GitError on failure.
fn run_git(args: &[&str]) -> GitResult<String> {
    let output = Command::new("git")
        .args(args)
        .env_remove("GIT_DIR")
        .env_remove("GIT_WORK_TREE")
        .env_remove("GIT_INDEX_FILE")
        .env_remove("GIT_OBJECT_DIRECTORY")
        .env_remove("GIT_ALTERNATE_OBJECT_DIRECTORIES")
        .output()
        .map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                GitError::GitNotFound
            } else {
                GitError::Io(e)
            }
        })?;

    if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        let command = args.join(" ");
        Err(GitError::CommandFailed {
            command,
            stderr,
            exit_code: GitExitCode(output.status.code()),
        })
    }
}

/// Clone a repository. Uses --depth 1 for remote repos. Returns commit SHA.
pub fn clone_repository(
    repo_url: &str,
    target_dir: &Path,
    git_ref: Option<&str>,
) -> GitResult<String> {
    let is_local = repo_url.starts_with("file://") || Path::new(repo_url).exists();

    if let Some(parent) = target_dir.parent() {
        std::fs::create_dir_all(parent)?;
    }

    if target_dir.exists() {
        std::fs::remove_dir_all(target_dir)?;
    }

    let mut args: Vec<String> = vec!["clone".to_string()];

    if !is_local {
        args.push("--depth".to_string());
        args.push("1".to_string());
    }

    if let Some(ref_value) = git_ref {
        args.push("--branch".to_string());
        args.push(ref_value.to_string());
    }

    args.push(repo_url.to_string());
    args.push(target_dir.to_string_lossy().to_string());

    let arg_refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
    run_git(&arg_refs).map_err(|e| match e {
        GitError::CommandFailed {
            stderr, exit_code, ..
        } => GitError::CommandFailed {
            command: format!("clone {repo_url}"),
            stderr,
            exit_code,
        },
        other => other,
    })?;

    get_commit_sha(target_dir)
}

/// Resolve a git reference to a commit SHA without cloning.
pub fn resolve_reference(repo_url: &str, git_ref: &str) -> GitResult<String> {
    let heads_ref = format!("refs/heads/{git_ref}");
    let tags_ref = format!("refs/tags/{git_ref}");

    let args = [
        "ls-remote".to_string(),
        "--exit-code".to_string(),
        repo_url.to_string(),
        git_ref.to_string(),
        heads_ref,
        tags_ref,
    ];
    let arg_refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect();

    let output = run_git(&arg_refs);

    match output {
        Ok(stdout) => {
            // Parse first line: "<sha>\t<refname>"
            if let Some(sha) = stdout
                .lines()
                .next()
                .and_then(|line| line.split('\t').next())
            {
                Ok(sha.to_string())
            } else {
                Err(GitError::ReferenceNotFound {
                    url: repo_url.to_string(),
                    ref_name: git_ref.to_string(),
                })
            }
        }
        Err(GitError::CommandFailed {
            exit_code: GitExitCode(Some(2)),
            ..
        }) => Err(GitError::ReferenceNotFound {
            url: repo_url.to_string(),
            ref_name: git_ref.to_string(),
        }),
        Err(GitError::CommandFailed {
            exit_code: GitExitCode(Some(128)),
            ..
        }) => Err(GitError::RepositoryNotFound {
            url: repo_url.to_string(),
        }),
        Err(other) => Err(other),
    }
}

/// Check if a URL points to a valid Git repository.
pub fn validate_repository(repo_url: &str) -> GitResult<()> {
    let output = Command::new("git")
        .args(["ls-remote", "--exit-code", repo_url, "HEAD"])
        .env_remove("GIT_DIR")
        .env_remove("GIT_WORK_TREE")
        .env_remove("GIT_INDEX_FILE")
        .env_remove("GIT_OBJECT_DIRECTORY")
        .env_remove("GIT_ALTERNATE_OBJECT_DIRECTORIES")
        .output()
        .map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                GitError::GitNotFound
            } else {
                GitError::Io(e)
            }
        })?;

    match output.status.code() {
        Some(0) | Some(2) => Ok(()),
        _ => {
            let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
            if stderr.contains("not found")
                || stderr.contains("does not exist")
                || stderr.contains("Could not read from remote")
            {
                Err(GitError::RepositoryNotFound {
                    url: repo_url.to_string(),
                })
            } else {
                Err(GitError::CommandFailed {
                    command: format!("ls-remote {repo_url}"),
                    stderr,
                    exit_code: GitExitCode(output.status.code()),
                })
            }
        }
    }
}

/// Get the current commit SHA of a local repository.
pub fn get_commit_sha(repo_dir: &Path) -> GitResult<String> {
    let dir_str = repo_dir.to_string_lossy();
    run_git(&["-C", &dir_str, "rev-parse", "HEAD"])
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::process::Command;
    use tempfile::tempdir;

    /// Create a test git repo with one commit. Disables gpgsign for CI/local compat.
    fn init_test_repo(path: &Path) {
        let run = |args: &[&str]| {
            let output = Command::new("git")
                .args(args)
                .current_dir(path)
                .output()
                .unwrap_or_else(|e| panic!("failed to run git {}: {e}", args.join(" ")));
            assert!(
                output.status.success(),
                "git {} failed: {}",
                args.join(" "),
                String::from_utf8_lossy(&output.stderr)
            );
        };
        run(&["init"]);
        run(&["config", "user.email", "test@test.com"]);
        run(&["config", "user.name", "Test"]);
        run(&["config", "commit.gpgsign", "false"]);
        std::fs::write(path.join("README.md"), "test").expect("write file");
        run(&["add", "-A"]);
        run(&["commit", "-m", "initial commit"]);
    }

    #[test]
    fn test_get_commit_sha_valid_repo() {
        let dir = tempdir().unwrap();
        init_test_repo(dir.path());
        let sha = get_commit_sha(dir.path()).unwrap();
        assert_eq!(sha.len(), 40, "SHA should be 40 hex chars");
        assert!(
            sha.chars().all(|c| c.is_ascii_hexdigit()),
            "SHA should be hex"
        );
    }

    #[test]
    fn test_get_commit_sha_not_a_repo() {
        let dir = tempdir().unwrap();
        // No git init -- just an empty directory
        let result = get_commit_sha(dir.path());
        assert!(result.is_err());
        match result.unwrap_err() {
            GitError::CommandFailed {
                command, stderr, ..
            } => {
                assert!(
                    command.contains("rev-parse"),
                    "command should mention rev-parse"
                );
                assert!(!stderr.is_empty(), "stderr should have details");
            }
            other => panic!("expected CommandFailed, got: {other:?}"),
        }
    }

    #[test]
    fn test_clone_local_repository() {
        let source = tempdir().unwrap();
        init_test_repo(source.path());

        let target = tempdir().unwrap();
        let clone_path = target.path().join("cloned");

        let sha = clone_repository(source.path().to_str().unwrap(), &clone_path, None).unwrap();
        assert_eq!(sha.len(), 40);
        assert!(clone_path.join("README.md").exists());
    }

    #[test]
    fn test_clone_local_with_branch() {
        let source = tempdir().unwrap();
        init_test_repo(source.path());

        // Create a branch with a new file
        let run = |args: &[&str]| {
            let output = Command::new("git")
                .args(args)
                .current_dir(source.path())
                .output()
                .unwrap_or_else(|e| panic!("failed to run git {}: {e}", args.join(" ")));
            assert!(
                output.status.success(),
                "git {} failed: {}",
                args.join(" "),
                String::from_utf8_lossy(&output.stderr)
            );
        };
        run(&["checkout", "-b", "feature"]);
        std::fs::write(source.path().join("feature.txt"), "feature content").unwrap();
        run(&["add", "-A"]);
        run(&["commit", "-m", "feature commit"]);

        let target = tempdir().unwrap();
        let clone_path = target.path().join("cloned");

        let sha = clone_repository(
            source.path().to_str().unwrap(),
            &clone_path,
            Some("feature"),
        )
        .unwrap();
        assert_eq!(sha.len(), 40);
        assert!(clone_path.join("feature.txt").exists());
    }

    #[test]
    fn test_clone_nonexistent_source() {
        let target = tempdir().unwrap();
        let clone_path = target.path().join("cloned");
        let result = clone_repository("/nonexistent/path/repo", &clone_path, None);
        match result.unwrap_err() {
            GitError::CommandFailed {
                command, exit_code, ..
            } => {
                assert!(command.contains("clone"), "should be a clone error");
                assert_eq!(
                    exit_code,
                    GitExitCode(Some(128)),
                    "git clone should exit 128 for invalid repo"
                );
            }
            other => panic!("expected CommandFailed, got: {other:?}"),
        }
    }

    #[test]
    fn test_resolve_reference_head() {
        let dir = tempdir().unwrap();
        init_test_repo(dir.path());

        let sha = resolve_reference(dir.path().to_str().unwrap(), "HEAD").unwrap();
        let expected = get_commit_sha(dir.path()).unwrap();
        assert_eq!(sha, expected);
    }

    #[test]
    fn test_resolve_reference_branch() {
        let dir = tempdir().unwrap();
        init_test_repo(dir.path());

        // Read the default branch name from .git/HEAD
        let head = std::fs::read_to_string(dir.path().join(".git/HEAD")).unwrap();
        let branch = head.trim().strip_prefix("ref: refs/heads/").unwrap();

        let sha = resolve_reference(dir.path().to_str().unwrap(), branch).unwrap();
        assert_eq!(sha.len(), 40);
    }

    #[test]
    fn test_resolve_reference_not_found() {
        let dir = tempdir().unwrap();
        init_test_repo(dir.path());

        let result = resolve_reference(dir.path().to_str().unwrap(), "nonexistent-ref-xyz");
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            GitError::ReferenceNotFound { .. }
        ));
    }

    #[test]
    fn test_validate_local_repo() {
        let dir = tempdir().unwrap();
        init_test_repo(dir.path());
        assert!(validate_repository(dir.path().to_str().unwrap()).is_ok());
    }

    #[test]
    fn test_validate_nonexistent() {
        let result = validate_repository("/nonexistent/path/repo");
        assert!(
            matches!(result.unwrap_err(), GitError::RepositoryNotFound { .. }),
            "should return RepositoryNotFound for nonexistent path"
        );
    }

    #[test]
    fn test_error_messages_include_suggestions() {
        let errors: Vec<GitError> = vec![
            GitError::GitNotFound,
            GitError::RepositoryNotFound {
                url: "test".to_string(),
            },
            GitError::ReferenceNotFound {
                url: "test".to_string(),
                ref_name: "main".to_string(),
            },
            GitError::CommandFailed {
                command: "test".to_string(),
                stderr: "fail".to_string(),
                exit_code: GitExitCode(Some(1)),
            },
        ];
        for err in errors {
            assert!(
                err.to_string().contains("Suggestion:"),
                "Missing Suggestion in: {err}"
            );
        }
    }
}