use anyhow::{Context, Result};
use clap::Parser;
#[derive(Parser)]
pub struct InfoCommand {
#[arg(value_name = "BASE_BRANCH")]
pub base_branch: Option<String>,
}
impl InfoCommand {
pub fn execute(self) -> Result<()> {
use crate::data::{
AiInfo, BranchInfo, FieldExplanation, FileStatusInfo, RepositoryView, VersionInfo,
WorkingDirectoryInfo,
};
use crate::git::{GitRepository, RemoteInfo};
use crate::utils::ai_scratch;
crate::utils::check_git_repository()?;
let repo = GitRepository::open()
.context("Failed to open git repository. Make sure you're in a git repository.")?;
let current_branch = repo.get_current_branch().context(
"Failed to get current branch. Make sure you're not in detached HEAD state.",
)?;
let base_branch = match self.base_branch {
Some(branch) => {
if !repo.branch_exists(&branch)? {
anyhow::bail!("Base branch '{branch}' does not exist");
}
branch
}
None => {
if repo.branch_exists("main")? {
"main".to_string()
} else if repo.branch_exists("master")? {
"master".to_string()
} else {
anyhow::bail!("No default base branch found (main or master)");
}
}
};
let commit_range = format!("{base_branch}..HEAD");
let wd_status = repo.get_working_directory_status()?;
let working_directory = WorkingDirectoryInfo {
clean: wd_status.clean,
untracked_changes: wd_status
.untracked_changes
.into_iter()
.map(|fs| FileStatusInfo {
status: fs.status,
file: fs.file,
})
.collect(),
};
let remotes = RemoteInfo::get_all_remotes(repo.repository())?;
let commits = repo.get_commits_in_range(&commit_range)?;
let pr_template_result = Self::read_pr_template().ok();
let (pr_template, pr_template_location) = match pr_template_result {
Some((content, location)) => (Some(content), Some(location)),
None => (None, None),
};
let branch_prs = Self::get_branch_prs(¤t_branch)
.ok()
.filter(|prs| !prs.is_empty());
let versions = Some(VersionInfo {
omni_dev: env!("CARGO_PKG_VERSION").to_string(),
});
let ai_scratch_path =
ai_scratch::get_ai_scratch_dir().context("Failed to determine AI scratch directory")?;
let ai_info = AiInfo {
scratch: ai_scratch_path.to_string_lossy().to_string(),
};
let mut repo_view = RepositoryView {
versions,
explanation: FieldExplanation::default(),
working_directory,
remotes,
ai: ai_info,
branch_info: Some(BranchInfo {
branch: current_branch,
}),
pr_template,
pr_template_location,
branch_prs,
commits,
};
let yaml_output = repo_view.to_yaml_output()?;
println!("{yaml_output}");
Ok(())
}
pub(crate) fn read_pr_template() -> Result<(String, String)> {
use std::fs;
use std::path::Path;
let template_path = Path::new(".github/pull_request_template.md");
if template_path.exists() {
let content = fs::read_to_string(template_path)
.context("Failed to read .github/pull_request_template.md")?;
Ok((content, template_path.to_string_lossy().to_string()))
} else {
anyhow::bail!("PR template file does not exist")
}
}
pub(crate) fn get_branch_prs(branch_name: &str) -> Result<Vec<crate::data::PullRequest>> {
use serde_json::Value;
use std::process::Command;
let output = Command::new("gh")
.args([
"pr",
"list",
"--head",
branch_name,
"--json",
"number,title,state,url,body,baseRefName",
"--limit",
"50",
])
.output()
.context("Failed to execute gh command")?;
if !output.status.success() {
anyhow::bail!(
"gh command failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
let json_str = String::from_utf8_lossy(&output.stdout);
let prs_json: Value =
serde_json::from_str(&json_str).context("Failed to parse PR JSON from gh")?;
let mut prs = Vec::new();
if let Some(prs_array) = prs_json.as_array() {
for pr_json in prs_array {
if let (Some(number), Some(title), Some(state), Some(url), Some(body)) = (
pr_json.get("number").and_then(serde_json::Value::as_u64),
pr_json.get("title").and_then(|t| t.as_str()),
pr_json.get("state").and_then(|s| s.as_str()),
pr_json.get("url").and_then(|u| u.as_str()),
pr_json.get("body").and_then(|b| b.as_str()),
) {
let base = pr_json
.get("baseRefName")
.and_then(|b| b.as_str())
.unwrap_or("")
.to_string();
prs.push(crate::data::PullRequest {
number,
title: title.to_string(),
state: state.to_string(),
url: url.to_string(),
body: body.to_string(),
base,
});
}
}
}
Ok(prs)
}
}