Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

RFC: A proposal for setting callbacks

  • Loading branch information...
commit a41cdff5447a5f9fcc7f7f8c533a60dff2c0cde4 1 parent 7eeec8f
@carlosmn authored
View
21 examples/network/fetch.c
@@ -39,7 +39,7 @@ static void *download(void *ptr)
pthread_exit(&data->ret);
}
-int update_cb(const char *refname, const git_oid *a, const git_oid *b)
+int update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
{
const char *action;
char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1];
@@ -58,6 +58,19 @@ int update_cb(const char *refname, const git_oid *a, const git_oid *b)
return 0;
}
+int progress_cb(const char *str, void *data)
+{
+ fputs(str, stderr);
+ return 0;
+}
+
+int error_cb(const char *str, void *data)
+{
+ fputs("The world is ending!", stderr);
+ fputs(str, stderr);
+ return 0;
+}
+
int fetch(git_repository *repo, int argc, char **argv)
{
git_remote *remote = NULL;
@@ -73,6 +86,10 @@ int fetch(git_repository *repo, int argc, char **argv)
return -1;
}
+ git_remote_callback(remote, GIT_CALLBACK_PROGRESS, progress_cb, NULL);
+ git_remote_callback(remote, GIT_CALLBACK_ERROR, error_cb, NULL);
+ git_remote_callback(remote, GIT_CALLBACK_UPDATE_TIPS, update_cb, NULL);
+
// Set up the information for the background worker thread
data.remote = remote;
data.bytes = &bytes;
@@ -100,7 +117,7 @@ int fetch(git_repository *repo, int argc, char **argv)
// right commits. This may be needed even if there was no packfile
// to download, which can happen e.g. when the branches have been
// changed but all the neede objects are available locally.
- if (git_remote_update_tips(remote, update_cb) < 0)
+ if (git_remote_update_tips(remote) < 0)
return -1;
git_remote_free(remote);
View
21 include/git2/remote.h
@@ -190,7 +190,7 @@ GIT_EXTERN(void) git_remote_free(git_remote *remote);
* @param remote the remote to update
* @param cb callback to run on each ref update. 'a' is the old value, 'b' is then new value
*/
-GIT_EXTERN(int) git_remote_update_tips(git_remote *remote, int (*cb)(const char *refname, const git_oid *a, const git_oid *b));
+GIT_EXTERN(int) git_remote_update_tips(git_remote *remote);
/**
* Return whether a string is a valid remote URL
@@ -229,6 +229,25 @@ GIT_EXTERN(int) git_remote_list(git_strarray *remotes_list, git_repository *repo
*/
GIT_EXTERN(int) git_remote_add(git_remote **out, git_repository *repo, const char *name, const char *url);
+typedef enum {
+ GIT_CALLBACK_PROGRESS,
+ GIT_CALLBACK_ERROR,
+ GIT_CALLBACK_AUTH,
+ GIT_CALLBACK_UPDATE_TIPS,
+ GIT_CALLBACK_DONE,
+} git_callback_t;
+
+typedef int (*git_update_tips_cb)(const char *refname, const git_oid *a, const git_oid *b, void *data);
+/**
+ * Set a callback for a particular event
+ *
+ * @param remote The remote to act on
+ * @param opt Which callback to set
+ * @param callback function pointer to the callback
+ * @param data user-supplied data to pass to the callback
+ */
+GIT_EXTERN(int) git_remote_callback(git_remote *remote, git_callback_t opt, void *callback, void *data);
+
/** @} */
GIT_END_DECL
#endif
View
22 src/remote.c
@@ -324,7 +324,7 @@ int git_remote_download(git_remote *remote, git_off_t *bytes, git_indexer_stats
return git_fetch_download_pack(remote, bytes, stats);
}
-int git_remote_update_tips(git_remote *remote, int (*cb)(const char *refname, const git_oid *a, const git_oid *b))
+int git_remote_update_tips(git_remote *remote)
{
int error = 0;
unsigned int i = 0;
@@ -371,8 +371,8 @@ int git_remote_update_tips(git_remote *remote, int (*cb)(const char *refname, co
git_reference_free(ref);
- if (cb != NULL) {
- if (cb(refname.ptr, &old, &head->oid) < 0)
+ if (remote->update_tips != NULL) {
+ if (remote->update_tips(refname.ptr, &old, &head->oid, remote->update_tips_data) < 0)
goto on_error;
}
}
@@ -386,6 +386,22 @@ int git_remote_update_tips(git_remote *remote, int (*cb)(const char *refname, co
}
+int git_remote_callback(git_remote *remote, git_callback_t opt, void *fn, void *data)
+{
+ switch (opt) {
+ case GIT_CALLBACK_UPDATE_TIPS:
+ remote->update_tips = (git_update_tips_cb)fn;
+ remote->update_tips_data = data;
+ break;
+ default:
+ /* Set an error when we do this for real */
+ return -1;
+ break;
+ }
+
+ return 0;
+}
+
int git_remote_connected(git_remote *remote)
{
assert(remote);
View
2  src/remote.h
@@ -20,6 +20,8 @@ struct git_remote {
git_transport *transport;
git_repository *repo;
unsigned int need_pack:1;
+ git_update_tips_cb update_tips;
+ void *update_tips_data;
};
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.