Merge branch 'sourcegit-scm:master' into master

This commit is contained in:
qiufengshe 2025-04-28 11:28:01 +08:00 committed by GitHub
commit af3fe56d54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
64 changed files with 1353 additions and 549 deletions

View file

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class AIAssistant : ObservableObject
{
public bool IsGenerating
{
get => _isGenerating;
private set => SetProperty(ref _isGenerating, value);
}
public string Text
{
get => _text;
private set => SetProperty(ref _text, value);
}
public AIAssistant(Repository repo, Models.OpenAIService service, List<Models.Change> changes, Action<string> onApply)
{
_repo = repo;
_service = service;
_changes = changes;
_onApply = onApply;
_cancel = new CancellationTokenSource();
Gen();
}
public void Regen()
{
if (_cancel is { IsCancellationRequested: false })
_cancel.Cancel();
Gen();
}
public void Apply()
{
_onApply?.Invoke(Text);
}
public void Cancel()
{
_cancel?.Cancel();
}
private void Gen()
{
Text = string.Empty;
IsGenerating = true;
_cancel = new CancellationTokenSource();
Task.Run(() =>
{
new Commands.GenerateCommitMessage(_service, _repo.FullPath, _changes, _cancel.Token, message =>
{
Dispatcher.UIThread.Invoke(() => Text = message);
}).Exec();
Dispatcher.UIThread.Invoke(() => IsGenerating = false);
}, _cancel.Token);
}
private readonly Repository _repo = null;
private Models.OpenAIService _service = null;
private List<Models.Change> _changes = null;
private Action<string> _onApply = null;
private CancellationTokenSource _cancel = null;
private bool _isGenerating = false;
private string _text = string.Empty;
}
}

View file

@ -134,31 +134,7 @@ namespace SourceGit.ViewModels
public CommitDetail(Repository repo)
{
_repo = repo;
foreach (var remote in repo.Remotes)
{
if (remote.TryGetVisitURL(out var url))
{
var trimmedUrl = url;
if (url.EndsWith(".git"))
trimmedUrl = url.Substring(0, url.Length - 4);
if (url.StartsWith("https://github.com/", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = $"Github ({trimmedUrl.Substring(19)})", URLPrefix = $"{url}/commit/" });
else if (url.StartsWith("https://gitlab.", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = $"GitLab ({trimmedUrl.Substring(trimmedUrl.Substring(15).IndexOf('/') + 16)})", URLPrefix = $"{url}/-/commit/" });
else if (url.StartsWith("https://gitee.com/", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = $"Gitee ({trimmedUrl.Substring(18)})", URLPrefix = $"{url}/commit/" });
else if (url.StartsWith("https://bitbucket.org/", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = $"BitBucket ({trimmedUrl.Substring(22)})", URLPrefix = $"{url}/commits/" });
else if (url.StartsWith("https://codeberg.org/", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = $"Codeberg ({trimmedUrl.Substring(21)})", URLPrefix = $"{url}/commit/" });
else if (url.StartsWith("https://gitea.org/", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = $"Gitea ({trimmedUrl.Substring(18)})", URLPrefix = $"{url}/commit/" });
else if (url.StartsWith("https://git.sr.ht/", StringComparison.Ordinal))
WebLinks.Add(new Models.CommitLink() { Name = $"sourcehut ({trimmedUrl.Substring(18)})", URLPrefix = $"{url}/commit/" });
}
}
WebLinks = Models.CommitLink.Get(repo.Remotes);
}
public void Cleanup()
@ -173,7 +149,6 @@ namespace SourceGit.ViewModels
_diffContext = null;
_viewRevisionFileContent = null;
_cancellationSource = null;
WebLinks.Clear();
_revisionFiles = null;
_revisionFileSearchSuggestion = null;
}
@ -332,8 +307,7 @@ namespace SourceGit.ViewModels
history.Icon = App.CreateMenuIcon("Icons.Histories");
history.Click += (_, ev) =>
{
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, change.Path, _commit.SHA) };
window.Show();
App.ShowWindow(new FileHistories(_repo, change.Path, _commit.SHA), false);
ev.Handled = true;
};
@ -343,8 +317,7 @@ namespace SourceGit.ViewModels
blame.IsEnabled = change.Index != Models.ChangeState.Deleted;
blame.Click += (_, ev) =>
{
var window = new Views.Blame() { DataContext = new Blame(_repo.FullPath, change.Path, _commit.SHA) };
window.Show();
App.ShowWindow(new Blame(_repo.FullPath, change.Path, _commit.SHA), false);
ev.Handled = true;
};
@ -508,8 +481,7 @@ namespace SourceGit.ViewModels
history.Icon = App.CreateMenuIcon("Icons.Histories");
history.Click += (_, ev) =>
{
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, file.Path, _commit.SHA) };
window.Show();
App.ShowWindow(new FileHistories(_repo, file.Path, _commit.SHA), false);
ev.Handled = true;
};
@ -519,8 +491,7 @@ namespace SourceGit.ViewModels
blame.IsEnabled = file.Type == Models.ObjectType.Blob;
blame.Click += (_, ev) =>
{
var window = new Views.Blame() { DataContext = new Blame(_repo.FullPath, file.Path, _commit.SHA) };
window.Show();
App.ShowWindow(new Blame(_repo.FullPath, file.Path, _commit.SHA), false);
ev.Handled = true;
};
@ -607,10 +578,10 @@ namespace SourceGit.ViewModels
Task.Run(() =>
{
var message = new Commands.QueryCommitFullMessage(_repo.FullPath, _commit.SHA).Result();
var links = ParseLinksInMessage(message);
var inlines = ParseInlinesInMessage(message);
if (!token.IsCancellationRequested)
Dispatcher.UIThread.Invoke(() => FullMessage = new Models.CommitFullMessage { Message = message, Links = links });
Dispatcher.UIThread.Invoke(() => FullMessage = new Models.CommitFullMessage { Message = message, Inlines = inlines });
});
Task.Run(() =>
@ -662,13 +633,13 @@ namespace SourceGit.ViewModels
});
}
private List<Models.Hyperlink> ParseLinksInMessage(string message)
private List<Models.InlineElement> ParseInlinesInMessage(string message)
{
var links = new List<Models.Hyperlink>();
var inlines = new List<Models.InlineElement>();
if (_repo.Settings.IssueTrackerRules is { Count: > 0 } rules)
{
foreach (var rule in rules)
rule.Matches(links, message);
rule.Matches(inlines, message);
}
var matches = REG_SHA_FORMAT().Matches(message);
@ -681,7 +652,7 @@ namespace SourceGit.ViewModels
var start = match.Index;
var len = match.Length;
var intersect = false;
foreach (var link in links)
foreach (var link in inlines)
{
if (link.Intersect(start, len))
{
@ -696,13 +667,13 @@ namespace SourceGit.ViewModels
var sha = match.Groups[1].Value;
var isCommitSHA = new Commands.IsCommitSHA(_repo.FullPath, sha).Result();
if (isCommitSHA)
links.Add(new Models.Hyperlink(start, len, sha, true));
inlines.Add(new Models.InlineElement(Models.InlineElementType.CommitSHA, start, len, sha));
}
if (links.Count > 0)
links.Sort((l, r) => l.Start - r.Start);
if (inlines.Count > 0)
inlines.Sort((l, r) => l.Start - r.Start);
return links;
return inlines;
}
private void RefreshVisibleChanges()

View file

@ -305,11 +305,23 @@ namespace SourceGit.ViewModels
_repo.NavigateToCommit(commit.SHA);
}
public string GetCommitFullMessage(Models.Commit commit)
{
var sha = commit.SHA;
if (_fullCommitMessages.TryGetValue(sha, out var msg))
return msg;
msg = new Commands.QueryCommitFullMessage(_repo.FullPath, sha).Result();
_fullCommitMessages[sha] = msg;
return msg;
}
private readonly Repository _repo = null;
private readonly string _file = null;
private bool _isLoading = true;
private bool _prevIsDiffMode = true;
private List<Models.Commit> _commits = null;
private Dictionary<string, string> _fullCommitMessages = new Dictionary<string, string>();
private object _viewContent = null;
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
@ -62,6 +63,12 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _detailContext, value);
}
public Models.Bisect Bisect
{
get => _bisect;
private set => SetProperty(ref _bisect, value);
}
public GridLength LeftArea
{
get => _leftArea;
@ -111,6 +118,37 @@ namespace SourceGit.ViewModels
_detailContext = null;
}
public Models.BisectState UpdateBisectInfo()
{
var test = Path.Combine(_repo.GitDir, "BISECT_START");
if (!File.Exists(test))
{
Bisect = null;
return Models.BisectState.None;
}
var info = new Models.Bisect();
var dir = Path.Combine(_repo.GitDir, "refs", "bisect");
if (Directory.Exists(dir))
{
var files = new DirectoryInfo(dir).GetFiles();
foreach (var file in files)
{
if (file.Name.StartsWith("bad"))
info.Bads.Add(File.ReadAllText(file.FullName).Trim());
else if (file.Name.StartsWith("good"))
info.Goods.Add(File.ReadAllText(file.FullName).Trim());
}
}
Bisect = info;
if (info.Bads.Count == 0 || info.Goods.Count == 0)
return Models.BisectState.WaitingForRange;
else
return Models.BisectState.Detecting;
}
public void NavigateTo(string commitSHA)
{
var commit = _commits.Find(x => x.SHA.StartsWith(commitSHA, StringComparison.Ordinal));
@ -304,7 +342,7 @@ namespace SourceGit.ViewModels
if (picker.Count == 1)
{
log = _repo.CreateLog("Save as Patch");
var succ = false;
for (var i = 0; i < selected.Count; i++)
{
@ -570,11 +608,7 @@ namespace SourceGit.ViewModels
return;
}
App.OpenDialog(new Views.InteractiveRebase()
{
DataContext = new InteractiveRebase(_repo, current, commit)
});
App.ShowWindow(new InteractiveRebase(_repo, current, commit), true);
e.Handled = true;
};
@ -1213,7 +1247,7 @@ namespace SourceGit.ViewModels
}
builder.Append(".patch");
return System.IO.Path.Combine(dir, builder.ToString());
return Path.Combine(dir, builder.ToString());
}
private Repository _repo = null;
@ -1224,6 +1258,8 @@ namespace SourceGit.ViewModels
private long _navigationId = 0;
private object _detailContext = null;
private Models.Bisect _bisect = null;
private GridLength _leftArea = new GridLength(1, GridUnitType.Star);
private GridLength _rightArea = new GridLength(1, GridUnitType.Star);
private GridLength _topArea = new GridLength(1, GridUnitType.Star);

View file

@ -380,7 +380,7 @@ namespace SourceGit.ViewModels
configure.Header = App.Text("Workspace.Configure");
configure.Click += (_, e) =>
{
App.OpenDialog(new Views.ConfigureWorkspace() { DataContext = new ConfigureWorkspace() });
App.ShowWindow(new ConfigureWorkspace(), true);
e.Handled = true;
};
menu.Items.Add(configure);

View file

@ -212,6 +212,19 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _useSyntaxHighlighting, value);
}
public bool IgnoreCRAtEOLInDiff
{
get => Models.DiffOption.IgnoreCRAtEOL;
set
{
if (Models.DiffOption.IgnoreCRAtEOL != value)
{
Models.DiffOption.IgnoreCRAtEOL = value;
OnPropertyChanged();
}
}
}
public bool IgnoreWhitespaceChangesInDiff
{
get => _ignoreWhitespaceChangesInDiff;

View file

@ -114,6 +114,9 @@ namespace SourceGit.ViewModels
// Set default selected local branch.
if (localBranch != null)
{
if (LocalBranches.Count == 0)
LocalBranches.Add(localBranch);
_selectedLocalBranch = localBranch;
HasSpecifiedLocalBranch = true;
}

View file

@ -398,6 +398,18 @@ namespace SourceGit.ViewModels
get => _workingCopy?.InProgressContext;
}
public Models.BisectState BisectState
{
get => _bisectState;
private set => SetProperty(ref _bisectState, value);
}
public bool IsBisectCommandRunning
{
get => _isBisectCommandRunning;
private set => SetProperty(ref _isBisectCommandRunning, value);
}
public bool IsAutoFetching
{
get => _isAutoFetching;
@ -939,6 +951,31 @@ namespace SourceGit.ViewModels
return actions;
}
public void Bisect(string subcmd)
{
IsBisectCommandRunning = true;
SetWatcherEnabled(false);
var log = CreateLog($"Bisect({subcmd})");
Task.Run(() =>
{
var succ = new Commands.Bisect(_fullpath, subcmd).Use(log).Exec();
log.Complete();
Dispatcher.UIThread.Invoke(() =>
{
if (!succ)
App.RaiseException(_fullpath, log.Content.Substring(log.Content.IndexOf('\n')).Trim());
else if (log.Content.Contains("is the first bad commit"))
App.SendNotification(_fullpath, log.Content.Substring(log.Content.IndexOf('\n')).Trim());
MarkBranchesDirtyManually();
SetWatcherEnabled(true);
IsBisectCommandRunning = false;
});
});
}
public void RefreshBranches()
{
var branches = new Commands.QueryBranches(_fullpath).Result();
@ -1023,6 +1060,8 @@ namespace SourceGit.ViewModels
_histories.Commits = commits;
_histories.Graph = graph;
BisectState = _histories.UpdateBisectInfo();
if (!string.IsNullOrEmpty(_navigateToBranchDelayed))
{
var branch = _branches.Find(x => x.FullName == _navigateToBranchDelayed);
@ -1405,8 +1444,7 @@ namespace SourceGit.ViewModels
{
locks.Click += (_, e) =>
{
var dialog = new Views.LFSLocks() { DataContext = new LFSLocks(this, _remotes[0].Name) };
App.OpenDialog(dialog);
App.ShowWindow(new LFSLocks(this, _remotes[0].Name), true);
e.Handled = true;
};
}
@ -1419,8 +1457,7 @@ namespace SourceGit.ViewModels
lockRemote.Header = remoteName;
lockRemote.Click += (_, e) =>
{
var dialog = new Views.LFSLocks() { DataContext = new LFSLocks(this, remoteName) };
App.OpenDialog(dialog);
App.ShowWindow(new LFSLocks(this, remoteName), true);
e.Handled = true;
};
locks.Items.Add(lockRemote);
@ -1706,10 +1743,7 @@ namespace SourceGit.ViewModels
compareWithHead.Icon = App.CreateMenuIcon("Icons.Compare");
compareWithHead.Click += (_, _) =>
{
App.OpenDialog(new Views.BranchCompare()
{
DataContext = new BranchCompare(_fullpath, branch, _currentBranch)
});
App.ShowWindow(new BranchCompare(_fullpath, branch, _currentBranch), false);
};
menu.Items.Add(new MenuItem() { Header = "-" });
menu.Items.Add(compareWithHead);
@ -1989,10 +2023,7 @@ namespace SourceGit.ViewModels
compareWithHead.Icon = App.CreateMenuIcon("Icons.Compare");
compareWithHead.Click += (_, _) =>
{
App.OpenDialog(new Views.BranchCompare()
{
DataContext = new BranchCompare(_fullpath, branch, _currentBranch)
});
App.ShowWindow(new BranchCompare(_fullpath, branch, _currentBranch), false);
};
menu.Items.Add(compareWithHead);
@ -2635,6 +2666,9 @@ namespace SourceGit.ViewModels
private Timer _autoFetchTimer = null;
private DateTime _lastFetchTime = DateTime.MinValue;
private Models.BisectState _bisectState = Models.BisectState.None;
private bool _isBisectCommandRunning = false;
private string _navigateToBranchDelayed = string.Empty;
}
}

View file

@ -87,11 +87,8 @@ namespace SourceGit.ViewModels
var subdirs = dir.GetDirectories("*", opts);
foreach (var subdir in subdirs)
{
if (subdir.Name.Equals("node_modules", StringComparison.Ordinal) ||
subdir.Name.Equals(".svn", StringComparison.Ordinal) ||
subdir.Name.Equals(".vs", StringComparison.Ordinal) ||
subdir.Name.Equals(".vscode", StringComparison.Ordinal) ||
subdir.Name.Equals(".idea", StringComparison.Ordinal))
if (subdir.Name.StartsWith(".", StringComparison.Ordinal) ||
subdir.Name.Equals("node_modules", StringComparison.Ordinal))
continue;
CallUIThread(() => ProgressDescription = $"Scanning {subdir.FullName}...");

View file

@ -323,10 +323,7 @@ namespace SourceGit.ViewModels
public void OpenAssumeUnchanged()
{
App.OpenDialog(new Views.AssumeUnchangedManager()
{
DataContext = new AssumeUnchangedManager(_repo)
});
App.ShowWindow(new AssumeUnchangedManager(_repo), true);
}
public void StashAll(bool autoStart)
@ -726,8 +723,7 @@ namespace SourceGit.ViewModels
history.Icon = App.CreateMenuIcon("Icons.Histories");
history.Click += (_, e) =>
{
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, change.Path) };
window.Show();
App.ShowWindow(new FileHistories(_repo, change.Path), false);
e.Handled = true;
};
@ -1093,8 +1089,7 @@ namespace SourceGit.ViewModels
{
ai.Click += (_, e) =>
{
var dialog = new Views.AIAssistant(services[0], _repo.FullPath, this, _selectedStaged);
App.OpenDialog(dialog);
App.ShowWindow(new AIAssistant(_repo, services[0], _selectedStaged, t => CommitMessage = t), true);
e.Handled = true;
};
}
@ -1108,8 +1103,7 @@ namespace SourceGit.ViewModels
item.Header = service.Name;
item.Click += (_, e) =>
{
var dialog = new Views.AIAssistant(dup, _repo.FullPath, this, _selectedStaged);
App.OpenDialog(dialog);
App.ShowWindow(new AIAssistant(_repo, dup, _selectedStaged, t => CommitMessage = t), true);
e.Handled = true;
};
@ -1193,8 +1187,7 @@ namespace SourceGit.ViewModels
history.Icon = App.CreateMenuIcon("Icons.Histories");
history.Click += (_, e) =>
{
var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, change.Path) };
window.Show();
App.ShowWindow(new FileHistories(_repo, change.Path), false);
e.Handled = true;
};
@ -1456,9 +1449,11 @@ namespace SourceGit.ViewModels
{
for (int i = 0; i < historiesCount; i++)
{
var message = _repo.Settings.CommitMessages[i];
var message = _repo.Settings.CommitMessages[i].Trim().ReplaceLineEndings("\n");
var subjectEndIdx = message.IndexOf('\n');
var subject = subjectEndIdx > 0 ? message.Substring(0, subjectEndIdx) : message;
var item = new MenuItem();
item.Header = message;
item.Header = subject;
item.Icon = App.CreateMenuIcon("Icons.Histories");
item.Click += (_, e) =>
{
@ -1490,8 +1485,7 @@ namespace SourceGit.ViewModels
if (services.Count == 1)
{
var dialog = new Views.AIAssistant(services[0], _repo.FullPath, this, _staged);
App.OpenDialog(dialog);
App.ShowWindow(new AIAssistant(_repo, services[0], _staged, t => CommitMessage = t), true);
return null;
}
@ -1503,8 +1497,7 @@ namespace SourceGit.ViewModels
item.Header = service.Name;
item.Click += (_, e) =>
{
var dialog = new Views.AIAssistant(dup, _repo.FullPath, this, _staged);
App.OpenDialog(dialog);
App.ShowWindow(new AIAssistant(_repo, dup, _staged, t => CommitMessage = t), true);
e.Handled = true;
};
@ -1533,7 +1526,10 @@ namespace SourceGit.ViewModels
private List<Models.Change> GetStagedChanges()
{
if (_useAmend)
return new Commands.QueryStagedChangesWithAmend(_repo.FullPath).Result();
{
var head = new Commands.QuerySingleCommit(_repo.FullPath, "HEAD").Result();
return new Commands.QueryStagedChangesWithAmend(_repo.FullPath, head.Parents.Count == 0 ? "4b825dc642cb6eb9a060e54bf8d69288fbee4904" : $"{head.SHA}^").Result();
}
var rs = new List<Models.Change>();
foreach (var c in _cached)
@ -1705,14 +1701,7 @@ namespace SourceGit.ViewModels
if (!string.IsNullOrEmpty(_filter) && _staged.Count > _visibleStaged.Count && !confirmWithFilter)
{
var confirmMessage = App.Text("WorkingCopy.ConfirmCommitWithFilter", _staged.Count, _visibleStaged.Count, _staged.Count - _visibleStaged.Count);
App.OpenDialog(new Views.ConfirmCommit()
{
DataContext = new ConfirmCommit(confirmMessage, () =>
{
DoCommit(autoStage, autoPush, allowEmpty, true);
})
});
App.ShowWindow(new ConfirmCommit(confirmMessage, () => DoCommit(autoStage, autoPush, allowEmpty, true)), true);
return;
}
@ -1720,14 +1709,7 @@ namespace SourceGit.ViewModels
{
if ((autoStage && _count == 0) || (!autoStage && _staged.Count == 0))
{
App.OpenDialog(new Views.ConfirmEmptyCommit()
{
DataContext = new ConfirmEmptyCommit(_count > 0, stageAll =>
{
DoCommit(stageAll, autoPush, true, confirmWithFilter);
})
});
App.ShowWindow(new ConfirmEmptyCommit(_count > 0, stageAll => DoCommit(stageAll, autoPush, true, confirmWithFilter)), true);
return;
}
}
@ -1756,7 +1738,18 @@ namespace SourceGit.ViewModels
UseAmend = false;
if (autoPush && _repo.Remotes.Count > 0)
_repo.ShowAndStartPopup(new Push(_repo, null));
{
if (_repo.CurrentBranch == null)
{
var currentBranchName = Commands.Branch.ShowCurrent(_repo.FullPath);
var tmp = new Models.Branch() { Name = currentBranchName };
_repo.ShowAndStartPopup(new Push(_repo, tmp));
}
else
{
_repo.ShowAndStartPopup(new Push(_repo, null));
}
}
}
_repo.MarkBranchesDirtyManually();