davit 0.4.0

Kubernetes management utilities
use anyhow::{Context, Result};
use std::path::Path;
use std::process::Command;

pub struct Git;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitPullReport {
    pub stdout: String,
    pub stderr: String,
    pub success: bool,
}

pub struct GitLogEntry {
    pub hash: String,
    pub author: String,
    pub date: String,
    pub message: String,
}

impl Git {
    /// Checks if the given directory is inside a git repository.
    pub fn is_repo(path: &Path) -> bool {
        Command::new("git")
            .arg("-C")
            .arg(path)
            .arg("rev-parse")
            .arg("--is-inside-work-tree")
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    /// Reports whether the working copy has uncommitted changes.
    pub fn is_dirty(path: &Path) -> Result<bool> {
        let output = Command::new("git")
            .arg("-C")
            .arg(path)
            .arg("status")
            .arg("--porcelain")
            .output()
            .context("Failed to execute git status")?;

        if !output.status.success() {
            return Err(anyhow::anyhow!(
                "git status failed for {}: {}",
                path.display(),
                String::from_utf8_lossy(&output.stderr).trim()
            ));
        }

        Ok(!output.stdout.is_empty())
    }

    /// Performs a git pull.
    pub fn pull(path: &Path, dry_run: bool) -> Result<GitPullReport> {
        if dry_run {
            return Ok(GitPullReport {
                stdout: format!("Dry-run: git -C {} pull\n", path.display()),
                stderr: String::new(),
                success: true,
            });
        }

        let output = Command::new("git")
            .arg("-C")
            .arg(path)
            .arg("pull")
            .output()
            .context("Failed to execute git pull")?;

        Ok(GitPullReport {
            stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
            stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
            success: output.status.success(),
        })
    }

    /// Gets the last commit that modified a specific file.
    pub fn last_commit_for_file(repo_path: &Path, file_path: &Path) -> Result<Option<GitLogEntry>> {
        let output = Command::new("git")
            .arg("-C")
            .arg(repo_path)
            .arg("log")
            .arg("-1")
            .arg("--format=%H%n%an%n%ai%n%s")
            .arg("--")
            .arg(file_path)
            .output()
            .context("Failed to execute git log")?;

        if !output.status.success() || output.stdout.is_empty() {
            return Ok(None);
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let lines: Vec<&str> = stdout.trim().lines().collect();
        if lines.len() < 4 {
            return Ok(None);
        }

        Ok(Some(GitLogEntry {
            hash: lines[0].to_string(),
            author: lines[1].to_string(),
            date: lines[2].to_string(),
            message: lines[3].to_string(),
        }))
    }

    /// Adds, commits and pushes the change.
    pub fn commit_and_push(path: &Path, message: &str, file: &Path, dry_run: bool) -> Result<()> {
        if dry_run {
            println!("Dry-run: git -C {} add {}", path.display(), file.display());
            println!(
                "Dry-run: git -C {} commit -m \"{}\"",
                path.display(),
                message
            );
            println!("Dry-run: git -C {} push", path.display());
            return Ok(());
        }

        if !Self::is_repo(path) {
            return Err(anyhow::anyhow!(
                "Not inside a git repository: {}",
                path.display()
            ));
        }

        // git add <file>
        let status = Command::new("git")
            .arg("-C")
            .arg(path)
            .arg("add")
            .arg(file)
            .status()
            .context("Failed to execute git add")?;

        if !status.success() {
            return Err(anyhow::anyhow!("git add failed"));
        }

        // git commit -m <message>
        let status = Command::new("git")
            .arg("-C")
            .arg(path)
            .arg("commit")
            .arg("-m")
            .arg(message)
            .status()
            .context("Failed to execute git commit")?;

        if !status.success() {
            return Err(anyhow::anyhow!("git commit failed"));
        }

        // git push
        let status = Command::new("git")
            .arg("-C")
            .arg(path)
            .arg("push")
            .status()
            .context("Failed to execute git push")?;

        if !status.success() {
            return Err(anyhow::anyhow!("git push failed"));
        }

        Ok(())
    }
}