Skip to main content

second_brain_adapters/
git.rs

1use std::ffi::OsStr;
2use std::path::{Path, PathBuf};
3use std::process::{Command, Output};
4
5use anyhow::{Context, bail};
6use chrono::{DateTime, Utc};
7use second_brain_core::schema::MemoryType;
8use sha2::{Digest, Sha256};
9use uuid::Uuid;
10
11use crate::document::{Document, v5, versioned};
12use crate::{IngestResult, encode_project_dir};
13
14pub const COMMIT_SOURCE: &str = "git-commit";
15pub const PR_SOURCE: &str = "git-pr";
16
17const FIELD: char = '\x1f';
18
19pub struct Commit {
20    pub sha: String,
21    pub author: String,
22    pub committed_at: DateTime<Utc>,
23    pub message: String,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct GitHubRepo {
28    pub owner: String,
29    pub name: String,
30}
31
32pub struct PullRequest {
33    pub number: u64,
34    pub title: String,
35    pub body: String,
36    pub url: String,
37    pub updated_at: DateTime<Utc>,
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct Checkout {
42    root: PathBuf,
43}
44
45impl Checkout {
46    /// The canonical main working tree of the repository containing `dir`, so a
47    /// linked worktree or a subdirectory maps to the main checkout. None outside a
48    /// work tree, in a bare repo, or when git fails.
49    pub fn containing(dir: &Path) -> Option<Checkout> {
50        let out = git(
51            dir,
52            [
53                "rev-parse",
54                "--is-inside-work-tree",
55                "--path-format=absolute",
56                "--git-common-dir",
57                "--show-toplevel",
58            ],
59        )
60        .ok()?;
61        if !out.status.success() {
62            return None;
63        }
64        let stdout = String::from_utf8(out.stdout).ok()?;
65        let mut lines = stdout.lines();
66        if lines.next()? != "true" {
67            return None;
68        }
69        let common = Path::new(lines.next()?);
70        let toplevel = lines.next()?;
71        let root = if common.file_name() == Some(OsStr::new(".git")) {
72            common.parent()?
73        } else {
74            Path::new(toplevel)
75        };
76        Some(Checkout {
77            root: root.canonicalize().ok()?,
78        })
79    }
80
81    pub fn root(&self) -> &Path {
82        &self.root
83    }
84
85    pub fn project_slug(&self) -> String {
86        encode_project_dir(&self.root)
87    }
88
89    /// HEAD's sha, None for a repo without commits.
90    pub fn head(&self) -> anyhow::Result<Option<String>> {
91        let out = git(
92            &self.root,
93            ["rev-parse", "--verify", "--quiet", "HEAD^{commit}"],
94        )?;
95        match out.status.code() {
96            Some(0) => Ok(Some(
97                String::from_utf8_lossy(&out.stdout).trim().to_string(),
98            )),
99            Some(1) if out.stdout.is_empty() => Ok(None),
100            _ => bail!(
101                "git rev-parse HEAD in {}: {}",
102                self.root.display(),
103                String::from_utf8_lossy(&out.stderr).trim()
104            ),
105        }
106    }
107
108    /// Non-merge commits reachable from HEAD but not from `since`, newest first,
109    /// at most `limit`. A `since` that is not an ancestor of HEAD (rewritten
110    /// history, unknown object) is treated as None. Empty for a repo with no commits.
111    pub fn commits_since(&self, since: Option<&str>, limit: usize) -> anyhow::Result<Vec<Commit>> {
112        let Some(head) = self.head()? else {
113            return Ok(Vec::new());
114        };
115        if limit == 0 {
116            return Ok(Vec::new());
117        }
118        let since = match since {
119            Some(sha) if self.is_ancestor_of(sha, &head)? => Some(sha),
120            _ => None,
121        };
122        let mut args = vec![
123            "log".to_string(),
124            "--no-merges".to_string(),
125            "-z".to_string(),
126            "--format=%H%x1f%an%x1f%cI%x1f%B".to_string(),
127            format!("--max-count={limit}"),
128            head,
129        ];
130        if let Some(sha) = since {
131            args.push(format!("^{sha}"));
132        }
133        args.push("--".to_string());
134        let out = git(&self.root, &args)?;
135        if !out.status.success() {
136            bail!(
137                "git log in {}: {}",
138                self.root.display(),
139                String::from_utf8_lossy(&out.stderr).trim()
140            );
141        }
142        String::from_utf8_lossy(&out.stdout)
143            .split('\0')
144            .filter(|record| !record.trim().is_empty())
145            .map(parse_commit)
146            .collect()
147    }
148
149    fn is_ancestor_of(&self, since: &str, head: &str) -> anyhow::Result<bool> {
150        if since.len() < 4 || since.len() > 64 || !since.bytes().all(|b| b.is_ascii_hexdigit()) {
151            return Ok(false);
152        }
153        let out = git(
154            &self.root,
155            [
156                "merge-base",
157                "--is-ancestor",
158                &format!("{since}^{{commit}}"),
159                head,
160            ],
161        )?;
162        Ok(out.status.success())
163    }
164
165    /// The GitHub repository of remote `origin`, else of the first remote that is
166    /// on github.com. None when no remote is on github.com.
167    pub fn github_remote(&self) -> Option<GitHubRepo> {
168        let out = git(&self.root, ["remote"]).ok()?;
169        if !out.status.success() {
170            return None;
171        }
172        let names = String::from_utf8_lossy(&out.stdout).to_string();
173        let mut names: Vec<&str> = names.lines().filter(|n| !n.is_empty()).collect();
174        if let Some(i) = names.iter().position(|n| *n == "origin") {
175            let origin = names.remove(i);
176            names.insert(0, origin);
177        }
178        names.into_iter().find_map(|name| {
179            let out = git(&self.root, ["remote", "get-url", "--", name]).ok()?;
180            if !out.status.success() {
181                return None;
182            }
183            parse_github_url(&String::from_utf8_lossy(&out.stdout))
184        })
185    }
186}
187
188fn git<I, S>(dir: &Path, args: I) -> anyhow::Result<Output>
189where
190    I: IntoIterator<Item = S>,
191    S: AsRef<OsStr>,
192{
193    Command::new("git")
194        .arg("-C")
195        .arg(dir)
196        .args([
197            "-c",
198            "core.quotepath=off",
199            "-c",
200            "i18n.logOutputEncoding=UTF-8",
201        ])
202        .args(args)
203        .env("GIT_TERMINAL_PROMPT", "0")
204        .env("GIT_OPTIONAL_LOCKS", "0")
205        .env_remove("GIT_DIR")
206        .env_remove("GIT_WORK_TREE")
207        .env_remove("GIT_COMMON_DIR")
208        .env_remove("GIT_INDEX_FILE")
209        .output()
210        .with_context(|| format!("running git in {}", dir.display()))
211}
212
213fn parse_commit(record: &str) -> anyhow::Result<Commit> {
214    let record = record.trim_start_matches('\n');
215    let mut fields = record.splitn(4, FIELD);
216    let (Some(sha), Some(author), Some(date), Some(message)) =
217        (fields.next(), fields.next(), fields.next(), fields.next())
218    else {
219        bail!("malformed git log record");
220    };
221    let committed_at = DateTime::parse_from_rfc3339(date)
222        .with_context(|| format!("commit {sha} has date {date:?}"))?
223        .with_timezone(&Utc);
224    Ok(Commit {
225        sha: sha.to_string(),
226        author: author.to_string(),
227        committed_at,
228        message: message.to_string(),
229    })
230}
231
232/// https://github.com/o/r(.git)(/), http://, git@github.com:o/r(.git),
233/// ssh://git@github.com(:port)/o/r(.git). Host match is exact `github.com`
234/// (case-insensitive); anything else is None.
235pub fn parse_github_url(url: &str) -> Option<GitHubRepo> {
236    let url = url.trim();
237    let (authority, path) = if let Some(rest) =
238        strip_scheme(url, &["https://", "http://", "ssh://"])
239    {
240        let (authority, path) = rest.split_once('/')?;
241        let host_port = authority.rsplit_once('@').map_or(authority, |(_, h)| h);
242        let host = match host_port.split_once(':') {
243            Some((host, port)) if !port.is_empty() && port.bytes().all(|b| b.is_ascii_digit()) => {
244                host
245            }
246            Some(_) => return None,
247            None => host_port,
248        };
249        (host, path)
250    } else if url.contains("://") {
251        return None;
252    } else {
253        let (authority, path) = url.split_once(':')?;
254        if authority.contains('/') {
255            return None;
256        }
257        (
258            authority.rsplit_once('@').map_or(authority, |(_, h)| h),
259            path,
260        )
261    };
262    if !authority.eq_ignore_ascii_case("github.com") {
263        return None;
264    }
265    let path = path.strip_suffix('/').unwrap_or(path);
266    let path = path.strip_suffix(".git").unwrap_or(path);
267    let (owner, name) = path.split_once('/')?;
268    if !is_segment(owner) || !is_segment(name) {
269        return None;
270    }
271    Some(GitHubRepo {
272        owner: owner.to_string(),
273        name: name.to_string(),
274    })
275}
276
277fn strip_scheme<'a>(url: &'a str, schemes: &[&str]) -> Option<&'a str> {
278    schemes.iter().find_map(|scheme| {
279        let head = url.get(..scheme.len())?;
280        head.eq_ignore_ascii_case(scheme)
281            .then(|| &url[scheme.len()..])
282    })
283}
284
285fn is_segment(s: &str) -> bool {
286    !s.is_empty()
287        && s != "."
288        && s != ".."
289        && s.bytes()
290            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.'))
291}
292
293/// The memory id depends on the sha alone, so a commit seen through any
294/// checkout or machine is stored once.
295pub fn commit_result(checkout: &Checkout, commit: &Commit, machine_id: &str) -> IngestResult {
296    let short: String = commit.sha.chars().take(12).collect();
297    let trailer = format!("Commit {short} by {}", commit.author);
298    let message = commit.message.trim();
299    let content = if message.is_empty() {
300        trailer
301    } else {
302        format!("{message}\n\n{trailer}")
303    };
304    let root = checkout.root.display();
305    Document {
306        content,
307        memory_id: v5(&format!("git-commit:{}", commit.sha)),
308        memory_type: MemoryType::Episodic,
309        source: COMMIT_SOURCE,
310        source_id: format!("{root}@{}", commit.sha),
311        conversation_id: v5(&format!("git-commit-repo:{root}")),
312        at: Some(commit.committed_at),
313        project: checkout.project_slug(),
314        machine_id,
315    }
316    .into_result()
317}
318
319/// The memory id changes with the title, the body and the version `after` it
320/// follows, so an edited PR is a new memory sharing the url as `source_id`.
321pub fn pull_request_result(
322    checkout: &Checkout,
323    repo: &GitHubRepo,
324    pr: &PullRequest,
325    machine_id: &str,
326    after: Option<Uuid>,
327) -> IngestResult {
328    let body = pr.body.trim();
329    let content = if body.is_empty() {
330        format!("PR #{}: {}\n\n{}", pr.number, pr.title, pr.url)
331    } else {
332        format!("PR #{}: {}\n\n{body}\n\n{}", pr.number, pr.title, pr.url)
333    };
334    let mut hasher = Sha256::new();
335    hasher.update(pr.title.as_bytes());
336    hasher.update([0]);
337    hasher.update(pr.body.as_bytes());
338    let digest = format!("{:x}", hasher.finalize());
339    let full_name = format!("{}/{}", repo.owner, repo.name);
340    Document {
341        content,
342        memory_id: v5(&versioned(
343            &format!("git-pr:{full_name}#{}:{digest}", pr.number),
344            after,
345        )),
346        memory_type: MemoryType::Episodic,
347        source: PR_SOURCE,
348        source_id: pr.url.clone(),
349        conversation_id: v5(&format!("git-pr-repo:{full_name}")),
350        at: Some(pr.updated_at),
351        project: checkout.project_slug(),
352        machine_id,
353    }
354    .into_result()
355}