@@ -391,3 +391,71 @@ SEXP git2r_remote_url(SEXP repo, SEXP remote)
391391
392392 return url ;
393393}
394+
395+ SEXP git2r_ls_remote (SEXP repo , SEXP name )
396+ {
397+ if (git2r_arg_check_string (name ))
398+ git2r_error (__func__ , NULL , "'name'" , git2r_err_string_arg );
399+
400+ const char * name_ = CHAR (STRING_ELT (name , 0 ));
401+ SEXP result = R_NilValue ;
402+ SEXP names = R_NilValue ;
403+ git_remote * remote = NULL ;
404+ int error ;
405+ const git_remote_head * * refs ;
406+ size_t refs_len , i ;
407+ git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT ;
408+ git_repository * repo_ = NULL ;
409+
410+ repo_ = git2r_repository_open (repo );
411+
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+
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 ;
425+
426+ error = git_remote_connect (remote , GIT_DIRECTION_FETCH , & callbacks );
427+ if (error < 0 ) {
428+ goto cleanup ;
429+ }
430+
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 ;
437+ }
438+
439+ PROTECT (result = allocVector (STRSXP , refs_len ));
440+ PROTECT (names = allocVector (STRSXP , refs_len ));
441+
442+ 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 ));
447+ }
448+ setAttrib (result , R_NamesSymbol , names );
449+
450+ cleanup :
451+ if (repo_ )
452+ git_repository_free (repo_ );
453+
454+ if (result != R_NilValue )
455+ UNPROTECT (2 );
456+
457+ if (error )
458+ git2r_error (__func__ , giterr_last (), NULL , NULL );
459+
460+ return (result );
461+ }
0 commit comments