@@ -213,6 +213,8 @@ SEXP git2r_revwalk_list(
213213 * @param time Sort the commits by commit time; can be combined with
214214 * topological.
215215 * @param reverse Sort the commits in reverse order
216+ * @param max_n n The upper limit of the number of commits to
217+ * output. Use max_n < 0 for unlimited number of commits.
216218 * @param path Only commits modifying this path are selected
217219 * @return list with S3 class git_commit objects
218220 */
@@ -222,9 +224,10 @@ SEXP git2r_revwalk_list2 (
222224 SEXP topological ,
223225 SEXP time ,
224226 SEXP reverse ,
227+ SEXP max_n ,
225228 SEXP path )
226229{
227- int error = GIT_OK , nprotect = 0 ;
230+ int i = 0 , error = GIT_OK , nprotect = 0 ;
228231 SEXP result = R_NilValue ;
229232 int n ;
230233 unsigned int sort_mode = GIT_SORT_NONE ;
@@ -242,6 +245,8 @@ SEXP git2r_revwalk_list2 (
242245 git2r_error (__func__ , NULL , "'time'" , git2r_err_logical_arg );
243246 if (git2r_arg_check_logical (reverse ))
244247 git2r_error (__func__ , NULL , "'reverse'" , git2r_err_logical_arg );
248+ if (git2r_arg_check_integer (max_n ))
249+ git2r_error (__func__ , NULL , "'max_n'" , git2r_err_integer_arg );
245250 if (git2r_arg_check_string (path ))
246251 git2r_error (__func__ , NULL , "'path'" , git2r_err_string_arg );
247252
@@ -286,12 +291,11 @@ SEXP git2r_revwalk_list2 (
286291 if (error )
287292 goto cleanup ;
288293
289- /* Count number of revisions before creating the list. */
290- n = git2r_revwalk_count (walker , -1 );
291-
292- /* Create the list to store the result. */
293- PROTECT (result = Rf_allocVector (VECSXP , n ));
294- nprotect ++ ;
294+ n = Rf_asInteger (max_n );
295+ if (n < 0 ) {
296+ /* Count number of revisions before creating the list. */
297+ n = git2r_revwalk_count (walker , n );
298+ }
295299
296300 /* Restart the revwalker. */
297301 git_revwalk_reset (walker );
@@ -300,7 +304,11 @@ SEXP git2r_revwalk_list2 (
300304 if (error )
301305 goto cleanup ;
302306
303- for (int i = 0 ; i < n ; i ++ ) {
307+ /* Create the list to store the result. */
308+ PROTECT (result = Rf_allocVector (VECSXP , n ));
309+ nprotect ++ ;
310+
311+ while (i < n ) {
304312 git_commit * commit ;
305313 SEXP item ;
306314 git_oid oid ;
@@ -359,6 +367,7 @@ SEXP git2r_revwalk_list2 (
359367 Rf_mkString (git2r_S3_class__git_commit ));
360368 git2r_commit_init (commit , repo , item );
361369 git_commit_free (commit );
370+ i ++ ;
362371 }
363372
364373cleanup :
0 commit comments