Skip to content

Commit

Permalink
RFC: A proposal for setting callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmn committed May 24, 2012
1 parent 7eeec8f commit a41cdff
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
21 changes: 19 additions & 2 deletions examples/network/fetch.c
Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 20 additions & 1 deletion include/git2/remote.h
Expand Up @@ -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
Expand Down Expand Up @@ -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
22 changes: 19 additions & 3 deletions src/remote.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/remote.h
Expand Up @@ -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

0 comments on commit a41cdff

Please sign in to comment.