Skip to main content

tagit_diff/
lib.rs

1//! Implementation for the `tagit diff` command.
2
3use std::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                Command::new("git")
33                    .arg("--no-pager")
34                    .arg("diff")
35                    .arg("-U0")
36                    .arg("--cached")
37                    .arg(tag)
38                    .arg("--")
39                    .args(paths)
40                    .output()?;
41            }
42            Ok(())
43        },
44    )
45}