diff --git a/include/git2/remote.h b/include/git2/remote.h index 83ad195f4..8c21870de 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -310,6 +310,17 @@ GIT_EXTERN(void) git_remote_free(git_remote *remote); */ GIT_EXTERN(int) git_remote_update_tips(git_remote *remote); +/** + * Download new data and update tips + * + * Convenience function to connect to a remote, download the data, + * disconnect and update the remote-tracking branches. + * + * @param remote the remote to fetch from + * @return 0 or an error code + */ +GIT_EXTERN(int) git_remote_fetch(git_remote *remote); + /** * Return whether a string is a valid remote URL * diff --git a/src/clone.c b/src/clone.c index 436fdff43..13f2a8eea 100644 --- a/src/clone.c +++ b/src/clone.c @@ -350,24 +350,6 @@ on_error: return error; } -static int do_fetch(git_remote *origin) -{ - int retcode; - - /* Connect and download everything */ - if ((retcode = git_remote_connect(origin, GIT_DIRECTION_FETCH)) < 0) - return retcode; - - if ((retcode = git_remote_download(origin)) < 0) - return retcode; - - /* Create "origin/foo" branches for all remote branches */ - if ((retcode = git_remote_update_tips(origin)) < 0) - return retcode; - - return 0; -} - static int setup_remotes_and_fetch( git_repository *repo, const char *url, @@ -391,7 +373,7 @@ static int setup_remotes_and_fetch( ((retcode = git_remote_add_fetch(origin, "refs/tags/*:refs/tags/*")) < 0)) goto on_error; - if ((retcode = do_fetch(origin)) < 0) + if ((retcode = git_remote_fetch(origin)) < 0) goto on_error; /* Point HEAD to the requested branch */ @@ -459,7 +441,7 @@ int git_clone_into(git_repository *repo, git_remote *remote, git_checkout_opts * old_fetchhead = git_remote_update_fetchhead(remote); git_remote_set_update_fetchhead(remote, 0); - if ((error = do_fetch(remote)) < 0) + if ((error = git_remote_fetch(remote)) < 0) goto cleanup; if (branch) diff --git a/src/remote.c b/src/remote.c index 2d0321eb3..ace886502 100644 --- a/src/remote.c +++ b/src/remote.c @@ -767,6 +767,24 @@ int git_remote_download(git_remote *remote) return git_fetch_download_pack(remote); } +int git_remote_fetch(git_remote *remote) +{ + int error; + + /* Connect and download everything */ + if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH)) < 0) + return error; + + if ((error = git_remote_download(remote)) < 0) + return error; + + /* We don't need to be connected anymore */ + git_remote_disconnect(remote); + + /* Create "remote/foo" branches for all remote branches */ + return git_remote_update_tips(remote); +} + static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *update_heads, const char *fetchspec_src) { unsigned int i;