Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 33 additions & 38 deletions src/ViewModels/Push.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace SourceGit.ViewModels
Expand Down Expand Up @@ -44,20 +45,32 @@ public Models.Remote SelectedRemote
}
}

public List<Models.Branch> RemoteBranches
public Models.Branch SelectedRemoteBranch
{
get => _remoteBranches;
private set => SetProperty(ref _remoteBranches, value);
get => _selectedRemoteBranch;
private set => SetProperty(ref _selectedRemoteBranch, value);
}

[Required(ErrorMessage = "Remote branch is required!!!")]
public Models.Branch SelectedRemoteBranch
public string SelectedRemoteBranchName
{
get => _selectedRemoteBranch;
get => _selectedRemoteBranchName;
set
{
if (SetProperty(ref _selectedRemoteBranch, value))
IsSetTrackOptionVisible = value != null && _selectedLocalBranch.Upstream != value.FullName;
if (SetProperty(ref _selectedRemoteBranchName, value))
{
SelectedRemoteBranch = null;

foreach (var branch in _repo.Branches.Where(branch => branch.Remote == _selectedRemote.Name))
{
if (_selectedRemoteBranchName == branch.Name)
{
SelectedRemoteBranch = branch;
}
}

IsSetTrackOptionVisible = SelectedRemoteBranch != null && _selectedLocalBranch.Upstream != SelectedRemoteBranch.FullName;
}
}
}

Expand Down Expand Up @@ -130,7 +143,7 @@ public Push(Repository repo, Models.Branch localBranch)
{
if (!branch.IsLocal && _selectedLocalBranch.Upstream == branch.FullName)
{
_selectedRemote = repo.Remotes.Find(x => x.Name == branch.Remote);
_selectedRemote = repo.Remotes.Find(remote => remote.Name == branch.Remote);
break;
}
}
Expand All @@ -141,7 +154,7 @@ public Push(Repository repo, Models.Branch localBranch)
{
var remote = null as Models.Remote;
if (!string.IsNullOrEmpty(_repo.Settings.DefaultRemote))
remote = repo.Remotes.Find(x => x.Name == _repo.Settings.DefaultRemote);
remote = repo.Remotes.Find(remote => remote.Name == _repo.Settings.DefaultRemote);

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

public override bool CanStartDirectly()
{
return !string.IsNullOrEmpty(_selectedRemoteBranch?.Head);
return !string.IsNullOrEmpty(SelectedRemoteBranch?.Head);
}

public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);

var remoteBranchName = _selectedRemoteBranch.Name;
ProgressDescription = $"Push {_selectedLocalBranch.Name} -> {_selectedRemote.Name}/{remoteBranchName} ...";
ProgressDescription =
$"Push {_selectedLocalBranch.Name} -> {_selectedRemote.Name}/{_selectedRemoteBranchName} ...";

return Task.Run(() =>
{
var succ = new Commands.Push(
_repo.FullPath,
_selectedLocalBranch.Name,
_selectedRemote.Name,
remoteBranchName,
_selectedRemoteBranchName,
PushAllTags,
_repo.Submodules.Count > 0 && CheckSubmodules,
_isSetTrackOptionVisible && Tracking,
Expand All @@ -183,54 +196,36 @@ public override Task<bool> Sure()

private void AutoSelectBranchByRemote()
{
// Gather branches.
var branches = new List<Models.Branch>();
foreach (var branch in _repo.Branches)
{
if (branch.Remote == _selectedRemote.Name)
branches.Add(branch);
}

// If selected local branch has upstream. Try to find it in current remote branches.
if (!string.IsNullOrEmpty(_selectedLocalBranch.Upstream))
{
foreach (var branch in branches)
foreach (var branch in _repo.Branches.Where(branch => branch.Remote == _selectedRemote.Name))
{
if (_selectedLocalBranch.Upstream == branch.FullName)
{
RemoteBranches = branches;
SelectedRemoteBranch = branch;
SelectedRemoteBranchName = branch.Name;
return;
}
}
}

// Try to find a remote branch with the same name of selected local branch.
foreach (var branch in branches)
foreach (var branch in _repo.Branches.Where(branch => branch.Remote == _selectedRemote.Name))
{
if (_selectedLocalBranch.Name == branch.Name)
{
RemoteBranches = branches;
SelectedRemoteBranch = branch;
SelectedRemoteBranchName = branch.Name;
return;
}
}

// Add a fake new branch.
var fake = new Models.Branch()
{
Name = _selectedLocalBranch.Name,
Remote = _selectedRemote.Name,
};
branches.Add(fake);
RemoteBranches = branches;
SelectedRemoteBranch = fake;
SelectedRemoteBranchName = _selectedLocalBranch.Name;
}

private readonly Repository _repo = null;
private Models.Branch _selectedLocalBranch = null;
private Models.Remote _selectedRemote = null;
private List<Models.Branch> _remoteBranches = [];
private string _selectedRemoteBranchName = null;
private Models.Branch _selectedRemoteBranch = null;
private bool _isSetTrackOptionVisible = false;
}
Expand Down
52 changes: 23 additions & 29 deletions src/Views/Push.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,29 @@
HorizontalAlignment="Right" VerticalAlignment="Center"
Margin="0,0,8,0"
Text="{DynamicResource Text.Push.To}"/>
<ComboBox Grid.Row="2" Grid.Column="1"
Height="28" Padding="8,0"
VerticalAlignment="Center" HorizontalAlignment="Stretch"
ItemsSource="{Binding RemoteBranches}"
IsTextSearchEnabled="True"
SelectedItem="{Binding SelectedRemoteBranch, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="{x:Type m:Branch}">
<StackPanel Orientation="Horizontal" Height="20" VerticalAlignment="Center">
<Path Margin="0,0,8,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG1}" Data="{StaticResource Icons.Branch}"/>
<TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
<Border Height="14"
CornerRadius="7"
Margin="4,0,0,0" Padding="6,0"
VerticalAlignment="Center"
Background="Green"
IsVisible="{Binding Head, Converter={x:Static StringConverters.IsNullOrEmpty}}">
<TextBlock Text="NEW" FontSize="9" FontFamily="{DynamicResource Fonts.Monospace}" Foreground="White" VerticalAlignment="Center"/>
</Border>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>

<ComboBox.ItemContainerTheme>
<ControlTheme TargetType="ComboBoxItem" x:DataType="m:Branch" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
<Setter Property="TextSearch.Text" Value="{Binding Name}"/>
</ControlTheme>
</ComboBox.ItemContainerTheme>
</ComboBox>
<TextBox Grid.Row="2" Grid.Column="1"
Height="28" Padding="8,0"
CornerRadius="3"
Text="{Binding SelectedRemoteBranchName, Mode=TwoWay}">
<TextBox.InnerLeftContent>
<Path Margin="8,0,0,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG1}" Data="{StaticResource Icons.Branch}"/>
</TextBox.InnerLeftContent>
<TextBox.InnerRightContent>
<Border Height="14"
Margin="0,0,8,0"
CornerRadius="7"
Padding="6,0"
VerticalAlignment="Center"
Background="Green"
IsVisible="{Binding SelectedRemoteBranch.Head, Converter={x:Static StringConverters.IsNullOrEmpty}}">
<TextBlock Text="NEW"
FontSize="9"
FontFamily="{DynamicResource Fonts.Monospace}"
Foreground="White"
VerticalAlignment="Center"/>
</Border>
</TextBox.InnerRightContent>
</TextBox>

<CheckBox Grid.Row="3" Grid.Column="1"
Height="32"
Expand Down