Skip to content

Commit e9c41f2

Browse files
committed
feat: push to any remote branch
1 parent e39351b commit e9c41f2

File tree

2 files changed

+67
-77
lines changed

2 files changed

+67
-77
lines changed

src/ViewModels/Push.cs

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.ComponentModel.DataAnnotations;
3+
using System.Linq;
34
using System.Threading.Tasks;
45

56
namespace SourceGit.ViewModels
@@ -44,20 +45,32 @@ public Models.Remote SelectedRemote
4445
}
4546
}
4647

47-
public List<Models.Branch> RemoteBranches
48+
public Models.Branch SelectedRemoteBranch
4849
{
49-
get => _remoteBranches;
50-
private set => SetProperty(ref _remoteBranches, value);
50+
get => _selectedRemoteBranch;
51+
private set => SetProperty(ref _selectedRemoteBranch, value);
5152
}
52-
53+
5354
[Required(ErrorMessage = "Remote branch is required!!!")]
54-
public Models.Branch SelectedRemoteBranch
55+
public string SelectedRemoteBranchName
5556
{
56-
get => _selectedRemoteBranch;
57+
get => _selectedRemoteBranchName;
5758
set
5859
{
59-
if (SetProperty(ref _selectedRemoteBranch, value))
60-
IsSetTrackOptionVisible = value != null && _selectedLocalBranch.Upstream != value.FullName;
60+
if (SetProperty(ref _selectedRemoteBranchName, value))
61+
{
62+
SelectedRemoteBranch = null;
63+
64+
foreach (var branch in _repo.Branches.Where(branch => branch.Remote == _selectedRemote.Name))
65+
{
66+
if (_selectedRemoteBranchName == branch.Name)
67+
{
68+
SelectedRemoteBranch = branch;
69+
}
70+
}
71+
72+
IsSetTrackOptionVisible = SelectedRemoteBranch != null && _selectedLocalBranch.Upstream != SelectedRemoteBranch.FullName;
73+
}
6174
}
6275
}
6376

@@ -130,7 +143,7 @@ public Push(Repository repo, Models.Branch localBranch)
130143
{
131144
if (!branch.IsLocal && _selectedLocalBranch.Upstream == branch.FullName)
132145
{
133-
_selectedRemote = repo.Remotes.Find(x => x.Name == branch.Remote);
146+
_selectedRemote = repo.Remotes.Find(remote => remote.Name == branch.Remote);
134147
break;
135148
}
136149
}
@@ -141,7 +154,7 @@ public Push(Repository repo, Models.Branch localBranch)
141154
{
142155
var remote = null as Models.Remote;
143156
if (!string.IsNullOrEmpty(_repo.Settings.DefaultRemote))
144-
remote = repo.Remotes.Find(x => x.Name == _repo.Settings.DefaultRemote);
157+
remote = repo.Remotes.Find(remote => remote.Name == _repo.Settings.DefaultRemote);
145158

146159
_selectedRemote = remote ?? repo.Remotes[0];
147160
}
@@ -154,23 +167,23 @@ public Push(Repository repo, Models.Branch localBranch)
154167

155168
public override bool CanStartDirectly()
156169
{
157-
return !string.IsNullOrEmpty(_selectedRemoteBranch?.Head);
170+
return !string.IsNullOrEmpty(SelectedRemoteBranch?.Head);
158171
}
159172

160173
public override Task<bool> Sure()
161174
{
162175
_repo.SetWatcherEnabled(false);
163176

164-
var remoteBranchName = _selectedRemoteBranch.Name;
165-
ProgressDescription = $"Push {_selectedLocalBranch.Name} -> {_selectedRemote.Name}/{remoteBranchName} ...";
177+
ProgressDescription =
178+
$"Push {_selectedLocalBranch.Name} -> {_selectedRemote.Name}/{_selectedRemoteBranchName} ...";
166179

167180
return Task.Run(() =>
168181
{
169182
var succ = new Commands.Push(
170183
_repo.FullPath,
171184
_selectedLocalBranch.Name,
172185
_selectedRemote.Name,
173-
remoteBranchName,
186+
_selectedRemoteBranchName,
174187
PushAllTags,
175188
_repo.Submodules.Count > 0 && CheckSubmodules,
176189
_isSetTrackOptionVisible && Tracking,
@@ -183,54 +196,36 @@ public override Task<bool> Sure()
183196

184197
private void AutoSelectBranchByRemote()
185198
{
186-
// Gather branches.
187-
var branches = new List<Models.Branch>();
188-
foreach (var branch in _repo.Branches)
189-
{
190-
if (branch.Remote == _selectedRemote.Name)
191-
branches.Add(branch);
192-
}
193-
194199
// If selected local branch has upstream. Try to find it in current remote branches.
195200
if (!string.IsNullOrEmpty(_selectedLocalBranch.Upstream))
196201
{
197-
foreach (var branch in branches)
202+
foreach (var branch in _repo.Branches.Where(branch => branch.Remote == _selectedRemote.Name))
198203
{
199204
if (_selectedLocalBranch.Upstream == branch.FullName)
200205
{
201-
RemoteBranches = branches;
202-
SelectedRemoteBranch = branch;
206+
SelectedRemoteBranchName = branch.Name;
203207
return;
204208
}
205209
}
206210
}
207211

208212
// Try to find a remote branch with the same name of selected local branch.
209-
foreach (var branch in branches)
213+
foreach (var branch in _repo.Branches.Where(branch => branch.Remote == _selectedRemote.Name))
210214
{
211215
if (_selectedLocalBranch.Name == branch.Name)
212216
{
213-
RemoteBranches = branches;
214-
SelectedRemoteBranch = branch;
217+
SelectedRemoteBranchName = branch.Name;
215218
return;
216219
}
217220
}
218221

219-
// Add a fake new branch.
220-
var fake = new Models.Branch()
221-
{
222-
Name = _selectedLocalBranch.Name,
223-
Remote = _selectedRemote.Name,
224-
};
225-
branches.Add(fake);
226-
RemoteBranches = branches;
227-
SelectedRemoteBranch = fake;
222+
SelectedRemoteBranchName = _selectedLocalBranch.Name;
228223
}
229224

230225
private readonly Repository _repo = null;
231226
private Models.Branch _selectedLocalBranch = null;
232227
private Models.Remote _selectedRemote = null;
233-
private List<Models.Branch> _remoteBranches = [];
228+
private string _selectedRemoteBranchName = null;
234229
private Models.Branch _selectedRemoteBranch = null;
235230
private bool _isSetTrackOptionVisible = false;
236231
}

src/Views/Push.axaml

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
<StackPanel Orientation="Vertical" Margin="8,0">
1111
<TextBlock FontSize="18"
1212
Classes="bold"
13-
Text="{DynamicResource Text.Push.Title}"/>
13+
Text="{DynamicResource Text.Push.Title}" />
1414

1515
<Grid Margin="0,16,0,0" RowDefinitions="32,32,32,Auto,Auto,32,32" ColumnDefinitions="130,*">
1616
<TextBlock Grid.Row="0" Grid.Column="0"
1717
HorizontalAlignment="Right" VerticalAlignment="Center"
1818
Margin="0,0,8,0"
19-
Text="{DynamicResource Text.Push.Local}"/>
19+
Text="{DynamicResource Text.Push.Local}" />
2020
<ComboBox Grid.Row="0" Grid.Column="1"
2121
Height="28" Padding="8,0"
2222
VerticalAlignment="Center" HorizontalAlignment="Stretch"
@@ -28,22 +28,22 @@
2828
<DataTemplate x:DataType="{x:Type m:Branch}">
2929
<StackPanel Orientation="Horizontal" Height="20" VerticalAlignment="Center">
3030
<Path Margin="0,0,8,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG1}" Data="{StaticResource Icons.Branch}"/>
31-
<TextBlock Text="{Binding Name}"/>
31+
<TextBlock Text="{Binding Name}" />
3232
</StackPanel>
3333
</DataTemplate>
3434
</ComboBox.ItemTemplate>
3535

3636
<ComboBox.ItemContainerTheme>
3737
<ControlTheme TargetType="ComboBoxItem" x:DataType="m:Branch" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
38-
<Setter Property="TextSearch.Text" Value="{Binding Name}"/>
38+
<Setter Property="TextSearch.Text" Value="{Binding Name}" />
3939
</ControlTheme>
4040
</ComboBox.ItemContainerTheme>
4141
</ComboBox>
4242

4343
<TextBlock Grid.Row="1" Grid.Column="0"
4444
HorizontalAlignment="Right" VerticalAlignment="Center"
4545
Margin="0,0,8,0"
46-
Text="{DynamicResource Text.Push.Remote}"/>
46+
Text="{DynamicResource Text.Push.Remote}" />
4747
<ComboBox Grid.Row="1" Grid.Column="1"
4848
Height="28" Padding="8,0"
4949
VerticalAlignment="Center" HorizontalAlignment="Stretch"
@@ -53,7 +53,7 @@
5353
<DataTemplate x:DataType="{x:Type m:Remote}">
5454
<StackPanel Orientation="Horizontal" Height="20" VerticalAlignment="Center">
5555
<Path Margin="0,2,8,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG1}" Data="{StaticResource Icons.Remote}"/>
56-
<TextBlock Text="{Binding Name}"/>
56+
<TextBlock Text="{Binding Name}" />
5757
</StackPanel>
5858
</DataTemplate>
5959
</ComboBox.ItemTemplate>
@@ -62,60 +62,55 @@
6262
<TextBlock Grid.Row="2" Grid.Column="0"
6363
HorizontalAlignment="Right" VerticalAlignment="Center"
6464
Margin="0,0,8,0"
65-
Text="{DynamicResource Text.Push.To}"/>
66-
<ComboBox Grid.Row="2" Grid.Column="1"
67-
Height="28" Padding="8,0"
68-
VerticalAlignment="Center" HorizontalAlignment="Stretch"
69-
ItemsSource="{Binding RemoteBranches}"
70-
IsTextSearchEnabled="True"
71-
SelectedItem="{Binding SelectedRemoteBranch, Mode=TwoWay}">
72-
<ComboBox.ItemTemplate>
73-
<DataTemplate x:DataType="{x:Type m:Branch}">
74-
<StackPanel Orientation="Horizontal" Height="20" VerticalAlignment="Center">
75-
<Path Margin="0,0,8,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG1}" Data="{StaticResource Icons.Branch}"/>
76-
<TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
77-
<Border Height="14"
78-
CornerRadius="7"
79-
Margin="4,0,0,0" Padding="6,0"
80-
VerticalAlignment="Center"
81-
Background="Green"
82-
IsVisible="{Binding Head, Converter={x:Static StringConverters.IsNullOrEmpty}}">
83-
<TextBlock Text="NEW" FontSize="9" FontFamily="{DynamicResource Fonts.Monospace}" Foreground="White" VerticalAlignment="Center"/>
84-
</Border>
85-
</StackPanel>
86-
</DataTemplate>
87-
</ComboBox.ItemTemplate>
65+
Text="{DynamicResource Text.Push.To}" />
8866

89-
<ComboBox.ItemContainerTheme>
90-
<ControlTheme TargetType="ComboBoxItem" x:DataType="m:Branch" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
91-
<Setter Property="TextSearch.Text" Value="{Binding Name}"/>
92-
</ControlTheme>
93-
</ComboBox.ItemContainerTheme>
94-
</ComboBox>
67+
<TextBox Grid.Row="2" Grid.Column="1"
68+
Height="28" Padding="8,0"
69+
CornerRadius="3"
70+
Text="{Binding SelectedRemoteBranchName, Mode=TwoWay}">
71+
<TextBox.InnerLeftContent>
72+
<Path Margin="8,0,0,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG1}" Data="{StaticResource Icons.Branch}" />
73+
</TextBox.InnerLeftContent>
74+
<TextBox.InnerRightContent>
75+
<Border Height="14"
76+
Margin="0,0,8,0"
77+
CornerRadius="7"
78+
Padding="6,0"
79+
VerticalAlignment="Center"
80+
Background="Green"
81+
IsVisible="{Binding SelectedRemoteBranch.Head, Converter={x:Static StringConverters.IsNullOrEmpty}}">
82+
<TextBlock Text="NEW"
83+
FontSize="9"
84+
FontFamily="{DynamicResource Fonts.Monospace}"
85+
Foreground="White"
86+
VerticalAlignment="Center" />
87+
</Border>
88+
</TextBox.InnerRightContent>
89+
</TextBox>
9590

9691
<CheckBox Grid.Row="3" Grid.Column="1"
9792
Height="32"
9893
Content="{DynamicResource Text.Push.Tracking}"
9994
IsChecked="{Binding Tracking, Mode=TwoWay}"
10095
IsVisible="{Binding IsSetTrackOptionVisible}"
101-
ToolTip.Tip="-u"/>
96+
ToolTip.Tip="-u" />
10297

10398
<CheckBox Grid.Row="4" Grid.Column="1"
10499
Height="32"
105100
Content="{DynamicResource Text.Push.CheckSubmodules}"
106101
IsChecked="{Binding CheckSubmodules, Mode=TwoWay}"
107102
IsVisible="{Binding IsCheckSubmodulesVisible}"
108-
ToolTip.Tip="--recurse-submodules=check"/>
103+
ToolTip.Tip="--recurse-submodules=check" />
109104

110105
<CheckBox Grid.Row="5" Grid.Column="1"
111106
Content="{DynamicResource Text.Push.WithAllTags}"
112107
IsChecked="{Binding PushAllTags, Mode=TwoWay}"
113-
ToolTip.Tip="--tags"/>
108+
ToolTip.Tip="--tags" />
114109

115110
<CheckBox Grid.Row="6" Grid.Column="1"
116111
Content="{DynamicResource Text.Push.Force}"
117112
IsChecked="{Binding ForcePush, Mode=TwoWay}"
118-
ToolTip.Tip="--force-with-lease"/>
113+
ToolTip.Tip="--force-with-lease" />
119114
</Grid>
120115
</StackPanel>
121116
</UserControl>

0 commit comments

Comments
 (0)