1#include "GitWip.h"
2
3#include <GitBase.h>
4#include <GitCache.h>
5
6#include <QLogger.h>
7
8#include <QFile>
9
10using namespace QLogger;
11
12GitWip::GitWip(const QSharedPointer<GitBase> &git, const QSharedPointer<GitCache> &cache)
13 : mGit(git)
14 , mCache(cache)
15{
16}
17
18QVector<QString> GitWip::getUntrackedFiles() const
19{
20 QLog_Debug("Git", QString("Executing getUntrackedFiles."));
21
22 auto runCmd = QString("git ls-files --others");
23 const auto exFile = QString("info/exclude");
24 const auto path = QString("%1/%2").arg(mGit->getGitDir(), exFile);
25
26 if (QFile::exists(path))
27 runCmd.append(QString(" --exclude-from=$%1$").arg(path));
28
29 runCmd.append(QString(" --exclude-per-directory=$%1$").arg(".gitignore"));
30
31#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
32 const auto ret = mGit->run(runCmd).output.split('\n', Qt::SkipEmptyParts).toVector();
33#else
34 const auto ret = mGit->run(runCmd).output.split('\n', QString::SkipEmptyParts).toVector();
35#endif
36
37 return ret;
38}
39
40WipRevisionInfo GitWip::getWipInfo() const
41{
42 QLog_Debug("Git", QString("Executing processWip."));
43
44 const auto ret = mGit->run("git rev-parse --revs-only HEAD");
45
46 if (ret.success)
47 {
48 QString diffIndex;
49 QString diffIndexCached;
50
51 auto parentSha = ret.output.trimmed();
52
53 if (parentSha.isEmpty())
54 parentSha = CommitInfo::INIT_SHA;
55
56 const auto ret3 = mGit->run(QString("git diff-index %1").arg(parentSha));
57 diffIndex = ret3.success ? ret3.output : QString();
58
59 const auto ret4 = mGit->run(QString("git diff-index --cached %1").arg(parentSha));
60 diffIndexCached = ret4.success ? ret4.output : QString();
61
62 return { parentSha, diffIndex, diffIndexCached };
63 }
64
65 return {};
66}
67
68bool GitWip::updateWip() const
69{
70 const auto files = getUntrackedFiles();
71 mCache->setUntrackedFilesList(std::move(files));
72
73 if (const auto wipInfo = getWipInfo(); wipInfo.isValid())
74 return mCache->updateWipCommit(wipInfo);
75
76 return false;
77}
78