Skip to content

Commit 5c36046

Browse files
authored
Merge pull request #68 from lekhmanrus/target-branch-selection-settings
Target branch selection settings
2 parents 9bd8a03 + 2b49155 commit 5c36046

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/GitExtensions.GerritPlugin/FormGerritPublish.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ public partial class FormGerritPublish : FormGerritBase
2727

2828
private string _currentBranchRemote;
2929
private readonly GerritCapabilities _capabilities;
30+
private readonly bool _shouldTargetLocalBranch;
3031

31-
public FormGerritPublish(IGitUICommands uiCommand, GerritCapabilities capabilities)
32+
public FormGerritPublish(IGitUICommands uiCommand, GerritCapabilities capabilities, bool shouldTargetLocalBranch)
3233
: base(uiCommand)
3334
{
3435
_capabilities = capabilities;
36+
_shouldTargetLocalBranch = shouldTargetLocalBranch;
3537
InitializeComponent();
3638
Publish.Image = Images.Push.AdaptLightness();
3739
InitializeComplete();
@@ -137,18 +139,22 @@ private bool PublishChange(IWin32Window owner)
137139
private string GetTopic(string targetBranch)
138140
{
139141
string branchName = GetBranchName(targetBranch);
140-
141142
string[] branchParts = branchName.Split('/');
142-
143143
if (branchParts.Length >= 3 && branchParts[0] == "review")
144144
{
145145
branchName = string.Join("/", branchParts.Skip(2));
146146

147147
// Don't use the Gerrit change number as a topic branch.
148-
149148
if (int.TryParse(branchName, out _))
150149
{
151-
branchName = null;
150+
return null;
151+
}
152+
153+
// check if patchset number is provided
154+
branchParts = branchName.Split('/');
155+
if (branchParts.Length == 2 && int.TryParse(branchParts.First(), out _) && int.TryParse(branchParts.Last(), out _))
156+
{
157+
return null;
152158
}
153159
}
154160

@@ -158,8 +164,7 @@ private string GetTopic(string targetBranch)
158164
private string GetBranchName(string targetBranch)
159165
{
160166
string branch = Module.GetSelectedBranch();
161-
162-
if (string.IsNullOrWhiteSpace(branch) || branch.StartsWith("(no "))
167+
if (string.IsNullOrWhiteSpace(branch) || branch.StartsWith("(no ") || branch.StartsWith("review/"))
163168
{
164169
return targetBranch;
165170
}
@@ -179,11 +184,15 @@ private void FormGerritPublishLoad(object sender, EventArgs e)
179184
_NO_TRANSLATE_Branch.DataSource = Module.GetRefs(RefsFilter.Remotes)
180185
.Select(branch => branch.LocalName)
181186
.ToList();
182-
_NO_TRANSLATE_Branch.Text = GetBranchName(Settings.DefaultBranch);
187+
_NO_TRANSLATE_Branch.Text = Settings.DefaultBranch;
183188

184-
var branches = (IList<string>)_NO_TRANSLATE_Branch.DataSource;
185-
int branchIndex = branches.IndexOf(_NO_TRANSLATE_Branch.Text);
186-
_NO_TRANSLATE_Branch.SelectedIndex = branchIndex >= 0 ? branchIndex : 0;
189+
if (_shouldTargetLocalBranch || string.IsNullOrEmpty(_NO_TRANSLATE_Branch.Text))
190+
{
191+
_NO_TRANSLATE_Branch.Text = GetBranchName(Settings.DefaultBranch);
192+
var branches = (IList<string>)_NO_TRANSLATE_Branch.DataSource;
193+
int branchIndex = branches.IndexOf(_NO_TRANSLATE_Branch.Text);
194+
_NO_TRANSLATE_Branch.SelectedIndex = branchIndex >= 0 ? branchIndex : 0;
195+
}
187196

188197
if (!string.IsNullOrEmpty(_NO_TRANSLATE_Branch.Text))
189198
{
@@ -195,7 +204,10 @@ private void FormGerritPublishLoad(object sender, EventArgs e)
195204
_NO_TRANSLATE_Topic.Text = null;
196205
}
197206

198-
_NO_TRANSLATE_Branch.Select();
207+
if (_shouldTargetLocalBranch)
208+
{
209+
_NO_TRANSLATE_Branch.Select();
210+
}
199211

200212
Text = string.Concat(_publishGerritChangeCaption.Text, " (", Module.WorkingDir, ")");
201213
}

src/GitExtensions.GerritPlugin/GerritPlugin.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ public class GerritPlugin : GitPluginBase, IGitPluginForRepository
3333
#endregion
3434

3535
private const string DefaultGerritVersion = "2.15 or newer";
36+
private const string DefaultPublishTargetBranch = "local";
3637

3738
private readonly BoolSetting _gerritEnabled = new("Gerrit plugin enabled", true);
3839
private readonly ChoiceSetting _predefinedGerritVersion = new(
3940
"Treat Gerrit as having version",
4041
new[] { DefaultGerritVersion, "Older then 2.15" },
4142
DefaultGerritVersion);
4243
private readonly BoolSetting _hidePushButton = new("Hide Push button", false);
44+
private readonly ChoiceSetting _predefinedPublishTargetBranch = new (
45+
"Target branch selection",
46+
new[] { DefaultPublishTargetBranch, ".gitreview file" },
47+
DefaultPublishTargetBranch);
4348

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

298-
using (var form = new FormGerritPublish(_gitUiCommands, capabilities))
304+
using (var form = new FormGerritPublish(_gitUiCommands, capabilities, shouldTargetLocalBranch))
299305
{
300306
form.ShowDialog(_mainForm);
301307
}
@@ -486,6 +492,7 @@ public override IEnumerable<ISetting> GetSettings()
486492
yield return _gerritEnabled;
487493
yield return _predefinedGerritVersion;
488494
yield return _hidePushButton;
495+
yield return _predefinedPublishTargetBranch;
489496
}
490497
}
491498
}

0 commit comments

Comments
 (0)