Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Add since-last-tag command #14

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ enum Subcommand {
#[clap(flatten)]
files: FileSelection,
},
/// Select the (local) mainline branch and the most recent tag.
///
/// Many Git projects use tags to manage project releases and/or milestones. This command is
/// useful for viewing the set of changes since the most recent tag. This can be helpful when
/// for example, analyzing changes for determining which SemVer version to use for a next
/// release, performing risk assessments, or writing release notes.
SinceLastTag {
#[clap(long, short)]
base: Option<String>,
#[clap(flatten)]
files: FileSelection,
},
/// Select a custom set of commit-ish refs.
Select {
/// Additional branches to include.
Expand Down Expand Up @@ -150,35 +162,40 @@ fn main() {

Ok(branches)
};

let fallback_base = || -> git_glimpse::Result<_> {
let base = git_config("glimpse.base")?;
let base = base.unwrap_or_else(|| {
let default = "main";
log::debug!(
"no base branch specified in command line or configuration, falling back to \
{default:?}"
);
default.to_owned()
});
Ok(base)
};

let (branches, files) = match subcommand {
Subcommand::Stack {
base,
config,
files: FileSelection { files },
} => {
let specified_base = base
.map(Ok)
.or_else(|| git_config("glimpse.base").transpose())
.transpose()?;
let base = specified_base.as_deref().unwrap_or_else(|| {
let default = "main";
log::debug!("no base branch specified in command line or configuration, falling back to {default:?}");
default
});

let base = base.map(Ok).unwrap_or_else(fallback_base)?;
let branches = if let Some(current_branch) = current_branch()? {
let mut config = config;
if current_branch == base {
config.select_upstreams = true;
}
branches(&config, &|cmd| {
if base != current_branch {
cmd.arg(base);
cmd.arg(&base);
}
cmd.arg(&current_branch).arg("--all")
})?
} else {
let mut branches = branches(&config, &|cmd| cmd.arg(base).arg("--all"))?;
let mut branches = branches(&config, &|cmd| cmd.arg(&base).arg("--all"))?;
branches.push("HEAD".to_owned());
branches
};
Expand All @@ -188,6 +205,21 @@ fn main() {
config,
files: FileSelection { files },
} => (branches(&config, &|cmd| cmd)?, files),
Subcommand::SinceLastTag {
base,
files: FileSelection { files },
} => {
let base = base.map(Ok).unwrap_or_else(fallback_base)?;
let branches = branches(
&PresetConfig {
select_upstreams: false,
select_pushes: false,
select_last_tag: true,
},
&|cmd| cmd.arg(&base),
)?;
(branches, files)
}
Subcommand::Select {
branches,
files: FileSelection { files },
Expand Down
Loading