-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqloghistorytablewidget.cpp
More file actions
324 lines (278 loc) · 9.4 KB
/
Copy pathqloghistorytablewidget.cpp
File metadata and controls
324 lines (278 loc) · 9.4 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
#include "qloghistorytablewidget.h"
QLogHistoryTableWidget::QLogHistoryTableWidget(QWidget *parent)
: QTableWidget(parent)
{
QPalette p = palette();
p.setColor(QPalette::Inactive, QPalette::Highlight, p.color(QPalette::Active, QPalette::Highlight));
p.setColor(QPalette::Active, QPalette::HighlightedText, p.color(QPalette::Inactive, QPalette::HighlightedText));
setPalette(p);
}
void QLogHistoryTableWidget::addCommit(const QGitCommit &commit)
{
if (columnCount() == 4)
{
int row = rowCount();
insertRow(row);
QTableWidgetItem *item = nullptr;
item = new QTableWidgetItem(commit.message().split('\n').first());
item->setData(Qt::UserRole, commit.message());
item->setData(Qt::UserRole + 1, commit.id());
setItem(row, 0, item);
item = new QTableWidgetItem(commit.time().toString());
setItem(row, 1, item);
item = new QTableWidgetItem(QString("%1 <%2>").arg(commit.author().name(), commit.author().email()));
item->setData(Qt::UserRole, commit.author().name());
item->setData(Qt::UserRole + 1, commit.author().email());
setItem(row, 2, item);
item = new QTableWidgetItem(commit.id().left(7));
item->setData(Qt::UserRole, commit.id());
setItem(row, 3, item);
return;
}
QString commitId = commit.id().toLower();
int row = rowCount();
// Reset lane tracking if the table is cleared/empty
if (row == 0)
{
m_hashIndex.clear();
}
// Keep track of active lanes at the top of this row (BEFORE we process/modify for the current commit)
QList<int> activeLanesAtTop;
for (int i = 0; i < m_hashIndex.size(); ++i)
{
if (!m_hashIndex[i].isEmpty())
{
activeLanesAtTop.append(i);
}
}
// Find all lanes that currently track this commitId case-insensitively
QList<int> mergingLanes;
for (int i = 0; i < m_hashIndex.size(); ++i)
{
if (m_hashIndex[i].toLower() == commitId)
{
mergingLanes.append(i);
}
}
int commitLane = -1;
QList<QVariantList> childConnections; // Connections from child lanes (top) to node (center)
if (!mergingLanes.isEmpty())
{
// Node is drawn at the first lane where it is found
commitLane = mergingLanes.first();
// All other lanes tracking this commit merge into the node lane from top
for (int i = 1; i < mergingLanes.size(); ++i)
{
int otherLane = mergingLanes[i];
childConnections.append(QVariantList{otherLane, commitLane});
}
}
else
{
// Start a new lane
m_hashIndex.append(commitId);
commitLane = m_hashIndex.length() - 1;
}
// Determine parents and build connections
QList<QVariantList> parentConnections;
QList<QString> nextHashIndex = m_hashIndex;
// Clear all merging lanes from nextHashIndex so they don't continue down
for (int lane : mergingLanes)
{
nextHashIndex[lane] = "";
}
if (commit.parents().count() > 0)
{
// First parent continues in our commit's main lane
const QString firstParent = commit.parents().at(0).commitHash().toLower();
nextHashIndex[commitLane] = firstParent;
parentConnections.append(QVariantList{commitLane, commitLane});
// Other parents (merges)
for (int c = 1; c < commit.parents().count(); c++)
{
const QString otherParent = commit.parents().at(c).commitHash().toLower();
int parentIdx = -1;
for (int i = 0; i < nextHashIndex.size(); ++i)
{
if (nextHashIndex[i].toLower() == otherParent)
{
parentIdx = i;
break;
}
}
if (parentIdx < 0)
{
// Find empty slot or append
int emptySlot = -1;
for (int i = 0; i < nextHashIndex.size(); ++i)
{
if (nextHashIndex[i].isEmpty())
{
emptySlot = i;
break;
}
}
if (emptySlot >= 0)
{
nextHashIndex[emptySlot] = otherParent;
parentConnections.append(QVariantList{commitLane, emptySlot});
}
else
{
nextHashIndex.append(otherParent);
parentConnections.append(QVariantList{commitLane, nextHashIndex.length() - 1});
}
}
else
{
parentConnections.append(QVariantList{commitLane, parentIdx});
}
}
}
else
{
// Root commit, lane terminates
nextHashIndex[commitLane] = "";
}
// Clean trailing empty lanes
while (!nextHashIndex.isEmpty() && nextHashIndex.last().isEmpty())
{
nextHashIndex.removeLast();
}
// Passing lines are lanes active at top that:
// 1. are not commitLane
// 2. are not in mergingLanes (since those terminate at this node)
QList<int> passingLines;
for (int lane : activeLanesAtTop)
{
if (lane != commitLane && !mergingLanes.contains(lane))
{
passingLines.append(lane);
}
}
// Update m_hashIndex for the next row
m_hashIndex = nextHashIndex;
// Pack into data for column 0 QTableWidgetItem
QList<QVariant> data;
data.append(commitLane);
QList<QVariant> passingLanesVar;
for (int lane : passingLines)
{
passingLanesVar.append(lane);
}
data.append(QVariant(passingLanesVar));
QList<QVariant> parentConnsVar;
for (const auto &conn : parentConnections)
{
if (conn.size() >= 2)
{
parentConnsVar.append(conn.at(0).toInt());
parentConnsVar.append(conn.at(1).toInt());
}
}
data.append(QVariant(parentConnsVar));
QList<QVariant> activeLanesVar;
for (int lane : activeLanesAtTop)
{
activeLanesVar.append(lane);
}
data.append(QVariant(activeLanesVar));
QList<QVariant> childConnsVar;
for (const auto &conn : childConnections)
{
if (conn.size() >= 2)
{
childConnsVar.append(conn.at(0).toInt());
childConnsVar.append(conn.at(1).toInt());
}
}
data.append(QVariant(childConnsVar));
insertRow(row);
QTableWidgetItem *item = nullptr;
item = new QTableWidgetItem();
item->setData(Qt::UserRole, data);
setItem(row, 0, item);
item = new QTableWidgetItem(commit.message().split('\n').first());
item->setData(Qt::UserRole, commit.message());
item->setData(Qt::UserRole + 1, commit.id());
setItem(row, 1, item);
item = new QTableWidgetItem(commit.time().toString());
setItem(row, 2, item);
item = new QTableWidgetItem(QString("%1 <%2>").arg(commit.author().name(), commit.author().email()));
item->setData(Qt::UserRole, commit.author().name());
item->setData(Qt::UserRole + 1, commit.author().email());
setItem(row, 3, item);
item = new QTableWidgetItem(commit.id().left(7));
item->setData(Qt::UserRole, commit.id());
setItem(row, 4, item);
}
bool QLogHistoryTableWidget::selectCommit(const QString &hash)
{
for (int row = 0; row < rowCount(); ++row)
{
int col = (columnCount() == 4) ? 3 : 4;
QTableWidgetItem *item = this->item(row, col);
if (item && item->data(Qt::UserRole).toString().compare(hash, Qt::CaseInsensitive) == 0)
{
this->selectRow(row);
this->scrollToItem(item);
return true;
}
}
return false;
}
void QLogHistoryTableWidget::setReferences(const QList<QGitBranch> &branches, const QList<QGitTag> &tags, const QString ¤tBranch)
{
m_refMap.clear();
for (const auto &branch : branches)
{
QString refName = branch.name();
QGitRef::Type refType = QGitRef::LocalBranch;
if (branch.type() & GIT_BRANCH_LOCAL)
{
if (refName.startsWith(QStringLiteral("refs/heads/")))
{
refName = refName.mid(11);
}
if (refName == currentBranch)
{
refType = QGitRef::CurrentBranch;
}
else
{
refType = QGitRef::LocalBranch;
}
}
else if (branch.type() & GIT_BRANCH_REMOTE)
{
if (refName.startsWith(QStringLiteral("refs/remotes/")))
{
refName = refName.mid(13);
}
refType = QGitRef::RemoteBranch;
}
QGitRef refObj{refName, refType};
m_refMap[branch.hash().toUpper()].append(refObj);
m_refMap[branch.hash().toLower()].append(refObj);
}
for (const auto &tag : tags)
{
QString refName = tag.name();
if (refName.startsWith(QStringLiteral("refs/tags/")))
{
refName = refName.mid(10);
}
QGitRef refObj{refName, QGitRef::Tag};
m_refMap[tag.hash().toUpper()].append(refObj);
m_refMap[tag.hash().toLower()].append(refObj);
}
viewport()->update();
}
QList<QGitRef> QLogHistoryTableWidget::getReferences(const QString &sha) const
{
if (m_refMap.contains(sha.toUpper()))
{
return m_refMap.value(sha.toUpper());
}
return m_refMap.value(sha.toLower());
}