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 #ifndef LIBQGIT2_OID_H
0022 #define LIBQGIT2_OID_H
0023 
0024 #include <QtCore/QString>
0025 #include <QtCore/QDateTime>
0026 
0027 #include "git2.h"
0028 
0029 #include "libqgit2_export.h"
0030 
0031 namespace LibQGit2
0032 {
0033     /**
0034      * @brief Wrapper class for git_oid.
0035      *
0036      * This class holds a Git SHA1 object id, i.e. 40 hexadecimal digits.
0037      * Instead of using the git_oid structure, which is an unsigned char array,
0038      * the object redefines it as a QByteArray and provides the conversion
0039      * to git_oid through data() and constData().
0040      *
0041      * Remember that the QByteArray stores 2 hexadecimal digits for each
0042      * element, so the length of the array is half that of the SHA1, namely 20 bytes.
0043      * This can either encompass a full oid (40 hexadecimal digits) or a part of it,
0044      * (short reference).
0045      *
0046      * @ingroup LibQGit2
0047      * @{
0048      */
0049     class LIBQGIT2_EXPORT OId
0050     {
0051         public:
0052 
0053             /**
0054              * Constructor
0055              */
0056             explicit OId(const git_oid *oid = 0);
0057 
0058             /**
0059              * Copy constructor
0060              */
0061             OId(const OId& other);
0062 
0063             /**
0064              * Destructor
0065              */
0066             ~OId();
0067 
0068             /**
0069              * Set the value of the object parsing a hex array.
0070              *
0071              * This method parses an array of hexadecimal values trying to
0072              * convert it into an OId.
0073              * If the array contains more than 40 values, only the first 40
0074              * will be converted.
0075              * If the array contains less than 40 values the resulting OId
0076              * will be a shortened OId, or a prefix.
0077              *
0078              * @param hex
0079              * Input hex string; must be at least 4 bytes.
0080              *
0081              * @throws Exception
0082              */
0083             void fromHex(const QByteArray& hex);
0084 
0085             /**
0086              * Set the value of the object parsing a string.
0087              *
0088              * This method behaves just like fromHex() but parses a string
0089              * that contains hexadecimal values. The same rules of fromHex()
0090              * apply here.
0091              *
0092              * @param string
0093              * Input string; must be at least 4 characters long.
0094              *
0095              * @throws Exception
0096              */
0097             void fromString(const QString& string);
0098 
0099             /**
0100              * Set the value of the object from a raw oid.
0101              *
0102              * This method uses the input raw hexadecimal array without parsing
0103              * it and without performing prefix lookup. The raw array must be
0104              * 40 characters long, otherwise throws Exception.
0105              *
0106              * @param raw the raw input bytes to be copied.
0107              * @throws Exception
0108              */
0109             void fromRawData(const QByteArray& raw);
0110 
0111             /**
0112              * Parse a hex formatted object id into a OId.
0113              *
0114              * @param string
0115              * Input hex string; if less than 40 bytes, prefix lookup will be performed. Must be
0116              * at least 4 bytes.
0117              *
0118              * @return OId; null OId on failure.
0119              * @throws Exception
0120              */
0121             static OId stringToOid(const QByteArray& string);
0122 
0123             /**
0124              * Copy an already raw oid into a git_oid structure.
0125              * @param raw the raw input bytes to be copied.
0126              */
0127             static OId rawDataToOid(const QByteArray& raw);
0128 
0129             /**
0130               Checks if this is a valid Git OId. An OId is invalid if it is empty or 0x0000... (20 byte).
0131               @return True, if the OId is valid. False if not.
0132               */
0133             bool isValid() const;
0134 
0135             /**
0136              * Format a OId into a hex string.
0137              */
0138             QByteArray format() const;
0139 
0140             /**
0141              * Format a git_oid into a loose-object path string.
0142              *
0143              * The resulting string is "aa/...", where "aa" is the first two
0144              * hex digitis of the oid and "..." is the remaining 38 digits.
0145              */
0146             QByteArray pathFormat() const;
0147 
0148             git_oid* data();
0149             const git_oid* constData() const;
0150 
0151             /**
0152              * Returns the length of the OId as a number of hexadecimal
0153              * characters.
0154              *
0155              * The full length of a OId is 40, but the OId represented by this
0156              * class may be shorter.
0157              */
0158             int length() const;
0159 
0160         private:
0161             QByteArray d;
0162     };
0163 
0164     /**
0165      * Compare two OIds.
0166      */
0167     LIBQGIT2_EXPORT bool operator ==(const OId &oid1, const OId &oid2);
0168     /**
0169      * Compare two OIds.
0170      */
0171     LIBQGIT2_EXPORT bool operator !=(const OId &oid1, const OId &oid2);
0172 
0173     /**@}*/
0174 }
0175 
0176 #endif // LIBQGIT2_OID_H