Commit 60d5cc57 by Vicent Martí

Merge pull request #834 from carlosmn/network-callbacks

Add a struct for network callbacks
parents f0244463 b3aaa7a7
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h>
struct dl_data { struct dl_data {
git_remote *remote; git_remote *remote;
...@@ -39,7 +40,7 @@ exit: ...@@ -39,7 +40,7 @@ exit:
pthread_exit(&data->ret); 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; const char *action;
char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1]; char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1];
...@@ -65,6 +66,7 @@ int fetch(git_repository *repo, int argc, char **argv) ...@@ -65,6 +66,7 @@ int fetch(git_repository *repo, int argc, char **argv)
git_indexer_stats stats; git_indexer_stats stats;
pthread_t worker; pthread_t worker;
struct dl_data data; struct dl_data data;
git_remote_callbacks callbacks;
// Figure out whether it's a named remote or a URL // Figure out whether it's a named remote or a URL
printf("Fetching %s\n", argv[1]); printf("Fetching %s\n", argv[1]);
...@@ -73,6 +75,11 @@ int fetch(git_repository *repo, int argc, char **argv) ...@@ -73,6 +75,11 @@ int fetch(git_repository *repo, int argc, char **argv)
return -1; return -1;
} }
// Set up the callbacks (only update_tips for now)
memset(&callbacks, 0, sizeof(callbacks));
callbacks.update_tips = &update_cb;
git_remote_set_callbacks(remote, &callbacks);
// Set up the information for the background worker thread // Set up the information for the background worker thread
data.remote = remote; data.remote = remote;
data.bytes = &bytes; data.bytes = &bytes;
...@@ -101,7 +108,7 @@ int fetch(git_repository *repo, int argc, char **argv) ...@@ -101,7 +108,7 @@ int fetch(git_repository *repo, int argc, char **argv)
// right commits. This may be needed even if there was no packfile // right commits. This may be needed even if there was no packfile
// to download, which can happen e.g. when the branches have been // to download, which can happen e.g. when the branches have been
// changed but all the neede objects are available locally. // 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; return -1;
git_remote_free(remote); git_remote_free(remote);
......
...@@ -220,7 +220,7 @@ GIT_EXTERN(void) git_remote_free(git_remote *remote); ...@@ -220,7 +220,7 @@ GIT_EXTERN(void) git_remote_free(git_remote *remote);
* @param remote the remote to update * @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 * @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 * Return whether a string is a valid remote URL
...@@ -268,6 +268,39 @@ GIT_EXTERN(int) git_remote_add(git_remote **out, git_repository *repo, const cha ...@@ -268,6 +268,39 @@ GIT_EXTERN(int) git_remote_add(git_remote **out, git_repository *repo, const cha
GIT_EXTERN(void) git_remote_check_cert(git_remote *remote, int check); GIT_EXTERN(void) git_remote_check_cert(git_remote *remote, int check);
/**
* Argument to the completion callback which tells it which operation
* finished.
*/
typedef enum git_remote_completion_type {
GIT_REMOTE_COMPLETION_DOWNLOAD,
GIT_REMOTE_COMPLETION_INDEXING,
GIT_REMOTE_COMPLETION_ERROR,
} git_remote_completion_type;
/**
* The callback settings structure
*
* Set the calbacks to be called by the remote.
*/
struct git_remote_callbacks {
int (*progress)(const char *str, void *data);
int (*completion)(git_remote_completion_type type, void *data);
int (*update_tips)(const char *refname, const git_oid *a, const git_oid *b, void *data);
void *data;
};
/**
* Set the callbacks for a remote
*
* Note that the remote keeps its own copy of the data and you need to
* call this function again if you want to change the callbacks.
*
* @param remote the remote to configure
* @param callbacks a pointer to the user's callback settings
*/
GIT_EXTERN(void) git_remote_set_callbacks(git_remote *remote, git_remote_callbacks *callbacks);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
#endif #endif
...@@ -179,6 +179,7 @@ typedef struct git_refspec git_refspec; ...@@ -179,6 +179,7 @@ typedef struct git_refspec git_refspec;
typedef struct git_remote git_remote; typedef struct git_remote git_remote;
typedef struct git_remote_head git_remote_head; typedef struct git_remote_head git_remote_head;
typedef struct git_remote_callbacks git_remote_callbacks;
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
......
...@@ -423,7 +423,7 @@ int git_remote_download(git_remote *remote, git_off_t *bytes, git_indexer_stats ...@@ -423,7 +423,7 @@ int git_remote_download(git_remote *remote, git_off_t *bytes, git_indexer_stats
return git_fetch_download_pack(remote, bytes, 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; int error = 0;
unsigned int i = 0; unsigned int i = 0;
...@@ -473,8 +473,8 @@ int git_remote_update_tips(git_remote *remote, int (*cb)(const char *refname, co ...@@ -473,8 +473,8 @@ int git_remote_update_tips(git_remote *remote, int (*cb)(const char *refname, co
git_reference_free(ref); git_reference_free(ref);
if (cb != NULL) { if (remote->callbacks.update_tips != NULL) {
if (cb(refname.ptr, &old, &head->oid) < 0) if (remote->callbacks.update_tips(refname.ptr, &old, &head->oid, remote->callbacks.data) < 0)
goto on_error; goto on_error;
} }
} }
...@@ -618,3 +618,10 @@ void git_remote_check_cert(git_remote *remote, int check) ...@@ -618,3 +618,10 @@ void git_remote_check_cert(git_remote *remote, int check)
remote->check_cert = check; remote->check_cert = check;
} }
void git_remote_set_callbacks(git_remote *remote, git_remote_callbacks *callbacks)
{
assert(remote && callbacks);
memcpy(&remote->callbacks, callbacks, sizeof(git_remote_callbacks));
}
...@@ -20,6 +20,7 @@ struct git_remote { ...@@ -20,6 +20,7 @@ struct git_remote {
struct git_refspec push; struct git_refspec push;
git_transport *transport; git_transport *transport;
git_repository *repo; git_repository *repo;
git_remote_callbacks callbacks;
unsigned int need_pack:1, unsigned int need_pack:1,
check_cert; check_cert;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment