@@ -392,69 +392,76 @@ SEXP git2r_remote_url(SEXP repo, SEXP remote)
392392 return url ;
393393}
394394
395- SEXP git2r_ls_remote (SEXP repo , SEXP name )
395+ /**
396+ * Get the remote's url
397+ *
398+ * Based on https://github.com/libgit2/libgit2/blob/babdc376c7/examples/network/ls-remote.c
399+ * @param repo S4 class git_repository
400+ * @param name Character vector with URL of remote.
401+ * @return Character vector for each reference with the associated commit IDs.
402+ */
403+ SEXP git2r_remote_ls (SEXP name , SEXP repo , SEXP credentials )
396404{
397- if (git2r_arg_check_string (name ))
398- git2r_error (__func__ , NULL , "'name'" , git2r_err_string_arg );
399-
400405 const char * name_ = CHAR (STRING_ELT (name , 0 ));
401406 SEXP result = R_NilValue ;
402407 SEXP names = R_NilValue ;
403408 git_remote * remote = NULL ;
404- int error ;
409+ int err ;
405410 const git_remote_head * * refs ;
406411 size_t refs_len , i ;
407412 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT ;
408- git_repository * repo_ = NULL ;
413+ git2r_transfer_data payload = GIT2R_TRANSFER_DATA_INIT ;
414+ git_repository * repository = NULL ;
409415
410- repo_ = git2r_repository_open (repo );
416+ if (git2r_arg_check_string (name ))
417+ git2r_error (__func__ , NULL , "'name'" , git2r_err_string_arg );
411418
412- error = git_remote_lookup (& remote , repo_ , name_ );
413- if (error < 0 ) {
414- error = git_remote_create_anonymous (& remote , repo_ , name_ );
415- if (error < 0 ) {
416- goto cleanup ;
417- }
418- }
419+ if (git2r_arg_check_credentials (credentials ))
420+ git2r_error (__func__ , NULL , "'credentials'" , git2r_err_credentials_arg );
419421
420- /**
421- * Connect to the remote and call the printing function for
422- * each of the remote references.
423- */
424- callbacks .credentials = git2r_cred_acquire_cb ;
422+ repository = git2r_repository_open (repo );
425423
426- error = git_remote_connect (remote , GIT_DIRECTION_FETCH , & callbacks );
427- if (error < 0 ) {
428- goto cleanup ;
429- }
424+ if (!repository )
425+ git2r_error (__func__ , NULL , git2r_err_invalid_repository , NULL );
430426
431- /**
432- * Get the list of references on the remote and print out
433- * their name next to what they point to.
434- */
435- if ( git_remote_ls ( & refs , & refs_len , remote ) < 0 ) {
436- goto cleanup ;
427+ err = git_remote_lookup ( & remote , repository , name_ );
428+ if ( err < 0 ) {
429+ err = git_remote_create_anonymous ( & remote , repository , name_ );
430+ if ( err < 0 ) {
431+ goto cleanup ;
432+ }
437433 }
438434
435+ payload .credentials = credentials ;
436+ callbacks .payload = & payload ;
437+ callbacks .credentials = & git2r_cred_acquire_cb ;
438+
439+ err = git_remote_connect (remote , GIT_DIRECTION_FETCH , & callbacks );
440+ if (err < 0 )
441+ goto cleanup ;
442+
443+ err = git_remote_ls (& refs , & refs_len , remote );
444+ if (err < 0 )
445+ goto cleanup ;
446+
439447 PROTECT (result = allocVector (STRSXP , refs_len ));
440- PROTECT ( names = allocVector (STRSXP , refs_len ));
448+ setAttrib ( result , R_NamesSymbol , names = allocVector (STRSXP , refs_len ));
441449
442450 for (i = 0 ; i < refs_len ; i ++ ) {
443- char oid [GIT_OID_HEXSZ + 1 ] = {0 };
444- git_oid_fmt (oid , & refs [i ]-> oid );
445- SET_STRING_ELT (result , i , mkChar (oid ));
446- SET_STRING_ELT (names , i , mkChar (refs [i ]-> name ));
451+ char oid [GIT_OID_HEXSZ + 1 ] = {0 };
452+ git_oid_fmt (oid , & refs [i ]-> oid );
453+ SET_STRING_ELT (result , i , mkChar (oid ));
454+ SET_STRING_ELT (names , i , mkChar (refs [i ]-> name ));
447455 }
448- setAttrib (result , R_NamesSymbol , names );
449456
450457cleanup :
451- if (repo_ )
452- git_repository_free (repo_ );
458+ if (repository )
459+ git_repository_free (repository );
453460
454461 if (result != R_NilValue )
455- UNPROTECT (2 );
462+ UNPROTECT (1 );
456463
457- if (error )
464+ if (err )
458465 git2r_error (__func__ , giterr_last (), NULL , NULL );
459466
460467 return (result );
0 commit comments