Skip to content

Commit 726f609

Browse files
Nikolaus Wittensteinarxanas
authored andcommitted
Add new 'smartlog --exact' flag to skip adding main and HEAD
1 parent 8b1ae8b commit 726f609

File tree

7 files changed

+119
-9
lines changed

7 files changed

+119
-9
lines changed

git-branchless-navigation/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ pub fn switch(
507507
&event_replayer,
508508
event_cursor,
509509
&commits,
510+
false,
510511
)?;
511512

512513
let initial_query = match switch_options {

git-branchless-opts/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ pub struct SmartlogArgs {
345345
#[clap(long)]
346346
pub reverse: bool,
347347

348+
/// Don't automatically add HEAD and the main branch to the list of commits
349+
/// to present. They will still be added if included in the revset.
350+
#[clap(long)]
351+
pub exact: bool,
352+
348353
/// Options for resolving revset expressions.
349354
#[clap(flatten)]
350355
pub resolve_revset_options: ResolveRevsetOptions,

git-branchless-smartlog/src/lib.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,17 @@ mod graph {
163163
dag: &Dag,
164164
commits: &CommitSet,
165165
) -> eyre::Result<SmartlogGraph<'repo>> {
166+
let commits_include_main =
167+
!dag.set_is_empty(&dag.main_branch_commit.intersection(commits))?;
166168
let mut graph: HashMap<NonZeroOid, Node> = {
167169
let mut result = HashMap::new();
168170
for vertex in dag.commit_set_to_vec(commits)? {
169171
let vertex = CommitSet::from(vertex);
170-
let merge_bases = dag.query_gca_all(dag.main_branch_commit.union(&vertex))?;
172+
let merge_bases = if commits_include_main {
173+
dag.query_gca_all(dag.main_branch_commit.union(&vertex))?
174+
} else {
175+
dag.query_gca_all(commits.union(&vertex))?
176+
};
171177
let vertices = vertex.union(&merge_bases);
172178

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

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

350-
// HEAD and main head must be included
351-
let commits = commits
352-
.union(&dag.head_commit)
353-
.union(&dag.main_branch_commit);
357+
// HEAD and main head are automatically included unless `exact` is set
358+
let commits = if exact {
359+
commits.clone()
360+
} else {
361+
commits
362+
.union(&dag.head_commit)
363+
.union(&dag.main_branch_commit)
364+
};
354365

355366
for oid in dag.commit_set_to_vec(&commits)? {
356367
mark_commit_reachable(repo, oid)?;
@@ -741,6 +752,9 @@ mod render {
741752
/// Reverse the ordering of items in the smartlog output, list the most
742753
/// recent commits first.
743754
pub reverse: bool,
755+
756+
/// Normally HEAD and the main branch are included. Set this to exclude them.
757+
pub exact: bool,
744758
}
745759
}
746760

@@ -756,6 +770,7 @@ pub fn smartlog(
756770
revset,
757771
resolve_revset_options,
758772
reverse,
773+
exact,
759774
} = options;
760775

761776
let repo = Repo::from_dir(&git_run_info.working_directory)?;
@@ -809,6 +824,7 @@ pub fn smartlog(
809824
&event_replayer,
810825
event_cursor,
811826
&commits,
827+
exact,
812828
)?;
813829

814830
let mut lines = render_graph(
@@ -901,6 +917,7 @@ pub fn command_main(ctx: CommandContext, args: SmartlogArgs) -> EyreExitOr<()> {
901917
revset,
902918
resolve_revset_options,
903919
reverse,
920+
exact,
904921
} = args;
905922

906923
smartlog(
@@ -911,6 +928,7 @@ pub fn command_main(ctx: CommandContext, args: SmartlogArgs) -> EyreExitOr<()> {
911928
revset,
912929
resolve_revset_options,
913930
reverse,
931+
exact,
914932
},
915933
)
916934
}

git-branchless-smartlog/tests/test_smartlog.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,73 @@ fn test_default_smartlog_revset() -> eyre::Result<()> {
742742

743743
Ok(())
744744
}
745+
746+
#[test]
747+
fn test_exact() -> eyre::Result<()> {
748+
let git = make_git()?;
749+
git.init_repo()?;
750+
751+
git.commit_file("test1", 1)?;
752+
git.detach_head()?;
753+
git.commit_file("test2", 2)?;
754+
git.commit_file("test3", 3)?;
755+
git.commit_file("test4", 4)?;
756+
git.run(&["checkout", "master"])?;
757+
git.commit_file("test5", 5)?;
758+
git.detach_head()?;
759+
git.commit_file("test6", 6)?;
760+
git.run(&["checkout", "96d1c37"])?;
761+
762+
{
763+
// Here '--exact' doesn't change anything because draft() covers all these commits
764+
let (stdout, _stderr) = git.branchless("smartlog", &["--exact"])?;
765+
insta::assert_snapshot!(stdout, @r###"
766+
:
767+
O 62fc20d create test1.txt
768+
|\
769+
| @ 96d1c37 create test2.txt
770+
| |
771+
| o 70deb1e create test3.txt
772+
| |
773+
| o 355e173 create test4.txt
774+
|
775+
O ea7aa06 (master) create test5.txt
776+
|
777+
o da42aeb create test6.txt
778+
"###);
779+
}
780+
781+
{
782+
// Show no commits
783+
let (stdout, _stderr) = git.branchless("smartlog", &["--exact", "none()"])?;
784+
insta::assert_snapshot!(stdout, @"");
785+
}
786+
787+
{
788+
// Show one commit - no master or HEAD
789+
let (stdout, _stderr) = git.branchless("smartlog", &["--exact", "70deb1e"])?;
790+
insta::assert_snapshot!(stdout, @r###"
791+
:
792+
o 70deb1e create test3.txt
793+
:
794+
# 1 omitted descendant commit
795+
"###);
796+
}
797+
798+
{
799+
// Show head commits and their common ancestor, which is not main.
800+
let (stdout, _stderr) = git.branchless("smartlog", &["--exact", "heads(draft())"])?;
801+
insta::assert_snapshot!(stdout, @r###"
802+
:
803+
O 62fc20d create test1.txt
804+
|\
805+
: # 2 omitted commits
806+
: :
807+
: o 355e173 create test4.txt
808+
:
809+
o da42aeb create test6.txt
810+
"###);
811+
}
812+
813+
Ok(())
814+
}

git-branchless-undo/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ fn render_cursor_smartlog(
7777
};
7878

7979
let commits = resolve_default_smartlog_commits(effects, repo, &mut dag)?;
80-
let graph = make_smartlog_graph(effects, repo, &dag, event_replayer, event_cursor, &commits)?;
80+
let graph = make_smartlog_graph(
81+
effects,
82+
repo,
83+
&dag,
84+
event_replayer,
85+
event_cursor,
86+
&commits,
87+
false,
88+
)?;
8189
let result = render_graph(
8290
effects,
8391
repo,

git-branchless/src/commands/bug_report.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,15 @@ fn describe_event_cursor(
133133
let glyphs = Glyphs::text();
134134
let effects = Effects::new(glyphs.clone());
135135
let commits = resolve_default_smartlog_commits(&effects, repo, dag)?;
136-
let graph = make_smartlog_graph(&effects, repo, dag, event_replayer, event_cursor, &commits)?;
136+
let graph = make_smartlog_graph(
137+
&effects,
138+
repo,
139+
dag,
140+
event_replayer,
141+
event_cursor,
142+
&commits,
143+
false,
144+
)?;
137145
let graph_lines = render_graph(
138146
&effects,
139147
repo,

git-branchless/tests/test_init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ fn test_main_branch_not_found_error_message() -> eyre::Result<()> {
316316
317317
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")>
318318
at some/file/path.rs:123
319-
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 }
319+
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 }
320320
at some/file/path.rs:123
321-
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 } }
321+
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 } }
322322
at some/file/path.rs:123
323323
324324
Suggestion:

0 commit comments

Comments
 (0)