Skip to main content

wisp/runtime/
git.rs

1use crate::session::workspace_status::{WorkspaceStatus, home_relative_path};
2use std::path::Path;
3
4pub async fn resolve_workspace_status(cwd: &Path) -> WorkspaceStatus {
5    let git_ref = match git_ref(cwd, &["branch", "--show-current"]).await {
6        Some(reference) => Some(reference),
7        None => git_ref(cwd, &["rev-parse", "--short", "HEAD"]).await,
8    };
9    WorkspaceStatus::new(home_relative_path(cwd), git_ref)
10}
11
12async fn git_ref(cwd: &Path, args: &[&str]) -> Option<String> {
13    let output = tokio::process::Command::new("git").args(args).current_dir(cwd).output().await.ok()?;
14    if !output.status.success() {
15        return None;
16    }
17    let text = String::from_utf8_lossy(&output.stdout).trim().to_string();
18    (!text.is_empty()).then_some(text)
19}