2024-02-05 23:08:37 -08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class Push : Popup
|
|
|
|
|
{
|
|
|
|
|
public bool HasSpecifiedLocalBranch
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Required(ErrorMessage = "Local branch is required!!!")]
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Models.Branch SelectedLocalBranch
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _selectedLocalBranch;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SetProperty(ref _selectedLocalBranch, value))
|
2024-02-05 23:08:37 -08:00
|
|
|
|
AutoSelectBranchByRemote();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public List<Models.Branch> LocalBranches
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public List<Models.Remote> Remotes
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _repo.Remotes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Required(ErrorMessage = "Remote is required!!!")]
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Models.Remote SelectedRemote
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _selectedRemote;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (SetProperty(ref _selectedRemote, value))
|
|
|
|
|
AutoSelectBranchByRemote();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public List<Models.Branch> RemoteBranches
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _remoteBranches;
|
|
|
|
|
private set => SetProperty(ref _remoteBranches, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Required(ErrorMessage = "Remote branch is required!!!")]
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Models.Branch SelectedRemoteBranch
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _selectedRemoteBranch;
|
2024-06-17 23:27:59 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SetProperty(ref _selectedRemoteBranch, value))
|
|
|
|
|
IsSetTrackOptionVisible = value != null && _selectedLocalBranch.Upstream != value.FullName;
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool PushAllTags
|
|
|
|
|
{
|
2024-07-02 23:41:43 -07:00
|
|
|
|
get => _repo.Settings.PushAllTags;
|
|
|
|
|
set => _repo.Settings.PushAllTags = value;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 23:27:59 -07:00
|
|
|
|
public bool IsSetTrackOptionVisible
|
|
|
|
|
{
|
|
|
|
|
get => _isSetTrackOptionVisible;
|
|
|
|
|
private set => SetProperty(ref _isSetTrackOptionVisible, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 20:37:06 -07:00
|
|
|
|
public bool Tracking
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
} = true;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool ForcePush
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Push(Repository repo, Models.Branch localBranch)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
|
|
|
|
|
|
|
|
|
// Gather all local branches and find current branch.
|
|
|
|
|
LocalBranches = new List<Models.Branch>();
|
|
|
|
|
var current = null as Models.Branch;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var branch in _repo.Branches)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (branch.IsLocal)
|
|
|
|
|
LocalBranches.Add(branch);
|
|
|
|
|
if (branch.IsCurrent)
|
|
|
|
|
current = branch;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set default selected local branch.
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (localBranch != null)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_selectedLocalBranch = localBranch;
|
|
|
|
|
HasSpecifiedLocalBranch = true;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_selectedLocalBranch = current;
|
|
|
|
|
HasSpecifiedLocalBranch = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find preferred remote if selected local branch has upstream.
|
2024-07-14 09:30:31 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(_selectedLocalBranch?.Upstream))
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
|
|
|
|
foreach (var branch in repo.Branches)
|
|
|
|
|
{
|
|
|
|
|
if (!branch.IsLocal && _selectedLocalBranch.Upstream == branch.FullName)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_selectedRemote = repo.Remotes.Find(x => x.Name == branch.Remote);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set default remote to the first if haven't been set.
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (_selectedRemote == null)
|
|
|
|
|
_selectedRemote = repo.Remotes[0];
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
|
|
// Auto select preferred remote branch.
|
|
|
|
|
AutoSelectBranchByRemote();
|
|
|
|
|
|
|
|
|
|
View = new Views.Push() { DataContext = this };
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public override Task<bool> Sure()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo.SetWatcherEnabled(false);
|
2024-02-25 19:29:57 -08:00
|
|
|
|
|
|
|
|
|
var remoteBranchName = _selectedRemoteBranch.Name.Replace(" (new)", "");
|
|
|
|
|
ProgressDescription = $"Push {_selectedLocalBranch.Name} -> {_selectedRemote.Name}/{remoteBranchName} ...";
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var succ = new Commands.Push(
|
|
|
|
|
_repo.FullPath,
|
|
|
|
|
_selectedLocalBranch.Name,
|
|
|
|
|
_selectedRemote.Name,
|
|
|
|
|
remoteBranchName,
|
|
|
|
|
PushAllTags,
|
|
|
|
|
ForcePush,
|
2024-06-17 23:27:59 -07:00
|
|
|
|
_isSetTrackOptionVisible && Tracking,
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SetProgressDescription).Exec();
|
|
|
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
|
|
|
return succ;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private void AutoSelectBranchByRemote()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
// Gather branches.
|
|
|
|
|
var branches = new List<Models.Branch>();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var branch in _repo.Branches)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (branch.Remote == _selectedRemote.Name)
|
|
|
|
|
branches.Add(branch);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If selected local branch has upstream branch. Try to find it in current remote branches.
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(_selectedLocalBranch.Upstream))
|
|
|
|
|
{
|
|
|
|
|
foreach (var branch in branches)
|
|
|
|
|
{
|
|
|
|
|
if (_selectedLocalBranch.Upstream == branch.FullName)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
RemoteBranches = branches;
|
|
|
|
|
SelectedRemoteBranch = branch;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find best remote branch by name.
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var branch in branches)
|
|
|
|
|
{
|
|
|
|
|
if (_selectedLocalBranch.Name == branch.Name)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
RemoteBranches = branches;
|
|
|
|
|
SelectedRemoteBranch = branch;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add a fake new branch.
|
2024-03-17 18:37:06 -07:00
|
|
|
|
var fake = new Models.Branch()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Name = $"{_selectedLocalBranch.Name} (new)",
|
|
|
|
|
Remote = _selectedRemote.Name,
|
|
|
|
|
};
|
|
|
|
|
branches.Add(fake);
|
|
|
|
|
RemoteBranches = branches;
|
|
|
|
|
SelectedRemoteBranch = fake;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly Repository _repo = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private Models.Branch _selectedLocalBranch = null;
|
|
|
|
|
private Models.Remote _selectedRemote = null;
|
|
|
|
|
private List<Models.Branch> _remoteBranches = new List<Models.Branch>();
|
|
|
|
|
private Models.Branch _selectedRemoteBranch = null;
|
2024-06-17 23:27:59 -07:00
|
|
|
|
private bool _isSetTrackOptionVisible = false;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|