committer-cli 0.1.1

Fast AI-powered git commit message generator
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
//! Pull request generation and GitHub CLI integration.
//!
//! This module handles the `committer pr` subcommand workflow:
//!
//! 1. Validates GitHub CLI is installed and authenticated
//! 2. Detects base branch automatically (or uses `--base`)
//! 3. Handles uncommitted changes (commit, skip, or quit)
//! 4. Generates PR title and description using LLM
//! 5. Pushes branch and creates PR via GitHub CLI
//!
//! # Example
//!
//! ```bash
//! committer pr              # Interactive PR creation
//! committer pr --yes        # Auto-create without confirmation
//! committer pr --draft      # Create as draft PR
//! committer pr --dry-run    # Preview without creating
//! ```

use console::{style, Term};
use indicatif::{ProgressBar, ProgressStyle};
use reqwest::Client;
use tokio::process::Command;

use crate::api::{stream_commit_message, stream_pr_content};
use crate::branch::PROTECTED_BRANCHES;
use crate::cli::PrArgs;
use crate::config::{get_api_key, Config};
use crate::git::{
    branch_has_merge_base, get_branch_commits, get_branch_diff, get_cached_remote_head,
    get_current_branch, get_git_diff, get_pr_changed_files, get_remote_default_branch,
    get_staged_files, get_uncommitted_changes, get_upstream_remote, push_branch_with_spinner,
    run_git_commit, stage_all_changes,
};
use crate::ui::{
    prompt_commit, prompt_pr, prompt_uncommitted_changes, CommitAction, PrAction, UncommittedAction,
};

/// Checks if the GitHub CLI (`gh`) is installed.
pub async fn check_gh_installed() -> Result<(), Box<dyn std::error::Error>> {
    let output = Command::new("gh").args(["--version"]).output().await;

    match output {
        Ok(o) if o.status.success() => Ok(()),
        _ => Err("GitHub CLI (gh) is not installed.\n\
                 Install it from: https://cli.github.com/\n\
                 Then run: gh auth login"
            .into()),
    }
}

/// Detects the default base branch for the PR.
///
/// Tries multiple strategies: GitHub CLI, cached origin/HEAD, remote query,
/// and common branch name fallbacks.
pub async fn get_default_base_branch(verbose: bool) -> Result<String, Box<dyn std::error::Error>> {
    // Strategy 1: Try gh CLI (works for GitHub repos)
    let gh_output = Command::new("gh")
        .args([
            "repo",
            "view",
            "--json",
            "defaultBranchRef",
            "-q",
            ".defaultBranchRef.name",
        ])
        .output()
        .await;

    if let Ok(output) = gh_output {
        if output.status.success() {
            let branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
            if !branch.is_empty() && branch_has_merge_base(&branch).await {
                if verbose {
                    eprintln!("— Base branch detection: gh CLI (GitHub API)");
                }
                return Ok(branch);
            }
            // gh returned a branch but no merge base - try with origin/ prefix
            let origin_branch = format!("origin/{}", branch);
            if branch_has_merge_base(&origin_branch).await {
                if verbose {
                    eprintln!("— Base branch detection: gh CLI (GitHub API, using origin/)");
                }
                return Ok(origin_branch);
            }
        }
    }

    // Strategy 2: Try cached git symbolic-ref for origin/HEAD
    if let Some(branch) = get_cached_remote_head().await {
        if branch_has_merge_base(&branch).await {
            if verbose {
                eprintln!("— Base branch detection: cached origin/HEAD ref");
            }
            return Ok(branch);
        }
        let origin_branch = format!("origin/{}", branch);
        if branch_has_merge_base(&origin_branch).await {
            if verbose {
                eprintln!("— Base branch detection: cached origin/HEAD ref (using origin/)");
            }
            return Ok(origin_branch);
        }
    }

    // Strategy 3: Query remote directly (works for any git host)
    if let Some(branch) = get_remote_default_branch().await {
        if branch_has_merge_base(&branch).await {
            if verbose {
                eprintln!("— Base branch detection: git ls-remote (queried remote)");
            }
            return Ok(branch);
        }
        let origin_branch = format!("origin/{}", branch);
        if branch_has_merge_base(&origin_branch).await {
            if verbose {
                eprintln!("— Base branch detection: git ls-remote (queried remote, using origin/)");
            }
            return Ok(origin_branch);
        }
    }

    // Strategy 4: Last resort - check common default branch names
    let common_branches = [
        "origin/main",
        "origin/master",
        "origin/mainline",
        "origin/develop",
        "main",
        "master",
        "mainline",
        "develop",
    ];

    for branch in common_branches {
        if branch_has_merge_base(branch).await {
            if verbose {
                eprintln!("— Base branch detection: fallback (checked common names)");
            }
            return Ok(branch.to_string());
        }
    }

    Err("Could not determine default base branch. Use --base <branch> to specify manually.".into())
}

/// Creates a pull request via GitHub CLI.
///
/// Returns the PR URL on success.
pub async fn create_pr(
    title: &str,
    body: &str,
    draft: bool,
) -> Result<String, Box<dyn std::error::Error>> {
    let mut args = vec!["pr", "create", "--title", title, "--body", body];
    if draft {
        args.push("--draft");
    }

    let output = Command::new("gh").args(&args).output().await?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        if stderr.contains("auth") {
            return Err(format!(
                "GitHub authentication failed.\nRun: gh auth login\n\nError: {}",
                stderr
            )
            .into());
        }
        return Err(format!("Failed to create PR: {}", stderr).into());
    }

    // gh pr create outputs the PR URL on success
    let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
    Ok(url)
}

/// Main handler for the `committer pr` subcommand.
///
/// Orchestrates the full PR creation workflow.
pub async fn handle_pr_command(
    args: PrArgs,
    config: &Config,
) -> Result<(), Box<dyn std::error::Error>> {
    // Check gh CLI is installed
    check_gh_installed().await?;

    // Get API key
    let api_key = match get_api_key() {
        Some(key) => key,
        None => {
            println!("{} No API key found", style("").red());
            println!(
                "  {} Set OPENROUTER_API_KEY environment variable",
                style("").dim()
            );
            std::process::exit(1);
        }
    };

    let verbose = args.verbose || config.verbose;
    let model = args.model.as_ref().unwrap_or(&config.model);

    // Get current branch
    let current_branch = get_current_branch().await?;

    // Check if on protected branch
    if PROTECTED_BRANCHES.contains(&current_branch.as_str()) {
        // Check for upstream remote (fork workflow)
        if get_upstream_remote().await?.is_none() {
            println!(
                "{} Cannot create PR from protected branch '{}'",
                style("").red(),
                style(&current_branch).yellow()
            );
            println!(
                "  {} Create a feature branch first: git checkout -b feat/your-feature",
                style("").dim()
            );
            std::process::exit(1);
        }
    }

    // Determine base branch
    let base_branch = match &args.base {
        Some(base) => base.clone(),
        None => get_default_base_branch(verbose).await?,
    };

    if verbose {
        eprintln!("— Base branch: {}", base_branch);
        eprintln!("— Current branch: {}", current_branch);
    }

    // Check for uncommitted changes
    let uncommitted = get_uncommitted_changes().await?;
    if !uncommitted.staged.is_empty() || !uncommitted.unstaged.is_empty() {
        match prompt_uncommitted_changes(&uncommitted) {
            UncommittedAction::Commit => {
                // Stage all and run commit flow
                stage_all_changes().await?;

                let commit_diff = get_git_diff(true, verbose).await?;
                let commit_files = get_staged_files(verbose).await?;

                if commit_diff.trim().is_empty() {
                    println!("{} No changes to commit", style("").dim());
                } else {
                    let client = Client::builder().build()?;
                    let term = Term::stdout();
                    let _ = term.hide_cursor();

                    let spinner = ProgressBar::new_spinner();
                    spinner.set_style(
                        ProgressStyle::default_spinner()
                            .tick_strings(&["", "", "", "", "", "", "", "", "", ""])
                            .template("{spinner:.cyan} Generating commit message...")
                            .unwrap(),
                    );
                    spinner.enable_steady_tick(std::time::Duration::from_millis(80));

                    let commit_msg = stream_commit_message(
                        &client,
                        &api_key,
                        model,
                        &commit_diff,
                        &commit_files,
                        &spinner,
                        verbose,
                        false,
                    )
                    .await?;

                    let _ = term.show_cursor();

                    if !commit_msg.is_empty() {
                        match prompt_commit(&commit_msg, false) {
                            CommitAction::Commit(msg) => {
                                run_git_commit(&msg).await?;
                                println!("{} Committed", style("").green());
                                println!();
                            }
                            CommitAction::Cancel => {
                                println!(
                                    "{} Commit cancelled, continuing with PR...",
                                    style("").dim()
                                );
                                println!();
                            }
                            _ => {}
                        }
                    }
                }
            }
            UncommittedAction::Skip => {
                println!("{} Skipping uncommitted changes", style("").dim());
                println!();
            }
            UncommittedAction::Quit => {
                println!("{} Cancelled", style("").dim());
                std::process::exit(0);
            }
        }
    }

    // Get commits on this branch
    let commits = get_branch_commits(&base_branch).await?;
    if commits.is_empty() {
        println!(
            "{} No commits found between '{}' and '{}'",
            style("").red(),
            style(&base_branch).dim(),
            style(&current_branch).cyan()
        );
        println!(
            "  {} Make some commits first, or check your base branch",
            style("").dim()
        );
        std::process::exit(1);
    }

    if verbose {
        eprintln!("— Found {} commits on branch", commits.len());
    }

    // Get diff and file list
    let (diff_result, files_result) = tokio::join!(
        get_branch_diff(&base_branch, verbose),
        get_pr_changed_files(&base_branch, verbose)
    );

    let diff = diff_result?;
    let files = files_result?;

    if diff.trim().is_empty() {
        println!(
            "{} No changes found between '{}' and '{}'",
            style("").red(),
            style(&base_branch).dim(),
            style(&current_branch).cyan()
        );
        std::process::exit(1);
    }

    // Create HTTP client
    let client = Client::builder().build()?;
    let term = Term::stdout();
    let _ = term.hide_cursor();

    // Stream PR content with spinner
    let spinner = ProgressBar::new_spinner();
    spinner.set_style(
        ProgressStyle::default_spinner()
            .tick_strings(&["", "", "", "", "", "", "", "", "", ""])
            .template("{spinner:.cyan} Generating PR content...")
            .unwrap(),
    );
    spinner.enable_steady_tick(std::time::Duration::from_millis(80));

    let (title, body) = stream_pr_content(
        &client, &api_key, model, &diff, &files, &commits, &spinner, verbose,
    )
    .await?;

    let _ = term.show_cursor();

    if args.dry_run {
        println!();
        println!("{} Dry run complete (PR not created)", style("").green());
        return Ok(());
    }

    if args.yes {
        // Push branch if needed
        push_branch_with_spinner(&current_branch).await?;
        let url = create_pr(&title, &body, args.draft).await?;
        println!(
            "{} PR created: {}",
            style("").green(),
            style(&url).cyan().underlined()
        );
    } else {
        match prompt_pr(&title, &body) {
            PrAction::Create(final_title, final_body) => {
                // Push branch if needed
                push_branch_with_spinner(&current_branch).await?;
                let url = create_pr(&final_title, &final_body, args.draft).await?;
                println!(
                    "{} PR created: {}",
                    style("").green(),
                    style(&url).cyan().underlined()
                );
            }
            PrAction::Cancel => {
                println!("{} Cancelled", style("").dim());
            }
        }
    }

    Ok(())
}