Skip to content

Commit

Permalink
refactor reflog support
Browse files Browse the repository at this point in the history
- deduplicate implementation
- fix implementation to work just like Git does (according to their source code)
- add tests
  • Loading branch information
Byron committed Dec 21, 2024
1 parent 2a9274c commit 3f5c117
Show file tree
Hide file tree
Showing 4 changed files with 683 additions and 84 deletions.
151 changes: 67 additions & 84 deletions gix/src/revision/spec/parse/delegate/revision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,98 +105,81 @@ impl delegate::Revision for Delegate<'_> {

fn reflog(&mut self, query: ReflogLookup) -> Option<()> {
self.unset_disambiguate_call();
match query {
ReflogLookup::Date(date) => {
let r = match &mut self.refs[self.idx] {
Some(r) => r.clone().attach(self.repo),
val @ None => match self.repo.head().map(crate::Head::try_into_referent) {
Ok(Some(r)) => {
*val = Some(r.clone().detach());
r
}
Ok(None) => {
self.err.push(Error::UnbornHeadsHaveNoRefLog);
return None;
}
Err(err) => {
self.err.push(err.into());
return None;
}
},
};
let r = match &mut self.refs[self.idx] {
Some(r) => r.clone().attach(self.repo),
val @ None => match self.repo.head().map(crate::Head::try_into_referent) {
Ok(Some(r)) => {
*val = Some(r.clone().detach());
r
}
Ok(None) => {
self.err.push(Error::UnbornHeadsHaveNoRefLog);
return None;
}
Err(err) => {
self.err.push(err.into());
return None;
}
},
};

let mut platform = r.log_iter();
match platform.rev().ok().flatten() {
Some(it) => match it
let mut platform = r.log_iter();
match platform.rev().ok().flatten() {
Some(mut it) => match query {
ReflogLookup::Date(date) => {
let mut last = None;
let id_to_insert = match it
.filter_map(Result::ok)
.min_by_key(|l| (date - l.signature.time).abs())
.inspect(|d| {
last = Some(if d.previous_oid.is_null() {
d.new_oid
} else {
d.previous_oid
});
})
.find(|l| l.signature.time.seconds <= date.seconds)
{
Some(closest_line) => {
self.objs[self.idx]
.get_or_insert_with(HashSet::default)
.insert(closest_line.new_oid);
Some(())
}
None => {
// do we need an another error variant?
self.err.push(Error::SingleNotFound);
None
}
},
None => {
self.err.push(Error::MissingRefLog {
reference: r.name().as_bstr().into(),
action: "lookup entry",
});
None
}
Some(closest_line) => closest_line.new_oid,
None => match last {
None => {
self.err.push(Error::EmptyReflog);
return None;
}
Some(id) => id,
},
};
self.objs[self.idx]
.get_or_insert_with(HashSet::default)
.insert(id_to_insert);
Some(())
}
}
ReflogLookup::Entry(no) => {
let r = match &mut self.refs[self.idx] {
Some(r) => r.clone().attach(self.repo),
val @ None => match self.repo.head().map(crate::Head::try_into_referent) {
Ok(Some(r)) => {
*val = Some(r.clone().detach());
r
}
Ok(None) => {
self.err.push(Error::UnbornHeadsHaveNoRefLog);
return None;
}
Err(err) => {
self.err.push(err.into());
return None;
}
},
};
let mut platform = r.log_iter();
match platform.rev().ok().flatten() {
Some(mut it) => match it.nth(no).and_then(Result::ok) {
Some(line) => {
self.objs[self.idx]
.get_or_insert_with(HashSet::default)
.insert(line.new_oid);
Some(())
}
None => {
let available = platform.rev().ok().flatten().map_or(0, Iterator::count);
self.err.push(Error::RefLogEntryOutOfRange {
reference: r.detach(),
desired: no,
available,
});
None
}
},
ReflogLookup::Entry(no) => match it.nth(no).and_then(Result::ok) {
Some(line) => {
self.objs[self.idx]
.get_or_insert_with(HashSet::default)
.insert(line.new_oid);
Some(())
}
None => {
self.err.push(Error::MissingRefLog {
reference: r.name().as_bstr().into(),
action: "lookup entry",
let available = platform.rev().ok().flatten().map_or(0, Iterator::count);
self.err.push(Error::RefLogEntryOutOfRange {
reference: r.detach(),
desired: no,
available,
});
None
}
}
},
},
None => {
self.err.push(Error::MissingRefLog {
reference: r.name().as_bstr().into(),
action: match query {
ReflogLookup::Entry(_) => "lookup reflog entry by index",
ReflogLookup::Date(_) => "lookup reflog entry by date",
},
});
None
}
}
}
Expand Down
Binary file not shown.
Loading

0 comments on commit 3f5c117

Please sign in to comment.