Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --exact flag to skip adding main and HEAD in smartlog #1241

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions git-branchless-navigation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ pub fn switch(
&event_replayer,
event_cursor,
&commits,
false,
)?;

let initial_query = match switch_options {
Expand Down
5 changes: 5 additions & 0 deletions git-branchless-opts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ pub struct SmartlogArgs {
#[clap(long)]
pub reverse: bool,

/// Don't automatically add HEAD and the main branch to the list of commits
/// to present. They will still be added if included in the revset.
#[clap(long)]
pub exact: bool,

/// Options for resolving revset expressions.
#[clap(flatten)]
pub resolve_revset_options: ResolveRevsetOptions,
Expand Down
28 changes: 23 additions & 5 deletions git-branchless-smartlog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,17 @@ mod graph {
dag: &Dag,
commits: &CommitSet,
) -> eyre::Result<SmartlogGraph<'repo>> {
let commits_include_main =
!dag.set_is_empty(&dag.main_branch_commit.intersection(commits))?;
let mut graph: HashMap<NonZeroOid, Node> = {
let mut result = HashMap::new();
for vertex in dag.commit_set_to_vec(commits)? {
let vertex = CommitSet::from(vertex);
let merge_bases = dag.query_gca_all(dag.main_branch_commit.union(&vertex))?;
let merge_bases = if commits_include_main {
dag.query_gca_all(dag.main_branch_commit.union(&vertex))?
} else {
dag.query_gca_all(commits.union(&vertex))?
};
let vertices = vertex.union(&merge_bases);

for oid in dag.commit_set_to_vec(&vertices)? {
Expand Down Expand Up @@ -341,16 +347,21 @@ mod graph {
event_replayer: &EventReplayer,
event_cursor: EventCursor,
commits: &CommitSet,
exact: bool,
) -> eyre::Result<SmartlogGraph<'repo>> {
let (effects, _progress) = effects.start_operation(OperationType::MakeGraph);

let mut graph = {
let (effects, _progress) = effects.start_operation(OperationType::WalkCommits);

// HEAD and main head must be included
let commits = commits
.union(&dag.head_commit)
.union(&dag.main_branch_commit);
// HEAD and main head are automatically included unless `exact` is set
let commits = if exact {
commits.clone()
} else {
commits
.union(&dag.head_commit)
.union(&dag.main_branch_commit)
};

for oid in dag.commit_set_to_vec(&commits)? {
mark_commit_reachable(repo, oid)?;
Expand Down Expand Up @@ -741,6 +752,9 @@ mod render {
/// Reverse the ordering of items in the smartlog output, list the most
/// recent commits first.
pub reverse: bool,

/// Normally HEAD and the main branch are included. Set this to exclude them.
pub exact: bool,
}
}

Expand All @@ -756,6 +770,7 @@ pub fn smartlog(
revset,
resolve_revset_options,
reverse,
exact,
} = options;

let repo = Repo::from_dir(&git_run_info.working_directory)?;
Expand Down Expand Up @@ -809,6 +824,7 @@ pub fn smartlog(
&event_replayer,
event_cursor,
&commits,
exact,
)?;

let mut lines = render_graph(
Expand Down Expand Up @@ -901,6 +917,7 @@ pub fn command_main(ctx: CommandContext, args: SmartlogArgs) -> EyreExitOr<()> {
revset,
resolve_revset_options,
reverse,
exact,
} = args;

smartlog(
Expand All @@ -911,6 +928,7 @@ pub fn command_main(ctx: CommandContext, args: SmartlogArgs) -> EyreExitOr<()> {
revset,
resolve_revset_options,
reverse,
exact,
},
)
}
70 changes: 70 additions & 0 deletions git-branchless-smartlog/tests/test_smartlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,73 @@ fn test_default_smartlog_revset() -> eyre::Result<()> {

Ok(())
}

#[test]
fn test_exact() -> eyre::Result<()> {
let git = make_git()?;
git.init_repo()?;

git.commit_file("test1", 1)?;
git.detach_head()?;
git.commit_file("test2", 2)?;
git.commit_file("test3", 3)?;
git.commit_file("test4", 4)?;
git.run(&["checkout", "master"])?;
git.commit_file("test5", 5)?;
git.detach_head()?;
git.commit_file("test6", 6)?;
git.run(&["checkout", "96d1c37"])?;

{
// Here '--exact' doesn't change anything because draft() covers all these commits
let (stdout, _stderr) = git.branchless("smartlog", &["--exact"])?;
insta::assert_snapshot!(stdout, @r###"
:
O 62fc20d create test1.txt
|\
| @ 96d1c37 create test2.txt
| |
| o 70deb1e create test3.txt
| |
| o 355e173 create test4.txt
|
O ea7aa06 (master) create test5.txt
|
o da42aeb create test6.txt
"###);
}

{
// Show no commits
let (stdout, _stderr) = git.branchless("smartlog", &["--exact", "none()"])?;
insta::assert_snapshot!(stdout, @"");
}

{
// Show one commit - no master or HEAD
let (stdout, _stderr) = git.branchless("smartlog", &["--exact", "70deb1e"])?;
insta::assert_snapshot!(stdout, @r###"
:
o 70deb1e create test3.txt
:
# 1 omitted descendant commit
"###);
}

{
// Show head commits and their common ancestor, which is not main.
let (stdout, _stderr) = git.branchless("smartlog", &["--exact", "heads(draft())"])?;
insta::assert_snapshot!(stdout, @r###"
:
O 62fc20d create test1.txt
|\
: # 2 omitted commits
: :
: o 355e173 create test4.txt
:
o da42aeb create test6.txt
"###);
}

Ok(())
}
10 changes: 9 additions & 1 deletion git-branchless-undo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ fn render_cursor_smartlog(
};

let commits = resolve_default_smartlog_commits(effects, repo, &mut dag)?;
let graph = make_smartlog_graph(effects, repo, &dag, event_replayer, event_cursor, &commits)?;
let graph = make_smartlog_graph(
effects,
repo,
&dag,
event_replayer,
event_cursor,
&commits,
false,
)?;
let result = render_graph(
effects,
repo,
Expand Down
10 changes: 9 additions & 1 deletion git-branchless/src/commands/bug_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,15 @@ fn describe_event_cursor(
let glyphs = Glyphs::text();
let effects = Effects::new(glyphs.clone());
let commits = resolve_default_smartlog_commits(&effects, repo, dag)?;
let graph = make_smartlog_graph(&effects, repo, dag, event_replayer, event_cursor, &commits)?;
let graph = make_smartlog_graph(
&effects,
repo,
dag,
event_replayer,
event_cursor,
&commits,
false,
)?;
let graph_lines = render_graph(
&effects,
repo,
Expand Down
4 changes: 2 additions & 2 deletions git-branchless/tests/test_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ fn test_main_branch_not_found_error_message() -> eyre::Result<()> {

0: branchless::core::eventlog::from_event_log_db with effects=<Output fancy=false> repo=<Git repository at: "<repo-path>/.git/"> event_log_db=<EventLogDb path=Some("<repo-path>/.git/branchless/db.sqlite3")>
at some/file/path.rs:123
1: git_branchless_smartlog::smartlog with effects=<Output fancy=false> git_run_info=<GitRunInfo path_to_git="<git-executable>" working_directory="<repo-path>" env=not shown> options=SmartlogOptions { event_id: None, revset: None, resolve_revset_options: ResolveRevsetOptions { show_hidden_commits: false }, reverse: false }
1: git_branchless_smartlog::smartlog with effects=<Output fancy=false> git_run_info=<GitRunInfo path_to_git="<git-executable>" working_directory="<repo-path>" env=not shown> options=SmartlogOptions { event_id: None, revset: None, resolve_revset_options: ResolveRevsetOptions { show_hidden_commits: false }, reverse: false, exact: false }
at some/file/path.rs:123
2: git_branchless_smartlog::command_main with ctx=CommandContext { effects: <Output fancy=false>, git_run_info: <GitRunInfo path_to_git="<git-executable>" working_directory="<repo-path>" env=not shown> } args=SmartlogArgs { event_id: None, revset: None, reverse: false, resolve_revset_options: ResolveRevsetOptions { show_hidden_commits: false } }
2: git_branchless_smartlog::command_main with ctx=CommandContext { effects: <Output fancy=false>, git_run_info: <GitRunInfo path_to_git="<git-executable>" working_directory="<repo-path>" env=not shown> } args=SmartlogArgs { event_id: None, revset: None, reverse: false, exact: false, resolve_revset_options: ResolveRevsetOptions { show_hidden_commits: false } }
at some/file/path.rs:123

Suggestion:
Expand Down
Loading