remote: add a convenience 'fetch' function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
diff --git a/include/git2/remote.h b/include/git2/remote.h
index 83ad195..8c21870 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -311,6 +311,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
*
* @param url the url to check
diff --git a/src/clone.c b/src/clone.c
index 436fdff..13f2a8e 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 2d0321e..ace8865 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;