enhance: Git LFS supports (#209)

* add a new context menu to push local LFS object to selected remote
* supports to choose remote for fetch/pull/push/lock/unlock actions
* auto select remote if there's only one remote
This commit is contained in:
leo 2024-06-26 11:49:56 +08:00
parent f18ecf53eb
commit 0c21bcd06a
No known key found for this signature in database
14 changed files with 359 additions and 59 deletions

View file

@ -1,12 +1,22 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class LFSFetch : Popup
{
public List<Models.Remote> Remotes => _repo.Remotes;
public Models.Remote SelectedRemote
{
get;
set;
}
public LFSFetch(Repository repo)
{
_repo = repo;
SelectedRemote = _repo.Remotes[0];
View = new Views.LFSFetch() { DataContext = this };
}
@ -16,7 +26,7 @@ namespace SourceGit.ViewModels
ProgressDescription = $"Fetching LFS objects from remote ...";
return Task.Run(() =>
{
new Commands.LFS(_repo.FullPath).Fetch(SetProgressDescription);
new Commands.LFS(_repo.FullPath).Fetch(SelectedRemote.Name, SetProgressDescription);
CallUIThread(() => _repo.SetWatcherEnabled(true));
return true;
});

View file

@ -27,14 +27,15 @@ namespace SourceGit.ViewModels
private set;
}
public LFSLocks(string repo)
public LFSLocks(string repo, string remote)
{
_repo = repo;
_remote = remote;
Locks = new AvaloniaList<Models.LFSLock>();
Task.Run(() =>
{
var collect = new Commands.LFS(_repo).Locks();
var collect = new Commands.LFS(_repo).Locks(_remote);
Dispatcher.UIThread.Invoke(() =>
{
if (collect.Count > 0)
@ -54,7 +55,7 @@ namespace SourceGit.ViewModels
IsLoading = true;
Task.Run(() =>
{
var succ = new Commands.LFS(_repo).Unlock(lfsLock.ID, force);
var succ = new Commands.LFS(_repo).Unlock(_remote, lfsLock.ID, force);
Dispatcher.UIThread.Invoke(() =>
{
if (succ)
@ -67,6 +68,7 @@ namespace SourceGit.ViewModels
}
private string _repo = string.Empty;
private string _remote = string.Empty;
private bool _isLoading = true;
private bool _isEmpty = false;
}

View file

@ -1,12 +1,22 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class LFSPull : Popup
{
public List<Models.Remote> Remotes => _repo.Remotes;
public Models.Remote SelectedRemote
{
get;
set;
}
public LFSPull(Repository repo)
{
_repo = repo;
SelectedRemote = _repo.Remotes[0];
View = new Views.LFSPull() { DataContext = this };
}
@ -16,7 +26,7 @@ namespace SourceGit.ViewModels
ProgressDescription = $"Pull LFS objects from remote ...";
return Task.Run(() =>
{
new Commands.LFS(_repo.FullPath).Pull(SetProgressDescription);
new Commands.LFS(_repo.FullPath).Pull(SelectedRemote.Name, SetProgressDescription);
CallUIThread(() => _repo.SetWatcherEnabled(true));
return true;
});

37
src/ViewModels/LFSPush.cs Normal file
View file

@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class LFSPush : Popup
{
public List<Models.Remote> Remotes => _repo.Remotes;
public Models.Remote SelectedRemote
{
get;
set;
}
public LFSPush(Repository repo)
{
_repo = repo;
SelectedRemote = _repo.Remotes[0];
View = new Views.LFSPush() { DataContext = this };
}
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
ProgressDescription = $"Push LFS objects to remote ...";
return Task.Run(() =>
{
new Commands.LFS(_repo.FullPath).Push(SelectedRemote.Name, SetProgressDescription);
CallUIThread(() => _repo.SetWatcherEnabled(true));
return true;
});
}
private readonly Repository _repo = null;
}
}

View file

@ -911,6 +911,24 @@ namespace SourceGit.ViewModels
};
menu.Items.Add(pull);
var push = new MenuItem();
push.Header = App.Text("GitLFS.Push");
push.Icon = App.CreateMenuIcon("Icons.Push");
push.IsEnabled = Remotes.Count > 0;
push.Click += (o, e) =>
{
if (PopupHost.CanCreatePopup())
{
if (Remotes.Count == 1)
PopupHost.ShowAndStartPopup(new LFSPush(this));
else
PopupHost.ShowPopup(new LFSPush(this));
}
e.Handled = true;
};
menu.Items.Add(push);
var prune = new MenuItem();
prune.Header = App.Text("GitLFS.Prune");
prune.Icon = App.CreateMenuIcon("Icons.Clean");
@ -927,13 +945,33 @@ namespace SourceGit.ViewModels
var locks = new MenuItem();
locks.Header = App.Text("GitLFS.Locks");
locks.Icon = App.CreateMenuIcon("Icons.Lock");
locks.Click += (o, e) =>
locks.IsEnabled = Remotes.Count > 0;
if (Remotes.Count == 1)
{
var dialog = new Views.LFSLocks() { DataContext = new LFSLocks(_fullpath) };
dialog.Show(App.GetTopLevel() as Window);
locks.Click += (o, e) =>
{
var dialog = new Views.LFSLocks() { DataContext = new LFSLocks(_fullpath, Remotes[0].Name) };
dialog.Show(App.GetTopLevel() as Window);
e.Handled = true;
};
}
else
{
foreach (var remote in Remotes)
{
var remoteName = remote.Name;
var lockRemote = new MenuItem();
lockRemote.Header = remoteName;
lockRemote.Click += (o, e) =>
{
var dialog = new Views.LFSLocks() { DataContext = new LFSLocks(_fullpath, remoteName) };
dialog.Show(App.GetTopLevel() as Window);
e.Handled = true;
};
locks.Items.Add(lockRemote);
}
}
e.Handled = true;
};
menu.Items.Add(new MenuItem() { Header = "-" });
menu.Items.Add(locks);
}

View file

@ -643,27 +643,71 @@ namespace SourceGit.ViewModels
var lfsLock = new MenuItem();
lfsLock.Header = App.Text("GitLFS.Locks.Lock");
lfsLock.Icon = App.CreateMenuIcon("Icons.Lock");
lfsLock.Click += async (_, e) =>
lfsLock.IsEnabled = _repo.Remotes.Count > 0;
if (_repo.Remotes.Count == 1)
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Lock(change.Path));
if (succ)
App.SendNotification(_repo.FullPath, $"Lock file \"{change.Path}\" successfully!");
lfsLock.Click += async (o, e) =>
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Lock(_repo.Remotes[0].Name, change.Path));
if (succ)
App.SendNotification(_repo.FullPath, $"Lock file \"{change.Path}\" successfully!");
e.Handled = true;
};
e.Handled = true;
};
}
else
{
foreach (var remote in _repo.Remotes)
{
var remoteName = remote.Name;
var lockRemote = new MenuItem();
lockRemote.Header = remoteName;
lockRemote.Click += async (o, e) =>
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Lock(remoteName, change.Path));
if (succ)
App.SendNotification(_repo.FullPath, $"Lock file \"{change.Path}\" successfully!");
e.Handled = true;
};
lfsLock.Items.Add(lockRemote);
}
}
lfs.Items.Add(lfsLock);
var lfsUnlock = new MenuItem();
lfsUnlock.Header = App.Text("GitLFS.Locks.Unlock");
lfsUnlock.Icon = App.CreateMenuIcon("Icons.Unlock");
lfsUnlock.Click += async (_, e) =>
lfsUnlock.IsEnabled = _repo.Remotes.Count > 0;
if (_repo.Remotes.Count == 1)
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Unlock(change.Path, false));
if (succ)
App.SendNotification(_repo.FullPath, $"Unlock file \"{change.Path}\" successfully!");
lfsUnlock.Click += async (o, e) =>
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Unlock(_repo.Remotes[0].Name, change.Path, false));
if (succ)
App.SendNotification(_repo.FullPath, $"Unlock file \"{change.Path}\" successfully!");
e.Handled = true;
};
e.Handled = true;
};
}
else
{
foreach (var remote in _repo.Remotes)
{
var remoteName = remote.Name;
var unlockRemote = new MenuItem();
unlockRemote.Header = remoteName;
unlockRemote.Click += async (o, e) =>
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Unlock(remoteName, change.Path, false));
if (succ)
App.SendNotification(_repo.FullPath, $"Unlock file \"{change.Path}\" successfully!");
e.Handled = true;
};
lfsUnlock.Items.Add(unlockRemote);
}
}
lfs.Items.Add(lfsUnlock);
menu.Items.Add(lfs);
@ -926,28 +970,71 @@ namespace SourceGit.ViewModels
var lfsLock = new MenuItem();
lfsLock.Header = App.Text("GitLFS.Locks.Lock");
lfsLock.Icon = App.CreateMenuIcon("Icons.Lock");
lfsLock.Click += async (_, e) =>
lfsLock.IsEnabled = _repo.Remotes.Count > 0;
if (_repo.Remotes.Count == 1)
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Lock(change.Path));
if (succ)
App.SendNotification(_repo.FullPath, $"Lock file \"{change.Path}\" successfully!");
lfsLock.Click += async (o, e) =>
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Lock(_repo.Remotes[0].Name, change.Path));
if (succ)
App.SendNotification(_repo.FullPath, $"Lock file \"{change.Path}\" successfully!");
e.Handled = true;
};
lfs.Items.Add(new MenuItem() { Header = "-" });
e.Handled = true;
};
}
else
{
foreach (var remote in _repo.Remotes)
{
var remoteName = remote.Name;
var lockRemote = new MenuItem();
lockRemote.Header = remoteName;
lockRemote.Click += async (o, e) =>
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Lock(remoteName, change.Path));
if (succ)
App.SendNotification(_repo.FullPath, $"Lock file \"{change.Path}\" successfully!");
e.Handled = true;
};
lfsLock.Items.Add(lockRemote);
}
}
lfs.Items.Add(lfsLock);
var lfsUnlock = new MenuItem();
lfsUnlock.Header = App.Text("GitLFS.Locks.Unlock");
lfsUnlock.Icon = App.CreateMenuIcon("Icons.Unlock");
lfsUnlock.Click += async (_, e) =>
lfsUnlock.IsEnabled = _repo.Remotes.Count > 0;
if (_repo.Remotes.Count == 1)
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Unlock(change.Path, false));
if (succ)
App.SendNotification(_repo.FullPath, $"Unlock file \"{change.Path}\" successfully!");
lfsUnlock.Click += async (o, e) =>
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Unlock(_repo.Remotes[0].Name, change.Path, false));
if (succ)
App.SendNotification(_repo.FullPath, $"Unlock file \"{change.Path}\" successfully!");
e.Handled = true;
};
e.Handled = true;
};
}
else
{
foreach (var remote in _repo.Remotes)
{
var remoteName = remote.Name;
var unlockRemote = new MenuItem();
unlockRemote.Header = remoteName;
unlockRemote.Click += async (o, e) =>
{
var succ = await Task.Run(() => new Commands.LFS(_repo.FullPath).Unlock(remoteName, change.Path, false));
if (succ)
App.SendNotification(_repo.FullPath, $"Unlock file \"{change.Path}\" successfully!");
e.Handled = true;
};
lfsUnlock.Items.Add(unlockRemote);
}
}
lfs.Items.Add(lfsUnlock);
menu.Items.Add(lfs);