From 508a1002682c6ef204a16fbe1a2986d85dc7cb5c Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sun, 31 May 2020 14:13:14 -0400 Subject: [PATCH] implement automagic tagging, uploading and the like for release --- src/cmd/release.rs | 11 ++++++++++- src/git.rs | 28 ++++++++++++++++++++-------- src/main.rs | 2 +- src/version.rs | 2 +- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/cmd/release.rs b/src/cmd/release.rs index 95b8d3f..9141252 100644 --- a/src/cmd/release.rs +++ b/src/cmd/release.rs @@ -5,9 +5,18 @@ use std::path::PathBuf; pub(crate) async fn run( common: Common, fname: PathBuf, - tag: String, + tag: Option, rm: ReleaseMeta, ) -> Result<()> { + let repo = git2::Repository::open(".")?; + let tag = tag.unwrap_or(version::read_version("VERSION".into())?); + let desc = changelog::read(fname.clone(), tag.clone())?; + + if !git::has_tag(&repo, tag.clone())? { + git::tag_version(&repo, tag.clone(), desc.clone())?; + let _ = git::push_tags(&repo); + } + let desc = changelog::read(fname, tag.clone())?; let cli = client(&common)?; let cr = CreateRelease { diff --git a/src/git.rs b/src/git.rs index ac8f7f2..de7447b 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,9 +1,22 @@ use anyhow::Result; -use git2::Repository; -use std::path::PathBuf; +use git2::{Repository, Signature}; -pub(crate) fn has_tag(repo_path: PathBuf, tag: String) -> Result { - let repo = Repository::init(repo_path)?; +pub(crate) fn push_tags(repo: &Repository) -> Result<()> { + let mut remote = repo.find_remote("origin")?; + remote.connect(git2::Direction::Push)?; + remote.push(&["refs/tags/*:refs/tags/*"], None)?; + Ok(()) +} + +pub(crate) fn tag_version(repo: &Repository, tag: String, desc: String) -> Result<()> { + let sig = &Signature::now("Gitea Release Tool", "gitea-release@tulpa.dev")?; + let obj = repo.revparse_single("HEAD")?; + repo.tag(&tag, &obj, &sig, &desc, false)?; + + Ok(()) +} + +pub(crate) fn has_tag(repo: &Repository, tag: String) -> Result { let tags = repo.tag_names(Some(&tag))?; for tag_obj in tags.iter() { @@ -40,7 +53,7 @@ mod tests { let oid = index.write_tree()?; let tree = repo.find_tree(oid)?; - let sig = Signature::now("Testificate", "test@ifica.te")?; + let sig = &Signature::now("Gitea Release Tool", "gitea-release@tulpa.dev")?; repo.commit( Some("HEAD"), &sig, @@ -50,9 +63,8 @@ mod tests { &[], )?; - let obj = repo.revparse_single("HEAD")?; - repo.tag(TAG, &obj, &sig, &format!("version {}", TAG), false)?; - assert!(super::has_tag(dir.path().into(), TAG.into())?); + super::tag_version(&repo, TAG.into(), format!("version {}", TAG))?; + assert!(super::has_tag(&repo, TAG.into())?); Ok(()) } diff --git a/src/main.rs b/src/main.rs index 7d141ec..bf66167 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,7 +106,7 @@ pub(crate) enum Cmd { #[structopt(short, long, default_value = "./CHANGELOG.md")] changelog: PathBuf, /// The version tag to operate on - tag: String, + tag: Option, #[structopt(flatten)] release_meta: ReleaseMeta, }, diff --git a/src/version.rs b/src/version.rs index 2a8e6d2..eb1ff10 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,7 +1,7 @@ use anyhow::Result; use std::{fs, path::PathBuf}; -fn read_version(fname: PathBuf) -> Result { +pub(crate) fn read_version(fname: PathBuf) -> Result { let version = fs::read_to_string(fname)?; Ok(version.trim().into()) }