-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqgitblamedialog.cpp
More file actions
372 lines (313 loc) · 13.5 KB
/
Copy pathqgitblamedialog.cpp
File metadata and controls
372 lines (313 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "qgitblamedialog.h"
#include "ui_qgitblamedialog.h"
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QHeaderView>
#include <QColor>
#include <QFont>
#include <QApplication>
#include <QPalette>
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
static const QStringList kIgnoredDirs = { ".git", "build", ".cache", "__pycache__" };
static const QStringList kIgnoredExts = {
".o", ".a", ".so", ".dll", ".exe", ".obj",
".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico",
".ttf", ".woff", ".woff2",
".zip", ".tar", ".gz", ".bz2", ".xz",
".pdf", ".pyc"
};
// ---------------------------------------------------------------------------
// Colour palette for blame hunks (one colour per unique commit, cycling)
// ---------------------------------------------------------------------------
QColor QGitBlameDialog::hunkColor(const QString &hash, int index)
{
Q_UNUSED(hash)
// Use two alternating neutral tints so adjacent hunks from different
// commits are visually separated without being distracting.
static const QColor kPalette[] = {
QColor(230, 240, 255), // soft blue
QColor(225, 255, 230), // soft green
QColor(255, 245, 220), // soft amber
QColor(245, 225, 255), // soft violet
QColor(220, 248, 248), // soft cyan
QColor(255, 230, 230), // soft rose
QColor(240, 255, 220), // soft lime
QColor(255, 235, 245), // soft pink
};
return kPalette[index % 8];
}
// ---------------------------------------------------------------------------
// Construction
// ---------------------------------------------------------------------------
QGitBlameDialog::QGitBlameDialog(QGit *git, QWidget *parent)
: QDialog(parent)
, ui(new Ui::QGitBlameDialog)
, m_git(git)
{
ui->setupUi(this);
setWindowTitle(tr("Git Blame Browser"));
resize(1100, 680);
// Splitter initial proportions (left: 28%, right: 72%)
ui->splitter->setSizes({ 300, 800 });
// Blame table column sizing
ui->blameTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); // Line
ui->blameTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents); // Commit
ui->blameTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents); // Author
ui->blameTable->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents); // Date
ui->blameTable->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Stretch); // Content
ui->blameTable->verticalHeader()->setVisible(false);
ui->blameTable->verticalHeader()->setDefaultSectionSize(20);
// Monospace font for the content column
QFont monoFont;
monoFont.setFamily(QStringLiteral("Monospace"));
monoFont.setFixedPitch(true);
monoFont.setPointSize(9);
ui->blameTable->setFont(monoFont);
// File tree
ui->fileTree->setIconSize(QSize(16, 16));
populateFileTree(git->path().absolutePath());
}
QGitBlameDialog::~QGitBlameDialog()
{
delete ui;
}
// ---------------------------------------------------------------------------
// File tree population
// ---------------------------------------------------------------------------
void QGitBlameDialog::populateFileTree(const QString &rootPath)
{
ui->fileTree->clear();
auto *root = new QTreeWidgetItem(ui->fileTree);
root->setText(0, QFileInfo(rootPath).fileName());
root->setData(0, Qt::UserRole, QString()); // not a selectable file
root->setData(0, Qt::UserRole + 1, rootPath);
QIcon folderIcon = QApplication::style()->standardIcon(QStyle::SP_DirIcon);
QIcon fileIcon = QApplication::style()->standardIcon(QStyle::SP_FileIcon);
root->setIcon(0, folderIcon);
addDirectoryToTree(root, rootPath, rootPath);
root->setExpanded(true);
// Store icons for re-use during filtering
ui->fileTree->setProperty("folderIcon", QVariant::fromValue(folderIcon));
ui->fileTree->setProperty("fileIcon", QVariant::fromValue(fileIcon));
}
void QGitBlameDialog::addDirectoryToTree(QTreeWidgetItem *parent,
const QString &dirPath,
const QString &repoRoot)
{
QDir dir(dirPath);
QIcon folderIcon = QApplication::style()->standardIcon(QStyle::SP_DirIcon);
QIcon fileIcon = QApplication::style()->standardIcon(QStyle::SP_FileIcon);
// Directories first
const QFileInfoList dirs = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
for (const QFileInfo &info : dirs) {
if (kIgnoredDirs.contains(info.fileName())) continue;
auto *item = new QTreeWidgetItem(parent);
item->setText(0, info.fileName());
item->setIcon(0, folderIcon);
item->setData(0, Qt::UserRole, QString()); // directory — no blame path
item->setData(0, Qt::UserRole + 1, info.absoluteFilePath());
addDirectoryToTree(item, info.absoluteFilePath(), repoRoot);
}
// Files
const QFileInfoList files = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name);
for (const QFileInfo &info : files) {
if (kIgnoredExts.contains(info.suffix().prepend('.'))) continue;
QString relPath = QDir(repoRoot).relativeFilePath(info.absoluteFilePath());
auto *item = new QTreeWidgetItem(parent);
item->setText(0, info.fileName());
item->setIcon(0, fileIcon);
item->setData(0, Qt::UserRole, relPath); // relative path for blame
item->setData(0, Qt::UserRole + 1, info.absoluteFilePath()); // absolute for reading
}
}
// ---------------------------------------------------------------------------
// Filter
// ---------------------------------------------------------------------------
void QGitBlameDialog::filterTree(QTreeWidgetItem *item, const QString &text)
{
bool anyChildVisible = false;
for (int i = 0; i < item->childCount(); i++) {
QTreeWidgetItem *child = item->child(i);
filterTree(child, text);
if (!child->isHidden()) anyChildVisible = true;
}
if (item->childCount() > 0) {
// Directory: show if any child is visible
item->setHidden(!anyChildVisible && !text.isEmpty());
} else {
// File: show if name matches (or filter is empty)
item->setHidden(!text.isEmpty() && !item->text(0).contains(text, Qt::CaseInsensitive));
}
}
void QGitBlameDialog::on_lineEdit_filter_textChanged(const QString &text)
{
QTreeWidgetItem *root = ui->fileTree->topLevelItem(0);
if (!root) return;
filterTree(root, text);
if (!text.isEmpty()) {
ui->fileTree->expandAll();
} else {
// Collapse back but keep root expanded
ui->fileTree->collapseAll();
root->setExpanded(true);
}
}
// ---------------------------------------------------------------------------
// File selection → load blame
// ---------------------------------------------------------------------------
void QGitBlameDialog::on_fileTree_itemClicked(QTreeWidgetItem *item, int /*column*/)
{
QString relPath = item->data(0, Qt::UserRole).toString();
if (relPath.isEmpty()) return; // directory node
showBlameForFile(relPath);
}
void QGitBlameDialog::showBlameForFile(const QString &relPath)
{
ui->blameTable->clearContents();
ui->blameTable->setRowCount(0);
ui->label_filePath->setText(relPath);
ui->label_status->setText(tr("Loading blame for %1...").arg(relPath));
QApplication::processEvents();
// Read the actual file lines
QString absPath = m_git->path().absoluteFilePath(relPath);
QFile f(absPath);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
ui->label_status->setText(tr("Cannot open file: %1").arg(relPath));
return;
}
QStringList lines;
QTextStream ts(&f);
while (!ts.atEnd()) lines << ts.readLine();
f.close();
if (lines.isEmpty()) {
ui->label_status->setText(tr("File is empty."));
return;
}
// Compute blame hunks (pinned to m_pinnedCommitId if set, otherwise HEAD)
QList<QGitBlameHunk> hunks = m_git->blameFile(relPath, m_pinnedCommitId);
if (hunks.isEmpty()) {
ui->label_status->setText(tr("No blame data (file may be untracked)."));
return;
}
// Build a per-line hunk lookup (lineNumber → hunk index)
// hunks are already ordered by final_start_line_number
QVector<int> lineHunk(lines.size() + 1, -1); // 1-based
for (int h = 0; h < hunks.size(); h++) {
const auto &hunk = hunks[h];
for (size_t l = 0; l < hunk.linesInHunk; l++) {
size_t lineNo = hunk.finalStartLineNumber + l;
if (lineNo >= 1 && lineNo <= static_cast<size_t>(lines.size())) {
lineHunk[static_cast<int>(lineNo)] = h;
}
}
}
// Assign palette index per unique commit (stable insertion-order map)
QMap<QString, int> commitColorIndex;
int colorCounter = 0;
for (const auto &hunk : hunks) {
if (!commitColorIndex.contains(hunk.commitHash)) {
commitColorIndex[hunk.commitHash] = colorCounter++;
}
}
QFont monoFont;
monoFont.setFamily(QStringLiteral("Monospace"));
monoFont.setFixedPitch(true);
monoFont.setPointSize(9);
ui->blameTable->setUpdatesEnabled(false);
ui->blameTable->setRowCount(lines.size());
for (int row = 0; row < lines.size(); row++) {
int lineNo = row + 1;
int hunkIdx = (lineNo < lineHunk.size()) ? lineHunk[lineNo] : -1;
QString shortHash, authorName, dateStr, summary, fullHash;
QColor bgColor = QColor(245, 245, 245);
bool isFirstInHunk = false;
if (hunkIdx >= 0) {
const auto &hunk = hunks[hunkIdx];
fullHash = hunk.commitHash;
int pIdx = commitColorIndex.value(fullHash, 0);
bgColor = hunkColor(fullHash, pIdx);
isFirstInHunk = (static_cast<size_t>(lineNo) == hunk.finalStartLineNumber);
if (isFirstInHunk) {
shortHash = hunk.commitHash.left(8);
authorName = hunk.authorName;
dateStr = QDateTime::fromSecsSinceEpoch(hunk.authorTime)
.toString(QStringLiteral("yyyy-MM-dd"));
summary = hunk.summary;
}
}
auto makeItem = [&](const QString &text) -> QTableWidgetItem * {
auto *i = new QTableWidgetItem(text);
i->setBackground(bgColor);
i->setData(Qt::UserRole, fullHash); // full hash on every cell for double-click
return i;
};
// Line number (right-aligned)
auto *lineItem = makeItem(QString::number(lineNo));
lineItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
lineItem->setForeground(QColor(120, 120, 120));
// Commit hash (monospace, shown only on first line of hunk)
auto *hashItem = makeItem(shortHash);
hashItem->setFont(monoFont);
if (isFirstInHunk && !shortHash.isEmpty()) {
hashItem->setForeground(QColor(30, 80, 180));
}
// Author
auto *authorItem = makeItem(authorName);
authorItem->setForeground(QColor(60, 60, 60));
// Date
auto *dateItem = makeItem(dateStr);
dateItem->setForeground(QColor(100, 100, 100));
// File content (monospace, full text)
auto *contentItem = makeItem(lines[row]);
contentItem->setFont(monoFont);
contentItem->setForeground(QApplication::palette().color(QPalette::Text));
contentItem->setBackground(QApplication::palette().color(QPalette::Base)); // neutral bg for content
ui->blameTable->setItem(row, 0, lineItem);
ui->blameTable->setItem(row, 1, hashItem);
ui->blameTable->setItem(row, 2, authorItem);
ui->blameTable->setItem(row, 3, dateItem);
ui->blameTable->setItem(row, 4, contentItem);
}
ui->blameTable->setUpdatesEnabled(true);
ui->label_status->setText(tr("%1 lines — %2 blame hunks").arg(lines.size()).arg(hunks.size()));
}
// ---------------------------------------------------------------------------
// Double-click on blame row → navigate to commit
// ---------------------------------------------------------------------------
void QGitBlameDialog::on_blameTable_itemDoubleClicked(QTableWidgetItem *item)
{
QString hash = item->data(Qt::UserRole).toString();
if (!hash.isEmpty()) {
emit commitNavigationRequested(hash);
}
}
// ---------------------------------------------------------------------------
// Pre-selection: load blame for a specific file (e.g. from logHistory_files)
// ---------------------------------------------------------------------------
void QGitBlameDialog::preSelectFile(const QString &relPath, const QString &commitId)
{
// Walk the tree to find (and visually select) the matching item
QTreeWidgetItemIterator it(ui->fileTree);
while (*it) {
QString itemRel = (*it)->data(0, Qt::UserRole).toString();
if (itemRel == relPath) {
ui->fileTree->setCurrentItem(*it);
// Make sure all ancestors are expanded
QTreeWidgetItem *parent = (*it)->parent();
while (parent) {
parent->setExpanded(true);
parent = parent->parent();
}
ui->fileTree->scrollToItem(*it);
break;
}
++it;
}
// Load the blame, pinned to commitId if provided
m_pinnedCommitId = commitId;
showBlameForFile(relPath);
}