File indexing completed on 2024-04-28 16:01:32

0001 /******************************************************************************
0002  * This file is part of the libqgit2 library
0003  * Copyright (c) 2011 Laszlo Papp <djszapi@archlinux.us>
0004  * Copyright (C) 2013 Leonardo Giordani
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Lesser General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library; if not, write to the Free Software
0018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0019  */
0020 
0021 #include "qgitobject.h"
0022 
0023 #include "qgitoid.h"
0024 #include "qgitrepository.h"
0025 #include "qgitcommit.h"
0026 #include "qgittag.h"
0027 #include "qgittree.h"
0028 #include "qgitblob.h"
0029 
0030 namespace LibQGit2
0031 {
0032 
0033 // git_object_free() is automatically invoked on the pointer when
0034 // it is no more referenced.
0035 Object::Object(git_object *object)
0036     : d(object, git_object_free)
0037 {
0038 }
0039 
0040 Object::Object(const Object& other)
0041     : d(other.d)
0042 {
0043 }
0044 
0045 Object::~Object()
0046 {
0047 }
0048 
0049 Object::Type Object::type() const
0050 {
0051     Type t = BadType;
0052     if (!isNull()) {
0053         t = resolveType(git_object_type(d.data()));
0054     }
0055 
0056     return t;
0057 }
0058 
0059 Object::Type Object::resolveType(git_otype rawType)
0060 {
0061     switch (rawType) {
0062     case GIT_OBJ_COMMIT:
0063         return CommitType;
0064     case GIT_OBJ_TAG:
0065         return TagType;
0066     case GIT_OBJ_TREE:
0067         return TreeType;
0068     case GIT_OBJ_BLOB:
0069         return BlobType;
0070     default:
0071         return BadType;
0072     }
0073 }
0074 
0075 Commit Object::toCommit() const
0076 {
0077     Commit commit;
0078     if (isCommit()) {
0079         commit.d = d;
0080     }
0081     return commit;
0082 }
0083 
0084 Tag Object::toTag() const
0085 {
0086     Tag tag;
0087     if (isTag()) {
0088         tag.d = d;
0089     }
0090     return tag;
0091 }
0092 
0093 Tree Object::toTree() const
0094 {
0095     Tree tree;
0096     if (isTree()) {
0097         tree.d = d;
0098     }
0099     return tree;
0100 }
0101 
0102 Blob Object::toBlob() const
0103 {
0104     Blob blob;
0105     if (isBlob()) {
0106         blob.d = d;
0107     }
0108     return blob;
0109 }
0110 
0111 bool Object::isNull() const
0112 {
0113     return d.isNull();
0114 }
0115 
0116 OId Object::oid() const
0117 {
0118     return OId(git_object_id(d.data()));
0119 }
0120 
0121 bool Object::isCommit() const
0122 {
0123     return type() == CommitType;
0124 }
0125 
0126 bool Object::isTag() const
0127 {
0128     return type() == TagType;
0129 }
0130 
0131 bool Object::isTree() const
0132 {
0133     return type() == TreeType;
0134 }
0135 
0136 bool Object::isBlob() const
0137 {
0138     return type() == BlobType;
0139 }
0140 
0141 QString Object::typeString() const
0142 {
0143     return QString(git_object_type2string(git_object_type(d.data())));
0144 }
0145 
0146 Repository Object::owner() const
0147 {
0148     return Repository(git_object_owner(d.data()));
0149 }
0150 
0151 git_object* Object::data() const
0152 {
0153     return d.data();
0154 }
0155 
0156 const git_object* Object::constData() const
0157 {
0158     return d.data();
0159 }
0160 
0161 bool operator ==(const Object &o1, const Object &o2)
0162 {
0163     return (o1.oid() == o2.oid());
0164 }
0165 
0166 bool operator !=(const Object &o1, const Object &o2)
0167 {
0168     return !(operator ==(o1, o2));
0169 }
0170 
0171 } // namespace LibQGit2