gitclient.h 18.3 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

Tobias Hunger's avatar
Tobias Hunger committed
26
#pragma once
con's avatar
con committed
27

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

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

34
35
#include <utils/fileutils.h>

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

QT_BEGIN_NAMESPACE
43
class QProcessEnvironment;
jkobus's avatar
jkobus committed
44
class QMenu;
con's avatar
con committed
45
46
QT_END_NAMESPACE

47
namespace Core { class ICore; }
con's avatar
con committed
48

hjk's avatar
hjk committed
49
namespace VcsBase {
hjk's avatar
hjk committed
50
    class VcsCommand;
51
    class SubmitFileModel;
52
    class VcsBaseEditorWidget;
Orgad Shaneh's avatar
Orgad Shaneh committed
53
    class VcsBaseEditorParameterWidget;
con's avatar
con committed
54
55
}

jkobus's avatar
jkobus committed
56
57
58
59
namespace DiffEditor {
class DiffEditorDocument;
class DiffEditorController;
}
60

con's avatar
con committed
61
62
63
namespace Git {
namespace Internal {

64
class CommitData;
Tobias Hunger's avatar
Tobias Hunger committed
65
class GitSubmitEditorPanelData;
66
class Stash;
con's avatar
con committed
67

68
69
70
71
72
73
74
enum StatusMode
{
    ShowAll = 0,
    NoUntracked = 1,
    NoSubmodules = 2
};

75
76
77
78
79
80
enum StashFlag {
    Default        = 0x00, /* Prompt and do not allow unstashed */
    AllowUnstashed = 0x01,
    NoPrompt       = 0x02
};

81
82
83
84
85
86
87
88
89
90
class SubmoduleData
{
public:
    QString dir;
    QString url;
    QString ignore;
};

typedef QMap<QString, SubmoduleData> SubmoduleDataMap;

91
class GitClient : public VcsBase::VcsBaseClientImpl
con's avatar
con committed
92
93
{
    Q_OBJECT
hjk's avatar
hjk committed
94

con's avatar
con committed
95
public:
Petar Perisin's avatar
Petar Perisin committed
96
97
98
    enum CommandInProgress { NoCommand, Revert, CherryPick,
                             Rebase, Merge, RebaseMerge };

Orgad Shaneh's avatar
Orgad Shaneh committed
99
    class StashInfo
Orgad Shaneh's avatar
Orgad Shaneh committed
100
101
    {
    public:
Tobias Hunger's avatar
Tobias Hunger committed
102
        StashInfo() = default;
103
104
105
        enum StashResult { StashUnchanged, StashCanceled, StashFailed,
                           Stashed, NotStashed /* User did not want it */ };

Petar Perisin's avatar
Petar Perisin committed
106
107
        bool init(const QString &workingDirectory, const QString &command,
                  StashFlag flag = Default, PushAction pushAction = NoPush);
108
        bool stashingFailed() const;
Orgad Shaneh's avatar
Orgad Shaneh committed
109
        void end();
110
111
        StashResult result() const { return m_stashResult; }
        QString stashMessage() const { return m_message; }
Orgad Shaneh's avatar
Orgad Shaneh committed
112
113

    private:
114
115
        void stashPrompt(const QString &command, const QString &statusOutput, QString *errorMessage);
        void executeStash(const QString &command, QString *errorMessage);
116

117
        StashResult m_stashResult = NotStashed;
118
119
        QString m_message;
        QString m_workingDir;
120
        StashFlag m_flags = Default;
Tobias Hunger's avatar
Tobias Hunger committed
121
        PushAction m_pushAction = NoPush;
Orgad Shaneh's avatar
Orgad Shaneh committed
122
123
    };

124
    explicit GitClient();
hjk's avatar
hjk committed
125

Orgad Shaneh's avatar
Orgad Shaneh committed
126
    Utils::FileName vcsBinary() const override;
127
    unsigned gitVersion(QString *errorMessage = nullptr) const;
128

129
130
    VcsBase::VcsCommand *vcsExecAbortable(const QString &workingDirectory, const QStringList &arguments);

jkobus's avatar
jkobus committed
131
    QString findRepositoryForDirectory(const QString &dir) const;
Orgad Shaneh's avatar
Orgad Shaneh committed
132
    QString findGitDirForRepository(const QString &repositoryDir) const;
133
    bool managesFile(const QString &workingDirectory, const QString &fileName) const;
hjk's avatar
hjk committed
134

135
136
137
138
139
140
    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;
141
    void diffRepository(const QString &workingDirectory);
142
    void diffBranch(const QString &workingDirectory,
jkobus's avatar
jkobus committed
143
                    const QString &branchName) const;
144
    void merge(const QString &workingDirectory, const QStringList &unmergedFileNames = QStringList());
hjk's avatar
hjk committed
145
146

    void status(const QString &workingDirectory);
147
    void log(const QString &workingDirectory, const QString &fileName = QString(),
148
             bool enableAnnotationContextMenu = false, const QStringList &args = QStringList());
Orgad Shaneh's avatar
Orgad Shaneh committed
149
    void reflog(const QString &workingDirectory);
150
151
152
    VcsBase::VcsBaseEditorWidget *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
153
    void reset(const QString &workingDirectory, const QString &argument, const QString &commit = QString());
hjk's avatar
hjk committed
154
    void addFile(const QString &workingDirectory, const QString &fileName);
155
156
    bool synchronousLog(const QString &workingDirectory, const QStringList &arguments,
                        QString *output, QString *errorMessage = nullptr,
157
                        unsigned flags = 0);
158
    bool synchronousAdd(const QString &workingDirectory, const QStringList &files);
159
160
161
    bool synchronousDelete(const QString &workingDirectory,
                           bool force,
                           const QStringList &files);
dt's avatar
dt committed
162
163
164
    bool synchronousMove(const QString &workingDirectory,
                         const QString &from,
                         const QString &to);
165
166
167
168
169
170
    bool synchronousReset(const QString &workingDirectory, const QStringList &files = QStringList(),
                          QString *errorMessage = nullptr);
    bool synchronousCleanList(const QString &workingDirectory, const QString &modulePath,
                              QStringList *files, QStringList *ignoredFiles, QString *errorMessage);
    bool synchronousApplyPatch(const QString &workingDirectory, const QString &file,
                               QString *errorMessage, const QStringList &extraArguments = QStringList());
171
    bool synchronousInit(const QString &workingDirectory);
172
173
    bool synchronousCheckoutFiles(const QString &workingDirectory, QStringList files = QStringList(),
                                  QString revision = QString(), QString *errorMessage = nullptr,
174
                                  bool revertStaging = true);
175
176
    // Checkout ref
    bool stashAndCheckout(const QString &workingDirectory, const QString &ref);
177
    bool synchronousCheckout(const QString &workingDirectory, const QString &ref,
178
                             QString *errorMessage = nullptr);
179
180

    QStringList setupCheckoutArguments(const QString &workingDirectory, const QString &ref);
181
    void updateSubmodulesIfNeeded(const QString &workingDirectory, bool prompt);
182
183
184
185
186

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

    bool executeSynchronousStash(const QString &workingDirectory,
                                 const QString &message = QString(),
Orgad Shaneh's avatar
Orgad Shaneh committed
191
                                 bool unstagedOnly = false,
192
                                 QString *errorMessage = nullptr) const;
193
194
    bool synchronousStashRestore(const QString &workingDirectory,
                                 const QString &stash,
195
                                 bool pop = false,
jkobus's avatar
jkobus committed
196
                                 const QString &branch = QString()) const;
197
198
    bool synchronousStashRemove(const QString &workingDirectory,
                                const QString &stash = QString(),
199
                                QString *errorMessage = nullptr) const;
200
    bool synchronousBranchCmd(const QString &workingDirectory, QStringList branchArgs,
jkobus's avatar
jkobus committed
201
                              QString *output, QString *errorMessage) const;
202
    bool synchronousTagCmd(const QString &workingDirectory, QStringList tagArgs,
jkobus's avatar
jkobus committed
203
                           QString *output, QString *errorMessage) const;
204
    bool synchronousForEachRefCmd(const QString &workingDirectory, QStringList args,
205
                               QString *output, QString *errorMessage = nullptr) const;
206
    VcsBase::VcsCommand *asyncForEachRefCmd(const QString &workingDirectory, QStringList args) const;
207
    bool synchronousRemoteCmd(const QString &workingDirectory, QStringList remoteArgs,
208
209
                              QString *output = nullptr, QString *errorMessage = nullptr,
                              bool silent = false) const;
210
211

    QMap<QString,QString> synchronousRemotesList(const QString &workingDirectory,
212
                                                 QString *errorMessage = nullptr) const;
213
    QStringList synchronousSubmoduleStatus(const QString &workingDirectory,
214
                                           QString *errorMessage = nullptr) const;
jkobus's avatar
jkobus committed
215
    SubmoduleDataMap submoduleList(const QString &workingDirectory) const;
216
    bool synchronousShow(const QString &workingDirectory, const QString &id,
217
                         QByteArray *output, QString *errorMessage) const;
218

219
220
    bool synchronousRevListCmd(const QString &workingDirectory, const QStringList &extraArguments,
                               QString *output, QString *errorMessage = nullptr) const;
221

222
223
224
    bool synchronousParentRevisions(const QString &workingDirectory,
                                    const QString &revision,
                                    QStringList *parents,
jkobus's avatar
jkobus committed
225
226
                                    QString *errorMessage) const;
    QString synchronousShortDescription(const QString &workingDirectory, const QString &revision) const;
227
    QString synchronousShortDescription(const QString &workingDirectory, const QString &revision,
jkobus's avatar
jkobus committed
228
                                     const QString &format) const;
229

jkobus's avatar
jkobus committed
230
    QString synchronousCurrentLocalBranch(const QString &workingDirectory) const;
231

232
    bool synchronousHeadRefs(const QString &workingDirectory, QStringList *output,
233
                             QString *errorMessage = nullptr) const;
jkobus's avatar
jkobus committed
234
    QString synchronousTopic(const QString &workingDirectory) const;
Petar Perisin's avatar
Petar Perisin committed
235
    bool synchronousRevParseCmd(const QString &workingDirectory, const QString &ref,
236
237
                                QString *output, QString *errorMessage = nullptr) const;
    QString synchronousTopRevision(const QString &workingDirectory, QString *errorMessage = nullptr);
238
    void synchronousTagsForCommit(const QString &workingDirectory, const QString &revision,
jkobus's avatar
jkobus committed
239
                                  QString &precedes, QString &follows) const;
Orgad Shaneh's avatar
Orgad Shaneh committed
240
    bool isRemoteCommit(const QString &workingDirectory, const QString &commit);
241
    bool isFastForwardMerge(const QString &workingDirectory, const QString &branch);
242

243
    void fetch(const QString &workingDirectory, const QString &remote);
244
    bool synchronousPull(const QString &workingDirectory, bool rebase);
245
    void push(const QString &workingDirectory, const QStringList &pushArgs = QStringList());
246
247
    bool synchronousMerge(const QString &workingDirectory, const QString &branch,
                          bool allowFastForward = true);
Orgad Shaneh's avatar
Orgad Shaneh committed
248
    bool canRebase(const QString &workingDirectory) const;
249
250
251
    void rebase(const QString &workingDirectory, const QString &argument);
    void cherryPick(const QString &workingDirectory, const QString &argument);
    void revert(const QString &workingDirectory, const QString &argument);
252

Petar Perisin's avatar
Petar Perisin committed
253
254
    bool synchronousRevert(const QString &workingDirectory, const QString &commit);
    bool synchronousCherryPick(const QString &workingDirectory, const QString &commit);
Orgad Shaneh's avatar
Orgad Shaneh committed
255
    void interactiveRebase(const QString &workingDirectory, const QString &commit, bool fixup);
256
    void synchronousAbortCommand(const QString &workingDir, const QString &abortCommand);
257
258
    QString synchronousTrackingBranch(const QString &workingDirectory,
                                      const QString &branch = QString());
Orgad Shaneh's avatar
Orgad Shaneh committed
259
260
261
    bool synchronousSetTrackingBranch(const QString &workingDirectory,
                                      const QString &branch,
                                      const QString &tracking);
hjk's avatar
hjk committed
262

263
    // git svn support (asynchronous).
264
    void synchronousSubversionFetch(const QString &workingDirectory);
265
266
    void subversionLog(const QString &workingDirectory);

267
    void stashPop(const QString &workingDirectory, const QString &stash = QString());
268
    void revert(const QStringList &files, bool revertStaging);
269
270
    bool synchronousStashList(const QString &workingDirectory, QList<Stash> *stashes,
                              QString *errorMessage = nullptr) const;
271
    // Resolve a stash name from message (for IVersionControl's names).
272
273
    bool stashNameFromMessage(const QString &workingDirectory, const QString &messge, QString *name,
                              QString *errorMessage = nullptr) const;
274

275
    QString readGitVar(const QString &workingDirectory, const QString &configVar) const;
276
    QString readConfigValue(const QString &workingDirectory, const QString &configVar) const;
hjk's avatar
hjk committed
277

Tobias Hunger's avatar
Tobias Hunger committed
278
    QTextCodec *encoding(const QString &workingDirectory, const QString &configVar) const;
279
    bool readDataFromCommit(const QString &repoDirectory, const QString &commit,
280
281
                            CommitData &commitData, QString *errorMessage = nullptr,
                            QString *commitTemplate = nullptr);
282
    bool getCommitData(const QString &workingDirectory, QString *commitTemplate,
283
                       CommitData &commitData, QString *errorMessage);
hjk's avatar
hjk committed
284
285
286

    bool addAndCommit(const QString &workingDirectory,
                      const GitSubmitEditorPanelData &data,
287
                      CommitType commitType,
288
                      const QString &amendSHA1,
hjk's avatar
hjk committed
289
                      const QString &messageFile,
hjk's avatar
hjk committed
290
                      VcsBase::SubmitFileModel *model);
con's avatar
con committed
291

292
    enum StatusResult { StatusChanged, StatusUnchanged, StatusFailed };
293
294
    StatusResult gitStatus(const QString &workingDirectory, StatusMode mode,
                           QString *output = nullptr, QString *errorMessage = nullptr) const;
295

jkobus's avatar
jkobus committed
296
297
    CommandInProgress checkCommandInProgress(const QString &workingDirectory) const;
    QString commandInProgressDescription(const QString &workingDirectory) const;
298

299
    void continueCommandIfNeeded(const QString &workingDirectory, bool allowContinue = true);
300

jkobus's avatar
jkobus committed
301
    QString extendedShowDescription(const QString &workingDirectory, const QString &text) const;
302

Petar Perisin's avatar
Petar Perisin committed
303
304
    void launchGitK(const QString &workingDirectory, const QString &fileName);
    void launchGitK(const QString &workingDirectory) { launchGitK(workingDirectory, QString()); }
305
    bool launchGitGui(const QString &workingDirectory);
306
    Utils::FileName gitBinDirectory();
Petar Perisin's avatar
Petar Perisin committed
307

308
309
    void launchRepositoryBrowser(const QString &workingDirectory);

310
311
    QStringList synchronousRepositoryBranches(const QString &repositoryURL,
                                              const QString &workingDirectory = QString()) const;
312

Orgad Shaneh's avatar
Orgad Shaneh committed
313
    QProcessEnvironment processEnvironment() const override;
314

Petar Perisin's avatar
Petar Perisin committed
315
316
    bool beginStashScope(const QString &workingDirectory, const QString &command,
                         StashFlag flag = Default, PushAction pushAction = NoPush);
Orgad Shaneh's avatar
Orgad Shaneh committed
317
318
    StashInfo &stashInfo(const QString &workingDirectory);
    void endStashScope(const QString &workingDirectory);
319
    bool isValidRevision(const QString &revision) const;
320
    void handleMergeConflicts(const QString &workingDir, const QString &commit, const QStringList &files, const QString &abortCommand);
321
    void addFuture(const QFuture<void> &future);
322

323
    static QString msgNoChangedFiles();
324
    static QString msgNoCommits(bool includeRemote);
325

Orgad Shaneh's avatar
Orgad Shaneh committed
326
public slots:
Tobias Hunger's avatar
Tobias Hunger committed
327
    void show(const QString &source, const QString &id, const QString &name = QString());
con's avatar
con committed
328

Orgad Shaneh's avatar
Orgad Shaneh committed
329
private:
330
    void finishSubmoduleUpdate();
Orgad Shaneh's avatar
Orgad Shaneh committed
331
    void slotChunkActionsRequested(QMenu *menu, bool isValid);
jkobus's avatar
jkobus committed
332
333
    void slotStageChunk();
    void slotUnstageChunk();
334
    void branchesForCommit(const QString &revision);
335

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

338
    enum CodecType { CodecSource, CodecLogOutput, CodecNone };
339
    QTextCodec *codecFor(CodecType codecType, const QString &source = QString()) const;
340

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

344
    // determine version as '(major << 16) + (minor << 8) + patch' or 0.
345
    unsigned synchronousGitVersion(QString *errorMessage = nullptr) const;
346

347
348
    QString readOneLine(const QString &workingDirectory, const QStringList &arguments) const;

349
    enum RevertResult { RevertOk, RevertUnchanged, RevertCanceled, RevertFailed };
350
351
352
353
    RevertResult revertI(QStringList files,
                         bool *isDirectory,
                         QString *errorMessage,
                         bool revertStaging);
hjk's avatar
hjk committed
354
    void connectRepositoryChanged(const QString & repository, VcsBase::VcsCommand *cmd);
355
    bool executeAndHandleConflicts(const QString &workingDirectory, const QStringList &arguments,
jkobus's avatar
jkobus committed
356
                                   const QString &abortCommand = QString()) const;
Robert Loehning's avatar
Robert Loehning committed
357
358
    bool tryLauchingGitK(const QProcessEnvironment &env,
                         const QString &workingDirectory,
Petar Perisin's avatar
Petar Perisin committed
359
                         const QString &fileName,
360
                         const QString &gitBinDirectory);
361
    bool cleanList(const QString &workingDirectory, const QString &modulePath, const QString &flag, QStringList *files, QString *errorMessage);
con's avatar
con committed
362

363
364
365
366
367
368
369
370
371
372
    enum ContinueCommandMode {
        ContinueOnly,
        SkipOnly,
        SkipIfNoChanges
    };

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

373
    mutable Utils::FileName m_gitVersionForBinary;
374
375
    mutable unsigned m_cachedGitVersion;

376
    QString m_gitQtcEditor;
Orgad Shaneh's avatar
Orgad Shaneh committed
377
    QMap<QString, StashInfo> m_stashInfo;
378
    QStringList m_updatedSubmodules;
379
    bool m_disableEditor;
380
    QPointer<DiffEditor::DiffEditorController> m_contextController;
381
    QFutureSynchronizer<void> m_synchronizer; // for commit updates
382
383
};

hjk's avatar
hjk committed
384
385
} // namespace Internal
} // namespace Git