Skip to content

Commit

Permalink
cleanup: propagate errors from Commit::predecessors()
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvonz committed May 13, 2024
1 parent c79afc4 commit 2230910
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
23 changes: 12 additions & 11 deletions cli/src/commands/obslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use itertools::Itertools;
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::rewrite::rebase_to_dest_parent;
use tracing::instrument;
Expand Down Expand Up @@ -102,20 +103,20 @@ pub(crate) fn cmd_obslog(
let mut formatter = ui.stdout_formatter();
let formatter = formatter.as_mut();

let mut commits = topo_order_reverse(
vec![start_commit],
let mut commits = topo_order_reverse_ok(
vec![Ok(start_commit)],
|commit: &Commit| commit.id().clone(),
|commit: &Commit| commit.predecessors(),
);
|commit: &Commit| commit.predecessors().collect_vec(),
)?;
if let Some(n) = args.limit {
commits.truncate(n);
}
if !args.no_graph {
let mut graph = get_graphlog(command.settings(), formatter.raw());
for commit in commits {
let mut edges = vec![];
for predecessor in &commit.predecessors() {
edges.push(Edge::Direct(predecessor.id().clone()));
for predecessor in commit.predecessors() {
edges.push(Edge::Direct(predecessor?.id().clone()));
}
let mut buffer = vec![];
with_content_format.write_graph_text(
Expand Down Expand Up @@ -164,13 +165,13 @@ fn show_predecessor_patch(
commit: &Commit,
diff_formats: &[DiffFormat],
) -> Result<(), CommandError> {
let predecessors = commit.predecessors();
let predecessor = match predecessors.first() {
Some(predecessor) => predecessor,
let mut predecessors = commit.predecessors();
let predecessor = match predecessors.next() {
Some(predecessor) => predecessor?,
None => return Ok(()),
};
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()?;
diff_util::show_diff(
ui,
Expand Down
5 changes: 2 additions & 3 deletions lib/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ impl Commit {
&self.data.predecessors
}

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

pub fn tree(&self) -> BackendResult<MergedTree> {
Expand Down
8 changes: 3 additions & 5 deletions lib/tests/test_commit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn test_initial(backend: TestRepoBackend) {

let parents: Vec<_> = commit.parents().try_collect().unwrap();
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.author(), &author_signature);
assert_eq!(commit.committer(), &committer_signature);
Expand Down Expand Up @@ -156,10 +156,8 @@ fn test_rewrite(backend: TestRepoBackend) {
tx.commit("test");
let parents: Vec<_> = rewritten_commit.parents().try_collect().unwrap();
assert_eq!(parents, vec![store.root_commit()]);
assert_eq!(
rewritten_commit.predecessors(),
vec![initial_commit.clone()]
);
let predecessors: Vec<_> = rewritten_commit.predecessors().try_collect().unwrap();
assert_eq!(predecessors, vec![initial_commit.clone()]);
assert_eq!(rewritten_commit.author().name, settings.user_name());
assert_eq!(rewritten_commit.author().email, settings.user_email());
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/test_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn test_init_checkout(backend: TestRepoBackend) {
wc_commit.store_commit().parents,
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.author().name, settings.user_name());
assert_eq!(wc_commit.author().email, settings.user_email());
Expand Down

0 comments on commit 2230910

Please sign in to comment.