[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [libqgit2] /: Add checkoutRemote
From: Peter_Kümmel <syntheticpp () gmx ! net>
Date: 2014-03-02 19:32:27
Message-ID: E1WKC7f-00048G-HE () scm ! kde ! org
[Download RAW message or body]
Git commit b47294e9000842d3022344f56bd1c738ddc121c0 by Peter Kümmel.
Committed on 02/03/2014 at 19:28.
Pushed by kuemmel into branch 'master'.
Add checkoutRemote
M +28 -0 src/qgitrepository.cpp
M +13 -1 src/qgitrepository.h
M +1 -1 tests/CMakeLists.txt
A +103 -0 tests/Checkout.cpp [License: UNKNOWN] *
The files marked with a * at the end have a non valid license. Please read: \
http://techbase.kde.org/Policies/Licensing_Policy and use the headers which are \
listed at that page.
http://commits.kde.org/libqgit2/b47294e9000842d3022344f56bd1c738ddc121c0
diff --git a/src/qgitrepository.cpp b/src/qgitrepository.cpp
index d006986..7b65c55 100644
--- a/src/qgitrepository.cpp
+++ b/src/qgitrepository.cpp
@@ -43,6 +43,12 @@ namespace {
RemoteRAII(git_remote* r) : remote(r) {}
~RemoteRAII() { if (remote) git_remote_free(remote); }
};
+
+ struct ObjectRAII {
+ git_object*const ptr;
+ ObjectRAII(git_object* p) : ptr(p) {}
+ ~ObjectRAII() { if (ptr) git_object_free(ptr); }
+ };
}
namespace LibQGit2
@@ -457,4 +463,26 @@ QStringList Repository::remoteBranches(const QString& \
remoteName) return heads;
}
+
+void Repository::checkoutRemote(const QString& branch, bool force, const QString& \
remote) +{
+ if (d.isNull()){
+ throw Exception("Repository::fetch(): no repository available");
+ }
+
+ const QString refspec = "refs/remotes/" + remote + "/" + branch;
+ git_object* tree = NULL;
+ qGitThrow(git_revparse_single(&tree, data(), refspec.toLatin1()));
+ ObjectRAII rai(tree); (void)rai;
+
+ git_checkout_opts opts;
+ memset(&opts, 0, sizeof(git_checkout_opts));
+ opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = force ? GIT_CHECKOUT_FORCE : GIT_CHECKOUT_SAFE;
+ qGitThrow(git_checkout_tree(data(), tree, &opts));
+
+ qGitThrow(git_repository_set_head(data(), refspec.toLatin1()));
+}
+
+
} // namespace LibQGit2
diff --git a/src/qgitrepository.h b/src/qgitrepository.h
index 0de464d..f59ce42 100644
--- a/src/qgitrepository.h
+++ b/src/qgitrepository.h
@@ -437,7 +437,7 @@ namespace LibQGit2
void remoteAdd(const QString& name, const QString& url);
/**
- * Ftech from known remote repository.
+ * Fetch from known remote repository.
*
* @param remote name of the remote repository (e.g. "origin")
* @param head name of head to fetch (e.g. "master"), default: "*" (all \
branches) @@ -447,6 +447,18 @@ namespace LibQGit2
QStringList remoteBranches(const QString& remoteName);
+
+ /**
+ * Checkout a remote branch without creating a local branch.
+ *
+ * @param branch branch name
+ * @param force use forced checkout, default is false
+ * @param remote remote which should be used, default is 'origin'
+ * @throws LibQGit2::Exception
+ */
+ void checkoutRemote(const QString& branch, bool force = false, const \
QString& remote = "origin"); +
+
git_repository* data() const;
const git_repository* constData() const;
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 6bc7dc6..b981874 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -47,5 +47,5 @@ addTest(Revision)
addTest(StatusOptions)
addTest(Clone)
addTest(Fetch)
-
+addTest(Checkout)
diff --git a/tests/Checkout.cpp b/tests/Checkout.cpp
new file mode 100644
index 0000000..5f0b9d7
--- /dev/null
+++ b/tests/Checkout.cpp
@@ -0,0 +1,103 @@
+/******************************************************************************
+* Copyright (C) 2014 Peter K�mmel <syntheticpp@gmx.net>
+*
+* Permission to use, copy, modify, and distribute the software
+* and its documentation for any purpose and without fee is hereby
+* granted, provided that the above copyright notice appear in all
+* copies and that both that the copyright notice and this
+* permission notice and warranty disclaimer appear in supporting
+* documentation, and that the name of the author not be used in
+* advertising or publicity pertaining to distribution of the
+* software without specific, written prior permission.
+*
+* The author disclaim all warranties with regard to this
+* software, including all implied warranties of merchantability
+* and fitness. In no event shall the author be liable for any
+* special, indirect or consequential damages or any damages
+* whatsoever resulting from loss of use, data or profits, whether
+* in an action of contract, negligence or other tortious action,
+* arising out of or in connection with the use or performance of
+* this software.
+*/
+#include "TestHelpers.h"
+
+#include "qgitrepository.h"
+
+
+using namespace LibQGit2;
+
+
+class TestCheckout : public QObject
+{
+ Q_OBJECT
+
+public:
+ TestCheckout() : testdir(VALUE_TO_STR(TEST_DIR)) {}
+
+public slots:
+
+private slots:
+ void checkoutRemote();
+ void checkoutRemoteKde();
+
+private:
+ const QString testdir;
+
+ void fetch(const QString& branch, const QString& repoPath, const QString& \
remote); +};
+
+
+void TestCheckout::fetch(const QString& branch, const QString& repoPath, const \
QString& remote = "origin") +{
+ QVERIFY(removeDir(repoPath));
+ sleep::ms(500);
+
+ try {
+ LibQGit2::Repository repo;
+ repo.init(repoPath);
+ repo.remoteAdd(remote, "http://anongit.kde.org/libqgit2");
+ repo.fetch(remote, branch);
+ }
+ catch (const LibQGit2::Exception& ex) {
+ QFAIL(ex.what());
+ }
+}
+
+
+void TestCheckout::checkoutRemote()
+{
+ const QString repoPath = testdir + "/checkout_test/checkout_remote";
+
+ fetch("master", repoPath);
+
+ try {
+ LibQGit2::Repository repo;
+ repo.open(repoPath);
+ repo.checkoutRemote("master");
+ }
+ catch (const LibQGit2::Exception& ex) {
+ QFAIL(ex.what());
+ }
+}
+
+
+void TestCheckout::checkoutRemoteKde()
+{
+ const QString repoPath = testdir + "/checkout_test/checkout_remote_kde";
+
+ fetch("master", repoPath, "kde");
+
+ try {
+ LibQGit2::Repository repo;
+ repo.open(repoPath);
+ repo.checkoutRemote("master", false, "kde");
+ }
+ catch (const LibQGit2::Exception& ex) {
+ QFAIL(ex.what());
+ }
+}
+
+QTEST_MAIN(TestCheckout);
+
+#include "Checkout.moc"
+
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic