Skip to content

Commit

Permalink
Add Target branch selection settings (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
lekhmanrus committed Jan 18, 2023
1 parent 9bd8a03 commit 9fc84b4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
38 changes: 26 additions & 12 deletions src/GitExtensions.GerritPlugin/FormGerritPublish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public partial class FormGerritPublish : FormGerritBase

private string _currentBranchRemote;
private readonly GerritCapabilities _capabilities;
private readonly bool _shouldTargetLocalBranch;

public FormGerritPublish(IGitUICommands uiCommand, GerritCapabilities capabilities)
public FormGerritPublish(IGitUICommands uiCommand, GerritCapabilities capabilities, bool shouldTargetLocalBranch)
: base(uiCommand)
{
_capabilities = capabilities;
_shouldTargetLocalBranch = shouldTargetLocalBranch;
InitializeComponent();
Publish.Image = Images.Push.AdaptLightness();
InitializeComplete();
Expand Down Expand Up @@ -137,18 +139,24 @@ private bool PublishChange(IWin32Window owner)
private string GetTopic(string targetBranch)
{
string branchName = GetBranchName(targetBranch);

string[] branchParts = branchName.Split('/');

if (branchParts.Length >= 3 && branchParts[0] == "review")
{
branchName = string.Join("/", branchParts.Skip(2));

// Don't use the Gerrit change number as a topic branch.

if (int.TryParse(branchName, out _))
{
branchName = null;
return null;
}
else
{
// check if patchset number is provided
branchParts = branchName.Split('/');
if (branchParts.Length == 2 && int.TryParse(branchParts.First(), out _) && int.TryParse(branchParts.Last(), out _))
{
return null;
}
}
}

Expand All @@ -158,8 +166,7 @@ private string GetTopic(string targetBranch)
private string GetBranchName(string targetBranch)
{
string branch = Module.GetSelectedBranch();

if (string.IsNullOrWhiteSpace(branch) || branch.StartsWith("(no "))
if (string.IsNullOrWhiteSpace(branch) || branch.StartsWith("(no ") || branch.StartsWith("review/"))
{
return targetBranch;
}
Expand All @@ -179,11 +186,15 @@ private void FormGerritPublishLoad(object sender, EventArgs e)
_NO_TRANSLATE_Branch.DataSource = Module.GetRefs(RefsFilter.Remotes)
.Select(branch => branch.LocalName)
.ToList();
_NO_TRANSLATE_Branch.Text = GetBranchName(Settings.DefaultBranch);
_NO_TRANSLATE_Branch.Text = Settings.DefaultBranch;

var branches = (IList<string>)_NO_TRANSLATE_Branch.DataSource;
int branchIndex = branches.IndexOf(_NO_TRANSLATE_Branch.Text);
_NO_TRANSLATE_Branch.SelectedIndex = branchIndex >= 0 ? branchIndex : 0;
if (_shouldTargetLocalBranch || string.IsNullOrEmpty(_NO_TRANSLATE_Branch.Text))
{
_NO_TRANSLATE_Branch.Text = GetBranchName(Settings.DefaultBranch);
var branches = (IList<string>)_NO_TRANSLATE_Branch.DataSource;
int branchIndex = branches.IndexOf(_NO_TRANSLATE_Branch.Text);
_NO_TRANSLATE_Branch.SelectedIndex = branchIndex >= 0 ? branchIndex : 0;
}

if (!string.IsNullOrEmpty(_NO_TRANSLATE_Branch.Text))
{
Expand All @@ -195,7 +206,10 @@ private void FormGerritPublishLoad(object sender, EventArgs e)
_NO_TRANSLATE_Topic.Text = null;
}

_NO_TRANSLATE_Branch.Select();
if (_shouldTargetLocalBranch)
{
_NO_TRANSLATE_Branch.Select();
}

Text = string.Concat(_publishGerritChangeCaption.Text, " (", Module.WorkingDir, ")");
}
Expand Down
9 changes: 8 additions & 1 deletion src/GitExtensions.GerritPlugin/GerritPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ public class GerritPlugin : GitPluginBase, IGitPluginForRepository
#endregion

private const string DefaultGerritVersion = "2.15 or newer";
private const string DefaultPublishTargetBranch = "local";

private readonly BoolSetting _gerritEnabled = new("Gerrit plugin enabled", true);
private readonly ChoiceSetting _predefinedGerritVersion = new(
"Treat Gerrit as having version",
new[] { DefaultGerritVersion, "Older then 2.15" },
DefaultGerritVersion);
private readonly BoolSetting _hidePushButton = new("Hide Push button", false);
private readonly ChoiceSetting _predefinedPublishTargetBranch = new (
"Target branch selection",
new[] { DefaultPublishTargetBranch, ".gitreview file" },
DefaultPublishTargetBranch);

private static readonly Dictionary<string, bool> ValidatedHooks = new(StringComparer.OrdinalIgnoreCase);
private static readonly object SyncRoot = new();
Expand Down Expand Up @@ -294,8 +299,9 @@ private void publishMenuItem_Click(object sender, EventArgs e)
var capabilities = _predefinedGerritVersion.ValueOrDefault(Settings) == DefaultGerritVersion
? GerritCapabilities.Version2_15
: GerritCapabilities.OldestVersion;
var shouldTargetLocalBranch = _predefinedPublishTargetBranch.ValueOrDefault(Settings) == DefaultPublishTargetBranch;

using (var form = new FormGerritPublish(_gitUiCommands, capabilities))
using (var form = new FormGerritPublish(_gitUiCommands, capabilities, shouldTargetLocalBranch))
{
form.ShowDialog(_mainForm);
}
Expand Down Expand Up @@ -486,6 +492,7 @@ public override IEnumerable<ISetting> GetSettings()
yield return _gerritEnabled;
yield return _predefinedGerritVersion;
yield return _hidePushButton;
yield return _predefinedPublishTargetBranch;
}
}
}

0 comments on commit 9fc84b4

Please sign in to comment.