gitclient.h 18.5 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12
13
14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16
17
18
19
20
21
22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
hjk's avatar
hjk committed
25

con's avatar
con committed
26
27
28
#ifndef GITCLIENT_H
#define GITCLIENT_H

29
#include "gitsettings.h"
Petar Perisin's avatar
Petar Perisin committed
30
#include "commitdata.h"
31

con's avatar
con committed
32
#include <coreplugin/editormanager/ieditor.h>
33
#include <vcsbase/vcsbaseclient.h>
con's avatar
con committed
34

35
36
#include <utils/fileutils.h>

37
#include <QFutureSynchronizer>
38
39
40
41
#include <QObject>
#include <QString>
#include <QStringList>
#include <QWidget>
con's avatar
con committed
42
43

QT_BEGIN_NAMESPACE
44
class QCheckBox;
45
class QSignalMapper;
46
class QDebug;
47
class QProcessEnvironment;
jkobus's avatar
jkobus committed
48
class QMenu;
con's avatar
con committed
49
50
QT_END_NAMESPACE

51
namespace Core { class ICore; }
con's avatar
con committed
52

hjk's avatar
hjk committed
53
namespace VcsBase {
hjk's avatar
hjk committed
54
    class VcsCommand;
55
    class SubmitFileModel;
56
    class VcsBaseEditorWidget;
Orgad Shaneh's avatar
Orgad Shaneh committed
57
    class VcsBaseEditorParameterWidget;
con's avatar
con committed
58
59
}

60
61
62
63
namespace Utils {
class ExitCodeInterpreter;
struct SynchronousProcessResponse;
} // namespace Utils
64

jkobus's avatar
jkobus committed
65
66
67
68
namespace DiffEditor {
class DiffEditorDocument;
class DiffEditorController;
}
69

con's avatar
con committed
70
71
72
namespace Git {
namespace Internal {

73
class CommitData;
con's avatar
con committed
74
struct GitSubmitEditorPanelData;
75
class Stash;
con's avatar
con committed
76

77
78
79
80
81
82
83
enum StatusMode
{
    ShowAll = 0,
    NoUntracked = 1,
    NoSubmodules = 2
};

84
85
86
87
88
89
enum StashFlag {
    Default        = 0x00, /* Prompt and do not allow unstashed */
    AllowUnstashed = 0x01,
    NoPrompt       = 0x02
};

90
91
92
93
94
95
96
97
98
99
class SubmoduleData
{
public:
    QString dir;
    QString url;
    QString ignore;
};

typedef QMap<QString, SubmoduleData> SubmoduleDataMap;

100
class GitClient : public VcsBase::VcsBaseClientImpl
con's avatar
con committed
101
102
{
    Q_OBJECT
hjk's avatar
hjk committed
103

con's avatar
con committed
104
public:
Petar Perisin's avatar
Petar Perisin committed
105
106
107
    enum CommandInProgress { NoCommand, Revert, CherryPick,
                             Rebase, Merge, RebaseMerge };

Orgad Shaneh's avatar
Orgad Shaneh committed
108
    class StashInfo
Orgad Shaneh's avatar
Orgad Shaneh committed
109
110
    {
    public:
Orgad Shaneh's avatar
Orgad Shaneh committed
111
        StashInfo();
112
113
114
        enum StashResult { StashUnchanged, StashCanceled, StashFailed,
                           Stashed, NotStashed /* User did not want it */ };

Petar Perisin's avatar
Petar Perisin committed
115
116
        bool init(const QString &workingDirectory, const QString &command,
                  StashFlag flag = Default, PushAction pushAction = NoPush);
117
        bool stashingFailed() const;
Orgad Shaneh's avatar
Orgad Shaneh committed
118
        void end();
119
120
        StashResult result() const { return m_stashResult; }
        QString stashMessage() const { return m_message; }
Orgad Shaneh's avatar
Orgad Shaneh committed
121
122

    private:
123
124
        void stashPrompt(const QString &command, const QString &statusOutput, QString *errorMessage);
        void executeStash(const QString &command, QString *errorMessage);
125

126
127
128
129
130
        StashResult m_stashResult;
        QString m_message;
        QString m_workingDir;
        GitClient *m_client;
        StashFlag m_flags;
Petar Perisin's avatar
Petar Perisin committed
131
        PushAction m_pushAction;
Orgad Shaneh's avatar
Orgad Shaneh committed
132
133
    };

134
135
    static const char *stashNamePrefix;

136
    explicit GitClient();
hjk's avatar
hjk committed
137

Orgad Shaneh's avatar
Orgad Shaneh committed
138
    Utils::FileName vcsBinary() const override;
Orgad Shaneh's avatar
Orgad Shaneh committed
139
    unsigned gitVersion(QString *errorMessage = 0) const;
140

141
142
    VcsBase::VcsCommand *vcsExecAbortable(const QString &workingDirectory, const QStringList &arguments);

jkobus's avatar
jkobus committed
143
    QString findRepositoryForDirectory(const QString &dir) const;
Orgad Shaneh's avatar
Orgad Shaneh committed
144
    QString findGitDirForRepository(const QString &repositoryDir) const;
145
    bool managesFile(const QString &workingDirectory, const QString &fileName) const;
hjk's avatar
hjk committed
146

147
148
149
150
151
152
    void diffFile(const QString &workingDirectory, const QString &fileName) const;
    void diffFiles(const QString &workingDirectory,
                   const QStringList &unstagedFileNames,
                   const QStringList &stagedFileNames) const;
    void diffProject(const QString &workingDirectory,
                     const QString &projectDirectory) const;
153
    void diffRepository(const QString &workingDirectory);
154
    void diffBranch(const QString &workingDirectory,
jkobus's avatar
jkobus committed
155
                    const QString &branchName) const;
156
    void merge(const QString &workingDirectory, const QStringList &unmergedFileNames = QStringList());
hjk's avatar
hjk committed
157
158

    void status(const QString &workingDirectory);
159
    void log(const QString &workingDirectory, const QString &fileName = QString(),
160
             bool enableAnnotationContextMenu = false, const QStringList &args = QStringList());
Orgad Shaneh's avatar
Orgad Shaneh committed
161
    void reflog(const QString &workingDirectory);
162
163
164
    void annotate(const QString &workingDir, const QString &file,
                  const QString &revision = QString(), int lineNumber = -1,
                  const QStringList &extraOptions = QStringList()) override;
Orgad Shaneh's avatar
Orgad Shaneh committed
165
    void reset(const QString &workingDirectory, const QString &argument, const QString &commit = QString());
hjk's avatar
hjk committed
166
    void addFile(const QString &workingDirectory, const QString &fileName);
167
168
    bool synchronousLog(const QString &workingDirectory,
                        const QStringList &arguments,
169
170
171
                        QString *output,
                        QString *errorMessage = 0,
                        unsigned flags = 0);
172
    bool synchronousAdd(const QString &workingDirectory, const QStringList &files);
173
174
175
    bool synchronousDelete(const QString &workingDirectory,
                           bool force,
                           const QStringList &files);
dt's avatar
dt committed
176
177
178
    bool synchronousMove(const QString &workingDirectory,
                         const QString &from,
                         const QString &to);
179
180
181
    bool synchronousReset(const QString &workingDirectory,
                          const QStringList &files = QStringList(),
                          QString *errorMessage = 0);
182
    bool synchronousCleanList(const QString &workingDirectory, const QString &modulePath, QStringList *files, QStringList *ignoredFiles, QString *errorMessage);
183
    bool synchronousApplyPatch(const QString &workingDirectory, const QString &file, QString *errorMessage, const QStringList &arguments = QStringList());
184
    bool synchronousInit(const QString &workingDirectory);
185
186
    bool synchronousCheckoutFiles(const QString &workingDirectory,
                                  QStringList files = QStringList(),
187
188
                                  QString revision = QString(), QString *errorMessage = 0,
                                  bool revertStaging = true);
189
190
    // Checkout ref
    bool stashAndCheckout(const QString &workingDirectory, const QString &ref);
191
192
    bool synchronousCheckout(const QString &workingDirectory, const QString &ref,
                             QString *errorMessage = 0);
193
194

    QStringList setupCheckoutArguments(const QString &workingDirectory, const QString &ref);
195
    void updateSubmodulesIfNeeded(const QString &workingDirectory, bool prompt);
196
197
198
199
200

    // Do a stash and return identier.
    enum { StashPromptDescription = 0x1, StashImmediateRestore = 0x2, StashIgnoreUnchanged = 0x4 };
    QString synchronousStash(const QString &workingDirectory,
                             const QString &messageKeyword = QString(),
jkobus's avatar
jkobus committed
201
                             unsigned flags = 0, bool *unchanged = 0) const;
202
203
204

    bool executeSynchronousStash(const QString &workingDirectory,
                                 const QString &message = QString(),
Orgad Shaneh's avatar
Orgad Shaneh committed
205
                                 bool unstagedOnly = false,
jkobus's avatar
jkobus committed
206
                                 QString *errorMessage = 0) const;
207
208
    bool synchronousStashRestore(const QString &workingDirectory,
                                 const QString &stash,
209
                                 bool pop = false,
jkobus's avatar
jkobus committed
210
                                 const QString &branch = QString()) const;
211
212
    bool synchronousStashRemove(const QString &workingDirectory,
                                const QString &stash = QString(),
jkobus's avatar
jkobus committed
213
                                QString *errorMessage = 0) const;
214
    bool synchronousBranchCmd(const QString &workingDirectory, QStringList branchArgs,
jkobus's avatar
jkobus committed
215
                              QString *output, QString *errorMessage) const;
216
    bool synchronousTagCmd(const QString &workingDirectory, QStringList tagArgs,
jkobus's avatar
jkobus committed
217
                           QString *output, QString *errorMessage) const;
218
    bool synchronousForEachRefCmd(const QString &workingDirectory, QStringList args,
jkobus's avatar
jkobus committed
219
                               QString *output, QString *errorMessage = 0) const;
220
    bool synchronousRemoteCmd(const QString &workingDirectory, QStringList remoteArgs,
221
                              QString *output = 0, QString *errorMessage = 0, bool silent = false) const;
222
223

    QMap<QString,QString> synchronousRemotesList(const QString &workingDirectory,
jkobus's avatar
jkobus committed
224
                                                 QString *errorMessage = 0) const;
225
    QStringList synchronousSubmoduleStatus(const QString &workingDirectory,
jkobus's avatar
jkobus committed
226
227
                                           QString *errorMessage = 0) const;
    SubmoduleDataMap submoduleList(const QString &workingDirectory) const;
228
    bool synchronousShow(const QString &workingDirectory, const QString &id,
229
                         QByteArray *output, QString *errorMessage) const;
230
231

    bool synchronousRevListCmd(const QString &workingDirectory, const QStringList &arguments,
jkobus's avatar
jkobus committed
232
                               QString *output, QString *errorMessage = 0) const;
233

234
235
236
    bool synchronousParentRevisions(const QString &workingDirectory,
                                    const QString &revision,
                                    QStringList *parents,
jkobus's avatar
jkobus committed
237
238
                                    QString *errorMessage) const;
    QString synchronousShortDescription(const QString &workingDirectory, const QString &revision) const;
239
    QString synchronousShortDescription(const QString &workingDirectory, const QString &revision,
jkobus's avatar
jkobus committed
240
                                     const QString &format) const;
241

jkobus's avatar
jkobus committed
242
    QString synchronousCurrentLocalBranch(const QString &workingDirectory) const;
243

244
    bool synchronousHeadRefs(const QString &workingDirectory, QStringList *output,
jkobus's avatar
jkobus committed
245
246
                             QString *errorMessage = 0) const;
    QString synchronousTopic(const QString &workingDirectory) const;
Petar Perisin's avatar
Petar Perisin committed
247
248
    bool synchronousRevParseCmd(const QString &workingDirectory, const QString &ref,
                                QString *output, QString *errorMessage = 0) const;
249
    QString synchronousTopRevision(const QString &workingDirectory, QString *errorMessage = 0);
250
    void synchronousTagsForCommit(const QString &workingDirectory, const QString &revision,
jkobus's avatar
jkobus committed
251
                                  QString &precedes, QString &follows) const;
Orgad Shaneh's avatar
Orgad Shaneh committed
252
    bool isRemoteCommit(const QString &workingDirectory, const QString &commit);
253
    bool isFastForwardMerge(const QString &workingDirectory, const QString &branch);
254

Tobias Hunger's avatar
Tobias Hunger committed
255
    bool cloneRepository(const QString &directory, const QByteArray &url);
256
    void fetch(const QString &workingDirectory, const QString &remote);
257
    bool synchronousPull(const QString &workingDirectory, bool rebase);
258
    void push(const QString &workingDirectory, const QStringList &pushArgs = QStringList());
259
260
    bool synchronousMerge(const QString &workingDirectory, const QString &branch,
                          bool allowFastForward = true);
Orgad Shaneh's avatar
Orgad Shaneh committed
261
    bool canRebase(const QString &workingDirectory) const;
262
263
264
    void rebase(const QString &workingDirectory, const QString &argument);
    void cherryPick(const QString &workingDirectory, const QString &argument);
    void revert(const QString &workingDirectory, const QString &argument);
265

Petar Perisin's avatar
Petar Perisin committed
266
267
    bool synchronousRevert(const QString &workingDirectory, const QString &commit);
    bool synchronousCherryPick(const QString &workingDirectory, const QString &commit);
Orgad Shaneh's avatar
Orgad Shaneh committed
268
    void interactiveRebase(const QString &workingDirectory, const QString &commit, bool fixup);
269
    void synchronousAbortCommand(const QString &workingDir, const QString &abortCommand);
270
271
    QString synchronousTrackingBranch(const QString &workingDirectory,
                                      const QString &branch = QString());
Orgad Shaneh's avatar
Orgad Shaneh committed
272
273
274
    bool synchronousSetTrackingBranch(const QString &workingDirectory,
                                      const QString &branch,
                                      const QString &tracking);
hjk's avatar
hjk committed
275

276
    // git svn support (asynchronous).
277
    void synchronousSubversionFetch(const QString &workingDirectory);
278
279
    void subversionLog(const QString &workingDirectory);

280
    void stashPop(const QString &workingDirectory, const QString &stash = QString());
281
    void revert(const QStringList &files, bool revertStaging);
282
283
    bool synchronousStashList(const QString &workingDirectory,
                              QList<Stash> *stashes,
jkobus's avatar
jkobus committed
284
                              QString *errorMessage = 0) const;
285
286
287
    // Resolve a stash name from message (for IVersionControl's names).
    bool stashNameFromMessage(const QString &workingDirectory,
                              const QString &messge, QString *name,
jkobus's avatar
jkobus committed
288
                              QString *errorMessage = 0) const;
289

290
    QString readGitVar(const QString &workingDirectory, const QString &configVar) const;
291
    QString readConfigValue(const QString &workingDirectory, const QString &configVar) const;
hjk's avatar
hjk committed
292

293
    QTextCodec *encoding(const QString &workingDirectory, const QByteArray &configVar) const;
294
295
296
    bool readDataFromCommit(const QString &repoDirectory, const QString &commit,
                            CommitData &commitData, QString *errorMessage = 0,
                            QString *commitTemplate = 0);
297
    bool getCommitData(const QString &workingDirectory, QString *commitTemplate,
298
                       CommitData &commitData, QString *errorMessage);
hjk's avatar
hjk committed
299
300
301

    bool addAndCommit(const QString &workingDirectory,
                      const GitSubmitEditorPanelData &data,
302
                      CommitType commitType,
303
                      const QString &amendSHA1,
hjk's avatar
hjk committed
304
                      const QString &messageFile,
hjk's avatar
hjk committed
305
                      VcsBase::SubmitFileModel *model);
con's avatar
con committed
306

307
308
    enum StatusResult { StatusChanged, StatusUnchanged, StatusFailed };
    StatusResult gitStatus(const QString &workingDirectory,
309
                           StatusMode mode,
310
                           QString *output = 0,
jkobus's avatar
jkobus committed
311
                           QString *errorMessage = 0) const;
312

jkobus's avatar
jkobus committed
313
314
    CommandInProgress checkCommandInProgress(const QString &workingDirectory) const;
    QString commandInProgressDescription(const QString &workingDirectory) const;
315

316
    void continueCommandIfNeeded(const QString &workingDirectory, bool allowContinue = true);
317

jkobus's avatar
jkobus committed
318
    QString extendedShowDescription(const QString &workingDirectory, const QString &text) const;
319

Petar Perisin's avatar
Petar Perisin committed
320
321
    void launchGitK(const QString &workingDirectory, const QString &fileName);
    void launchGitK(const QString &workingDirectory) { launchGitK(workingDirectory, QString()); }
322
    bool launchGitGui(const QString &workingDirectory);
323
    Utils::FileName gitBinDirectory();
Petar Perisin's avatar
Petar Perisin committed
324

325
326
    void launchRepositoryBrowser(const QString &workingDirectory);

327
328
    QStringList synchronousRepositoryBranches(const QString &repositoryURL,
                                              const QString &workingDirectory = QString()) const;
329

Orgad Shaneh's avatar
Orgad Shaneh committed
330
    QProcessEnvironment processEnvironment() const override;
331

Petar Perisin's avatar
Petar Perisin committed
332
333
    bool beginStashScope(const QString &workingDirectory, const QString &command,
                         StashFlag flag = Default, PushAction pushAction = NoPush);
Orgad Shaneh's avatar
Orgad Shaneh committed
334
335
    StashInfo &stashInfo(const QString &workingDirectory);
    void endStashScope(const QString &workingDirectory);
336
    bool isValidRevision(const QString &revision) const;
337
    void handleMergeConflicts(const QString &workingDir, const QString &commit, const QStringList &files, const QString &abortCommand);
338
    void addFuture(const QFuture<void> &future);
339

340
    static QString msgNoChangedFiles();
341
    static QString msgNoCommits(bool includeRemote);
342

con's avatar
con committed
343
public slots:
Tobias Hunger's avatar
Tobias Hunger committed
344
    void show(const QString &source, const QString &id, const QString &name = QString());
con's avatar
con committed
345

Orgad Shaneh's avatar
Orgad Shaneh committed
346
private:
347
    void finishSubmoduleUpdate();
348
    void fetchFinished(const QVariant &cookie);
Orgad Shaneh's avatar
Orgad Shaneh committed
349
    void slotChunkActionsRequested(QMenu *menu, bool isValid);
jkobus's avatar
jkobus committed
350
351
    void slotStageChunk();
    void slotUnstageChunk();
352
    void branchesForCommit(const QString &revision);
353

jkobus's avatar
jkobus committed
354
    void stage(const QString &patch, bool revert);
355

356
    enum CodecType { CodecSource, CodecLogOutput, CodecNone };
357
    QTextCodec *codecFor(CodecType codecType, const QString &source = QString()) const;
358

359
360
    void requestReload(const QString &documentId, const QString &source, const QString &title,
                       std::function<DiffEditor::DiffEditorController *(Core::IDocument *)> factory) const;
con's avatar
con committed
361

362
    // determine version as '(major << 16) + (minor << 8) + patch' or 0.
Orgad Shaneh's avatar
Orgad Shaneh committed
363
    unsigned synchronousGitVersion(QString *errorMessage = 0) const;
364

365
366
    QString readOneLine(const QString &workingDirectory, const QStringList &arguments) const;

367
    enum RevertResult { RevertOk, RevertUnchanged, RevertCanceled, RevertFailed };
368
369
370
371
    RevertResult revertI(QStringList files,
                         bool *isDirectory,
                         QString *errorMessage,
                         bool revertStaging);
hjk's avatar
hjk committed
372
    void connectRepositoryChanged(const QString & repository, VcsBase::VcsCommand *cmd);
373
    bool executeAndHandleConflicts(const QString &workingDirectory, const QStringList &arguments,
jkobus's avatar
jkobus committed
374
                                   const QString &abortCommand = QString()) const;
Robert Loehning's avatar
Robert Loehning committed
375
376
    bool tryLauchingGitK(const QProcessEnvironment &env,
                         const QString &workingDirectory,
Petar Perisin's avatar
Petar Perisin committed
377
                         const QString &fileName,
378
                         const QString &gitBinDirectory);
379
    bool cleanList(const QString &workingDirectory, const QString &modulePath, const QString &flag, QStringList *files, QString *errorMessage);
con's avatar
con committed
380

381
382
383
384
385
386
387
388
389
390
    enum ContinueCommandMode {
        ContinueOnly,
        SkipOnly,
        SkipIfNoChanges
    };

    void continuePreviousGitCommand(const QString &workingDirectory, const QString &msgBoxTitle,
                                    QString msgBoxText, const QString &buttonName,
                                    const QString &gitCommand, ContinueCommandMode continueMode);

391
    mutable Utils::FileName m_gitVersionForBinary;
392
393
    mutable unsigned m_cachedGitVersion;

394
    QString m_gitQtcEditor;
Orgad Shaneh's avatar
Orgad Shaneh committed
395
    QMap<QString, StashInfo> m_stashInfo;
396
    QStringList m_updatedSubmodules;
397
    bool m_disableEditor;
398
    QPointer<DiffEditor::DiffEditorController> m_contextController;
399
    QFutureSynchronizer<void> m_synchronizer; // for commit updates
400
401
};

hjk's avatar
hjk committed
402
403
} // namespace Internal
} // namespace Git
con's avatar
con committed
404
405

#endif // GITCLIENT_H