8000 fix: list-remote --filter --latest panics when filter has no results by julescubtree · Pull Request #1184 · Schniz/fnm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: list-remote --filter --latest panics when filter has no results #1184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/plenty-flies-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fnm": patch
---

fix panic when list-remote --filter --latest has no results
31 changes: 30 additions & 1 deletion src/commands/ls_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ pub enum SortingMethod {
Ascending,
}

/// Drain all elements but the last one
fn truncate_except_latest<T>(list: &mut Vec<T>) {
let len = list.len();
if len > 1 {
list.swap(0, len - 1);
list.truncate(1);
}
}

impl super::command::Command for LsRemote {
type Error = Error;

Expand All @@ -57,7 +66,7 @@ impl super::command::Command for LsRemote {
}

if self.latest {
all_versions.drain(0..all_versions.len() - 1);
truncate_except_latest(&mut all_versions);
}

if let SortingMethod::Descending = self.sort {
Expand Down Expand Up @@ -89,3 +98,23 @@ pub enum Error {
source: crate::http::Error,
},
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_truncate_except_latest() {
let mut list = vec![1, 2, 3, 4, 5];
truncate_except_latest(&mut list);
assert_eq!(list, vec![5]);

let mut list: Vec<()> = vec![];
truncate_except_latest(&mut list);
assert_eq!(list, vec![]);

let mut list = vec![1];
truncate_except_latest(&mut list);
assert_eq!(list, vec![1]);
}
}
Loading
0