1 | #include "gitrepository.h" |
2 | #include "gitmanager.h" |
3 | |
4 | #include <QDir> |
5 | |
6 | GitRepository::GitRepository(const QString& folder, QObject *parent) |
7 | : QObject{parent}, |
8 | mInRepository(false) |
9 | { |
10 | mManager = new GitManager(); |
11 | setFolder(folder); |
12 | } |
13 | |
14 | GitRepository::~GitRepository() |
15 | { |
16 | delete mManager; |
17 | } |
18 | |
19 | const QString &GitRepository::folder() const |
20 | { |
21 | return mRealFolder; |
22 | } |
23 | |
24 | void GitRepository::createRepository() |
25 | { |
26 | mManager->createRepository(mRealFolder); |
27 | } |
28 | |
29 | bool GitRepository::hasRepository(QString& currentBranch) |
30 | { |
31 | currentBranch = mBranch; |
32 | return mInRepository; |
33 | } |
34 | |
35 | bool GitRepository::add(const QString &path, QString& output) |
36 | { |
37 | return mManager->add(mFolder,path, output); |
38 | } |
39 | |
40 | bool GitRepository::remove(const QString &path, QString& output) |
41 | { |
42 | return mManager->remove(mFolder,path, output); |
43 | } |
44 | |
45 | bool GitRepository::rename(const QString &oldName, const QString &newName, QString& output) |
46 | { |
47 | return mManager->rename(mFolder, oldName, newName,output); |
48 | } |
49 | |
50 | bool GitRepository::restore(const QString &path, QString& output) |
51 | { |
52 | return mManager->restore(mFolder, path, output); |
53 | } |
54 | |
55 | QSet<QString> GitRepository::listFiles(bool refresh) |
56 | { |
57 | if (refresh) |
58 | update(); |
59 | return mFilesInRepositories; |
60 | } |
61 | |
62 | bool GitRepository::clone(const QString &url, QString& output) |
63 | { |
64 | return mManager->clone(mFolder,url, output); |
65 | } |
66 | |
67 | bool GitRepository::commit(const QString &message, QString& output, bool autoStage) |
68 | { |
69 | return mManager->commit(mRealFolder, message, autoStage, output); |
70 | } |
71 | |
72 | bool GitRepository::revert(QString& output) |
73 | { |
74 | return mManager->revert(mRealFolder, output); |
75 | } |
76 | |
77 | void GitRepository::setFolder(const QString &newFolder) |
78 | { |
79 | mFolder = newFolder; |
80 | if (!newFolder.isEmpty()) |
81 | mRealFolder = mManager->rootFolder(mFolder); |
82 | else |
83 | mRealFolder = newFolder; |
84 | update(); |
85 | } |
86 | |
87 | void GitRepository::update() |
88 | { |
89 | if (!mManager->isValid() || mFolder.isEmpty()) { |
90 | mInRepository = false; |
91 | mBranch = "" ; |
92 | mFilesInRepositories.clear(); |
93 | mChangedFiles.clear(); |
94 | mStagedFiles.clear(); |
95 | mConflicts.clear(); |
96 | } else { |
97 | mInRepository = mManager->hasRepository(mRealFolder,mBranch); |
98 | convertFilesListToSet(mManager->listFiles(mRealFolder),mFilesInRepositories); |
99 | convertFilesListToSet(mManager->listChangedFiles(mRealFolder),mChangedFiles); |
100 | convertFilesListToSet(mManager->listStagedFiles(mRealFolder),mStagedFiles); |
101 | convertFilesListToSet(mManager->listConflicts(mRealFolder),mConflicts); |
102 | // qDebug()<<"update"<<mRealFolder<<mBranch; |
103 | // qDebug()<<mFilesInRepositories; |
104 | // qDebug()<<mChangedFiles; |
105 | // qDebug()<<mStagedFiles; |
106 | } |
107 | } |
108 | |
109 | const QString &GitRepository::realFolder() const |
110 | { |
111 | return mRealFolder; |
112 | } |
113 | |
114 | void GitRepository::convertFilesListToSet(const QStringList &filesList, QSet<QString> &set) |
115 | { |
116 | set.clear(); |
117 | QDir dir(mRealFolder); |
118 | foreach (const QString& s, filesList) { |
119 | set.insert(dir.absoluteFilePath(s)); |
120 | } |
121 | } |
122 | |
123 | |