gitclient.h 14 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
#ifndef GITCLIENT_H
#define GITCLIENT_H

33
#include "gitsettings.h"
34
#include "gitcommand.h"
35

con's avatar
con committed
36
37
38
39
#include <coreplugin/editormanager/ieditor.h>

#include <QtCore/QString>
#include <QtCore/QStringList>
40
#include <QtGui/QWidget>
con's avatar
con committed
41
42

QT_BEGIN_NAMESPACE
43
class QCheckBox;
con's avatar
con committed
44
class QErrorMessage;
45
class QSignalMapper;
46
class QDebug;
47
class QProcessEnvironment;
con's avatar
con committed
48
49
50
51
52
53
54
55
56
57
QT_END_NAMESPACE

namespace Core {
    class ICore;
}

namespace VCSBase {
    class VCSBaseEditor;
}

58
59
60
61
namespace Utils {
    struct SynchronousProcessResponse;
}

con's avatar
con committed
62
63
64
65
66
67
68
69
namespace Git {
namespace Internal {

class GitPlugin;
class GitOutputWindow;
class GitCommand;
struct CommitData;
struct GitSubmitEditorPanelData;
70
class Stash;
con's avatar
con committed
71

72
class GitClient : public QObject
con's avatar
con committed
73
74
{
    Q_OBJECT
hjk's avatar
hjk committed
75

con's avatar
con committed
76
public:
77
78
    static const char *stashNamePrefix;

79
    explicit GitClient(GitPlugin *plugin);
hjk's avatar
hjk committed
80
81
82
83
    ~GitClient();

    static QString findRepositoryForDirectory(const QString &dir);

84
    void diff(const QString &workingDirectory, const QStringList &diffArgs, const QString &fileName);
85
86
    void diff(const QString &workingDirectory, const QStringList &diffArgs,
              const QStringList &unstagedFileNames, const QStringList &stagedFileNames= QStringList());
87
88
89
    void diffBranch(const QString &workingDirectory,
                    const QStringList &diffArgs,
                    const QString &branchName);
hjk's avatar
hjk committed
90
91

    void status(const QString &workingDirectory);
92
93
    void graphLog(const QString &workingDirectory) { graphLog(workingDirectory, QString()); }
    void graphLog(const QString &workingDirectory, const QString &branch);
94
95
    void log(const QString &workingDirectory, const QStringList &fileNames,
             bool enableAnnotationContextMenu = false);
96
    void blame(const QString &workingDirectory, const QStringList &args, const QString &fileName,
97
               const QString &revision = QString(), int lineNumber = -1);
hjk's avatar
hjk committed
98
    void checkout(const QString &workingDirectory, const QString &file);
99
    void checkoutBranch(const QString &workingDirectory, const QString &branch);
100
    void hardReset(const QString &workingDirectory, const QString &commit = QString());
hjk's avatar
hjk committed
101
    void addFile(const QString &workingDirectory, const QString &fileName);
102
103
104
105
106
107
108
    bool synchronousAdd(const QString &workingDirectory,
                        // Warning: Works only from 1.6.1 onwards
                        bool intendToAdd,
                        const QStringList &files);
    bool synchronousDelete(const QString &workingDirectory,
                           bool force,
                           const QStringList &files);
dt's avatar
dt committed
109
110
111
    bool synchronousMove(const QString &workingDirectory,
                         const QString &from,
                         const QString &to);
112
113
114
    bool synchronousReset(const QString &workingDirectory,
                          const QStringList &files = QStringList(),
                          QString *errorMessage = 0);
115
    bool synchronousCleanList(const QString &workingDirectory, QStringList *files, QString *errorMessage);
116
    bool synchronousApplyPatch(const QString &workingDirectory, const QString &file, QString *errorMessage);
117
    bool synchronousInit(const QString &workingDirectory);
118
119
    bool synchronousCheckoutFiles(const QString &workingDirectory,
                                  QStringList files = QStringList(),
120
121
                                  QString revision = QString(), QString *errorMessage = 0,
                                  bool revertStaging = true);
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    // Checkout branch
    bool synchronousCheckoutBranch(const QString &workingDirectory, const QString &branch, QString *errorMessage = 0);

    // Do a stash and return identier.
    enum { StashPromptDescription = 0x1, StashImmediateRestore = 0x2, StashIgnoreUnchanged = 0x4 };
    QString synchronousStash(const QString &workingDirectory,
                             const QString &messageKeyword = QString(),
                             unsigned flags = 0, bool *unchanged = 0);

    bool executeSynchronousStash(const QString &workingDirectory,
                                 const QString &message = QString(),
                                 QString *errorMessage = 0);
    bool synchronousStashRestore(const QString &workingDirectory,
                                 const QString &stash,
                                 const QString &branch = QString(),
                                 QString *errorMessage = 0);
    bool synchronousStashRemove(const QString &workingDirectory,
                                const QString &stash = QString(),
                                QString *errorMessage = 0);
141
142
143
144
    bool synchronousBranchCmd(const QString &workingDirectory, QStringList branchArgs,
                              QString *output, QString *errorMessage);
    bool synchronousShow(const QString &workingDirectory, const QString &id,
                              QString *output, QString *errorMessage);
145
146
147
148
149
150
151
152
153
154
155
    bool synchronousParentRevisions(const QString &workingDirectory,
                                    const QStringList &files /* = QStringList() */,
                                    const QString &revision,
                                    QStringList *parents,
                                    QString *errorMessage);
    bool synchronousShortDescription(const QString &workingDirectory, const QString &revision,
                                     QString *description, QString *errorMessage);
    bool synchronousShortDescription(const QString &workingDirectory, const QString &revision,
                                     const QString &format, QString *description, QString *errorMessage);
    bool synchronousShortDescriptions(const QString &workingDirectory, const QStringList &revisions,
                                      QStringList *descriptions, QString *errorMessage);
156
157
    bool synchronousTopRevision(const QString &workingDirectory, QString *revision = 0,
                                QString *branch = 0, QString *errorMessage = 0);
158
159
    // determine version as '(major << 16) + (minor << 8) + patch' or 0
    // with some smart caching.
160
161
    unsigned gitVersion(bool silent, QString *errorMessage = 0);
    QString gitVersionString(bool silent, QString *errorMessage = 0);
162

Tobias Hunger's avatar
Tobias Hunger committed
163
    bool cloneRepository(const QString &directory, const QByteArray &url);
164
    QString vcsGetRepositoryURL(const QString &directory);
Robert Loehning's avatar
Robert Loehning committed
165
    bool synchronousFetch(const QString &workingDirectory);
166
167
    bool synchronousPull(const QString &workingDirectory);
    bool synchronousPush(const QString &workingDirectory);
hjk's avatar
hjk committed
168

169
    // git svn support (asynchronous).
170
    void synchronousSubversionFetch(const QString &workingDirectory);
171
172
    void subversionLog(const QString &workingDirectory);

173
    void stashPop(const QString &workingDirectory);
174
    void revert(const QStringList &files, bool revertStaging);
175
176
    void branchList(const QString &workingDirectory);
    void stashList(const QString &workingDirectory);
177
178
179
180
181
182
183
    bool synchronousStashList(const QString &workingDirectory,
                              QList<Stash> *stashes,
                              QString *errorMessage = 0);
    // Resolve a stash name from message (for IVersionControl's names).
    bool stashNameFromMessage(const QString &workingDirectory,
                              const QString &messge, QString *name,
                              QString *errorMessage = 0);
184

hjk's avatar
hjk committed
185
186
187
188
    QString readConfig(const QString &workingDirectory, const QStringList &configVar);

    QString readConfigValue(const QString &workingDirectory, const QString &configVar);

189
190
191
192
193
    enum StashResult { StashUnchanged, StashCanceled, StashFailed,
                       Stashed, NotStashed /* User did not want it */ };
    StashResult ensureStash(const QString &workingDirectory, QString *errorMessage);
    StashResult ensureStash(const QString &workingDirectory);

194
195
    bool getCommitData(const QString &workingDirectory, bool amend,
                       QString *commitTemplate, CommitData *commitData,
hjk's avatar
hjk committed
196
197
198
199
                       QString *errorMessage);

    bool addAndCommit(const QString &workingDirectory,
                      const GitSubmitEditorPanelData &data,
200
                      const QString &amendSHA1,
hjk's avatar
hjk committed
201
                      const QString &messageFile,
202
                      const QStringList &checkedFiles,
203
204
                      const QStringList &origCommitFiles,
                      const QStringList &origDeletedFiles);
con's avatar
con committed
205

206
207
    enum StatusResult { StatusChanged, StatusUnchanged, StatusFailed };
    StatusResult gitStatus(const QString &workingDirectory,
208
                           bool untracked = false,
209
                           QString *output = 0,
210
211
                           QString *errorMessage = 0,
                           bool *onBranch = 0);
212

213
    void launchGitK(const QString &workingDirectory);
214
    QStringList synchronousRepositoryBranches(const QString &repositoryURL);
215

216
217
218
    GitSettings  settings() const;
    void setSettings(const GitSettings &s);

219
    QString binary() const; // Executable + basic arguments
220
    QProcessEnvironment processEnvironment() const;
221
    static QString fakeWinHome(const QProcessEnvironment &e);
222

223
224
    static QString msgNoChangedFiles();

225
226
    static const char *noColorOption;

con's avatar
con committed
227
public slots:
228
    void show(const QString &source, const QString &id, const QStringList &args = QStringList());
con's avatar
con committed
229

230
231
232
private slots:
    void slotBlameRevisionRequested(const QString &source, QString change, int lineNumber);

con's avatar
con committed
233
private:
234
235
    VCSBase::VCSBaseEditor *findExistingVCSEditor(const char *registerDynamicProperty,
                                                  const QString &dynamicPropertyValue) const;
hjk's avatar
hjk committed
236
    VCSBase::VCSBaseEditor *createVCSEditor(const QString &kind,
237
238
239
240
241
242
                                            QString title,
                                            const QString &source,
                                            bool setSourceCodec,
                                            const char *registerDynamicProperty,
                                            const QString &dynamicPropertyValue,
                                            QWidget *configWidget) const;
con's avatar
con committed
243

244
245
    GitCommand *createCommand(const QString &workingDirectory,
                             VCSBase::VCSBaseEditor* editor = 0,
246
247
                             bool outputToWindow = false,
                             int editorLineNumber = -1);
con's avatar
con committed
248

249
250
251
252
253
    GitCommand *executeGit(const QString &workingDirectory,
                           const QStringList &arguments,
                           VCSBase::VCSBaseEditor* editor = 0,
                           bool outputToWindow = false,
                           GitCommand::TerminationReportMode tm = GitCommand::NoReport,
254
255
                           int editorLineNumber = -1,
                           bool unixTerminalDisabled = false);
con's avatar
con committed
256

257
258
    // Fully synchronous git execution (QProcess-based).
    bool fullySynchronousGit(const QString &workingDirectory,
259
                        const QStringList &arguments,
260
261
                        QByteArray* outputText,
                        QByteArray* errorText,
262
                        bool logCommandToWindow = true);
263
264

    // Synchronous git execution using Utils::SynchronousProcess, with
265
    // log windows updating (using VCSBasePlugin::runVCS with flags).
266
267
268
269
    inline Utils::SynchronousProcessResponse
            synchronousGit(const QString &workingDirectory, const QStringList &arguments,
                           unsigned flags = 0, QTextCodec *outputCodec = 0);

270
    // determine version as '(major << 16) + (minor << 8) + patch' or 0.
271
    unsigned synchronousGitVersion(bool silent, QString *errorMessage = 0);
272
273

    enum RevertResult { RevertOk, RevertUnchanged, RevertCanceled, RevertFailed };
274
275
276
277
    RevertResult revertI(QStringList files,
                         bool *isDirectory,
                         QString *errorMessage,
                         bool revertStaging);
278
    void connectRepositoryChanged(const QString & repository, GitCommand *cmd);
279
280
    bool synchronousPull(const QString &workingDirectory, bool rebase);
    void syncAbortPullRebase(const QString &workingDir);
Robert Loehning's avatar
Robert Loehning committed
281
282
283
284
    bool tryLauchingGitK(const QProcessEnvironment &env,
                         const QString &workingDirectory,
                         const QString &gitBinDirectory,
                         bool silent);
con's avatar
con committed
285

hjk's avatar
hjk committed
286
287
288
    const QString m_msgWait;
    GitPlugin     *m_plugin;
    Core::ICore   *m_core;
289
    GitSettings   m_settings;
290
    QString m_binaryPath;
291
    QSignalMapper *m_repositoryChangedSignalMapper;
292
    unsigned      m_cachedGitVersion;
293
    bool          m_hasCachedGitVersion;
con's avatar
con committed
294
295
};

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
class BaseGitArgumentsWidget : public QWidget {
    Q_OBJECT

public:
    BaseGitArgumentsWidget(GitSettings *settings,
                           GitClient *client, const QString &directory,
                           const QStringList &args);

    virtual QStringList arguments() const = 0;
    virtual void redoCommand() = 0;

protected slots:
    virtual void testForArgumentsChanged() = 0;

protected:
    GitClient *m_client;

    const QString m_workingDirectory;
    QStringList m_diffArgs;
    GitSettings *m_settings;
};

hjk's avatar
hjk committed
318
319
} // namespace Internal
} // namespace Git
con's avatar
con committed
320
321

#endif // GITCLIENT_H