branchmodel.cpp 19 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
**
hjk's avatar
hjk committed
24
25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

30
31
32
#include "branchmodel.h"
#include "gitclient.h"

33
#include <utils/qtcassert.h>
Tobias Hunger's avatar
Tobias Hunger committed
34
35
#include <vcsbase/vcsbaseoutputwindow.h>

36
#include <QFont>
37
38

namespace Git {
Tobias Hunger's avatar
Tobias Hunger committed
39
40
41
42
43
namespace Internal {

// --------------------------------------------------------------------------
// BranchNode:
// --------------------------------------------------------------------------
44

Tobias Hunger's avatar
Tobias Hunger committed
45
class BranchNode
46
{
Tobias Hunger's avatar
Tobias Hunger committed
47
48
public:
    BranchNode() :
49
50
        parent(0),
        name(QLatin1String("<ROOT>"))
Tobias Hunger's avatar
Tobias Hunger committed
51
52
    { }

53
54
    BranchNode(const QString &n, const QString &s = QString(), const QString &t = QString()) :
        parent(0), name(n), sha(s), tracking(t)
Tobias Hunger's avatar
Tobias Hunger committed
55
56
57
58
    { }

    ~BranchNode()
    {
59
60
61
62
        while (!children.isEmpty())
            delete children.first();
        if (parent)
            parent->children.removeAll(this);
Tobias Hunger's avatar
Tobias Hunger committed
63
    }
64

Tobias Hunger's avatar
Tobias Hunger committed
65
    BranchNode *rootNode() const
Tobias Hunger's avatar
Tobias Hunger committed
66
    {
Tobias Hunger's avatar
Tobias Hunger committed
67
        return parent ? parent->rootNode() : const_cast<BranchNode *>(this);
Tobias Hunger's avatar
Tobias Hunger committed
68
    }
69

Tobias Hunger's avatar
Tobias Hunger committed
70
    int count() const
Tobias Hunger's avatar
Tobias Hunger committed
71
72
73
74
    {
        return children.count();
    }

Tobias Hunger's avatar
Tobias Hunger committed
75
    bool isLeaf() const
Tobias Hunger's avatar
Tobias Hunger committed
76
77
78
79
    {
        return children.isEmpty();
    }

Orgad Shaneh's avatar
Orgad Shaneh committed
80
81
82
83
84
85
86
87
88
89
90
91
    bool isTag() const
    {
        if (!parent)
            return false;
        for (const BranchNode *p = this; p->parent; p = p->parent) {
            // find root child with name "tags"
            if (!p->parent->parent && p->name == QLatin1String("tags"))
                return true;
        }
        return false;
    }

Tobias Hunger's avatar
Tobias Hunger committed
92
    bool childOf(BranchNode *node) const
Tobias Hunger's avatar
Tobias Hunger committed
93
94
95
96
97
98
    {
        if (this == node)
            return true;
        return parent ? parent->childOf(node) : false;
    }

Tobias Hunger's avatar
Tobias Hunger committed
99
    bool isLocal() const
Tobias Hunger's avatar
Tobias Hunger committed
100
101
102
103
104
105
106
    {
        BranchNode *rn = rootNode();
        if (rn->isLeaf())
            return false;
        return childOf(rn->children.at(0));
    }

Tobias Hunger's avatar
Tobias Hunger committed
107
    BranchNode *childOfName(const QString &name) const
Tobias Hunger's avatar
Tobias Hunger committed
108
109
110
111
112
113
114
115
    {
        for (int i = 0; i < children.count(); ++i) {
            if (children.at(i)->name == name)
                return children.at(i);
        }
        return 0;
    }

Tobias Hunger's avatar
Tobias Hunger committed
116
    QStringList fullName() const
Tobias Hunger's avatar
Tobias Hunger committed
117
    {
118
        QTC_ASSERT(isLeaf(), return QStringList());
Tobias Hunger's avatar
Tobias Hunger committed
119
120

        QStringList fn;
Tobias Hunger's avatar
Tobias Hunger committed
121
122
        QList<const BranchNode *> nodes;
        const BranchNode *current = this;
Tobias Hunger's avatar
Tobias Hunger committed
123
124
125
126
127
128
129
130
        while (current->parent) {
            nodes.prepend(current);
            current = current->parent;
        }

        if (current->children.at(0) == nodes.at(0))
            nodes.removeFirst(); // remove local branch designation

Tobias Hunger's avatar
Tobias Hunger committed
131
        foreach (const BranchNode *n, nodes)
Tobias Hunger's avatar
Tobias Hunger committed
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
            fn.append(n->name);

        return fn;
    }

    void insert(const QStringList path, BranchNode *n)
    {
        BranchNode *current = this;
        for (int i = 0; i < path.count(); ++i) {
            BranchNode *c = current->childOfName(path.at(i));
            if (c)
                current = c;
            else
                current = current->append(new BranchNode(path.at(i)));
        }
        current->append(n);
    }

    BranchNode *append(BranchNode *n)
    {
        n->parent = this;
        children.append(n);
        return n;
    }

Tobias Hunger's avatar
Tobias Hunger committed
157
    QStringList childrenNames() const
Tobias Hunger's avatar
Tobias Hunger committed
158
159
160
161
162
163
164
165
    {
        if (children.count() > 0) {
            QStringList names;
            foreach (BranchNode *n, children) {
                names.append(n->childrenNames());
            }
            return names;
        }
166
        return QStringList(fullName().join(QString(QLatin1Char('/'))));
Tobias Hunger's avatar
Tobias Hunger committed
167
    }
168

169
170
171
172
173
    int rowOf(BranchNode *node)
    {
        return children.indexOf(node);
    }

Tobias Hunger's avatar
Tobias Hunger committed
174
175
176
177
178
    BranchNode *parent;
    QList<BranchNode *> children;

    QString name;
    QString sha;
179
    QString tracking;
Tobias Hunger's avatar
Tobias Hunger committed
180
181
182
183
184
185
186
187
188
189
    mutable QString toolTip;
};

// --------------------------------------------------------------------------
// BranchModel:
// --------------------------------------------------------------------------

BranchModel::BranchModel(GitClient *client, QObject *parent) :
    QAbstractItemModel(parent),
    m_client(client),
190
191
    m_rootNode(new BranchNode),
    m_currentBranch(0)
192
{
193
    QTC_CHECK(m_client);
Tobias Hunger's avatar
Tobias Hunger committed
194
    m_rootNode->append(new BranchNode(tr("Local Branches")));
195
196
}

Tobias Hunger's avatar
Tobias Hunger committed
197
BranchModel::~BranchModel()
198
{
Tobias Hunger's avatar
Tobias Hunger committed
199
    delete m_rootNode;
200
201
}

202
QModelIndex BranchModel::index(int row, int column, const QModelIndex &parentIdx) const
203
{
204
205
206
207
208
    if (column != 0)
        return QModelIndex();
    BranchNode *parentNode = indexToNode(parentIdx);

    if (row >= parentNode->count())
Tobias Hunger's avatar
Tobias Hunger committed
209
        return QModelIndex();
210
    return nodeToIndex(parentNode->children.at(row));
211
212
}

Tobias Hunger's avatar
Tobias Hunger committed
213
QModelIndex BranchModel::parent(const QModelIndex &index) const
214
{
215
216
217
218
    if (!index.isValid())
        return QModelIndex();

    BranchNode *node = indexToNode(index);
Tobias Hunger's avatar
Tobias Hunger committed
219
220
    if (node->parent == m_rootNode)
        return QModelIndex();
221
    return nodeToIndex(node->parent);
222
223
}

224
int BranchModel::rowCount(const QModelIndex &parentIdx) const
225
{
226
    if (parentIdx.column() > 0)
Tobias Hunger's avatar
Tobias Hunger committed
227
        return 0;
228
229

    return indexToNode(parentIdx)->count();
230
231
}

Tobias Hunger's avatar
Tobias Hunger committed
232
int BranchModel::columnCount(const QModelIndex &parent) const
233
{
Tobias Hunger's avatar
Tobias Hunger committed
234
235
    Q_UNUSED(parent);
    return 1;
236
237
}

Tobias Hunger's avatar
Tobias Hunger committed
238
QVariant BranchModel::data(const QModelIndex &index, int role) const
239
{
240
241
242
    BranchNode *node = indexToNode(index);
    if (!node)
        return QVariant();
Tobias Hunger's avatar
Tobias Hunger committed
243

244
    switch (role) {
245
246
247
248
249
250
    case Qt::DisplayRole: {
        QString res = node->name;
        if (!node->tracking.isEmpty())
            res += QLatin1String(" [") + node->tracking + QLatin1Char(']');
        return res;
    }
Tobias Hunger's avatar
Tobias Hunger committed
251
252
253
254
255
256
257
258
259
260
261
262
263
    case Qt::EditRole:
        return node->name;
    case Qt::ToolTipRole:
        if (!node->isLeaf())
            return QVariant();
        if (node->toolTip.isEmpty())
            node->toolTip = toolTip(node->sha);
        return node->toolTip;
    case Qt::FontRole:
    {
        QFont font;
        if (!node->isLeaf()) {
            font.setBold(true);
264
        } else if (node == m_currentBranch) {
Tobias Hunger's avatar
Tobias Hunger committed
265
266
267
268
269
270
271
            font.setBold(true);
            font.setUnderline(true);
        }
        return font;
    }
    default:
        return QVariant();
272
273
274
    }
}

Tobias Hunger's avatar
Tobias Hunger committed
275
bool BranchModel::setData(const QModelIndex &index, const QVariant &value, int role)
276
{
Tobias Hunger's avatar
Tobias Hunger committed
277
278
    if (role != Qt::EditRole)
        return false;
279
280
281
    BranchNode *node = indexToNode(index);
    if (!node)
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
282
283
284
285
286
287
288
289
290
291
292

    const QString newName = value.toString();
    if (newName.isEmpty())
        return false;

    if (node->name == newName)
        return true;

    QStringList oldFullName = node->fullName();
    node->name = newName;
    QStringList newFullName = node->fullName();
293

294
295
    QString output;
    QString errorMessage;
Tobias Hunger's avatar
Tobias Hunger committed
296
297
298
299
300
301
    if (!m_client->synchronousBranchCmd(m_workingDirectory,
                                        QStringList() << QLatin1String("-m")
                                                      << oldFullName.last()
                                                      << newFullName.last(),
                                        &output, &errorMessage)) {
        node->name = oldFullName.last();
hjk's avatar
hjk committed
302
        VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
Tobias Hunger's avatar
Tobias Hunger committed
303
304
305
306
307
        return false;
    }

    emit dataChanged(index, index);
    return true;
308
309
}

Tobias Hunger's avatar
Tobias Hunger committed
310
Qt::ItemFlags BranchModel::flags(const QModelIndex &index) const
311
{
312
313
314
    BranchNode *node = indexToNode(index);
    if (!node)
        return Qt::NoItemFlags;
Tobias Hunger's avatar
Tobias Hunger committed
315
316
317
318
    if (node->isLeaf() && node->isLocal())
        return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
    else
        return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
319
320
}

Tobias Hunger's avatar
Tobias Hunger committed
321
void BranchModel::clear()
322
{
323
324
    while (m_rootNode->count() > 1)
        delete m_rootNode->children.takeLast();
Tobias Hunger's avatar
Tobias Hunger committed
325
    BranchNode *locals = m_rootNode->children.at(0);
326
327
328
329
    while (locals->count())
        delete locals->children.takeLast();

    m_currentBranch = 0;
330
331
}

Tobias Hunger's avatar
Tobias Hunger committed
332
bool BranchModel::refresh(const QString &workingDirectory, QString *errorMessage)
333
{
Tobias Hunger's avatar
Tobias Hunger committed
334
335
336
    if (workingDirectory.isEmpty())
        return false;

337
    m_currentSha = m_client->synchronousTopRevision(workingDirectory);
338
    QStringList args;
339
    args << QLatin1String("--format=%(objectname)\t%(refname)\t%(upstream:short)\t%(*objectname)");
340
    QString output;
341
    if (!m_client->synchronousForEachRefCmd(workingDirectory, args, &output, errorMessage))
hjk's avatar
hjk committed
342
        VcsBase::VcsBaseOutputWindow::instance()->appendError(*errorMessage);
Tobias Hunger's avatar
Tobias Hunger committed
343
344
345
346
347
348
349
350
351

    beginResetModel();
    clear();

    m_workingDirectory = workingDirectory;
    const QStringList lines = output.split(QLatin1Char('\n'));
    foreach (const QString &l, lines)
        parseOutputLine(l);

352
353
354
355
356
357
    if (m_currentBranch) {
        if (m_currentBranch->parent == m_rootNode->children[0])
            m_currentBranch = 0;
        setCurrentBranch();
    }

Tobias Hunger's avatar
Tobias Hunger committed
358
    endResetModel();
359

360
361
362
    return true;
}

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
void BranchModel::setCurrentBranch()
{
    QString currentBranch = m_client->synchronousCurrentLocalBranch(m_workingDirectory);
    if (currentBranch.isEmpty())
        return;

    BranchNode *local = m_rootNode->children.at(0);
    int pos = 0;
    for (pos = 0; pos < local->count(); ++pos) {
        if (local->children.at(pos)->name == currentBranch) {
            m_currentBranch = local->children[pos];
        }
    }
}

Tobias Hunger's avatar
Tobias Hunger committed
378
void BranchModel::renameBranch(const QString &oldName, const QString &newName)
379
{
Tobias Hunger's avatar
Tobias Hunger committed
380
381
382
383
384
    QString errorMessage;
    QString output;
    if (!m_client->synchronousBranchCmd(m_workingDirectory,
                                        QStringList() << QLatin1String("-m") << oldName << newName,
                                        &output, &errorMessage))
hjk's avatar
hjk committed
385
        VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
Tobias Hunger's avatar
Tobias Hunger committed
386
387
    else
        refresh(m_workingDirectory, &errorMessage);
388
389
}

Tobias Hunger's avatar
Tobias Hunger committed
390
QString BranchModel::workingDirectory() const
391
{
Tobias Hunger's avatar
Tobias Hunger committed
392
    return m_workingDirectory;
393
394
}

Tobias Hunger's avatar
Tobias Hunger committed
395
GitClient *BranchModel::client() const
396
{
Tobias Hunger's avatar
Tobias Hunger committed
397
    return m_client;
398
399
}

Tobias Hunger's avatar
Tobias Hunger committed
400
QModelIndex BranchModel::currentBranch() const
401
{
402
    if (!m_currentBranch)
Tobias Hunger's avatar
Tobias Hunger committed
403
        return QModelIndex();
404
    return nodeToIndex(m_currentBranch);
405
406
}

Tobias Hunger's avatar
Tobias Hunger committed
407
QString BranchModel::branchName(const QModelIndex &idx) const
408
{
Tobias Hunger's avatar
Tobias Hunger committed
409
410
    if (!idx.isValid())
        return QString();
411
412
    BranchNode *node = indexToNode(idx);
    if (!node || !node->isLeaf())
Tobias Hunger's avatar
Tobias Hunger committed
413
414
        return QString();
    QStringList path = node->fullName();
415
    return path.join(QString(QLatin1Char('/')));
416
417
}

Tobias Hunger's avatar
Tobias Hunger committed
418
QStringList BranchModel::localBranchNames() const
419
{
Tobias Hunger's avatar
Tobias Hunger committed
420
421
422
423
    if (!m_rootNode || m_rootNode->children.isEmpty())
        return QStringList();

    return m_rootNode->children.at(0)->childrenNames();
424
425
}

Tobias Hunger's avatar
Tobias Hunger committed
426
QString BranchModel::sha(const QModelIndex &idx) const
427
{
Tobias Hunger's avatar
Tobias Hunger committed
428
429
    if (!idx.isValid())
        return QString();
430
    BranchNode *node = indexToNode(idx);
Tobias Hunger's avatar
Tobias Hunger committed
431
432
    return node->sha;
}
433

Tobias Hunger's avatar
Tobias Hunger committed
434
435
436
437
bool BranchModel::isLocal(const QModelIndex &idx) const
{
    if (!idx.isValid())
        return false;
438
    BranchNode *node = indexToNode(idx);
Tobias Hunger's avatar
Tobias Hunger committed
439
    return node->isLocal();
440
441
}

Tobias Hunger's avatar
Tobias Hunger committed
442
bool BranchModel::isLeaf(const QModelIndex &idx) const
443
{
Tobias Hunger's avatar
Tobias Hunger committed
444
445
    if (!idx.isValid())
        return false;
446
    BranchNode *node = indexToNode(idx);
Tobias Hunger's avatar
Tobias Hunger committed
447
    return node->isLeaf();
448
449
}

Orgad Shaneh's avatar
Orgad Shaneh committed
450
451
452
453
454
455
456
bool BranchModel::isTag(const QModelIndex &idx) const
{
    if (!idx.isValid())
        return false;
    return indexToNode(idx)->isTag();
}

Tobias Hunger's avatar
Tobias Hunger committed
457
void BranchModel::removeBranch(const QModelIndex &idx)
458
{
Tobias Hunger's avatar
Tobias Hunger committed
459
460
461
462
463
464
465
466
467
468
    QString branch = branchName(idx);
    if (branch.isEmpty())
        return;

    QString errorMessage;
    QString output;
    QStringList args;

    args << QLatin1String("-D") << branch;
    if (!m_client->synchronousBranchCmd(m_workingDirectory, args, &output, &errorMessage)) {
hjk's avatar
hjk committed
469
        VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
Tobias Hunger's avatar
Tobias Hunger committed
470
471
472
        return;
    }

473
474
475
476
477
478
479
480
481
    QModelIndex tmp = idx; // tmp is a leaf, so count must be 0.
    while (indexToNode(tmp)->count() == 0) {
        QModelIndex tmpParent = parent(tmp);
        beginRemoveRows(tmpParent, tmp.row(), tmp.row());
        indexToNode(tmpParent)->children.removeAt(tmp.row());
        delete indexToNode(tmp);
        endRemoveRows();
        tmp = tmpParent;
    }
482
483
}

Tobias Hunger's avatar
Tobias Hunger committed
484
void BranchModel::checkoutBranch(const QModelIndex &idx)
485
{
Tobias Hunger's avatar
Tobias Hunger committed
486
487
488
489
    QString branch = branchName(idx);
    if (branch.isEmpty())
        return;

490
491
    // No StashGuard since this function for now is only used with clean working dir.
    // If it is ever used from another place, please add StashGuard here
Orgad Shaneh's avatar
Orgad Shaneh committed
492
    QString errorMessage;
493
    if (m_client->synchronousCheckout(m_workingDirectory, branch, &errorMessage)) {
Tobias Hunger's avatar
Tobias Hunger committed
494
        if (errorMessage.isEmpty()) {
495
496
            QModelIndex currentIdx = currentBranch();
            if (currentIdx.isValid()) {
497
498
                m_currentBranch = 0;
                emit dataChanged(currentIdx, currentIdx);
499
            }
500
            m_currentBranch = indexToNode(idx);
Tobias Hunger's avatar
Tobias Hunger committed
501
502
503
504
505
506
            emit dataChanged(idx, idx);
        } else {
            refresh(m_workingDirectory, &errorMessage); // not sure all went well... better refresh!
        }
    }
    if (!errorMessage.isEmpty())
hjk's avatar
hjk committed
507
        VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
508
509
}

Tobias Hunger's avatar
Tobias Hunger committed
510
bool BranchModel::branchIsMerged(const QModelIndex &idx)
511
{
Tobias Hunger's avatar
Tobias Hunger committed
512
513
514
515
516
517
518
519
    QString branch = branchName(idx);
    if (branch.isEmpty())
        return false;

    QString errorMessage;
    QString output;
    QStringList args;

520
    args << QLatin1String("-a") << QLatin1String("--contains") << sha(idx);
521
    if (!m_client->synchronousBranchCmd(m_workingDirectory, args, &output, &errorMessage))
hjk's avatar
hjk committed
522
        VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
Tobias Hunger's avatar
Tobias Hunger committed
523

524
    QStringList lines = output.split(QLatin1Char('\n'), QString::SkipEmptyParts);
Tobias Hunger's avatar
Tobias Hunger committed
525
    foreach (const QString &l, lines) {
526
527
528
529
        QString currentBranch = l.mid(2); // remove first letters (those are either
                                          // "  " or "* " depending on whether it is
                                          // the currently checked out branch or not)
        if (currentBranch != branch)
Tobias Hunger's avatar
Tobias Hunger committed
530
531
532
            return true;
    }
    return false;
533
534
}

535
536
537
538
539
540
541
542
543
544
static int positionForName(BranchNode *node, const QString &name)
{
    int pos = 0;
    for (pos = 0; pos < node->count(); ++pos) {
        if (node->children.at(pos)->name >= name)
            break;
    }
    return pos;
}

545
QModelIndex BranchModel::addBranch(const QString &name, bool track, const QModelIndex &startPoint)
546
{
Tobias Hunger's avatar
Tobias Hunger committed
547
548
549
    if (!m_rootNode || !m_rootNode->count())
        return QModelIndex();

550
    const QString trackedBranch = branchName(startPoint);
Tobias Hunger's avatar
Tobias Hunger committed
551
552
553
554
555
    QString output;
    QString errorMessage;

    QStringList args;
    args << (track ? QLatin1String("--track") : QLatin1String("--no-track"));
556
557
558
    args << name;
    if (!trackedBranch.isEmpty())
        args << trackedBranch;
Tobias Hunger's avatar
Tobias Hunger committed
559
560

    if (!m_client->synchronousBranchCmd(m_workingDirectory, args, &output, &errorMessage)) {
hjk's avatar
hjk committed
561
        VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
Tobias Hunger's avatar
Tobias Hunger committed
562
563
564
565
        return QModelIndex();
    }

    BranchNode *local = m_rootNode->children.at(0);
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
    const int slash = name.indexOf(QLatin1Char('/'));
    const QString leafName = slash == -1 ? name : name.mid(slash + 1);
    bool added = false;
    if (slash != -1) {
        const QString nodeName = name.left(slash);
        int pos = positionForName(local, nodeName);
        BranchNode *child = (pos == local->count()) ? 0 : local->children.at(pos);
        if (!child || child->name != nodeName) {
            child = new BranchNode(nodeName);
            beginInsertRows(nodeToIndex(local), pos, pos);
            added = true;
            child->parent = local;
            local->children.insert(pos, child);
        }
        local = child;
Tobias Hunger's avatar
Tobias Hunger committed
581
    }
582
583
584
585
    int pos = positionForName(local, leafName);
    BranchNode *newNode = new BranchNode(leafName, sha(startPoint), track ? trackedBranch : QString());
    if (!added)
        beginInsertRows(nodeToIndex(local), pos, pos);
Tobias Hunger's avatar
Tobias Hunger committed
586
587
588
    newNode->parent = local;
    local->children.insert(pos, newNode);
    endInsertRows();
589
    return nodeToIndex(newNode);
590
591
}

Tobias Hunger's avatar
Tobias Hunger committed
592
593
594
595
596
void BranchModel::parseOutputLine(const QString &line)
{
    if (line.size() < 3)
        return;

597
598
599
600
    QStringList lineParts = line.split(QLatin1Char('\t'));
    const QString shaDeref = lineParts.at(3);
    const QString sha = shaDeref.isEmpty() ? lineParts.at(0) : shaDeref;
    const QString fullName = lineParts.at(1);
Tobias Hunger's avatar
Tobias Hunger committed
601

602
    bool current = (sha == m_currentSha);
603
    bool showTags = m_client->settings()->boolValue(GitSettings::showTagsKey);
Tobias Hunger's avatar
Tobias Hunger committed
604
605

    // insert node into tree:
606
607
    QStringList nameParts = fullName.split(QLatin1Char('/'));
    nameParts.removeFirst(); // remove refs...
Tobias Hunger's avatar
Tobias Hunger committed
608

609
610
611
    if (nameParts.first() == QLatin1String("heads"))
        nameParts[0] = m_rootNode->children.at(0)->name; // Insert the local designator
    else if (nameParts.first() == QLatin1String("remotes"))
612
        nameParts.removeFirst(); // remove "remotes"
613
614
615
616
617
618
619
    else if (nameParts.first() == QLatin1String("stash"))
        return;
    else if (!showTags && (nameParts.first() == QLatin1String("tags")))
        return;

    // limit depth of list. Git basically only ever wants one / and considers the rest as part of
    // the name.
620
621
622
623
624
    while (nameParts.count() > 3) {
        nameParts[2] = nameParts.at(2) + QLatin1Char('/') + nameParts.at(3);
        nameParts.removeAt(3);
    }

625
    const QString name = nameParts.last();
Tobias Hunger's avatar
Tobias Hunger committed
626
627
    nameParts.removeLast();

628
    BranchNode *newNode = new BranchNode(name, sha, lineParts.at(2));
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
    m_rootNode->insert(nameParts, newNode);
    if (current)
        m_currentBranch = newNode;
}

BranchNode *BranchModel::indexToNode(const QModelIndex &index) const
{
    if (index.column() > 0)
        return 0;
    if (!index.isValid())
        return m_rootNode;
    return static_cast<BranchNode *>(index.internalPointer());
}

QModelIndex BranchModel::nodeToIndex(BranchNode *node) const
{
    if (node == m_rootNode)
        return QModelIndex();
    return createIndex(node->parent->rowOf(node), 0, static_cast<void *>(node));
648
}
Tobias Hunger's avatar
Tobias Hunger committed
649
650
651
652
653
654

QString BranchModel::toolTip(const QString &sha) const
{
    // Show the sha description excluding diff as toolTip
    QString output;
    QString errorMessage;
655
656
657
    QStringList arguments(QLatin1String("-n1"));
    arguments << sha;
    if (!m_client->synchronousLog(m_workingDirectory, arguments, &output, &errorMessage))
Tobias Hunger's avatar
Tobias Hunger committed
658
659
        return errorMessage;
    return output;
660
661
}

Tobias Hunger's avatar
Tobias Hunger committed
662
663
664
} // namespace Internal
} // namespace Git