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 "qgitoid.h"
0022 #include "qgitexception.h"
0023 
0024 namespace LibQGit2
0025 {
0026 
0027 OId::OId(const git_oid* oid)
0028     : d(GIT_OID_RAWSZ, 0)
0029 {
0030     if (oid != 0) {
0031         git_oid_cpy(data(), oid);
0032     }
0033 }
0034 
0035 OId::OId(const OId& other)
0036     : d(other.d)
0037 {
0038 }
0039 
0040 OId::~OId()
0041 {
0042 }
0043 
0044 bool OId::isValid() const
0045 {
0046     return ( !d.isEmpty() &&
0047              (d.length() <= GIT_OID_RAWSZ) &&
0048              (d != QByteArray(GIT_OID_RAWSZ, 0))
0049              );
0050 }
0051 
0052 void OId::fromHex(const QByteArray& hex)
0053 {
0054     int len = qMin(hex.length(), GIT_OID_HEXSZ);
0055     qGitThrow(git_oid_fromstrn(data(), hex.constData(), len));
0056     d.resize(len / 2);
0057 }
0058 
0059 void OId::fromString(const QString& string)
0060 {
0061     fromHex(string.toUtf8());
0062 }
0063 
0064 
0065 void OId::fromRawData(const QByteArray& raw)
0066 {
0067     qGitThrow(raw.length() < GIT_OID_HEXSZ);
0068     d = raw;
0069 }
0070 
0071 OId OId::stringToOid(const QByteArray& string)
0072 {
0073     int len = qMin(string.length(), GIT_OID_HEXSZ);
0074     OId oid;
0075     qGitThrow(git_oid_fromstrn(oid.data(), string.constData(), len));
0076     oid.d.resize(len / 2);
0077     return oid;
0078 }
0079 
0080 OId OId::rawDataToOid(const QByteArray& raw)
0081 {
0082     OId oid;
0083     oid.d = raw;
0084     return oid;
0085 }
0086 
0087 QByteArray OId::format() const
0088 {
0089     QByteArray ba(GIT_OID_HEXSZ, Qt::Uninitialized);
0090     git_oid_fmt(ba.data(), constData());
0091     return ba;
0092 }
0093 
0094 QByteArray OId::pathFormat() const
0095 {
0096     QByteArray ba(GIT_OID_HEXSZ+1, Qt::Uninitialized);
0097     git_oid_pathfmt(ba.data(), constData());
0098     return ba;
0099 }
0100 
0101 git_oid* OId::data()
0102 {
0103     return reinterpret_cast<git_oid*>(d.data());
0104 }
0105 
0106 const git_oid* OId::constData() const
0107 {
0108     return reinterpret_cast<const git_oid*>(d.constData());
0109 }
0110 
0111 bool operator ==(const OId &oid1, const OId &oid2)
0112 {
0113     return git_oid_cmp(oid1.constData(), oid2.constData()) == 0;
0114 }
0115 
0116 bool operator !=(const OId &oid1, const OId &oid2)
0117 {
0118     return !(operator ==(oid1, oid2));
0119 }
0120 
0121 int OId::length() const
0122 {
0123     return d.length() * 2;
0124 }
0125 
0126 } // LibQGit2