mure 0.2.5

A command line tool for creating and managing multiple repositories.
use crate::mure_error::Error;
use std::{path::PathBuf, process::Command};

pub fn get_default_branch(workdir: &PathBuf) -> Result<String, Error> {
    let result = match Command::new("gh")
        .args([
            "repo",
            "view",
            "--json",
            "defaultBranchRef",
            "-t",
            "{{.defaultBranchRef.name}}",
        ])
        .current_dir(workdir)
        .output()
    {
        Ok(output) => output,
        Err(e) => return Err(Error::GHCommandError(e.to_string())),
    };

    if !result.status.success() {
        let Ok(message) = String::from_utf8(result.stderr) else {
            return Err(Error::from_str("failed to get default branch"));
        };
        return Err(Error::from_str(&message));
    }

    let Ok(message) = String::from_utf8(result.stdout) else {
        return Err(Error::from_str("failed to get default branch"));
    };
    Ok(message)
}

#[cfg(test)]
mod tests {
    use std::env::current_dir;

    use super::*;
    use assay::assay;

    #[test]
    fn test_get_default_branch() {
        assert_eq!(get_default_branch(&current_dir().unwrap()).unwrap(), "main");
    }

    #[assay(
        env = [
          ("PATH", ""),
        ]
      )]
    fn test_gh_is_not_installed() {
        let result = get_default_branch(&current_dir().unwrap());
        assert!(result.is_err());
        assert_eq!(
            result.err().unwrap().to_string(),
            "No such file or directory (os error 2)"
        );
    }

    #[assay(
        env = [
          ("GH_TOKEN", ""),
        ]
      )]
    fn test_gh_token_is_not_set() {
        let result = get_default_branch(&current_dir().unwrap());
        assert!(result.is_err());
    }
}