Skip to content

Commit

Permalink
feat(args): allow GitLab groups with --gitlab-repo (#807)
Browse files Browse the repository at this point in the history
* feat: allow gitlab groups

* refactor(arg): improve remote value parser

---------

Co-authored-by: Orhun Parmaksız <[email protected]>
  • Loading branch information
weichweich and orhun authored Aug 24, 2024
1 parent 9831ccb commit 6fbfdb5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ log = "0.4.21"
secrecy = { version = "0.8.0", features = ["serde"] }
lazy_static = "1.5.0"
dirs = "5.0.1"
url = "2.5.2"

[profile.dev]
opt-level = 0
Expand Down
2 changes: 1 addition & 1 deletion git-cliff-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ tokio = { version = "1.39.3", features = [
"macros",
], optional = true }
futures = { version = "0.3.30", optional = true }
url = "2.5.2"
url.workspace = true
dyn-clone = "1.0.17"
urlencoding = "2.1.3"
cacache = { version = "13.0.0", features = ["mmap"], default-features = false }
Expand Down
1 change: 1 addition & 0 deletions git-cliff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ indicatif = { version = "0.17.8", optional = true }
env_logger = "=0.10.2"
pprof = { version = "0.13", optional = true }
rand = { version = "0.8.4", optional = true }
url.workspace = true

[dependencies.git-cliff-core]
version = "2.4.0" # managed by release.sh
Expand Down
37 changes: 30 additions & 7 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use glob::Pattern;
use regex::Regex;
use secrecy::SecretString;
use std::path::PathBuf;
use url::Url;

#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum Strip {
Expand Down Expand Up @@ -353,10 +354,16 @@ impl TypedValueParser for RemoteValueParser {
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
let inner = clap::builder::StringValueParser::new();
let value = inner.parse_ref(cmd, arg, value)?;
let parts = value.split('/').rev().collect::<Vec<&str>>();
if let (Some(owner), Some(repo)) = (parts.get(1), parts.first()) {
Ok(RemoteValue(Remote::new(*owner, *repo)))
let mut value = inner.parse_ref(cmd, arg, value)?;
if let Ok(url) = Url::parse(&value) {
value = url.path().trim_start_matches('/').to_string();
}
let parts = value.rsplit_once('/');
if let Some((owner, repo)) = parts {
Ok(RemoteValue(Remote::new(
owner.to_string(),
repo.to_string(),
)))
} else {
let mut err = clap::Error::new(ErrorKind::ValueValidation).with_cmd(cmd);
if let Some(arg) = arg {
Expand Down Expand Up @@ -460,9 +467,14 @@ mod tests {
OsStr::new("test/repo")
)?
);
assert!(remote_value_parser
.parse_ref(&Opt::command(), None, OsStr::new("test"))
.is_err());
assert_eq!(
RemoteValue(Remote::new("gitlab/group/test", "repo")),
remote_value_parser.parse_ref(
&Opt::command(),
None,
OsStr::new("gitlab/group/test/repo")
)?
);
assert_eq!(
RemoteValue(Remote::new("test", "testrepo")),
remote_value_parser.parse_ref(
Expand All @@ -471,6 +483,17 @@ mod tests {
OsStr::new("https://github.com/test/testrepo")
)?
);
assert_eq!(
RemoteValue(Remote::new("archlinux/packaging/packages", "arch-repro-status")),
remote_value_parser.parse_ref(
&Opt::command(),
None,
OsStr::new("https://gitlab.archlinux.org/archlinux/packaging/packages/arch-repro-status")
)?
);
assert!(remote_value_parser
.parse_ref(&Opt::command(), None, OsStr::new("test"))
.is_err());
assert!(remote_value_parser
.parse_ref(&Opt::command(), None, OsStr::new(""))
.is_err());
Expand Down

0 comments on commit 6fbfdb5

Please sign in to comment.