Skip to content

Commit

Permalink
FormChooseCommit & Rebase onto: Display only potential commits (#11703)
Browse files Browse the repository at this point in the history
to display only the commits the user could act on
to avoid selecting a commit that has no effects
and so that the commit choice is not overwhelming
and user has less data to process to find commit he wants

+ manage edge case where the user could try to do a rebase onto
where that's not possible with only one commit to rebase and displayed in the grid:
no commits are selected and so parents links must be hidden
because not initialized and a click on it (if user really want to do strange things)
could lead to a crash.
  • Loading branch information
pmiossec committed May 4, 2024
1 parent 9195b25 commit 61c7418
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
20 changes: 19 additions & 1 deletion GitUI/CommandsDialogs/FormRebase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GitCommands;
using GitCommands.Git;
using GitExtUtils;
using GitExtUtils.GitUI.Theming;
using GitUI.HelperDialogs;
using GitUIPluginInterfaces;
Expand Down Expand Up @@ -408,7 +409,24 @@ private void btnChooseFromRevision_Click(object sender, EventArgs e)
AppSettings.ShowStashes = false;
ObjectId firstParent = UICommands.Module.RevParse("HEAD~");
string preSelectedCommit = !string.IsNullOrWhiteSpace(txtFrom.Text) ? txtFrom.Text : firstParent?.ToString() ?? string.Empty;
using FormChooseCommit chooseForm = new(UICommands, preSelectedCommit, showCurrentBranchOnly: true);

string mergeBaseCommitId = null;

if (!string.IsNullOrWhiteSpace(cboBranches.Text))
{
try
{
ObjectId commit1 = UICommands.Module.RevParse(cboBranches.Text);
ObjectId commit2 = UICommands.Module.RevParse("HEAD");
mergeBaseCommitId = UICommands.Module.GetMergeBase(commit1, commit2)?.ToString();
}
catch (Exception)
{
// if an exception occurs, display whole history
}
}

using FormChooseCommit chooseForm = new(UICommands, preSelectedCommit, showCurrentBranchOnly: true, lastRevisionToDisplayHash: mergeBaseCommitId);
if (chooseForm.ShowDialog(this) == DialogResult.OK && chooseForm.SelectedRevision is not null)
{
txtFrom.Text = chooseForm.SelectedRevision.ObjectId.ToShortString();
Expand Down
14 changes: 12 additions & 2 deletions GitUI/HelperDialogs/FormChooseCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ private FormChooseCommit(GitUICommands commands)
InitializeComplete();
}

public FormChooseCommit(GitUICommands commands, string? preselectCommit, bool showArtificial = false, bool showCurrentBranchOnly = false)
public FormChooseCommit(GitUICommands commands, string? preselectCommit, bool showArtificial = false, bool showCurrentBranchOnly = false, string? lastRevisionToDisplayHash = null)
: this(commands)
{
revisionGrid.MultiSelect = false;
revisionGrid.ShowUncommittedChangesIfPossible = showArtificial;
if (lastRevisionToDisplayHash is not null)
{
revisionGrid.SetLastRevisionToDisplayHash(lastRevisionToDisplayHash);
}

if (showCurrentBranchOnly)
{
revisionGrid.ShowCurrentBranchOnly();
Expand Down Expand Up @@ -83,7 +88,12 @@ private void revisionGrid_SelectionChanged(object sender, EventArgs e)
{
IReadOnlyList<GitRevision> revisions = revisionGrid.GetSelectedRevisions();

if (revisions.Count != 1)
bool singleCommitSelected = revisions.Count == 1;
labelParents.Visible = singleCommitSelected;
linkLabelParent.Visible = singleCommitSelected;
linkLabelParent2.Visible = singleCommitSelected;

if (!singleCommitSelected)
{
return;
}
Expand Down
11 changes: 11 additions & 0 deletions GitUI/UserControls/RevisionGrid/FilterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ public bool HasRevisionFilter
|| ShowSimplifyByDecoration;
}

/// <summary>
/// The hash of the last revision to display (i.e. the oldest one displayed at the bottom).
/// This hash is used to query history only until this given commit.
/// </summary>
public string LastRevisionToDisplayHash { get; internal set; }

/// <summary>
/// Disables all active filters.
/// CurrentBranch and Reflog are not disabled.
Expand Down Expand Up @@ -483,6 +489,11 @@ private void GetBranchRevisionFilter(ArgumentBuilder filter, Lazy<ObjectId?> cur
}
}

if (!string.IsNullOrWhiteSpace(LastRevisionToDisplayHash))
{
filter.Add($"...{LastRevisionToDisplayHash}");
}

return;

// git-log filters add implicit /* to the filters, unless there are wildcard characters in the ref name
Expand Down
5 changes: 5 additions & 0 deletions GitUI/UserControls/RevisionGrid/RevisionGridControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,11 @@ public void ShowCurrentBranchOnly()
PerformRefreshRevisions();
}

public void SetLastRevisionToDisplayHash(string hash)
{
_filterInfo.LastRevisionToDisplayHash = hash;
}

public void ShowRevisionFilterDialog()
{
using FormRevisionFilter form = new(UICommands, _filterInfo);
Expand Down

0 comments on commit 61c7418

Please sign in to comment.