Skip to content

Commit

Permalink
feat(smartlog): Only HEAD and main should be mandatory in the smartlog
Browse files Browse the repository at this point in the history
Interactive undo needs to display a lot more context, so its behavior is
preserved.
  • Loading branch information
claytonrcarter committed Nov 4, 2022
1 parent 063e083 commit cb1a415
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
73 changes: 61 additions & 12 deletions git-branchless/src/commands/smartlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use lib::core::node_descriptors::{
};
use lib::git::{ConfigRead, GitRunInfo, Repo};

pub use graph::{make_smartlog_graph, SmartlogGraph};
pub use graph::{make_custom_smartlog_graph, make_smartlog_graph, SmartLogKind, SmartlogGraph};
pub use render::{render_graph, SmartlogOptions};

use crate::opts::Revset;
Expand Down Expand Up @@ -216,7 +216,18 @@ mod graph {
}
}

/// Construct the smartlog graph for the repo.
/// What kind of smartlog is desired?
#[derive(Debug)]
pub enum SmartLogKind {
/// The default smartlog, with all draft commits visible.
Default,

/// A smartlog representing only a specific set of a commits, such when
/// a user calls `smartlog` and specifies a revset .
Specific,
}

/// Construct the default smartlog graph for the repo.
#[instrument]
pub fn make_smartlog_graph<'repo>(
effects: &Effects,
Expand All @@ -226,6 +237,30 @@ mod graph {
event_cursor: EventCursor,
observed_commits: &CommitSet,
remove_commits: bool,
) -> eyre::Result<SmartlogGraph<'repo>> {
make_custom_smartlog_graph(
effects,
repo,
dag,
event_replayer,
event_cursor,
observed_commits,
remove_commits,
SmartLogKind::Default,
)
}

/// Construct a specific kind of smartlog graph for the repo.
#[instrument]
pub fn make_custom_smartlog_graph<'repo>(
effects: &Effects,
repo: &'repo Repo,
dag: &Dag,
event_replayer: &EventReplayer,
event_cursor: EventCursor,
observed_commits: &CommitSet,
remove_commits: bool,
smartlog_kind: SmartLogKind,
) -> eyre::Result<SmartlogGraph<'repo>> {
let (effects, _progress) = effects.start_operation(OperationType::MakeGraph);

Expand All @@ -240,12 +275,23 @@ mod graph {
observed_commits.clone()
};

let active_heads = dag.query_active_heads(&public_commits, &observed_commits)?;
for oid in commit_set_to_vec(&active_heads)? {
let observed_commits = match smartlog_kind {
SmartLogKind::Default => dag
.query_active_heads(&public_commits, &observed_commits)?
.union(&observed_commits),
SmartLogKind::Specific => {
// The only 2 commits that *must* be displayed are HEAD and
// main/master head.
observed_commits
.union(&dag.head_commit)
.union(&dag.main_branch_commit)
}
};
for oid in commit_set_to_vec(&observed_commits)? {
mark_commit_reachable(repo, oid)?;
}

walk_from_active_heads(&effects, repo, dag, &public_commits, &active_heads)?
walk_from_active_heads(&effects, repo, dag, &public_commits, &observed_commits)?
};
sort_children(&mut graph);
Ok(graph)
Expand Down Expand Up @@ -573,16 +619,18 @@ pub fn smartlog(
&references_snapshot,
)?;

let revset = match revset {
Some(revset) => revset.clone(),
let default_revset = Revset("draft()".to_string());
let (revset, smartlog_kind) = match revset {
Some(revset) if revset == &default_revset => (revset.clone(), SmartLogKind::Default),
Some(revset) => (revset.clone(), SmartLogKind::Specific),
None => {
// TODO add test for custom default revset
let custom_default: Option<String> = repo
let user_default: Option<String> = repo
.get_readonly_config()?
.get("branchless.revsets.default")?;
match custom_default {
Some(custom_default) => Revset(custom_default),
None => Revset("draft()".to_string()),
match user_default {
Some(user_default) => (Revset(user_default), SmartLogKind::Specific),
None => (default_revset, SmartLogKind::Default),
}
}
};
Expand Down Expand Up @@ -616,14 +664,15 @@ pub fn smartlog(
observed_commits
};

let graph = make_smartlog_graph(
let graph = make_custom_smartlog_graph(
effects,
&repo,
&dag,
&event_replayer,
event_cursor,
&observed_commits,
!show_hidden_commits,
smartlog_kind,
)?;

let lines = render_graph(
Expand Down
2 changes: 1 addition & 1 deletion git-branchless/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::str::FromStr;

/// A revset expression. Can be a commit hash, branch name, or one of the
/// various revset functions.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Revset(pub String);

impl FromStr for Revset {
Expand Down

0 comments on commit cb1a415

Please sign in to comment.