cleanup: propagate errors from Commit::predecessors()

This commit is contained in:
Martin von Zweigbergk 2024-05-05 10:03:30 -07:00 committed by Martin von Zweigbergk
parent 0a758e7024
commit ee9d3271c1
4 changed files with 18 additions and 20 deletions

View file

@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use itertools::Itertools;
use jj_lib::commit::Commit; use jj_lib::commit::Commit;
use jj_lib::dag_walk::topo_order_reverse; use jj_lib::dag_walk::topo_order_reverse_ok;
use jj_lib::matchers::EverythingMatcher; use jj_lib::matchers::EverythingMatcher;
use jj_lib::rewrite::rebase_to_dest_parent; use jj_lib::rewrite::rebase_to_dest_parent;
use tracing::instrument; use tracing::instrument;
@ -102,11 +103,11 @@ pub(crate) fn cmd_obslog(
let mut formatter = ui.stdout_formatter(); let mut formatter = ui.stdout_formatter();
let formatter = formatter.as_mut(); let formatter = formatter.as_mut();
let mut commits = topo_order_reverse( let mut commits = topo_order_reverse_ok(
vec![start_commit], vec![Ok(start_commit)],
|commit: &Commit| commit.id().clone(), |commit: &Commit| commit.id().clone(),
|commit: &Commit| commit.predecessors(), |commit: &Commit| commit.predecessors().collect_vec(),
); )?;
if let Some(n) = args.limit { if let Some(n) = args.limit {
commits.truncate(n); commits.truncate(n);
} }
@ -114,8 +115,8 @@ pub(crate) fn cmd_obslog(
let mut graph = get_graphlog(command.settings(), formatter.raw()); let mut graph = get_graphlog(command.settings(), formatter.raw());
for commit in commits { for commit in commits {
let mut edges = vec![]; let mut edges = vec![];
for predecessor in &commit.predecessors() { for predecessor in commit.predecessors() {
edges.push(Edge::Direct(predecessor.id().clone())); edges.push(Edge::Direct(predecessor?.id().clone()));
} }
let mut buffer = vec![]; let mut buffer = vec![];
with_content_format.write_graph_text( with_content_format.write_graph_text(
@ -164,13 +165,13 @@ fn show_predecessor_patch(
commit: &Commit, commit: &Commit,
diff_formats: &[DiffFormat], diff_formats: &[DiffFormat],
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let predecessors = commit.predecessors(); let mut predecessors = commit.predecessors();
let predecessor = match predecessors.first() { let predecessor = match predecessors.next() {
Some(predecessor) => predecessor, Some(predecessor) => predecessor?,
None => return Ok(()), None => return Ok(()),
}; };
let predecessor_tree = let predecessor_tree =
rebase_to_dest_parent(workspace_command.repo().as_ref(), predecessor, commit)?; rebase_to_dest_parent(workspace_command.repo().as_ref(), &predecessor, commit)?;
let tree = commit.tree()?; let tree = commit.tree()?;
diff_util::show_diff( diff_util::show_diff(
ui, ui,

View file

@ -92,12 +92,11 @@ impl Commit {
&self.data.predecessors &self.data.predecessors
} }
pub fn predecessors(&self) -> Vec<Commit> { pub fn predecessors(&self) -> impl Iterator<Item = BackendResult<Commit>> + '_ {
self.data self.data
.predecessors .predecessors
.iter() .iter()
.map(|id| self.store.get_commit(id).unwrap()) .map(|id| self.store.get_commit(id))
.collect()
} }
pub fn tree(&self) -> BackendResult<MergedTree> { pub fn tree(&self) -> BackendResult<MergedTree> {

View file

@ -80,7 +80,7 @@ fn test_initial(backend: TestRepoBackend) {
let parents: Vec<_> = commit.parents().try_collect().unwrap(); let parents: Vec<_> = commit.parents().try_collect().unwrap();
assert_eq!(parents, vec![store.root_commit()]); assert_eq!(parents, vec![store.root_commit()]);
assert_eq!(commit.predecessors(), vec![]); assert!(commit.predecessors().next().is_none());
assert_eq!(commit.description(), "description"); assert_eq!(commit.description(), "description");
assert_eq!(commit.author(), &author_signature); assert_eq!(commit.author(), &author_signature);
assert_eq!(commit.committer(), &committer_signature); assert_eq!(commit.committer(), &committer_signature);
@ -156,10 +156,8 @@ fn test_rewrite(backend: TestRepoBackend) {
tx.commit("test"); tx.commit("test");
let parents: Vec<_> = rewritten_commit.parents().try_collect().unwrap(); let parents: Vec<_> = rewritten_commit.parents().try_collect().unwrap();
assert_eq!(parents, vec![store.root_commit()]); assert_eq!(parents, vec![store.root_commit()]);
assert_eq!( let predecessors: Vec<_> = rewritten_commit.predecessors().try_collect().unwrap();
rewritten_commit.predecessors(), assert_eq!(predecessors, vec![initial_commit.clone()]);
vec![initial_commit.clone()]
);
assert_eq!(rewritten_commit.author().name, settings.user_name()); assert_eq!(rewritten_commit.author().name, settings.user_name());
assert_eq!(rewritten_commit.author().email, settings.user_email()); assert_eq!(rewritten_commit.author().email, settings.user_email());
assert_eq!( assert_eq!(

View file

@ -173,7 +173,7 @@ fn test_init_checkout(backend: TestRepoBackend) {
wc_commit.store_commit().parents, wc_commit.store_commit().parents,
vec![repo.store().root_commit_id().clone()] vec![repo.store().root_commit_id().clone()]
); );
assert_eq!(wc_commit.predecessors(), vec![]); assert!(wc_commit.predecessors().next().is_none());
assert_eq!(wc_commit.description(), ""); assert_eq!(wc_commit.description(), "");
assert_eq!(wc_commit.author().name, settings.user_name()); assert_eq!(wc_commit.author().name, settings.user_name());
assert_eq!(wc_commit.author().email, settings.user_email()); assert_eq!(wc_commit.author().email, settings.user_email());