File indexing completed on 2024-05-19 05:45:41
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "manager.h" 0008 0009 #include <QProcess> 0010 #include <gitglobal.h> 0011 0012 namespace Git 0013 { 0014 0015 MiniManager::MiniManager(const QString &path) 0016 { 0017 setPath(path); 0018 } 0019 0020 const QString &MiniManager::path() const 0021 { 0022 return mPath; 0023 } 0024 0025 void MiniManager::setPath(const QString &newPath) 0026 { 0027 if (mPath == newPath) 0028 return; 0029 0030 QProcess p; 0031 p.setProgram(QStringLiteral("git")); 0032 p.setArguments({QStringLiteral("rev-parse"), QStringLiteral("--show-toplevel")}); 0033 p.setWorkingDirectory(newPath); 0034 p.start(); 0035 p.waitForFinished(); 0036 auto ret = p.readAllStandardOutput() + p.readAllStandardError(); 0037 0038 if (p.exitStatus() == QProcess::CrashExit || ret == QLatin1String() || ret.contains("fatal")) { 0039 mPath = QString(); 0040 mIsValid = false; 0041 } else { 0042 mPath = ret.replace("\n", ""); 0043 mIsValid = true; 0044 } 0045 } 0046 0047 bool MiniManager::isValid() const 0048 { 0049 return mIsValid; 0050 } 0051 0052 QList<FileStatus> MiniManager::repoFilesStatus() const 0053 { 0054 const auto buffer = Git::readAllNonEmptyOutput(mPath, 0055 {QStringLiteral("status"), 0056 QStringLiteral("--untracked-files=all"), 0057 QStringLiteral("--ignored"), 0058 QStringLiteral("--short"), 0059 QStringLiteral("--ignore-submodules"), 0060 QStringLiteral("--porcelain")}, 0061 false); 0062 0063 QList<FileStatus> files; 0064 QList<FileStatus> unknownFiles; 0065 // TODO: read untrackeds 0066 for (const auto &item : buffer) { 0067 if (item.trimmed().isEmpty()) 0068 continue; 0069 0070 FileStatus fs; 0071 fs.parseStatusLine(item); 0072 fs.setFullPath(mPath + QLatin1Char('/') + fs.name()); 0073 0074 if (item.startsWith(QStringLiteral("??"))) 0075 unknownFiles << fs; 0076 else 0077 files.append(fs); 0078 } 0079 for (auto &f : unknownFiles) { 0080 const auto n = files.indexOf(f); 0081 if (n == -1) { 0082 f.setStatus(FileStatus::Untracked); 0083 files.append(f); 0084 } 0085 } 0086 return files; 0087 } 0088 0089 void MiniManager::repoFilesStatus(const std::function<bool(const FileStatus &)> &cb) const 0090 { 0091 const auto buffer = Git::readAllNonEmptyOutput(mPath, 0092 {QStringLiteral("status"), 0093 QStringLiteral("--untracked-files=all"), 0094 QStringLiteral("--ignored"), 0095 QStringLiteral("--short"), 0096 QStringLiteral("--ignore-submodules"), 0097 QStringLiteral("--porcelain")}, 0098 false); 0099 0100 QList<FileStatus> unknownFiles; 0101 // TODO: read untrackeds 0102 for (const auto &item : buffer) { 0103 if (item.trimmed().isEmpty()) 0104 continue; 0105 0106 FileStatus fs; 0107 fs.parseStatusLine(item); 0108 fs.setFullPath(mPath + QLatin1Char('/') + fs.name()); 0109 0110 if (item.startsWith(QStringLiteral("??"))) 0111 unknownFiles << fs; 0112 else if (!cb(fs)) 0113 return; 0114 // files.append(fs); 0115 } 0116 // for (auto &f : unknownFiles) { 0117 // const auto n = files.indexOf(f); 0118 // if (n == -1) { 0119 // f.setStatus(FileStatus::Untracked); 0120 // files.append(f); 0121 // } 0122 // } 0123 } 0124 }