Skip to content

Commit c2a9174

Browse files
committed
Increase speed of commits(path). Closes #406
Signed-off-by: Stefan Widgren <stefan.widgren@gmail.com>
1 parent 310fec9 commit c2a9174

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

R/commit.R

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -337,17 +337,8 @@ commits <- function(repo = ".",
337337
repo_wd <- normalizePath(workdir(repo), winslash = "/")
338338
path <- sanitize_path(path, repo_wd)
339339
path_revwalk <- .Call(git2r_revwalk_list2, repo, sha, topological,
340-
time, reverse, path)
341-
path_commits <- vapply(path_revwalk, function(x) !is.null(x),
342-
logical(1))
343-
344-
if (n == -1L) {
345-
max_n <- sum(path_commits)
346-
} else {
347-
max_n <- n
348-
}
349-
350-
return(path_revwalk[path_commits][seq_len(max_n)])
340+
time, reverse, n, path)
341+
return(path_revwalk[!vapply(path_revwalk, is.null, logical(1))])
351342
}
352343

353344
.Call(git2r_revwalk_list, repo, sha, topological, time, reverse, n)

src/git2r.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static const R_CallMethodDef callMethods[] =
137137
CALLDEF(git2r_revparse_single, 2),
138138
CALLDEF(git2r_revwalk_contributions, 4),
139139
CALLDEF(git2r_revwalk_list, 6),
140-
CALLDEF(git2r_revwalk_list2, 6),
140+
CALLDEF(git2r_revwalk_list2, 7),
141141
CALLDEF(git2r_signature_default, 1),
142142
CALLDEF(git2r_ssl_cert_locations, 2),
143143
CALLDEF(git2r_stash_apply, 2),

src/git2r_revwalk.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

364373
cleanup:

src/git2r_revwalk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424

2525
SEXP git2r_revwalk_contributions(SEXP repo, SEXP topological, SEXP time, SEXP reverse);
2626
SEXP git2r_revwalk_list(SEXP repo, SEXP sha, SEXP topological, SEXP time, SEXP reverse, SEXP max_n);
27-
SEXP git2r_revwalk_list2(SEXP repo, SEXP sha, SEXP topological, SEXP time, SEXP reverse, SEXP path);
27+
SEXP git2r_revwalk_list2(SEXP repo, SEXP sha, SEXP topological, SEXP time, SEXP reverse, SEXP max_n, SEXP path);
2828

2929
#endif

0 commit comments

Comments
 (0)