File indexing completed on 2024-04-14 05:34:12

0001 /*
0002     SPDX-FileCopyrightText: 2010 Sebastian Doerner <sebastian@sebastian-doerner.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "gitwrapper.h"
0008 
0009 #include <QTextCodec>
0010 
0011 GitWrapper* GitWrapper::m_instance = nullptr;
0012 const int GitWrapper::BUFFER_SIZE = 256;
0013 const int GitWrapper::SMALL_BUFFER_SIZE = 128;
0014 
0015 GitWrapper::GitWrapper()
0016 {
0017     m_localCodec = QTextCodec::codecForLocale();
0018 }
0019 
0020 
0021 GitWrapper* GitWrapper::instance()
0022 {
0023     if (m_instance == nullptr) {
0024         m_instance = new GitWrapper();
0025     }
0026     return m_instance;
0027 }
0028 
0029 void GitWrapper::freeInstance()
0030 {
0031     delete m_instance;
0032     m_instance = nullptr;
0033 }
0034 
0035 QString GitWrapper::userName()
0036 {
0037     QString result;
0038     char buffer[SMALL_BUFFER_SIZE];
0039     m_process.start(QStringLiteral("git"), {QStringLiteral("config"), QStringLiteral("--get"), QStringLiteral("user.name")});
0040     while (m_process.waitForReadyRead()) {
0041         if (m_process.readLine(buffer, sizeof(buffer)) > 0) {
0042             result = m_localCodec->toUnicode(buffer).trimmed();
0043         }
0044     }
0045     return result;
0046 }
0047 
0048 QString GitWrapper::userEmail()
0049 {
0050     QString result;
0051     char buffer[SMALL_BUFFER_SIZE];
0052     m_process.start(QStringLiteral("git"), {QStringLiteral("config"), QStringLiteral("--get"), QStringLiteral("user.email")});
0053     while (m_process.waitForReadyRead()) {
0054         if (m_process.readLine(buffer, sizeof(buffer)) > 0) {
0055             result = m_localCodec->toUnicode(buffer).trimmed();
0056         }
0057     }
0058     return result;
0059 }
0060 
0061 
0062 QStringList GitWrapper::branches(int* currentBranchIndex)
0063 {
0064     QStringList result;
0065     if (currentBranchIndex != nullptr) {
0066         *currentBranchIndex = -1;
0067     }
0068     m_process.start(QStringLiteral("git"), {QStringLiteral("branch"), QStringLiteral("-a")});
0069     while (m_process.waitForReadyRead()) {
0070         char buffer[BUFFER_SIZE];
0071         while (m_process.readLine(buffer, sizeof(buffer)) > 0){
0072             const QString branchName = m_localCodec->toUnicode(buffer).mid(2).trimmed();
0073             //don't list non-branches and HEAD-branches directly pointing to other branches
0074             if (!branchName.contains(QLatin1String("->")) && !branchName.startsWith(QLatin1Char('('))) {
0075                 result.append(branchName);
0076                 if (currentBranchIndex !=nullptr && buffer[0]=='*') {
0077                     *currentBranchIndex = result.size() - 1;
0078                 }
0079             }
0080         }
0081     }
0082     return result;
0083 }
0084 
0085 void GitWrapper::tagSet(QSet<QString>& result)
0086 {
0087     m_process.start(QStringLiteral("git"), {QStringLiteral("tag")});
0088     while (m_process.waitForReadyRead()) {
0089         char buffer[BUFFER_SIZE];
0090         while (m_process.readLine(buffer, sizeof(buffer)) > 0){
0091             const QString tagName = m_localCodec->toUnicode(buffer).trimmed();
0092             result.insert(tagName);
0093         }
0094     }
0095 }
0096 
0097 QStringList GitWrapper::tags()
0098 {
0099     QStringList result;
0100     m_process.start(QStringLiteral("git"), {QStringLiteral("tag")});
0101     while (m_process.waitForReadyRead()) {
0102         char buffer[BUFFER_SIZE];
0103         while (m_process.readLine(buffer, sizeof(buffer)) > 0){
0104             const QString tagName = m_localCodec->toUnicode(buffer).trimmed();
0105             result.append(tagName);
0106         }
0107     }
0108     return result;
0109 }
0110 
0111 inline QStringList GitWrapper::remotes(QLatin1String lineEnd)
0112 {
0113     QStringList result;
0114     m_process.start(QStringLiteral("git"), {QStringLiteral("remote"), QStringLiteral("-v")});
0115     while (m_process.waitForReadyRead()) {
0116         char buffer[BUFFER_SIZE];
0117         while (m_process.readLine(buffer, sizeof(buffer)) > 0){
0118             const QString line = QString::fromLocal8Bit(buffer).simplified();
0119             if (line.endsWith(lineEnd)) {
0120                 result.append(line.section(QLatin1Char(' '), 0, 0));
0121             }
0122         }
0123     }
0124     return result;
0125 }
0126 
0127 QStringList GitWrapper::pullRemotes()
0128 {
0129     return remotes(QLatin1String("(fetch)"));
0130 }
0131 
0132 QStringList GitWrapper::pushRemotes()
0133 {
0134     return remotes(QLatin1String("(push)"));
0135 }
0136 
0137 QString GitWrapper::lastCommitMessage()
0138 {
0139     QString result;
0140     char buffer[BUFFER_SIZE];
0141     m_process.start(QStringLiteral("git"), {QStringLiteral("log"), QStringLiteral("-1")});
0142     while (m_process.waitForReadyRead()) {
0143         bool inMessage = false;
0144         QStringList message;
0145         while (m_process.readLine(buffer, sizeof(buffer)) > 0) {
0146             const QString currentLine = QString::fromLocal8Bit(buffer);
0147             if (inMessage){
0148                 message << m_localCodec->toUnicode(buffer).trimmed();
0149             }
0150             else if (currentLine.startsWith(QLatin1String("Date:"))) {
0151                 m_process.readLine();
0152                 inMessage = true;
0153             }
0154         }
0155         result = message.join(QLatin1Char('\n'));
0156     }
0157     return result;
0158 }