this repo has no description

Show diff when writing PR title and desscription

Useful for autocomplete

+29 -8
+29 -8
src/main.rs
··· 6 6 use std::{ffi::OsStr, path::Path}; 7 7 8 8 use clap::Parser; 9 - use color_eyre::eyre::{Context as _, ContextCompat}; 9 + use color_eyre::eyre::{Context as _, ContextCompat, bail}; 10 10 use dialoguer::{Confirm, Editor}; 11 11 use futures::TryStreamExt as _; 12 12 use octocrab::{Octocrab, models::pulls::PullRequest}; ··· 360 360 { 361 361 let repo_pulls = octocrab.pulls(&repo_info.owner, &repo_info.name); 362 362 363 - let pull = if let Some((title, body)) = get_pr_title_and_body()? { 363 + let pull = if let Some((title, body)) = get_pr_title_and_body(branch, target)? { 364 364 repo_pulls.create(title, branch, target).body(body) 365 365 } else { 366 366 repo_pulls.create(branch, branch, target) ··· 470 470 Ok(()) 471 471 } 472 472 473 - fn get_pr_title_and_body() -> color_eyre::Result<Option<(String, String)>> { 474 - if let Some(text) = Editor::new() 475 - .extension(".md") 476 - .edit("Enter PR title...\n\n...and description here")? 477 - { 473 + fn get_pr_title_and_body( 474 + branch: &str, 475 + target: &str, 476 + ) -> color_eyre::Result<Option<(String, String)>> { 477 + let body = std::fs::read_to_string(".github/pull_request_template.md") 478 + .unwrap_or_else(|_| "...and description here".to_owned()); 479 + 480 + let diff = command( 481 + "jj", 482 + ["diff", "--git", "-r", &format!("{target}..{branch}")], 483 + )?; 484 + 485 + let ignored_marker = "Everything below this line will be ignored"; 486 + 487 + if let Some(text) = Editor::new().extension(".jjdescription").edit(&format!( 488 + "Enter PR title...\n\n{body}\n\ 489 + JJ: {ignored_marker}\n\n{diff}" 490 + ))? { 491 + if text.trim().is_empty() { 492 + bail!("empty PR title and description"); 493 + } 494 + 478 495 let mut lines = text.lines(); 479 496 let Some(title) = lines.next() else { 480 497 return Ok(None); 481 498 }; 482 - let msg = lines.skip(1).collect::<Vec<_>>().join("\n"); 499 + let msg = lines 500 + .skip(1) 501 + .take_while(|line| !line.contains(ignored_marker)) 502 + .collect::<Vec<_>>() 503 + .join("\n"); 483 504 Ok(Some((title.to_owned(), msg))) 484 505 } else { 485 506 Ok(None)