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

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_REVWALK_H
0022 #define LIBQGIT2_REVWALK_H
0023 
0024 #include "git2.h"
0025 
0026 #include <QtGlobal>
0027 
0028 #include "libqgit2_export.h"
0029 
0030 namespace LibQGit2
0031 {
0032 
0033 class Exception;
0034 class Repository;
0035 class OId;
0036 class Commit;
0037 class Reference;
0038 
0039 /**
0040   * @brief Wrapper class for git_revwalk.
0041   * The revision walker can be used to traverse Git commit history. It features sorting abilities and more.
0042   *
0043   * @ingroup LibQGit2
0044   * @{
0045   */
0046 class LIBQGIT2_EXPORT RevWalk
0047 {
0048 public:
0049     /**
0050      * Defines the sort mode when walking revisions.
0051      */
0052     enum SortMode
0053     {
0054         None = GIT_SORT_NONE,
0055         Time = GIT_SORT_TIME,
0056         Topological = GIT_SORT_TOPOLOGICAL,
0057         Reverse = GIT_SORT_REVERSE
0058     };
0059 
0060     Q_DECLARE_FLAGS(SortModes, SortMode) //!< Combination of SortMode
0061 
0062     /**
0063      * Allocate a new revision walker to iterate through a repo.
0064      *
0065      * @param repo the repo to walk through
0066      */
0067     explicit RevWalk(const Repository& repository);
0068 
0069     RevWalk( const RevWalk& other );
0070 
0071     /**
0072      * Delete a revwalk previously allocated.
0073      */
0074     ~RevWalk();
0075 
0076     /**
0077      * Reset the walking machinery for reuse.
0078      */
0079     void reset() const;
0080 
0081     /**
0082      * Mark the commit with the given oid as a starting point.
0083      *
0084      * This method adds the commit with the given id as one
0085      * of the starting points for the revision traversal.
0086      *
0087      * @param oid the oid of the commit to start from.
0088      * @throws LibQGit2::Exception
0089      */
0090     void push(const OId& oid) const;
0091 
0092     /**
0093      * Mark the given commit as a starting point.
0094      *
0095      * This method adds the given commit as one
0096      * of the starting points for the revision traversal.
0097      *
0098      * @param commit the commit to start from.
0099      * @throws LibQGit2::Exception
0100      */
0101     void push(const Commit& commit) const;
0102 
0103     /**
0104      * Mark the given reference as a starting point.
0105      *
0106      * This method adds the given reference as one
0107      * of the starting points for the revision traversal.
0108      *
0109      * @param reference the reference to start from.
0110      * @throws LibQGit2::Exception
0111      */
0112     void push(const Reference& reference) const;
0113 
0114     /**
0115      * Mark the references matching the given glob as a starting point.
0116      *
0117      * This method adds the references which name matches the given
0118      * glob as starting points for the revision traversal.
0119      *
0120      * @param glob the glob that reference names shall match.
0121      * @throws LibQGit2::Exception
0122      */
0123     void push(const QString& glob) const;
0124 
0125     /**
0126      * Mark HEAD as a starting point.
0127      *
0128      * This method adds HEAD as a starting point for the revision traversal.
0129      *
0130      * @throws LibQGit2::Exception
0131      */
0132     void pushHead() const;
0133 
0134     /**
0135      * Uses the given range to perform the traversal.
0136      *
0137      * This method accepts a range in the form <commit1>..<commit2>,
0138      * according to Git range syntax, i.e. walk all commits that are
0139      * reachable from commit2 excluding those reachable from commit1.
0140      */
0141     void pushRange(const QString& range) const;
0142 
0143     /**
0144      * Hide the commit with the given oid and its ancestors from the walker.
0145      *
0146      * This method hides the commit with the given id from
0147      * the output of the revision traversal.
0148      *
0149      * @param oid the oid of the commit to start from.
0150      * @throws LibQGit2::Exception
0151      */
0152     void hide(const OId& oid) const;
0153 
0154     /**
0155      * Hide the given commit and its ancestors from the walker.
0156      *
0157      * This method hides the given commit from
0158      * the output of the revision traversal.
0159      *
0160      * @param commit the commit to start from.
0161      * @throws LibQGit2::Exception
0162      */
0163     void hide(const Commit& commit) const;
0164 
0165     /**
0166      * Hide the given reference and its ancestors from the walker.
0167      *
0168      * This method hides the given reference from
0169      * the output of the revision traversal.
0170      *
0171      * @param reference the reference to start from.
0172      * @throws LibQGit2::Exception
0173      */
0174     void hide(const Reference& reference) const;
0175 
0176     /**
0177      * Hide the references matching the given glob and its ancestors from the walker.
0178      *
0179      * This method hides the references which name matches the given
0180      * glob from the output of the revision traversal.
0181      *
0182      * @param glob the glob that reference names shall match.
0183      * @throws LibQGit2::Exception
0184      */
0185     void hide(const QString& glob) const;
0186 
0187     /**
0188      * Hide HEAD from the walker.
0189      *
0190      * This method hides HEAD and its ancestors from the output of the revision traversal.
0191      *
0192      * @throws LibQGit2::Exception
0193      */
0194     void hideHead() const;
0195 
0196     /**
0197      * Get the oid of the next commit from the revision traversal.
0198      *
0199      * @param oid The oid of the next object in the revisions tree if it was found; otherwise it is undefined.
0200      * @return True if the object was found.
0201      */
0202     bool next(OId& oid) const;
0203 
0204     /**
0205      * Get the next commit from the revision traversal and look it up in the owner repository.
0206      *
0207      * @param commit The next commit within the set repository, if it was found; otherwise an empty Commit.
0208      * @return True when the commit was found.
0209      */
0210     bool next(Commit& commit);
0211 
0212     /**
0213      * Change the sorting mode when iterating through the
0214      * repository's contents.
0215      * Changing the sorting mode resets the walker.
0216      *
0217      * @param sortMode The sorting mode @see SortModes.
0218      */
0219     void setSorting(SortModes sortMode);
0220 
0221     /**
0222      * Return a new repository object initialized to the repository
0223      * on which this walker is operating.
0224      *
0225      * @return a copy of the repository being walked
0226      */
0227     Repository* repository();
0228 
0229     /**
0230      * Return the const repository on which this walker is operating.
0231      *
0232      * @return the repository being walked
0233      */
0234     const Repository* constRepository();
0235 
0236     git_revwalk* data() const;
0237     const git_revwalk* constData() const;
0238 
0239 private:
0240     const Repository* m_repository;
0241     git_revwalk* m_revWalk;
0242 };
0243 
0244 Q_DECLARE_OPERATORS_FOR_FLAGS(RevWalk::SortModes)
0245 
0246 /**@}*/
0247 }
0248 
0249 #endif // LIBQGIT2_REVWALK_H