Skip to main content

pgit_native/
publish.rs

1use std::path::Path;
2use std::process::Command;
3
4use anyhow::{Context, Result};
5
6/// Stage all changes, commit with `message`, and push to `remote`.
7pub fn commit_and_push(repo: &Path, message: &str, remote: &str) -> Result<()> {
8    git(repo, &["add", "."])?;
9    git(repo, &[
10        "-c", "user.name=pgit",
11        "-c", "user.email=pgit@users.noreply.github.com",
12        "commit", "--allow-empty", "-m", message,
13    ])?;
14    git(repo, &["push", remote, "HEAD"])?;
15    Ok(())
16}
17
18/// Create an annotated tag and push it.
19pub fn tag_and_push(repo: &Path, tag: &str, message: &str, remote: &str) -> Result<()> {
20    git(repo, &["tag", "-a", tag, "-m", message])?;
21    git(repo, &["push", remote, tag])?;
22    Ok(())
23}
24
25fn git(repo: &Path, args: &[&str]) -> Result<()> {
26    let output = Command::new("git")
27        .args(args)
28        .current_dir(repo)
29        .env_remove("GIT_DIR")
30        .env_remove("GIT_WORK_TREE")
31        .env_remove("GIT_INDEX_FILE")
32        .output()
33        .context("failed to run git")?;
34
35    if !output.status.success() {
36        let stderr = String::from_utf8_lossy(&output.stderr);
37        anyhow::bail!("git {:?} failed: {}", args, stderr.trim());
38    }
39    Ok(())
40}