2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
7 #ifndef INCLUDE_git_revwalk_h__
8 #define INCLUDE_git_revwalk_h__
15 * @file git2/revwalk.h
16 * @brief Git revision traversal routines
17 * @defgroup git_revwalk Git revision traversal routines
24 * Flags to specify the sorting which a revwalk should perform.
28 * Sort the output with the same default time-order method from git.
29 * This is the default sorting for new walkers.
34 * Sort the repository contents in topological order (parents before
35 * children); this sorting mode can be combined with time sorting to
36 * produce git's "time-order".
38 GIT_SORT_TOPOLOGICAL
= 1 << 0,
41 * Sort the repository contents by commit time;
42 * this sorting mode can be combined with
43 * topological sorting.
45 GIT_SORT_TIME
= 1 << 1,
48 * Iterate through the repository contents in reverse
49 * order; this sorting mode can be combined with
52 GIT_SORT_REVERSE
= 1 << 2,
56 * Allocate a new revision walker to iterate through a repo.
58 * This revision walker uses a custom memory pool and an internal
59 * commit cache, so it is relatively expensive to allocate.
61 * For maximum performance, this revision walker should be
62 * reused for different walks.
64 * This revision walker is *not* thread safe: it may only be
65 * used to walk a repository on a single thread; however,
66 * it is possible to have several revision walkers in
67 * several different threads walking the same repository.
69 * @param out pointer to the new revision walker
70 * @param repo the repo to walk through
71 * @return 0 or an error code
73 GIT_EXTERN(int) git_revwalk_new(git_revwalk
**out
, git_repository
*repo
);
76 * Reset the revision walker for reuse.
78 * This will clear all the pushed and hidden commits, and
79 * leave the walker in a blank state (just like at
80 * creation) ready to receive new commit pushes and
83 * The revision walk is automatically reset when a walk
86 * @param walker handle to reset.
88 GIT_EXTERN(void) git_revwalk_reset(git_revwalk
*walker
);
91 * Add a new root for the traversal
93 * The pushed commit will be marked as one of the roots from which to
94 * start the walk. This commit may not be walked if it or a child is
97 * At least one commit must be pushed onto the walker before a walk
100 * The given id must belong to a committish on the walked
103 * @param walk the walker being used for the traversal.
104 * @param id the oid of the commit to start from.
105 * @return 0 or an error code
107 GIT_EXTERN(int) git_revwalk_push(git_revwalk
*walk
, const git_oid
*id
);
110 * Push matching references
112 * The OIDs pointed to by the references that match the given glob
113 * pattern will be pushed to the revision walker.
115 * A leading 'refs/' is implied if not present as well as a trailing
116 * '/\*' if the glob lacks '?', '\*' or '['.
118 * Any references matching this glob which do not point to a
119 * committish will be ignored.
121 * @param walk the walker being used for the traversal
122 * @param glob the glob pattern references should match
123 * @return 0 or an error code
125 GIT_EXTERN(int) git_revwalk_push_glob(git_revwalk
*walk
, const char *glob
);
128 * Push the repository's HEAD
130 * @param walk the walker being used for the traversal
131 * @return 0 or an error code
133 GIT_EXTERN(int) git_revwalk_push_head(git_revwalk
*walk
);
136 * Mark a commit (and its ancestors) uninteresting for the output.
138 * The given id must belong to a committish on the walked
141 * The resolved commit and all its parents will be hidden from the
142 * output on the revision walk.
144 * @param walk the walker being used for the traversal.
145 * @param commit_id the oid of commit that will be ignored during the traversal
146 * @return 0 or an error code
148 GIT_EXTERN(int) git_revwalk_hide(git_revwalk
*walk
, const git_oid
*commit_id
);
151 * Hide matching references.
153 * The OIDs pointed to by the references that match the given glob
154 * pattern and their ancestors will be hidden from the output on the
157 * A leading 'refs/' is implied if not present as well as a trailing
158 * '/\*' if the glob lacks '?', '\*' or '['.
160 * Any references matching this glob which do not point to a
161 * committish will be ignored.
163 * @param walk the walker being used for the traversal
164 * @param glob the glob pattern references should match
165 * @return 0 or an error code
167 GIT_EXTERN(int) git_revwalk_hide_glob(git_revwalk
*walk
, const char *glob
);
170 * Hide the repository's HEAD
172 * @param walk the walker being used for the traversal
173 * @return 0 or an error code
175 GIT_EXTERN(int) git_revwalk_hide_head(git_revwalk
*walk
);
178 * Push the OID pointed to by a reference
180 * The reference must point to a committish.
182 * @param walk the walker being used for the traversal
183 * @param refname the reference to push
184 * @return 0 or an error code
186 GIT_EXTERN(int) git_revwalk_push_ref(git_revwalk
*walk
, const char *refname
);
189 * Hide the OID pointed to by a reference
191 * The reference must point to a committish.
193 * @param walk the walker being used for the traversal
194 * @param refname the reference to hide
195 * @return 0 or an error code
197 GIT_EXTERN(int) git_revwalk_hide_ref(git_revwalk
*walk
, const char *refname
);
200 * Get the next commit from the revision walk.
202 * The initial call to this method is *not* blocking when
203 * iterating through a repo with a time-sorting mode.
205 * Iterating with Topological or inverted modes makes the initial
206 * call blocking to preprocess the commit list, but this block should be
207 * mostly unnoticeable on most repositories (topological preprocessing
208 * times at 0.3s on the git.git repo).
210 * The revision walker is reset when the walk is over.
212 * @param out Pointer where to store the oid of the next commit
213 * @param walk the walker to pop the commit from.
214 * @return 0 if the next commit was found;
215 * GIT_ITEROVER if there are no commits left to iterate
217 GIT_EXTERN(int) git_revwalk_next(git_oid
*out
, git_revwalk
*walk
);
220 * Change the sorting mode when iterating through the
221 * repository's contents.
223 * Changing the sorting mode resets the walker.
225 * @param walk the walker being used for the traversal.
226 * @param sort_mode combination of GIT_SORT_XXX flags
228 GIT_EXTERN(void) git_revwalk_sorting(git_revwalk
*walk
, unsigned int sort_mode
);
231 * Push and hide the respective endpoints of the given range.
233 * The range should be of the form
235 * where each <commit> is in the form accepted by 'git_revparse_single'.
236 * The left-hand commit will be hidden and the right-hand commit pushed.
238 * @param walk the walker being used for the traversal
239 * @param range the range
240 * @return 0 or an error code
243 GIT_EXTERN(int) git_revwalk_push_range(git_revwalk
*walk
, const char *range
);
246 * Simplify the history by first-parent
248 * No parents other than the first for each commit will be enqueued.
250 GIT_EXTERN(void) git_revwalk_simplify_first_parent(git_revwalk
*walk
);
254 * Free a revision walker previously allocated.
256 * @param walk traversal handle to close. If NULL nothing occurs.
258 GIT_EXTERN(void) git_revwalk_free(git_revwalk
*walk
);
261 * Return the repository on which this walker
264 * @param walk the revision walker
265 * @return the repository being walked
267 GIT_EXTERN(git_repository
*) git_revwalk_repository(git_revwalk
*walk
);
270 * This is a callback function that user can provide to hide a
271 * commit and its parents. If the callback function returns non-zero value,
272 * then this commit and its parents will be hidden.
274 * @param commit_id oid of Commit
275 * @param payload User-specified pointer to data to be passed as data payload
277 typedef int(*git_revwalk_hide_cb
)(
278 const git_oid
*commit_id
,
282 * Adds a callback function to hide a commit and its parents
284 * @param walk the revision walker
285 * @param hide_cb callback function to hide a commit and its parents
286 * @param payload data payload to be passed to callback function
288 GIT_EXTERN(int) git_revwalk_add_hide_cb(
290 git_revwalk_hide_cb hide_cb
,