Skip to content

refactor: move I/O operations out of constructors #1498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public enum EditorType
public bool RaiseError { get; set; } = true;
public Models.ICommandLog Log { get; set; } = null;

public bool Exec()
public virtual bool Exec()
{
Log?.AppendLine($"$ git {Args}\n");

Expand Down
12 changes: 10 additions & 2 deletions src/Commands/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public Fetch(string repo, string remote, bool noTags, bool force)
{
WorkingDirectory = repo;
Context = repo;
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
_remote = remote;
Args = "fetch --progress --verbose ";

if (noTags)
Expand All @@ -24,8 +24,16 @@ public Fetch(string repo, Models.Branch local, Models.Branch remote)
{
WorkingDirectory = repo;
Context = repo;
SSHKey = new Config(repo).Get($"remote.{remote.Remote}.sshkey");
_remote = remote.Remote;
Args = $"fetch --progress --verbose {remote.Remote} {remote.Name}:{local.Name}";
}

public override bool Exec()
{
SSHKey = new Config(Context).Get($"remote.{_remote}.sshkey");
return base.Exec();
}

private readonly string _remote;
}
}
10 changes: 9 additions & 1 deletion src/Commands/Pull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ public Pull(string repo, string remote, string branch, bool useRebase)
{
WorkingDirectory = repo;
Context = repo;
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
_remote = remote;
Args = "pull --verbose --progress ";

if (useRebase)
Args += "--rebase=true ";

Args += $"{remote} {branch}";
}

public override bool Exec()
{
SSHKey = new Config(Context).Get($"remote.{_remote}.sshkey");
return base.Exec();
}

private readonly string _remote;
}
}
12 changes: 10 additions & 2 deletions src/Commands/Push.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public Push(string repo, string local, string remote, string remoteBranch, bool
{
WorkingDirectory = repo;
Context = repo;
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
_remote = remote;
Args = "push --progress --verbose ";

if (withTags)
Expand All @@ -25,13 +25,21 @@ public Push(string repo, string remote, string refname, bool isDelete)
{
WorkingDirectory = repo;
Context = repo;
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
_remote = remote;
Args = "push ";

if (isDelete)
Args += "--delete ";

Args += $"{remote} {refname}";
}

public override bool Exec()
{
SSHKey = new Config(Context).Get($"remote.{_remote}.sshkey");
return base.Exec();
}

private readonly string _remote;
}
}
4 changes: 1 addition & 3 deletions src/ViewModels/AIAssistant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public AIAssistant(Repository repo, Models.OpenAIService service, List<Models.Ch
_changes = changes;
_onApply = onApply;
_cancel = new CancellationTokenSource();

Gen();
}

public void Regen()
Expand All @@ -52,7 +50,7 @@ public void Cancel()
_cancel?.Cancel();
}

private void Gen()
public void Gen()
{
Text = string.Empty;
IsGenerating = true;
Expand Down
3 changes: 3 additions & 0 deletions src/ViewModels/AssumeUnchangedManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public AssumeUnchangedManager(Repository repo)
{
_repo = repo;
Files = new AvaloniaList<string>();
}

public void Load()
{
Task.Run(() =>
{
var collect = new Commands.QueryAssumeUnchangedFiles(_repo.FullPath).Result();
Expand Down
4 changes: 1 addition & 3 deletions src/ViewModels/BranchCompare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public BranchCompare(string repo, Models.Branch baseBranch, Models.Branch toBran
_repo = repo;
_based = baseBranch;
_to = toBranch;

Refresh();
}

public void NavigateTo(string commitSHA)
Expand Down Expand Up @@ -176,7 +174,7 @@ public ContextMenu CreateChangeContextMenu()
return menu;
}

private void Refresh()
public void Refresh()
{
Task.Run(() =>
{
Expand Down
3 changes: 3 additions & 0 deletions src/ViewModels/Clone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public Clone(string pageId)
_parentFolder = activeWorkspace?.DefaultCloneDir;
if (string.IsNullOrEmpty(ParentFolder))
_parentFolder = Preferences.Instance.GitDefaultCloneDir;
}

public void Load()
{
Task.Run(async () =>
{
try
Expand Down
27 changes: 17 additions & 10 deletions src/ViewModels/Conflict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public ConflictSourceBranch(string name, string head, Models.Commit revision)
Revision = revision;
}

public ConflictSourceBranch(Repository repo, Models.Branch branch)
public ConflictSourceBranch(Models.Branch branch, Models.Commit revision)
{
Name = branch.Name;
Head = branch.Head;
Revision = new Commands.QuerySingleCommit(repo.FullPath, branch.Head).Result() ?? new Models.Commit() { SHA = branch.Head };
Revision = revision ?? new Models.Commit() { SHA = branch.Head };
}
}

Expand Down Expand Up @@ -61,25 +61,31 @@ public bool CanUseExternalMergeTool

public Conflict(Repository repo, WorkingCopy wc, Models.Change change)
{
_repo = repo;
_wc = wc;
_change = change;
}

var isSubmodule = repo.Submodules.Find(x => x.Path.Equals(change.Path, StringComparison.Ordinal)) != null;
public void Load()
{
var isSubmodule = _repo.Submodules.Find(x => x.Path.Equals(_change.Path, StringComparison.Ordinal)) != null;
if (!isSubmodule && (_change.ConflictReason == Models.ConflictReason.BothAdded || _change.ConflictReason == Models.ConflictReason.BothModified))
{
CanUseExternalMergeTool = true;
IsResolved = new Commands.IsConflictResolved(repo.FullPath, change).Result();
IsResolved = new Commands.IsConflictResolved(_repo.FullPath, _change).Result();
}

var context = wc.InProgressContext;
var context = _wc.InProgressContext;
var revision = new Commands.QuerySingleCommit(_repo.FullPath, _repo.CurrentBranch.Head).Result();
var mine = new ConflictSourceBranch(_repo.CurrentBranch, revision);
if (context is CherryPickInProgress cherryPick)
{
Theirs = cherryPick.Head;
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
Mine = mine;
}
else if (context is RebaseInProgress rebase)
{
var b = repo.Branches.Find(x => x.IsLocal && x.Name == rebase.HeadName);
var b = _repo.Branches.Find(x => x.IsLocal && x.Name == rebase.HeadName);
if (b != null)
Theirs = new ConflictSourceBranch(b.Name, b.Head, rebase.StoppedAt);
else
Expand All @@ -90,17 +96,17 @@ public Conflict(Repository repo, WorkingCopy wc, Models.Change change)
else if (context is RevertInProgress revert)
{
Theirs = revert.Head;
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
Mine = mine;
}
else if (context is MergeInProgress merge)
{
Theirs = merge.Source;
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
Mine = mine;
}
else
{
Theirs = "Stash or Patch";
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
Mine = mine;
}
}

Expand All @@ -119,6 +125,7 @@ public void OpenExternalMergeTool()
_wc.UseExternalMergeTool(_change);
}

private readonly Repository _repo;
private WorkingCopy _wc = null;
private Models.Change _change = null;
}
Expand Down
7 changes: 5 additions & 2 deletions src/ViewModels/CreateTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public CreateTag(Repository repo, Models.Branch branch)
_basedOn = branch.Head;

BasedOn = branch;
SignTag = new Commands.Config(repo.FullPath).Get("tag.gpgsign").Equals("true", StringComparison.OrdinalIgnoreCase);
}

public CreateTag(Repository repo, Models.Commit commit)
Expand All @@ -60,7 +59,11 @@ public CreateTag(Repository repo, Models.Commit commit)
_basedOn = commit.SHA;

BasedOn = commit;
SignTag = new Commands.Config(repo.FullPath).Get("tag.gpgsign").Equals("true", StringComparison.OrdinalIgnoreCase);
}

public void Load()
{
SignTag = new Commands.Config(_repo.FullPath).Get("tag.gpgsign").Equals("true", StringComparison.OrdinalIgnoreCase);
}

public static ValidationResult ValidateTagName(string name, ValidationContext ctx)
Expand Down
5 changes: 4 additions & 1 deletion src/ViewModels/EditRemote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ public EditRemote(Repository repo, Models.Remote remote)
_name = remote.Name;
_url = remote.URL;
_useSSH = Models.Remote.IsSSH(remote.URL);
}

public void Load()
{
if (_useSSH)
{
SSHKey = new Commands.Config(repo.FullPath).Get($"remote.{remote.Name}.sshkey");
SSHKey = new Commands.Config(_repo.FullPath).Get($"remote.{_remote.Name}.sshkey");
}
}

Expand Down
36 changes: 27 additions & 9 deletions src/ViewModels/FileHistories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ public object ViewContent
set => SetProperty(ref _viewContent, value);
}

public FileHistoriesSingleRevision(Repository repo, string file, Models.Commit revision, bool prevIsDiffMode)
public static FileHistoriesSingleRevision Create(Repository repo, string file, Models.Commit revision, bool prevIsDiffMode)
{
var obj = new FileHistoriesSingleRevision(repo, file, revision, prevIsDiffMode);
obj.RefreshViewContent();
return obj;
}

private FileHistoriesSingleRevision(Repository repo, string file, Models.Commit revision, bool prevIsDiffMode)
{
_repo = repo;
_file = file;
_revision = revision;
_isDiffMode = prevIsDiffMode;
_viewContent = null;

RefreshViewContent();
}

public Task<bool> ResetToSelectedRevision()
Expand Down Expand Up @@ -188,13 +193,19 @@ public DiffContext ViewContent
set => SetProperty(ref _viewContent, value);
}

public FileHistoriesCompareRevisions(Repository repo, string file, Models.Commit start, Models.Commit end)
public static FileHistoriesCompareRevisions Create(Repository repo, string file, Models.Commit start, Models.Commit end)
{
var obj = new FileHistoriesCompareRevisions(repo, file, start, end);
obj.RefreshViewContent();
return obj;
}

private FileHistoriesCompareRevisions(Repository repo, string file, Models.Commit start, Models.Commit end)
{
_repo = repo;
_file = file;
_startPoint = start;
_endPoint = end;
RefreshViewContent();
}

public void Swap()
Expand Down Expand Up @@ -275,11 +286,16 @@ public FileHistories(Repository repo, string file, string commit = null)
Title = file;

_repo = repo;
_file = file;
_commit = commit;
}

public void Load()
{
Task.Run(() =>
{
var based = commit ?? string.Empty;
var commits = new Commands.QueryCommits(_repo.FullPath, $"--date-order -n 10000 {based} -- \"{file}\"", false).Result();
var based = _commit ?? string.Empty;
var commits = new Commands.QueryCommits(_repo.FullPath, $"--date-order -n 10000 {based} -- \"{_file}\"", false).Result();
Dispatcher.UIThread.Invoke(() =>
{
IsLoading = false;
Expand All @@ -296,8 +312,8 @@ public FileHistories(Repository repo, string file, string commit = null)

ViewContent = SelectedCommits.Count switch
{
1 => new FileHistoriesSingleRevision(_repo, file, SelectedCommits[0], _prevIsDiffMode),
2 => new FileHistoriesCompareRevisions(_repo, file, SelectedCommits[0], SelectedCommits[1]),
1 => FileHistoriesSingleRevision.Create(_repo, _file, SelectedCommits[0], _prevIsDiffMode),
2 => FileHistoriesCompareRevisions.Create(_repo, _file, SelectedCommits[0], SelectedCommits[1]),
_ => SelectedCommits.Count,
};
};
Expand All @@ -320,6 +336,8 @@ public string GetCommitFullMessage(Models.Commit commit)
}

private readonly Repository _repo = null;
private readonly string _file;
private readonly string _commit;
private bool _isLoading = true;
private bool _prevIsDiffMode = true;
private List<Models.Commit> _commits = null;
Expand Down
Loading