cli: Make patch listing deterministic

Help the users by making the patch listing consistent.  Order the
patches by their creation time and id.
This commit is contained in:
Slack Coder 2023-05-03 17:27:05 -05:00 committed by Alexis Sellier
parent 0349699200
commit 091154faed
No known key found for this signature in database
1 changed files with 14 additions and 21 deletions

View File

@ -18,16 +18,10 @@ pub fn run(
profile: &Profile, profile: &Profile,
filter: Option<patch::State>, filter: Option<patch::State>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let me = *profile.id();
let patches = Patches::open(repository)?; let patches = Patches::open(repository)?;
let all = patches.all()?;
// Patches the user authored. let mut all = Vec::new();
let mut own = Vec::new(); for patch in patches.all()? {
// Patches other users authored.
let mut other = Vec::new();
for patch in all {
let (id, patch, _) = patch?; let (id, patch, _) = patch?;
if let Some(filter) = filter { if let Some(filter) = filter {
@ -35,14 +29,10 @@ pub fn run(
continue; continue;
} }
} }
if patch.author().id().as_key() == &me { all.push((id, patch));
own.push((id, patch));
} else {
other.push((id, patch));
}
} }
if own.is_empty() && other.is_empty() { if all.is_empty() {
term::print(term::format::italic("Nothing to show.")); term::print(term::format::italic("Nothing to show."));
return Ok(()); return Ok(());
} }
@ -66,14 +56,17 @@ pub fn run(
]); ]);
table.divider(); table.divider();
let me = *profile.id();
all.sort_by(|(id1, p1), (id2, p2)| {
let is_me = (p2.author().id().as_key() == &me).cmp(&(p1.author().id().as_key() == &me));
let by_timestamp = p2.timestamp().cmp(&p1.timestamp());
let by_id = id1.cmp(id2);
is_me.then(by_timestamp).then(by_id)
});
let mut errors = Vec::new(); let mut errors = Vec::new();
for (id, patch) in &mut own { for (id, patch) in &mut all {
match row(&me, id, patch, repository) {
Ok(r) => table.push(r),
Err(e) => errors.push((patch.title(), id, e.to_string())),
}
}
for (id, patch) in &mut other {
match row(&me, id, patch, repository) { match row(&me, id, patch, repository) {
Ok(r) => table.push(r), Ok(r) => table.push(r),
Err(e) => errors.push((patch.title(), id, e.to_string())), Err(e) => errors.push((patch.title(), id, e.to_string())),