Skip to main content

tagit_diff/
lib.rs

1//! Implementation for the `tagit diff` command.
2
3use std::{io::Write, process::Command};
4
5use owo_colors::OwoColorize;
6use tagit_core::out;
7use tagit_workspace::{WorkspaceEntry, with_workspace_entries};
8
9/// List workspace members whose sources differ from the tag they declare.
10pub fn diff() -> anyhow::Result<()> {
11    with_workspace_entries(
12        false,
13        false,
14        |WorkspaceEntry {
15             version,
16             tag_prefix,
17             paths,
18             name,
19             ..
20         }| {
21            let tag = &*format!("{tag_prefix}{version}");
22            let no_diff = paths.is_empty()
23                || Command::new("git")
24                    .arg("diff-index")
25                    .arg("--quiet")
26                    .arg(tag)
27                    .args(paths)
28                    .status()?
29                    .success();
30            if !no_diff {
31                out!("differs", "{}", name.purple());
32                let stdout = Command::new("git")
33                    .arg("--no-pager")
34                    .arg("diff")
35                    .arg("-U0")
36                    .arg(tag)
37                    .arg("--")
38                    .args(paths)
39                    .output()?
40                    .stdout;
41                std::io::stdout().write_all(&stdout)?;
42            }
43            Ok(())
44        },
45    )
46}