Merge pull request #73 from stefanknotzer/load-uncommitted-blame

Load and display uncommitted file in BlameEditor
This commit is contained in:
Murmele 2022-01-18 18:22:29 +01:00 committed by GitHub
commit b5ebbd281a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 24 deletions

View File

@ -10,6 +10,7 @@
#include "DoubleTreeWidget.h"
#include "BlameEditor.h"
#include "DiffTreeModel.h"
#include "FileContextMenu.h"
#include "StatePushButton.h"
#include "TreeProxy.h"
#include "TreeView.h"
@ -201,6 +202,9 @@ DoubleTreeWidget::DoubleTreeWidget(const git::Repository &repo, QWidget *parent)
connect(repo.notifier(), &git::RepositoryNotifier::indexChanged, this, [this] (const QStringList &paths) {
mDiffTreeModel->refresh(paths);
});
RepoView *view = RepoView::parentView(this);
connect(mEditor, &BlameEditor::linkActivated, view, &RepoView::visitLink);
}
QModelIndex DoubleTreeWidget::selectedIndex() const
@ -245,6 +249,8 @@ void DoubleTreeWidget::setDiff(const git::Diff &diff,
{
Q_UNUSED(file)
Q_UNUSED(pathspec)
mDiff = diff;
// Remember selection.
storeSelection();
@ -285,6 +291,45 @@ void DoubleTreeWidget::setDiff(const git::Diff &diff,
loadSelection();
}
void DoubleTreeWidget::find()
{
mEditor->find();
}
void DoubleTreeWidget::findNext()
{
mEditor->findNext();
}
void DoubleTreeWidget::findPrevious()
{
mEditor->findPrevious();
}
void DoubleTreeWidget::contextMenuEvent(QContextMenuEvent *event)
{
QStringList files;
QModelIndexList indexes = unstagedFiles->selectionModel()->selectedIndexes();
foreach (const QModelIndex &index, indexes)
files.append(index.data(Qt::EditRole).toString());
indexes = stagedFiles->selectionModel()->selectedIndexes();
foreach (const QModelIndex &index, indexes)
files.append(index.data(Qt::EditRole).toString());
if (files.isEmpty())
return;
RepoView *view = RepoView::parentView(this);
FileContextMenu menu(view, files);
menu.exec(event->globalPos());
}
void DoubleTreeWidget::cancelBackgroundTasks()
{
mEditor->cancelBlame();
}
void DoubleTreeWidget::storeSelection()
{
QModelIndexList indexes = stagedFiles->selectionModel()->selectedIndexes();
@ -377,32 +422,13 @@ void DoubleTreeWidget::loadEditorContent(const QModelIndex &index)
QString name = index.data(Qt::EditRole).toString();
QList<git::Commit> commits = RepoView::parentView(this)->commits();
git::Commit commit = !commits.isEmpty() ? commits.first() : git::Commit();
git::Tree tree = commit.tree();
RepoView *view = RepoView::parentView(this);
int idx = mDiff.isValid() ? mDiff.indexOf(name) : -1;
git::Blob blob = idx < 0 ? git::Blob() :
view->repo().lookupBlob(mDiff.id(idx, git::Diff::NewFile));
// searching for the correct blob
auto list = name.split("/");
bool found = false;
git::Blob blob;
for (int path_depth = 0; path_depth < list.count(); path_depth++) {
auto element = list[path_depth];
found = false;
for (int i = 0; i < tree.count(); ++i) {
auto n = tree.name(i);
if (n == element) {
if (path_depth >= list.count() -1)
blob = tree.object(i);
else
tree = tree.object(i);
found = true;
break;
}
}
if (!found)
break;
}
mEditor->load(name, blob, commit);
if (found)
mEditor->load(name, blob, commit);
mDiffView->enable(true);
mDiffView->updateFiles();

11
src/ui/DoubleTreeWidget.h Normal file → Executable file
View File

@ -40,6 +40,15 @@ public:
const QString &file = QString(),
const QString &pathspec = QString()) override;
void cancelBackgroundTasks() override;
void find() override;
void findNext() override;
void findPrevious() override;
protected:
void contextMenuEvent(QContextMenuEvent *event) override;
private slots:
void collapseCountChanged(int count);
@ -91,5 +100,7 @@ private:
*/
QStackedWidget* mFileView{nullptr};
bool mIgnoreSelectionChange{false};
git::Diff mDiff;
};
#endif // DOUBLETREEWIDGET_H