comindly 0.3.2

Deterministic, always-fresh, cross-repo code intelligence for coding agents — self-hosted, single binary.
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
//! comindly's own checkout of each repository's default branch: a detached git worktree under
//! `~/.cache/comindly/worktrees`, so indexing never touches your working folder or branches. Uses the
//! `git` command, so your SSH keys and credential helpers apply.

use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{bail, Context, Result};

/// A checkout ready to index.
pub struct Checkout {
    pub dir: PathBuf,
    /// What was checked out, e.g. `origin/main`.
    pub target: String,
    pub commit: String,
    /// Set when fetching failed and the last fetched state was used instead.
    pub stale: Option<String>,
}

/// An HTTPS username and access token, handed to git for one command only.
#[derive(Clone)]
pub struct Auth {
    pub username: String,
    pub token: String,
}

/// Returned when an HTTPS remote needs a token that neither git's credential helper nor the environment has.
#[derive(Debug)]
pub struct NeedsToken {
    pub host: String,
    pub username: String,
}

impl std::fmt::Display for NeedsToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} needs an access token", self.host)
    }
}

impl std::error::Error for NeedsToken {}

fn https_host(url: &str) -> Option<String> {
    let rest = url
        .strip_prefix("https://")
        .or_else(|| url.strip_prefix("http://"))?;
    let authority = rest.split('/').next()?;
    let host = authority.rsplit('@').next()?.split(':').next()?;
    (!host.is_empty()).then(|| host.to_lowercase())
}

/// The username a host expects with an access token.
pub fn token_user(host: &str) -> &'static str {
    if host == "github.com" {
        "x-access-token"
    } else {
        "oauth2"
    }
}

/// A token for `url`'s host from the environment: `GH_TOKEN`/`GITHUB_TOKEN` for GitHub, `GITLAB_TOKEN` for GitLab.
pub fn env_auth(url: &str) -> Option<Auth> {
    let host = https_host(url)?;
    let token = env_token(&host)?;
    Some(Auth {
        username: token_user(&host).into(),
        token,
    })
}

/// The deployment's token for `host`: `GH_TOKEN`/`GITHUB_TOKEN` for github.com, `GITLAB_TOKEN` for
/// gitlab.com and the hosts in `COMINDLY_GITLAB_HOSTS` (or their `_FILE`).
pub fn env_token(host: &str) -> Option<String> {
    let host = host.to_ascii_lowercase();
    let gitlab = host == "gitlab.com"
        || crate::server::env_secret("COMINDLY_GITLAB_HOSTS").is_some_and(|hosts| {
            hosts
                .split([',', ' '])
                .any(|h| h.trim().eq_ignore_ascii_case(&host))
        });
    let vars: &[&str] = if host == "github.com" {
        &["GH_TOKEN", "GITHUB_TOKEN"]
    } else if gitlab {
        &["GITLAB_TOKEN"]
    } else {
        &[]
    };
    vars.iter().find_map(|v| crate::server::env_secret(v))
}

/// A token for cloning or fetching `url` without asking: the deployment's, else on a server the
/// one stored there. Elsewhere git's own credential helper answers.
pub fn auth_for(url: &str) -> Option<Auth> {
    if let Some(a) = env_auth(url) {
        return Some(a);
    }
    if !crate::server::enabled() {
        return None;
    }
    let host = https_host(url)?;
    crate::server::git_token(&host).map(|token| Auth {
        username: token_user(&host).into(),
        token,
    })
}

fn auth_failed(stderr: &str) -> bool {
    [
        "could not read Username",
        "could not read Password",
        "Authentication failed",
        "Access denied",
        "HTTP Basic",
        "returned error: 401",
        "returned error: 403",
    ]
    .iter()
    .any(|m| stderr.contains(m))
}

fn git(dir: &Path, args: &[&str]) -> Result<String> {
    git_auth(dir, args, None)
}

/// Run git; with `auth`, a credential helper that answers with it is tried after your own helpers.
fn git_auth(dir: &Path, args: &[&str], auth: Option<&Auth>) -> Result<String> {
    finish(args, command(dir, args, auth).output())
}

/// A git command in `dir` that never prompts and gives up on a stalled transfer.
fn command(dir: &Path, args: &[&str], auth: Option<&Auth>) -> Command {
    let mut cmd = Command::new("git");
    if let Some(a) = auth {
        cmd.arg("-c")
            .arg(r#"credential.helper=!f() { test "$1" = get && printf 'username=%s\npassword=%s\n' "$COMINDLY_GIT_USER" "$COMINDLY_GIT_TOKEN"; }; f"#)
            .env("COMINDLY_GIT_USER", &a.username)
            .env("COMINDLY_GIT_TOKEN", &a.token);
    }
    cmd.arg("-C")
        .arg(dir)
        .args(args)
        .env("GIT_TERMINAL_PROMPT", "0")
        // Give up on a stalled fetch instead of waiting on a struggling server.
        .env("GIT_HTTP_LOW_SPEED_LIMIT", "1000")
        .env("GIT_HTTP_LOW_SPEED_TIME", "20");
    cmd
}

fn finish(args: &[&str], out: std::io::Result<std::process::Output>) -> Result<String> {
    let out = out.context("run git (is it installed?)")?;
    if !out.status.success() {
        bail!(
            "git {}: {}",
            args.join(" "),
            String::from_utf8_lossy(&out.stderr).trim()
        );
    }
    Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
}

/// The repository's name: its folder's name.
pub fn name(repo: &Path) -> String {
    repo.file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_else(|| "repo".into())
}

fn has_origin(repo: &Path) -> bool {
    git(repo, &["remote", "get-url", "origin"]).is_ok()
}

/// The branch to index: `origin`'s default (from `origin/HEAD`), else `main` or `master`.
pub fn default_branch(repo: &Path) -> Result<String> {
    if let Ok(head) = git(
        repo,
        &["symbolic-ref", "--short", "refs/remotes/origin/HEAD"],
    ) {
        if let Some(b) = head.strip_prefix("origin/") {
            return Ok(b.to_string());
        }
    }
    let prefix = if has_origin(repo) {
        "refs/remotes/origin/"
    } else {
        "refs/heads/"
    };
    for b in ["main", "master"] {
        if git(
            repo,
            &["rev-parse", "--verify", "--quiet", &format!("{prefix}{b}")],
        )
        .is_ok()
        {
            return Ok(b.to_string());
        }
    }
    bail!("no main or master branch in {}", repo.display())
}

/// The remote's default branch and its commit, asked from the remote; also records it as `origin/HEAD`.
pub fn remote_default(repo: &Path) -> Result<(String, String)> {
    let auth = remote_url(repo).and_then(|u| auth_for(&u));
    let out = git_auth(
        repo,
        &["ls-remote", "--symref", "origin", "HEAD"],
        auth.as_ref(),
    )?;
    let branch = out
        .lines()
        .find_map(|l| l.strip_prefix("ref: refs/heads/"))
        .and_then(|l| l.split_whitespace().next())
        .context("the remote does not say which branch is its default")?
        .to_string();
    let commit = out
        .lines()
        .find(|l| !l.starts_with("ref:") && l.ends_with("HEAD"))
        .and_then(|l| l.split_whitespace().next())
        .context("the remote's default branch has no commit")?
        .to_string();
    let _ = git(repo, &["remote", "set-head", "origin", &branch]);
    Ok((branch, commit))
}

/// The branch checked out in your own folder (for display).
pub fn current_branch(repo: &Path) -> Option<String> {
    git(repo, &["rev-parse", "--abbrev-ref", "HEAD"]).ok()
}

pub fn remote_url(repo: &Path) -> Option<String> {
    git(repo, &["remote", "get-url", "origin"]).ok()
}

/// comindly's checkout of `repo`'s default branch, if one has been made.
pub fn checkout_dir(repo: &Path) -> Option<PathBuf> {
    worktree_dir(repo).ok().filter(|d| d.join(".git").exists())
}

fn worktree_dir(repo: &Path) -> Result<PathBuf> {
    let abs = std::fs::canonicalize(repo).with_context(|| format!("{}", repo.display()))?;
    let key = blake3::hash(abs.to_string_lossy().as_bytes()).to_hex();
    let home = std::env::var_os("HOME").context("HOME")?;
    Ok(PathBuf::from(home)
        .join(".cache/comindly/worktrees")
        .join(&key[..12])
        .join(name(repo)))
}

/// Fetch the remote's default branch and check it out in comindly's worktree, detached, so your
/// folder and branches are never touched. Offline, the last known default branch is used.
pub fn prepare(repo: &Path) -> Result<Checkout> {
    if !repo.join(".git").exists() {
        bail!("{} is not a git repository", repo.display());
    }
    let mut stale = None;
    let branch = if has_origin(repo) {
        match remote_default(repo) {
            Ok((b, _)) => b,
            Err(e) => {
                stale = Some(format!("{e:#}"));
                default_branch(repo)?
            }
        }
    } else {
        default_branch(repo)?
    };
    // Also used for checking out: a partial clone fetches file contents then, with the same token.
    let auth = has_origin(repo)
        .then(|| git(repo, &["remote", "get-url", "origin"]).ok())
        .flatten()
        .and_then(|u| auth_for(&u));
    let target = if has_origin(repo) {
        let target = format!("origin/{branch}");
        if let Err(e) = git_auth(
            repo,
            &["fetch", "--quiet", "origin", &branch],
            auth.as_ref(),
        ) {
            let why = format!("{e:#}");
            git(repo, &["rev-parse", "--verify", "--quiet", &target])
                .map_err(|_| e.context("and no earlier fetch to fall back to"))?;
            stale = Some(why);
        }
        target
    } else {
        branch.clone()
    };
    let dir = worktree_dir(repo)?;
    let _ = git(repo, &["worktree", "prune"]);
    if dir.join(".git").exists() {
        git_auth(
            &dir,
            &["checkout", "--quiet", "--detach", "--force", &target],
            auth.as_ref(),
        )?;
        git(&dir, &["clean", "-fdq"])?;
    } else {
        std::fs::create_dir_all(dir.parent().context("worktree parent")?)?;
        let d = dir.to_string_lossy();
        git_auth(
            repo,
            &[
                "worktree", "add", "--quiet", "--detach", "--force", &d, &target,
            ],
            auth.as_ref(),
        )?;
    }
    let commit = git(&dir, &["rev-parse", "--short", "HEAD"])?;
    Ok(Checkout {
        dir,
        target,
        commit,
        stale,
    })
}

/// Remove comindly's worktree for `repo` (your folder is untouched).
pub fn remove(repo: &Path) -> Result<()> {
    let dir = worktree_dir(repo)?;
    if dir.exists() {
        let d = dir.to_string_lossy();
        git(repo, &["worktree", "remove", "--force", &d])?;
    }
    Ok(())
}

/// Git repositories under `dir`, up to `depth` levels down (not inside other repositories).
pub fn scan(dir: &Path, depth: usize) -> Vec<PathBuf> {
    let mut out = Vec::new();
    let Ok(entries) = std::fs::read_dir(dir) else {
        return out;
    };
    let mut entries: Vec<PathBuf> = entries.flatten().map(|e| e.path()).collect();
    entries.sort();
    for path in entries.into_iter().filter(|p| p.is_dir()) {
        let hidden = path
            .file_name()
            .is_some_and(|n| n.to_string_lossy().starts_with('.'));
        if hidden || path.ends_with("node_modules") {
            continue;
        }
        if path.join(".git").exists() {
            out.push(path);
        } else if depth > 1 {
            out.extend(scan(&path, depth - 1));
        }
    }
    out
}

/// Clone `url` into `parent/<name>` with your git credentials; returns the new folder.
/// comindly's own copy of `url` at `dest`: cloned once, without a checkout (indexing uses its worktree)
/// and without file contents until needed; later runs only fetch.
pub fn ensure_clone(url: &str, dest: &Path) -> Result<()> {
    if dest.join(".git").exists() || dest.join("HEAD").exists() {
        return Ok(());
    }
    let parent = dest.parent().context("clone folder")?;
    std::fs::create_dir_all(parent)?;
    let auth = auth_for(url);
    let d = dest.to_string_lossy();
    let args = [
        "clone",
        "--quiet",
        "--filter=blob:none",
        "--no-checkout",
        url,
        &d,
    ];
    if let Err(e) = git_auth(parent, &args, auth.as_ref()) {
        let _ = std::fs::remove_dir_all(dest);
        if let Some(host) = https_host(url).filter(|_| auth_failed(&format!("{e:#}"))) {
            let username = token_user(&host).into();
            return Err(NeedsToken { host, username }.into());
        }
        return Err(e);
    }
    Ok(())
}

/// A token for `host`: the environment, the server's stored tokens, or your git credential helper.
pub fn stored_auth(host: &str) -> Option<Auth> {
    if let Some(a) = env_auth(&format!("https://{host}/")) {
        return Some(a);
    }
    if crate::server::enabled() {
        return crate::server::git_token(host).map(|token| Auth {
            username: token_user(host).into(),
            token,
        });
    }
    let input = format!("protocol=https\nhost={host}\n\n");
    let out = credential("fill", &input)?;
    let field = |k: &str| {
        out.lines()
            .find_map(|l| l.strip_prefix(&format!("{k}=")))
            .map(str::to_string)
    };
    Some(Auth {
        username: field("username").unwrap_or_else(|| token_user(host).into()),
        token: field("password")?,
    })
}

/// Hand a token that worked to your git credential helper, as git does after a successful login.
pub fn approve(host: &str, auth: &Auth) {
    let input = format!(
        "protocol=https\nhost={host}\nusername={}\npassword={}\n\n",
        auth.username, auth.token
    );
    let _ = credential("approve", &input);
}

fn credential(action: &str, input: &str) -> Option<String> {
    use std::io::Write;
    let mut child = Command::new("git")
        .args(["credential", action])
        .env("GIT_TERMINAL_PROMPT", "0")
        // Only read what is stored; Git Credential Manager would otherwise open a login window.
        .env("GCM_INTERACTIVE", "never")
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::null())
        .spawn()
        .ok()?;
    child.stdin.take()?.write_all(input.as_bytes()).ok()?;
    let out = child.wait_with_output().ok()?;
    out.status
        .success()
        .then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hosts_and_token_users() {
        assert_eq!(
            https_host("https://user@gitlab.example.com:8443/g/r.git").as_deref(),
            Some("gitlab.example.com")
        );
        assert_eq!(https_host("git@github.com:o/r.git"), None);
        assert_eq!(token_user("github.com"), "x-access-token");
        assert_eq!(token_user("gitlab.com"), "oauth2");
        assert!(auth_failed(
            "fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled"
        ));
        assert!(!auth_failed("fatal: repository 'x' does not exist"));
    }

    #[test]
    fn worktree_follows_main_and_leaves_the_folder_alone() {
        let base = std::env::temp_dir().join(format!("comindly-wt-{}", std::process::id()));
        let origin = base.join("origin");
        let mine = base.join("mine");
        std::fs::create_dir_all(&origin).unwrap();
        let g = |d: &Path, a: &[&str]| git(d, a).unwrap();
        g(&origin, &["init", "--quiet", "-b", "main"]);
        g(&origin, &["config", "user.email", "t@t"]);
        g(&origin, &["config", "user.name", "t"]);
        std::fs::write(origin.join("a.py"), "x = 1\n").unwrap();
        g(&origin, &["add", "."]);
        g(&origin, &["commit", "--quiet", "-m", "one"]);
        g(
            &base,
            &["clone", "--quiet", origin.to_str().unwrap(), "mine"],
        );
        g(&mine, &["checkout", "--quiet", "-b", "feature"]);
        std::fs::write(mine.join("wip.py"), "draft\n").unwrap();

        std::fs::write(origin.join("b.py"), "y = 2\n").unwrap();
        g(&origin, &["add", "."]);
        g(&origin, &["commit", "--quiet", "-m", "two"]);

        let c = prepare(&mine).unwrap();
        assert_eq!(c.target, "origin/main");
        assert!(
            c.dir.join("b.py").exists(),
            "the worktree has the newest main"
        );
        assert!(!c.dir.join("wip.py").exists());
        assert_eq!(current_branch(&mine).as_deref(), Some("feature"));
        assert!(mine.join("wip.py").exists(), "your folder is untouched");
        g(&mine, &["checkout", "--quiet", "main"]);
        assert!(prepare(&mine).is_ok(), "a second run reuses the worktree");
        remove(&mine).unwrap();
        assert!(!c.dir.exists());
        assert_eq!(scan(&base, 1).len(), 2);
        std::fs::remove_dir_all(&base).ok();
    }

    #[test]
    fn indexes_the_remotes_default_branch_whatever_its_name() {
        let base = std::env::temp_dir().join(format!("comindly-trunk-{}", std::process::id()));
        let origin = base.join("origin");
        std::fs::create_dir_all(&origin).unwrap();
        let g = |d: &Path, a: &[&str]| git(d, a).unwrap();
        g(&origin, &["init", "--quiet", "-b", "trunk"]);
        g(&origin, &["config", "user.email", "t@t"]);
        g(&origin, &["config", "user.name", "t"]);
        std::fs::write(origin.join("a.py"), "x = 1\n").unwrap();
        g(&origin, &["add", "."]);
        g(&origin, &["commit", "--quiet", "-m", "one"]);
        let url = format!("file://{}", origin.display());
        let mine = base.join("managed").join("origin");
        ensure_clone(&url, &mine).unwrap();
        ensure_clone(&url, &mine).unwrap();
        let (branch, commit) = remote_default(&mine).unwrap();
        assert_eq!(branch, "trunk");
        let c = prepare(&mine).unwrap();
        assert_eq!(c.target, "origin/trunk");
        assert_eq!(c.commit, commit[..c.commit.len()]);
        assert!(c.dir.join("a.py").exists());
        std::fs::remove_dir_all(&base).ok();
        std::fs::remove_dir_all(c.dir.parent().unwrap()).ok();
    }
}