gitclient.h 16.5 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
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 12 13 14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22
** 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.
23
**
hjk's avatar
hjk committed
24 25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

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

33 34
#include "gitsettings.h"

con's avatar
con committed
35
#include <coreplugin/editormanager/ieditor.h>
Tobias Hunger's avatar
Tobias Hunger committed
36
#include <vcsbase/command.h>
con's avatar
con committed
37

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;
con's avatar
con committed
48 49 50 51 52 53
QT_END_NAMESPACE

namespace Core {
    class ICore;
}

hjk's avatar
hjk committed
54 55
namespace VcsBase {
    class VcsBaseEditorWidget;
56
    class SubmitFileModel;
con's avatar
con committed
57 58
}

59 60 61 62
namespace Utils {
    struct SynchronousProcessResponse;
}

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

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

72 73 74 75 76 77 78
enum StatusMode
{
    ShowAll = 0,
    NoUntracked = 1,
    NoSubmodules = 2
};

79
class GitClient : public QObject
con's avatar
con committed
80 81
{
    Q_OBJECT
hjk's avatar
hjk committed
82

con's avatar
con committed
83
public:
Orgad Shaneh's avatar
Orgad Shaneh committed
84 85 86 87 88 89
    enum StashResult { StashUnchanged, StashCanceled, StashFailed,
                       Stashed, NotStashed /* User did not want it */ };

    class StashGuard
    {
    public:
90
        StashGuard(const QString &workingDirectory, const QString &keyword, bool askUser = true);
Orgad Shaneh's avatar
Orgad Shaneh committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104
        ~StashGuard();

        void preventPop();
        bool stashingFailed(bool includeNotStashed) const;
        StashResult result() const { return stashResult; }

    private:
        bool pop;
        StashResult stashResult;
        QString message;
        QString workingDir;
        GitClient *client;
    };

105 106
    static const char *stashNamePrefix;

107
    explicit GitClient(GitSettings *settings);
hjk's avatar
hjk committed
108 109
    ~GitClient();

110
    QString gitBinaryPath(bool *ok = 0, QString *errorMessage = 0) const;
Orgad Shaneh's avatar
Orgad Shaneh committed
111
    unsigned gitVersion(QString *errorMessage = 0) const;
112

Orgad Shaneh's avatar
Orgad Shaneh committed
113
    QString findRepositoryForDirectory(const QString &dir);
Orgad Shaneh's avatar
Orgad Shaneh committed
114
    QString findGitDirForRepository(const QString &repositoryDir) const;
hjk's avatar
hjk committed
115

116
    void diff(const QString &workingDirectory, const QStringList &diffArgs, const QString &fileName);
117 118
    void diff(const QString &workingDirectory, const QStringList &diffArgs,
              const QStringList &unstagedFileNames, const QStringList &stagedFileNames= QStringList());
119 120 121
    void diffBranch(const QString &workingDirectory,
                    const QStringList &diffArgs,
                    const QString &branchName);
122
    void merge(const QString &workingDirectory, const QStringList &unmergedFileNames = QStringList());
hjk's avatar
hjk committed
123 124

    void status(const QString &workingDirectory);
125 126
    void graphLog(const QString &workingDirectory) { graphLog(workingDirectory, QString()); }
    void graphLog(const QString &workingDirectory, const QString &branch);
127
    void log(const QString &workingDirectory, const QStringList &fileNames = QStringList(),
128
             bool enableAnnotationContextMenu = false, const QStringList &args = QStringList());
129
    void blame(const QString &workingDirectory, const QStringList &args, const QString &fileName,
130
               const QString &revision = QString(), int lineNumber = -1);
131
    void hardReset(const QString &workingDirectory, const QString &commit = QString());
Petar Perisin's avatar
Petar Perisin committed
132
    void softReset(const QString &workingDirectory, const QString &commit);
hjk's avatar
hjk committed
133
    void addFile(const QString &workingDirectory, const QString &fileName);
134 135 136
    bool synchronousLog(const QString &workingDirectory,
                        const QStringList &arguments,
                        QString *output, QString *errorMessage = 0);
137 138 139 140 141 142 143
    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
144 145 146
    bool synchronousMove(const QString &workingDirectory,
                         const QString &from,
                         const QString &to);
147 148 149
    bool synchronousReset(const QString &workingDirectory,
                          const QStringList &files = QStringList(),
                          QString *errorMessage = 0);
Orgad Shaneh's avatar
Orgad Shaneh committed
150
    bool synchronousCleanList(const QString &workingDirectory, QStringList *files, QStringList *ignoredFiles, QString *errorMessage);
151
    bool synchronousApplyPatch(const QString &workingDirectory, const QString &file, QString *errorMessage);
152
    bool synchronousInit(const QString &workingDirectory);
153 154
    bool synchronousCheckoutFiles(const QString &workingDirectory,
                                  QStringList files = QStringList(),
155 156
                                  QString revision = QString(), QString *errorMessage = 0,
                                  bool revertStaging = true);
157
    // Checkout branch
Petar Perisin's avatar
Petar Perisin committed
158 159 160
    bool synchronousCheckout(const QString &workingDirectory, const QString &ref, QString *errorMessage);
    bool synchronousCheckout(const QString &workingDirectory, const QString &ref)
         { return synchronousCheckout(workingDirectory, ref, 0); }
161 162 163 164 165 166 167 168 169 170 171 172

    // 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,
173
                                 bool pop = false,
174 175 176 177 178
                                 const QString &branch = QString(),
                                 QString *errorMessage = 0);
    bool synchronousStashRemove(const QString &workingDirectory,
                                const QString &stash = QString(),
                                QString *errorMessage = 0);
179 180
    bool synchronousBranchCmd(const QString &workingDirectory, QStringList branchArgs,
                              QString *output, QString *errorMessage);
181 182
    bool synchronousRemoteCmd(const QString &workingDirectory, QStringList remoteArgs,
                              QString *output, QString *errorMessage);
183 184
    bool synchronousShow(const QString &workingDirectory, const QString &id,
                              QString *output, QString *errorMessage);
185 186 187 188 189
    bool synchronousParentRevisions(const QString &workingDirectory,
                                    const QStringList &files /* = QStringList() */,
                                    const QString &revision,
                                    QStringList *parents,
                                    QString *errorMessage);
190 191 192
    QString synchronousShortDescription(const QString &workingDirectory, const QString &revision);
    QString synchronousShortDescription(const QString &workingDirectory, const QString &revision,
                                     const QString &format);
193 194
    bool synchronousHeadRefs(const QString &workingDirectory, QStringList *output,
                             QString *errorMessage = 0);
195
    QString synchronousTopic(const QString &workingDirectory);
196
    QString synchronousTopRevision(const QString &workingDirectory, QString *errorMessage = 0);
197 198
    void synchronousTagsForCommit(const QString &workingDirectory, const QString &revision,
                                  QByteArray &precedes, QByteArray &follows);
199

Tobias Hunger's avatar
Tobias Hunger committed
200
    bool cloneRepository(const QString &directory, const QByteArray &url);
201
    QString vcsGetRepositoryURL(const QString &directory);
Tobias Hunger's avatar
Tobias Hunger committed
202
    bool synchronousFetch(const QString &workingDirectory, const QString &remote);
203
    bool synchronousPull(const QString &workingDirectory, bool rebase);
204
    bool synchronousPush(const QString &workingDirectory, const QString &remote = QString());
Petar Perisin's avatar
Petar Perisin committed
205
    bool synchronousMerge(const QString &workingDirectory, const QString &branch);
Orgad Shaneh's avatar
Orgad Shaneh committed
206
    bool canRebase(const QString &workingDirectory) const;
Petar Perisin's avatar
Petar Perisin committed
207 208 209
    bool synchronousRebase(const QString &workingDirectory,
                           const QString &baseBranch,
                           const QString &topicBranch = QString());
210 211
    bool revertCommit(const QString &workingDirectory, const QString &commit);
    bool cherryPickCommit(const QString &workingDirectory, const QString &commit);
Orgad Shaneh's avatar
Orgad Shaneh committed
212
    void interactiveRebase(const QString &workingDirectory, const QString &commit);
213
    void synchronousAbortCommand(const QString &workingDir, const QString &abortCommand);
hjk's avatar
hjk committed
214

215
    // git svn support (asynchronous).
216
    void synchronousSubversionFetch(const QString &workingDirectory);
217 218
    void subversionLog(const QString &workingDirectory);

219
    void stashPop(const QString &workingDirectory, const QString &stash);
220
    void stashPop(const QString &workingDirectory);
221
    void revert(const QStringList &files, bool revertStaging);
222 223
    void branchList(const QString &workingDirectory);
    void stashList(const QString &workingDirectory);
224 225 226 227 228 229 230
    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);
231

232
    QString readConfig(const QString &workingDirectory, const QStringList &configVar) const;
hjk's avatar
hjk committed
233

234
    QString readConfigValue(const QString &workingDirectory, const QString &configVar) const;
hjk's avatar
hjk committed
235

Tobias Hunger's avatar
Tobias Hunger committed
236 237
    StashResult ensureStash(const QString &workingDirectory, const QString &keyword, bool askUser,
                            QString *message, QString *errorMessage = 0);
238

239 240
    bool getCommitData(const QString &workingDirectory, bool amend,
                       QString *commitTemplate, CommitData *commitData,
hjk's avatar
hjk committed
241 242 243 244
                       QString *errorMessage);

    bool addAndCommit(const QString &workingDirectory,
                      const GitSubmitEditorPanelData &data,
245
                      const QString &amendSHA1,
hjk's avatar
hjk committed
246
                      const QString &messageFile,
hjk's avatar
hjk committed
247
                      VcsBase::SubmitFileModel *model);
con's avatar
con committed
248

249 250
    enum StatusResult { StatusChanged, StatusUnchanged, StatusFailed };
    StatusResult gitStatus(const QString &workingDirectory,
251
                           StatusMode mode,
252
                           QString *output = 0,
253
                           QString *errorMessage = 0);
254

255 256
    void continueCommandIfNeeded(const QString &workingDirectory);
    void continuePreviousGitCommand(const QString &workingDirectory, const QString &msgBoxTitle, QString msgBoxText,
Orgad Shaneh's avatar
Orgad Shaneh committed
257
                                    const QString &buttonName, const QString &gitCommand, bool requireChanges = true);
258

Petar Perisin's avatar
Petar Perisin committed
259 260 261
    void launchGitK(const QString &workingDirectory, const QString &fileName);
    void launchGitK(const QString &workingDirectory) { launchGitK(workingDirectory, QString()); }

262 263
    void launchRepositoryBrowser(const QString &workingDirectory);

264
    QStringList synchronousRepositoryBranches(const QString &repositoryURL, bool *isDetached = 0);
265

266
    GitSettings *settings() const;
267

268
    QProcessEnvironment processEnvironment() const;
269

270
    bool isValidRevision(const QString &revision) const;
271
    void handleMergeConflicts(const QString &workingDir, const QString &commit, const QString &abortCommand);
272

273 274
    static QString msgNoChangedFiles();

275
    static const char *noColorOption;
276
    static const char *decorateOption;
277

con's avatar
con committed
278
public slots:
279
    void show(const QString &source, const QString &id, const QStringList &args = QStringList());
Tobias Hunger's avatar
Tobias Hunger committed
280
    void saveSettings();
con's avatar
con committed
281

282 283
private slots:
    void slotBlameRevisionRequested(const QString &source, QString change, int lineNumber);
284 285
    void appendOutputData(const QByteArray &data) const;
    void appendOutputDataSilently(const QByteArray &data) const;
286

con's avatar
con committed
287
private:
288
    QTextCodec *getSourceCodec(const QString &file) const;
hjk's avatar
hjk committed
289
    VcsBase::VcsBaseEditorWidget *findExistingVCSEditor(const char *registerDynamicProperty,
290
                                                  const QString &dynamicPropertyValue) const;
291
    enum CodecType { CodecSource, CodecLogOutput, CodecNone };
hjk's avatar
hjk committed
292
    VcsBase::VcsBaseEditorWidget *createVcsEditor(const Core::Id &kind,
293 294
                                            QString title,
                                            const QString &source,
295
                                            CodecType codecType,
296 297 298
                                            const char *registerDynamicProperty,
                                            const QString &dynamicPropertyValue,
                                            QWidget *configWidget) const;
con's avatar
con committed
299

hjk's avatar
hjk committed
300 301
    VcsBase::Command *createCommand(const QString &workingDirectory,
                             VcsBase::VcsBaseEditorWidget* editor = 0,
302
                             bool useOutputToWindow = false,
303
                             int editorLineNumber = -1);
con's avatar
con committed
304

hjk's avatar
hjk committed
305
    VcsBase::Command *executeGit(const QString &workingDirectory,
306
                           const QStringList &arguments,
hjk's avatar
hjk committed
307
                           VcsBase::VcsBaseEditorWidget* editor = 0,
308
                           bool useOutputToWindow = false,
hjk's avatar
hjk committed
309
                           VcsBase::Command::TerminationReportMode tm = VcsBase::Command::NoReport,
310 311
                           int editorLineNumber = -1,
                           bool unixTerminalDisabled = false);
con's avatar
con committed
312

313 314
    // Fully synchronous git execution (QProcess-based).
    bool fullySynchronousGit(const QString &workingDirectory,
315
                        const QStringList &arguments,
316
                        QByteArray* outputText,
317
                        QByteArray* errorText = 0,
318
                        bool logCommandToWindow = true) const;
319 320

    // Synchronous git execution using Utils::SynchronousProcess, with
hjk's avatar
hjk committed
321
    // log windows updating (using VcsBasePlugin::runVcs with flags).
322 323 324 325
    inline Utils::SynchronousProcessResponse
            synchronousGit(const QString &workingDirectory, const QStringList &arguments,
                           unsigned flags = 0, QTextCodec *outputCodec = 0);

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

    enum RevertResult { RevertOk, RevertUnchanged, RevertCanceled, RevertFailed };
330 331 332 333
    RevertResult revertI(QStringList files,
                         bool *isDirectory,
                         QString *errorMessage,
                         bool revertStaging);
hjk's avatar
hjk committed
334
    void connectRepositoryChanged(const QString & repository, VcsBase::Command *cmd);
335 336
    bool executeAndHandleConflicts(const QString &workingDirectory, const QStringList &arguments,
                                   const QString &abortCommand = QString());
Robert Loehning's avatar
Robert Loehning committed
337 338
    bool tryLauchingGitK(const QProcessEnvironment &env,
                         const QString &workingDirectory,
Petar Perisin's avatar
Petar Perisin committed
339
                         const QString &fileName,
Robert Loehning's avatar
Robert Loehning committed
340 341
                         const QString &gitBinDirectory,
                         bool silent);
Orgad Shaneh's avatar
Orgad Shaneh committed
342
    bool cleanList(const QString &workingDirectory, const QString &flag, QStringList *files, QString *errorMessage);
con's avatar
con committed
343

344 345 346
    mutable QString m_gitVersionForBinary;
    mutable unsigned m_cachedGitVersion;

hjk's avatar
hjk committed
347
    const QString m_msgWait;
348
    QSignalMapper *m_repositoryChangedSignalMapper;
349
    GitSettings *m_settings;
350
    QString m_gitQtcEditor;
351 352
};

hjk's avatar
hjk committed
353 354
} // namespace Internal
} // namespace Git
con's avatar
con committed
355 356

#endif // GITCLIENT_H