commitbot 0.6.2

A CLI assistant that generates commit and PR messages from your diffs using LLMs.
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
use anyhow::{anyhow, Context, Result};
use std::process::Command as GitCommand;

/// How we want to summarize a PR.
#[derive(Debug, Clone, Copy)]
pub enum PrSummaryMode {
    /// Summarize by commits
    ByCommits,
    /// Summarize by PR numbers
    ByPrs,
}

impl PrSummaryMode {
    /// Convert the mode to a string representation.
    pub fn as_str(&self) -> &'static str {
        match self {
            PrSummaryMode::ByCommits => "commits",
            PrSummaryMode::ByPrs => "prs",
        }
    }
}

/// A commit involved in the PR range, plus any detected PR number.
#[derive(Debug, Clone)]
pub struct PrItem {
    /// Git commit hash
    pub commit_hash: String,
    /// Commit title/message
    pub title: String,
    /// Commit body/description
    pub body: String,
    /// Detected PR number from commit message
    pub pr_number: Option<u32>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteRepo {
    path: String,
    web_base_url: String,
    pub provider: GitProvider,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GitProvider {
    GitHub,
    GitLab,
    Bitbucket,
    AzureDevOps,
    Unknown,
}

impl RemoteRepo {
    pub fn repo_id(&self) -> Option<String> {
        if self.provider == GitProvider::AzureDevOps {
            let segments: Vec<&str> = self.path.split('/').filter(|s| !s.is_empty()).collect();
            if segments.len() >= 4 && segments[2] == "_git" {
                return Some(format!("{}/{}", segments[1], segments[3]));
            }
        }

        let segments: Vec<&str> = self.path.split('/').filter(|s| !s.is_empty()).collect();
        if segments.len() >= 2 {
            let owner = segments[segments.len() - 2];
            let repo = segments[segments.len() - 1];
            Some(format!("{owner}/{repo}"))
        } else {
            None
        }
    }

    pub fn commit_url(&self, commit_hash: &str) -> Option<String> {
        match self.provider {
            GitProvider::GitHub => Some(format!("{}/commit/{}", self.web_base_url, commit_hash)),
            GitProvider::GitLab => Some(format!("{}/-/commit/{}", self.web_base_url, commit_hash)),
            GitProvider::Bitbucket => {
                Some(format!("{}/commits/{}", self.web_base_url, commit_hash))
            }
            GitProvider::AzureDevOps => {
                Some(format!("{}/commit/{}", self.web_base_url, commit_hash))
            }
            GitProvider::Unknown => None,
        }
    }
}

/// Run a git command and capture stdout as String.
pub fn git_output(args: &[&str]) -> Result<String> {
    let output = GitCommand::new("git")
        .args(args)
        .output()
        .with_context(|| format!("failed to run git {:?}", args))?;

    if !output.status.success() {
        return Err(anyhow!(
            "git {:?} exited with status {:?}",
            args,
            output.status.code()
        ));
    }

    Ok(String::from_utf8_lossy(&output.stdout).to_string())
}

fn remote_origin_url() -> Option<String> {
    let output = GitCommand::new("git")
        .args(["config", "--get", "remote.origin.url"])
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    String::from_utf8(output.stdout)
        .ok()
        .map(|url| url.trim().to_string())
        .filter(|url| !url.is_empty())
}

/// Get the current branch name.
pub fn current_branch() -> Result<String> {
    let name = git_output(&["rev-parse", "--abbrev-ref", "HEAD"])?
        .trim()
        .to_string();
    Ok(name)
}

/// Get a list of staged files.
pub fn staged_files() -> Result<Vec<String>> {
    let output = git_output(&["diff", "--cached", "--name-only"])?;
    let files = output
        .lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect();
    Ok(files)
}

/// Get per-file staged diff.
pub fn staged_diff_for_file(path: &str) -> Result<String> {
    let diff = git_output(&["diff", "--cached", "--", path])?;
    Ok(diff)
}

/// Find the first PR number in a string, based on '#123' pattern.
pub fn find_first_pr_number(text: &str) -> Option<u32> {
    let bytes = text.as_bytes();
    let len = bytes.len();
    let mut i = 0;

    while i < len {
        if bytes[i] == b'#' {
            let mut j = i + 1;
            let mut value: u32 = 0;
            let mut found_digit = false;

            while j < len {
                let b = bytes[j];
                if b.is_ascii_digit() {
                    found_digit = true;
                    value = value.saturating_mul(10).saturating_add((b - b'0') as u32);
                    j += 1;
                } else {
                    break;
                }
            }

            if found_digit {
                return Some(value);
            }
        }
        i += 1;
    }

    None
}

/// Collect commits between base..from as PrItem list.
pub fn collect_pr_items(base: &str, from: &str) -> Result<Vec<PrItem>> {
    let range = format!("{base}..{from}");
    let log_output = git_output(&[
        "log",
        "--reverse",
        "--pretty=format:%H%n%s%n%b%n---END---",
        &range,
    ])?;

    if log_output.trim().is_empty() {
        return Ok(vec![]);
    }

    let mut items = Vec::new();

    for block in log_output.split("\n---END---") {
        let block = block.trim();
        if block.is_empty() {
            continue;
        }

        let mut lines = block.lines();
        let hash = match lines.next() {
            Some(h) => h.trim().to_string(),
            None => continue,
        };
        let title = lines.next().unwrap_or("").trim().to_string();
        let body = lines.collect::<Vec<_>>().join("\n");

        let mut pr_number = find_first_pr_number(&title);
        if pr_number.is_none() {
            pr_number = find_first_pr_number(&body);
        }

        items.push(PrItem {
            commit_hash: hash,
            title,
            body,
            pr_number,
        });
    }

    Ok(items)
}

/// Split a combined diff string into (path, diff) pairs, one per file.
/// Handles both `diff --git` headers and legacy `--- a/` headers.
pub fn split_diff_by_file(diff: &str) -> Vec<(String, String)> {
    let mut results = Vec::new();
    let mut current_path: Option<String> = None;
    let mut current_lines: Vec<&str> = Vec::new();

    for line in diff.lines() {
        if line.starts_with("diff --git ") {
            // Save previous file
            if let Some(path) = current_path.take() {
                results.push((path, current_lines.join("\n")));
            }
            current_lines = vec![line];

            // Extract path from "diff --git a/foo b/foo"
            let path = line
                .split_whitespace()
                .last()
                .and_then(|s| s.strip_prefix("b/"))
                .unwrap_or("")
                .to_string();
            current_path = Some(path);
        } else {
            current_lines.push(line);
        }
    }

    // Flush the last file
    if let Some(path) = current_path.take() {
        results.push((path, current_lines.join("\n")));
    }

    results
}

/// Stage all new, modified, and deleted files
pub fn stage_all() -> Result<()> {
    log::info!("Staging all changes");
    git_output(&["add", "-A"])?;
    Ok(())
}

/// Try to derive a repo identifier like "owner/repo" from `git remote.origin.url`.
pub fn detect_repo_id() -> Option<String> {
    let remote = remote_origin_url()?;
    parse_remote_repo(&remote)?.repo_id()
}

pub fn format_pr_commit_appendix(items: &[PrItem]) -> String {
    if items.is_empty() {
        return String::new();
    }

    let remote = remote_origin_url().and_then(|url| parse_remote_repo(&url));
    format_pr_commit_appendix_with_remote(items, remote.as_ref())
}

pub fn format_pr_commit_appendix_with_remote(
    items: &[PrItem],
    remote: Option<&RemoteRepo>,
) -> String {
    let mut out = String::from("Commits in this PR:\n");
    for item in items {
        let short = short_commit_hash(&item.commit_hash);
        let title = item.title.trim();

        match remote.and_then(|repo| repo.commit_url(&item.commit_hash)) {
            Some(url) => {
                out.push_str(&format!("- [`{short}`]({url}) {title}\n"));
            }
            None => {
                out.push_str(&format!("- `{short}` {title}\n"));
            }
        }
    }

    out.trim_end().to_string()
}

pub fn short_commit_hash(hash: &str) -> String {
    hash.chars().take(7).collect()
}

pub fn parse_remote_repo(url: &str) -> Option<RemoteRepo> {
    let trimmed = url.trim().trim_end_matches(".git");
    if trimmed.is_empty() {
        return None;
    }

    if let Some(rest) = trimmed.strip_prefix("git@ssh.dev.azure.com:v3/") {
        let parts: Vec<&str> = rest.split('/').filter(|s| !s.is_empty()).collect();
        if parts.len() >= 3 {
            let org = parts[0];
            let project = parts[1];
            let repo = parts[2];
            return Some(RemoteRepo {
                path: format!("{org}/{project}/_git/{repo}"),
                web_base_url: format!("https://dev.azure.com/{org}/{project}/_git/{repo}"),
                provider: GitProvider::AzureDevOps,
            });
        }
    }

    if let Some(rest) = trimmed.strip_prefix("ssh://") {
        return parse_remote_repo_from_authority_path(rest);
    }

    if let Some((host_part, path_part)) = trimmed.split_once("://") {
        let _scheme = host_part;
        return parse_remote_repo_from_authority_path(path_part);
    }

    if let Some((user_host, path)) = trimmed.split_once(':') {
        let host = user_host.rsplit('@').next()?.to_string();
        return build_remote_repo(host, path.trim_start_matches('/'));
    }

    None
}

fn parse_remote_repo_from_authority_path(input: &str) -> Option<RemoteRepo> {
    let without_user = input.rsplit('@').next().unwrap_or(input);
    let (host, path) = without_user.split_once('/')?;
    build_remote_repo(host.to_string(), path)
}

fn build_remote_repo(host: String, path: &str) -> Option<RemoteRepo> {
    let clean_path = path.trim_matches('/').to_string();
    if clean_path.is_empty() {
        return None;
    }

    let provider = if host.contains("github") {
        GitProvider::GitHub
    } else if host.contains("gitlab") {
        GitProvider::GitLab
    } else if host.contains("bitbucket") {
        GitProvider::Bitbucket
    } else if host.contains("azure")
        || host == "dev.azure.com"
        || host.ends_with(".visualstudio.com")
    {
        GitProvider::AzureDevOps
    } else {
        GitProvider::Unknown
    };

    let web_base_url = match provider {
        GitProvider::AzureDevOps => normalize_azure_web_base(&host, &clean_path)?,
        _ => format!("https://{host}/{clean_path}"),
    };

    Some(RemoteRepo {
        path: clean_path,
        web_base_url,
        provider,
    })
}

fn normalize_azure_web_base(host: &str, path: &str) -> Option<String> {
    let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();

    if host == "dev.azure.com" && segments.len() >= 4 && segments[2] == "_git" {
        return Some(format!(
            "https://dev.azure.com/{}/{}/_git/{}",
            segments[0], segments[1], segments[3]
        ));
    }

    if host.ends_with(".visualstudio.com") {
        let org = host.trim_end_matches(".visualstudio.com");
        if segments.len() >= 3 && segments[1] == "_git" {
            return Some(format!(
                "https://{}.visualstudio.com/{}/_git/{}",
                org, segments[0], segments[2]
            ));
        }
    }

    None
}