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_INDEX_H
0022 #define LIBQGIT2_INDEX_H
0023 
0024 #include <QtCore/QSharedPointer>
0025 
0026 #include "git2.h"
0027 
0028 #include "libqgit2_export.h"
0029 
0030 namespace LibQGit2
0031 {
0032     class OId;
0033     class IndexEntry;
0034 
0035     /**
0036      * @brief Wrapper class for git_index.
0037      * Represents a Git index a.k.a "the stage".
0038      *
0039      * @ingroup LibQGit2
0040      * @{
0041      */
0042     class LIBQGIT2_EXPORT Index
0043     {
0044         public:
0045 
0046             /**
0047              * Creates a Index that points to 'index'. The pointer 'index' becomes managed by
0048              * this Index, and must not be passed to another Index or freed outside this
0049              * object.
0050              */
0051             explicit Index(git_index *index = 0);
0052 
0053             /**
0054              * Copy constructor; creates a copy of the object, sharing the same underlaying data
0055              * structure.
0056              */
0057             Index(const Index& other);
0058 
0059             /**
0060              * Destruct an existing index object.
0061              */
0062             ~Index();
0063 
0064             /**
0065              * Create a index object as a memory representation
0066              * of the Git index file in 'indexPath', without a repository
0067              * to back it.
0068              *
0069              * Since there is no ODB behind this index, any Index methods
0070              * which rely on the ODB (e.g. index_add) will fail with the
0071              * GIT_EBAREINDEX error code.
0072              *
0073              * @param index_path the path to the index file in disk
0074              * @throws LibQGit2::Exception
0075              */
0076             void open(const QString& indexPath);
0077 
0078             /**
0079              * Create a new tree object from the index
0080              *
0081              * @throws LibQGit2::Exception
0082              */
0083             OId createTree();
0084 
0085             /**
0086              * Clear the contents (all the entries) of an index object.
0087              * This clears the index object in memory; changes must be manually
0088              * written to disk for them to take effect.
0089              *
0090              * @throws LibQGit2::Exception
0091              */
0092             void clear();
0093 
0094             /**
0095              * Update the contents of an existing index object in memory by reading
0096              * from the hard disk.
0097              *
0098              * If `force` is true, this performs a "hard" read that discards in-memory
0099              * changes and always reloads the on-disk index data.  If there is no
0100              * on-disk version, the index will be cleared.
0101              *
0102              * If `force` is false, this does a "soft" read that reloads the index
0103              * data from disk only if it has changed since the last time it was
0104              * loaded.  Purely in-memory index data will be untouched.  Be aware: if
0105              * there are changes on disk, unwritten in-memory changes are discarded.
0106              *
0107              * @param index an existing index object
0108              * @param force if true, always reload, vs. only read if file has changed
0109              * @throws LibQGit2::Exception
0110              */
0111             void read(bool force = false) const;
0112 
0113             /**
0114              * Write an existing index object from memory back to disk
0115              * using an atomic file lock.
0116              *
0117              * @throws LibQGit2::Exception
0118              */
0119             void write();
0120 
0121             /**
0122              * Find the first index of any entires which point to given
0123              * path in the Git index.
0124              *
0125              * @param path path to search
0126              * @return an index >= 0 if found, -1 otherwise
0127              */
0128             int find(const QString& path);
0129 
0130             /**
0131              * Add or update an index entry from a file in disk.
0132              *
0133              * @param path filename to add
0134              * @throws LibQGit2::Exception
0135              */
0136             void addByPath(const QString& path);
0137 
0138             /**
0139              * Remove an entry from the index given the path
0140              *
0141              * @param stage stage of the entry to remove
0142              * @throws LibQGit2::Exception
0143              */
0144             void remove(const QString& path, int stage);
0145 
0146             /**
0147              * Insert an entry into the index.
0148              * A full copy (including the 'path' string) of the given
0149              * 'source_entry' will be inserted on the index; if the index
0150              * already contains an entry for the same path, the entry
0151              * will be updated.
0152              *
0153              * @param source_entry new entry object
0154              * @throws LibQGit2::Exception
0155              */
0156             void add(const IndexEntry& source_entry);
0157 
0158             /**
0159              * Update all index entries to match the working directory.
0160              * @throws LibQGit2::Exception
0161              */
0162             void updateAll();
0163 
0164             /**
0165              * Get a pointer to one of the entries in the index
0166              *
0167              * This entry can be modified, and the changes will be written
0168              * back to disk on the next write() call.
0169              *
0170              * @param n the position of the entry
0171              * @return a pointer to the entry; NULL if out of bounds
0172              */
0173             IndexEntry getByIndex(int n) const;
0174 
0175             /**
0176              * Get the count of entries currently in the index
0177              *
0178              * @return integer of count of current entries
0179              */
0180             unsigned int entryCount() const;
0181 
0182             /**
0183              * Checks if this Index has conflicts.
0184              * If this is a null index it never has conflicts.
0185              * @return true if there are conflicts in this Index.
0186              */
0187             bool hasConflicts() const;
0188 
0189             git_index* data() const;
0190             const git_index* constData() const;
0191 
0192         private:
0193             typedef QSharedPointer<git_index> ptr_type;
0194             ptr_type d;
0195     };
0196 
0197     /**@}*/
0198 }
0199 
0200 #endif // LIBQGIT2_INDEX_H