File indexing completed on 2024-04-21 15:32:17

0001 /******************************************************************************
0002 * Copyright (C) 2014 Peter Kümmel <syntheticpp@gmx.net>
0003 *
0004 * Permission to use, copy, modify, and distribute the software
0005 * and its documentation for any purpose and without fee is hereby
0006 * granted, provided that the above copyright notice appear in all
0007 * copies and that both that the copyright notice and this
0008 * permission notice and warranty disclaimer appear in supporting
0009 * documentation, and that the name of the author not be used in
0010 * advertising or publicity pertaining to distribution of the
0011 * software without specific, written prior permission.
0012 *
0013 * The author disclaim all warranties with regard to this
0014 * software, including all implied warranties of merchantability
0015 * and fitness.  In no event shall the author be liable for any
0016 * special, indirect or consequential damages or any damages
0017 * whatsoever resulting from loss of use, data or profits, whether
0018 * in an action of contract, negligence or other tortious action,
0019 * arising out of or in connection with the use or performance of
0020 * this software.
0021 */
0022 #include "TestHelpers.h"
0023 
0024 #include "qgitrepository.h"
0025 #include "qgitcommit.h"
0026 #include "qgitdiffdelta.h"
0027 #include "qgitdifffile.h"
0028 #include <QFile>
0029 
0030 using namespace LibQGit2;
0031 
0032 
0033 class TestCheckout : public TestBase
0034 {
0035     Q_OBJECT
0036 
0037 private slots:
0038     void checkoutRemote();
0039     void checkoutRemoteKde();
0040     void checkoutCommitAsTree();
0041     void checkoutHead();
0042     void checkoutPaths();
0043 
0044 private:
0045     void fetch(const QString& branch, const QString& repoPath, const QString& remote);
0046 };
0047 
0048 
0049 void TestCheckout::fetch(const QString& branch, const QString& repoPath, const QString& remote = "origin")
0050 {
0051     try {
0052         Repository repo;
0053         repo.init(repoPath);
0054         repo.remoteAdd(remote, HttpRemoteUrl);
0055         repo.fetch(remote, branch);
0056     }
0057     catch (const Exception& ex) {
0058         QFAIL(ex.what());
0059     }
0060 }
0061 
0062 
0063 void TestCheckout::checkoutRemote()
0064 {
0065     fetch("master", testdir);
0066 
0067     try {
0068         Repository repo;
0069         repo.open(testdir);
0070         repo.checkoutRemote("master");
0071     }
0072     catch (const Exception& ex) {
0073         QFAIL(ex.what());
0074     }
0075 }
0076 
0077 
0078 void TestCheckout::checkoutRemoteKde()
0079 {
0080     fetch("master", testdir, "kde");
0081 
0082     try {
0083         Repository repo;
0084         repo.open(testdir);
0085         repo.checkoutRemote("master", CheckoutOptions(), "kde");
0086     }
0087     catch (const Exception& ex) {
0088         QFAIL(ex.what());
0089     }
0090 }
0091 
0092 
0093 void TestCheckout::checkoutCommitAsTree()
0094 {
0095     Repository repo;
0096     try {
0097         repo.clone(FileRepositoryUrl, testdir);
0098         OId id = OId::stringToOid("127c9e7d17");  // 127c9e7d17 is a commit where CMakeLists.txt was modified
0099         repo.checkoutTree(repo.lookupCommit(id), CheckoutOptions(CheckoutOptions::Safe, CheckoutOptions::RecreateMissing));
0100     } catch (const Exception& ex) {
0101         QFAIL(ex.what());
0102     }
0103 
0104     StatusList status = repo.status(StatusOptions(StatusOptions::ShowOnlyIndex, StatusOptions::ExcludeSubmodules));
0105     bool found = false;
0106     for (size_t i = 0; i < status.entryCount(); ++i) {
0107         const StatusEntry entry = status.entryByIndex(i);
0108         if ((found = (entry.headToIndex().newFile().path() == "CMakeLists.txt"))) {
0109             break;
0110         }
0111     }
0112     QVERIFY2(found, "Expected path was not part of the checked out commit");
0113 }
0114 
0115 void TestCheckout::checkoutHead()
0116 {
0117     const QString fileName(testdir + "/CMakeLists.txt");
0118 
0119     Repository repo;
0120     try {
0121         repo.clone(FileRepositoryUrl, testdir);
0122         QVERIFY(QFile::remove(fileName));
0123         repo.checkoutHead(CheckoutOptions(CheckoutOptions::Force));
0124     } catch (const Exception& ex) {
0125         QFAIL(ex.what());
0126     }
0127 
0128     QVERIFY(QFile::exists(fileName));
0129 }
0130 
0131 void TestCheckout::checkoutPaths()
0132 {
0133     const QStringList paths("CMakeLists.txt");
0134     Repository repo;
0135     try {
0136         repo.clone(FileRepositoryUrl, testdir);
0137         OId id = OId::stringToOid("7752cf5867");  // 7752cf5867 is a commit where many files were modified
0138         CheckoutOptions opts(CheckoutOptions::Safe, CheckoutOptions::RecreateMissing);
0139         opts.setPaths(paths);
0140         repo.checkoutTree(repo.lookupCommit(id), opts);
0141     } catch (const Exception& ex) {
0142         QFAIL(ex.what());
0143     }
0144 
0145     StatusList status = repo.status(StatusOptions(StatusOptions::ShowOnlyIndex, StatusOptions::ExcludeSubmodules));
0146     QStringList checkedoutPaths;
0147     for (size_t i = 0; i < status.entryCount(); ++i) {
0148         const StatusEntry entry = status.entryByIndex(i);
0149         checkedoutPaths << entry.headToIndex().newFile().path();
0150     }
0151 
0152     QCOMPARE(checkedoutPaths, paths);
0153 }
0154 
0155 
0156 QTEST_MAIN(TestCheckout)
0157 
0158 #include "Checkout.moc"
0159