use crate::jj::constants::ROOT_CHANGE_ID;
use crate::model::{Change, ChangeId, CommitId};
use crate::ui::views::LogView;
#[derive(Debug, Clone)]
pub struct SelectedRevision {
pub change_id: ChangeId,
pub commit_id: CommitId,
}
impl SelectedRevision {
pub fn from_log_view(log_view: &LogView) -> Option<Self> {
log_view.selected_change().map(|c| Self {
change_id: c.change_id.clone(),
commit_id: c.commit_id.clone(),
})
}
}
pub fn short_id(id: &str) -> &str {
&id[..8.min(id.len())]
}
pub fn change_id_for_commit<'a>(changes: &'a [Change], commit_id: &str) -> Option<&'a str> {
changes
.iter()
.find(|c| c.commit_id == commit_id)
.map(|c| c.change_id.as_str())
}
#[allow(dead_code)] pub fn commit_id_for_change<'a>(changes: &'a [Change], change_id: &str) -> Option<&'a str> {
changes
.iter()
.find(|c| c.change_id == change_id)
.map(|c| c.commit_id.as_str())
}
pub fn is_root_by_commit_id(changes: &[Change], commit_id: &str) -> bool {
change_id_for_commit(changes, commit_id)
.map(|cid| cid == ROOT_CHANGE_ID)
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
fn make_change(change_id: &str, commit_id: &str) -> Change {
Change {
change_id: crate::model::ChangeId::new(change_id.to_string()),
commit_id: crate::model::CommitId::new(commit_id.to_string()),
..Default::default()
}
}
#[test]
fn test_short_id_normal() {
assert_eq!(short_id("abcdef1234567890"), "abcdef12");
}
#[test]
fn test_short_id_exact_8() {
assert_eq!(short_id("abcdef12"), "abcdef12");
}
#[test]
fn test_short_id_short_input() {
assert_eq!(short_id("abc"), "abc");
}
#[test]
fn test_short_id_empty() {
assert_eq!(short_id(""), "");
}
#[test]
fn test_change_id_for_commit_found() {
let changes = vec![make_change("aaa11111", "bbb22222")];
assert_eq!(change_id_for_commit(&changes, "bbb22222"), Some("aaa11111"));
}
#[test]
fn test_change_id_for_commit_not_found() {
let changes = vec![make_change("aaa11111", "bbb22222")];
assert_eq!(change_id_for_commit(&changes, "ccc33333"), None);
}
#[test]
fn test_change_id_for_commit_empty_list() {
let changes: Vec<Change> = vec![];
assert_eq!(change_id_for_commit(&changes, "bbb22222"), None);
}
#[test]
fn test_commit_id_for_change_found() {
let changes = vec![make_change("aaa11111", "bbb22222")];
assert_eq!(commit_id_for_change(&changes, "aaa11111"), Some("bbb22222"));
}
#[test]
fn test_commit_id_for_change_not_found() {
let changes = vec![make_change("aaa11111", "bbb22222")];
assert_eq!(commit_id_for_change(&changes, "xxx99999"), None);
}
#[test]
fn test_commit_id_for_change_divergent_returns_first() {
let changes = vec![
make_change("aaa11111", "bbb22222"),
make_change("aaa11111", "ccc33333"), ];
assert_eq!(commit_id_for_change(&changes, "aaa11111"), Some("bbb22222"));
}
#[test]
fn test_is_root_by_commit_id_true() {
let changes = vec![
make_change("zzzzzzzz", "root_cid"),
make_change("normal11", "normal22"),
];
assert!(is_root_by_commit_id(&changes, "root_cid"));
}
#[test]
fn test_is_root_by_commit_id_false() {
let changes = vec![
make_change("zzzzzzzz", "root_cid"),
make_change("normal11", "normal22"),
];
assert!(!is_root_by_commit_id(&changes, "normal22"));
}
#[test]
fn test_is_root_by_commit_id_unknown() {
let changes = vec![make_change("normal11", "normal22")];
assert!(!is_root_by_commit_id(&changes, "unknown_"));
}
}