Skip to main content

crate_seq_git/
tag_create.rs

1//! Annotated tag creation and deletion via `git` subprocess.
2
3use std::path::Path;
4use std::process::Command;
5
6use crate::Error;
7
8/// Creates an annotated git tag at `HEAD` in `repo_path`.
9///
10/// Tag message is `"crate-seq: version {version}"`.
11///
12/// # Errors
13///
14/// Returns [`Error::TagCreate`] if the git subprocess exits non-zero.
15/// Returns [`Error::TempDir`] if the subprocess cannot be spawned.
16pub fn create_annotated_tag(
17    repo_path: &Path,
18    tag_name: &str,
19    version: &semver::Version,
20) -> Result<(), Error> {
21    let message = format!("crate-seq: version {version}");
22    let output = Command::new("git")
23        .args(["-C", &repo_path.to_string_lossy(), "tag", "-a", tag_name, "-m", &message, "HEAD"])
24        .output()
25        .map_err(Error::TempDir)?;
26
27    if output.status.success() {
28        return Ok(());
29    }
30
31    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_owned();
32    Err(Error::TagCreate(stderr))
33}
34
35/// Deletes tag `tag_name` from the repository.
36///
37/// Used for rollback if ledger write succeeds but tag creation fails.
38///
39/// # Errors
40///
41/// Returns [`Error::TagCreate`] if the git subprocess exits non-zero.
42/// Returns [`Error::TempDir`] if the subprocess cannot be spawned.
43pub fn delete_tag(repo_path: &Path, tag_name: &str) -> Result<(), Error> {
44    let output = Command::new("git")
45        .args(["-C", &repo_path.to_string_lossy(), "tag", "-d", tag_name])
46        .output()
47        .map_err(Error::TempDir)?;
48
49    if output.status.success() {
50        return Ok(());
51    }
52
53    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_owned();
54    Err(Error::TagCreate(stderr))
55}