second-brain-adapters 0.13.4

Conversation parsers for second-brain: Claude Code and Cursor JSONL adapters
Documentation
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};

use anyhow::{Context, bail};
use chrono::{DateTime, Utc};
use second_brain_core::schema::MemoryType;
use sha2::{Digest, Sha256};
use uuid::Uuid;

use crate::document::{Document, v5, versioned};
use crate::{IngestResult, encode_project_dir};

pub const COMMIT_SOURCE: &str = "git-commit";
pub const PR_SOURCE: &str = "git-pr";

const FIELD: char = '\x1f';

pub struct Commit {
    pub sha: String,
    pub author: String,
    pub committed_at: DateTime<Utc>,
    pub message: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitHubRepo {
    pub owner: String,
    pub name: String,
}

pub struct PullRequest {
    pub number: u64,
    pub title: String,
    pub body: String,
    pub url: String,
    pub updated_at: DateTime<Utc>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Checkout {
    root: PathBuf,
}

impl Checkout {
    /// The canonical main working tree of the repository containing `dir`, so a
    /// linked worktree or a subdirectory maps to the main checkout. None outside a
    /// work tree, in a bare repo, or when git fails.
    pub fn containing(dir: &Path) -> Option<Checkout> {
        let out = git(
            dir,
            [
                "rev-parse",
                "--is-inside-work-tree",
                "--path-format=absolute",
                "--git-common-dir",
                "--show-toplevel",
            ],
        )
        .ok()?;
        if !out.status.success() {
            return None;
        }
        let stdout = String::from_utf8(out.stdout).ok()?;
        let mut lines = stdout.lines();
        if lines.next()? != "true" {
            return None;
        }
        let common = Path::new(lines.next()?);
        let toplevel = lines.next()?;
        let root = if common.file_name() == Some(OsStr::new(".git")) {
            common.parent()?
        } else {
            Path::new(toplevel)
        };
        Some(Checkout {
            root: root.canonicalize().ok()?,
        })
    }

    pub fn root(&self) -> &Path {
        &self.root
    }

    pub fn project_slug(&self) -> String {
        encode_project_dir(&self.root)
    }

    /// HEAD's sha, None for a repo without commits.
    pub fn head(&self) -> anyhow::Result<Option<String>> {
        let out = git(
            &self.root,
            ["rev-parse", "--verify", "--quiet", "HEAD^{commit}"],
        )?;
        match out.status.code() {
            Some(0) => Ok(Some(
                String::from_utf8_lossy(&out.stdout).trim().to_string(),
            )),
            Some(1) if out.stdout.is_empty() => Ok(None),
            _ => bail!(
                "git rev-parse HEAD in {}: {}",
                self.root.display(),
                String::from_utf8_lossy(&out.stderr).trim()
            ),
        }
    }

    /// Non-merge commits reachable from HEAD but not from `since`, newest first,
    /// at most `limit`. A `since` that is not an ancestor of HEAD (rewritten
    /// history, unknown object) is treated as None. Empty for a repo with no commits.
    pub fn commits_since(&self, since: Option<&str>, limit: usize) -> anyhow::Result<Vec<Commit>> {
        let Some(head) = self.head()? else {
            return Ok(Vec::new());
        };
        if limit == 0 {
            return Ok(Vec::new());
        }
        let since = match since {
            Some(sha) if self.is_ancestor_of(sha, &head)? => Some(sha),
            _ => None,
        };
        let mut args = vec![
            "log".to_string(),
            "--no-merges".to_string(),
            "-z".to_string(),
            "--format=%H%x1f%an%x1f%cI%x1f%B".to_string(),
            format!("--max-count={limit}"),
            head,
        ];
        if let Some(sha) = since {
            args.push(format!("^{sha}"));
        }
        args.push("--".to_string());
        let out = git(&self.root, &args)?;
        if !out.status.success() {
            bail!(
                "git log in {}: {}",
                self.root.display(),
                String::from_utf8_lossy(&out.stderr).trim()
            );
        }
        String::from_utf8_lossy(&out.stdout)
            .split('\0')
            .filter(|record| !record.trim().is_empty())
            .map(parse_commit)
            .collect()
    }

    fn is_ancestor_of(&self, since: &str, head: &str) -> anyhow::Result<bool> {
        if since.len() < 4 || since.len() > 64 || !since.bytes().all(|b| b.is_ascii_hexdigit()) {
            return Ok(false);
        }
        let out = git(
            &self.root,
            [
                "merge-base",
                "--is-ancestor",
                &format!("{since}^{{commit}}"),
                head,
            ],
        )?;
        Ok(out.status.success())
    }

    /// The GitHub repository of remote `origin`, else of the first remote that is
    /// on github.com. None when no remote is on github.com.
    pub fn github_remote(&self) -> Option<GitHubRepo> {
        let out = git(&self.root, ["remote"]).ok()?;
        if !out.status.success() {
            return None;
        }
        let names = String::from_utf8_lossy(&out.stdout).to_string();
        let mut names: Vec<&str> = names.lines().filter(|n| !n.is_empty()).collect();
        if let Some(i) = names.iter().position(|n| *n == "origin") {
            let origin = names.remove(i);
            names.insert(0, origin);
        }
        names.into_iter().find_map(|name| {
            let out = git(&self.root, ["remote", "get-url", "--", name]).ok()?;
            if !out.status.success() {
                return None;
            }
            parse_github_url(&String::from_utf8_lossy(&out.stdout))
        })
    }
}

fn git<I, S>(dir: &Path, args: I) -> anyhow::Result<Output>
where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    Command::new("git")
        .arg("-C")
        .arg(dir)
        .args([
            "-c",
            "core.quotepath=off",
            "-c",
            "i18n.logOutputEncoding=UTF-8",
        ])
        .args(args)
        .env("GIT_TERMINAL_PROMPT", "0")
        .env("GIT_OPTIONAL_LOCKS", "0")
        .env_remove("GIT_DIR")
        .env_remove("GIT_WORK_TREE")
        .env_remove("GIT_COMMON_DIR")
        .env_remove("GIT_INDEX_FILE")
        .output()
        .with_context(|| format!("running git in {}", dir.display()))
}

fn parse_commit(record: &str) -> anyhow::Result<Commit> {
    let record = record.trim_start_matches('\n');
    let mut fields = record.splitn(4, FIELD);
    let (Some(sha), Some(author), Some(date), Some(message)) =
        (fields.next(), fields.next(), fields.next(), fields.next())
    else {
        bail!("malformed git log record");
    };
    let committed_at = DateTime::parse_from_rfc3339(date)
        .with_context(|| format!("commit {sha} has date {date:?}"))?
        .with_timezone(&Utc);
    Ok(Commit {
        sha: sha.to_string(),
        author: author.to_string(),
        committed_at,
        message: message.to_string(),
    })
}

/// https://github.com/o/r(.git)(/), http://, git@github.com:o/r(.git),
/// ssh://git@github.com(:port)/o/r(.git). Host match is exact `github.com`
/// (case-insensitive); anything else is None.
pub fn parse_github_url(url: &str) -> Option<GitHubRepo> {
    let url = url.trim();
    let (authority, path) = if let Some(rest) =
        strip_scheme(url, &["https://", "http://", "ssh://"])
    {
        let (authority, path) = rest.split_once('/')?;
        let host_port = authority.rsplit_once('@').map_or(authority, |(_, h)| h);
        let host = match host_port.split_once(':') {
            Some((host, port)) if !port.is_empty() && port.bytes().all(|b| b.is_ascii_digit()) => {
                host
            }
            Some(_) => return None,
            None => host_port,
        };
        (host, path)
    } else if url.contains("://") {
        return None;
    } else {
        let (authority, path) = url.split_once(':')?;
        if authority.contains('/') {
            return None;
        }
        (
            authority.rsplit_once('@').map_or(authority, |(_, h)| h),
            path,
        )
    };
    if !authority.eq_ignore_ascii_case("github.com") {
        return None;
    }
    let path = path.strip_suffix('/').unwrap_or(path);
    let path = path.strip_suffix(".git").unwrap_or(path);
    let (owner, name) = path.split_once('/')?;
    if !is_segment(owner) || !is_segment(name) {
        return None;
    }
    Some(GitHubRepo {
        owner: owner.to_string(),
        name: name.to_string(),
    })
}

fn strip_scheme<'a>(url: &'a str, schemes: &[&str]) -> Option<&'a str> {
    schemes.iter().find_map(|scheme| {
        let head = url.get(..scheme.len())?;
        head.eq_ignore_ascii_case(scheme)
            .then(|| &url[scheme.len()..])
    })
}

fn is_segment(s: &str) -> bool {
    !s.is_empty()
        && s != "."
        && s != ".."
        && s.bytes()
            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.'))
}

/// The memory id depends on the sha alone, so a commit seen through any
/// checkout or machine is stored once.
pub fn commit_result(checkout: &Checkout, commit: &Commit, machine_id: &str) -> IngestResult {
    let short: String = commit.sha.chars().take(12).collect();
    let trailer = format!("Commit {short} by {}", commit.author);
    let message = commit.message.trim();
    let content = if message.is_empty() {
        trailer
    } else {
        format!("{message}\n\n{trailer}")
    };
    let root = checkout.root.display();
    Document {
        content,
        memory_id: v5(&format!("git-commit:{}", commit.sha)),
        memory_type: MemoryType::Episodic,
        source: COMMIT_SOURCE,
        source_id: format!("{root}@{}", commit.sha),
        conversation_id: v5(&format!("git-commit-repo:{root}")),
        at: Some(commit.committed_at),
        project: checkout.project_slug(),
        machine_id,
    }
    .into_result()
}

/// The memory id changes with the title, the body and the version `after` it
/// follows, so an edited PR is a new memory sharing the url as `source_id`.
pub fn pull_request_result(
    checkout: &Checkout,
    repo: &GitHubRepo,
    pr: &PullRequest,
    machine_id: &str,
    after: Option<Uuid>,
) -> IngestResult {
    let body = pr.body.trim();
    let content = if body.is_empty() {
        format!("PR #{}: {}\n\n{}", pr.number, pr.title, pr.url)
    } else {
        format!("PR #{}: {}\n\n{body}\n\n{}", pr.number, pr.title, pr.url)
    };
    let mut hasher = Sha256::new();
    hasher.update(pr.title.as_bytes());
    hasher.update([0]);
    hasher.update(pr.body.as_bytes());
    let digest = format!("{:x}", hasher.finalize());
    let full_name = format!("{}/{}", repo.owner, repo.name);
    Document {
        content,
        memory_id: v5(&versioned(
            &format!("git-pr:{full_name}#{}:{digest}", pr.number),
            after,
        )),
        memory_type: MemoryType::Episodic,
        source: PR_SOURCE,
        source_id: pr.url.clone(),
        conversation_id: v5(&format!("git-pr-repo:{full_name}")),
        at: Some(pr.updated_at),
        project: checkout.project_slug(),
        machine_id,
    }
    .into_result()
}