diff options
author | Russell Belfer <rb@github.com> | 2013-12-06 15:07:57 -0800 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-12-11 10:57:49 -0800 |
commit | 25e0b1576d5f9e5248603f81d3198a65bfccf0ed (patch) | |
tree | dac0da29acfbd6478bed93bcab3557e2877dabaa /src/config.c | |
parent | fcd324c625d8be3f368c924d787e945e5812d8dd (diff) | |
download | libgit2-25e0b1576d5f9e5248603f81d3198a65bfccf0ed.tar.gz |
Remove converting user error to GIT_EUSER
This changes the behavior of callbacks so that the callback error
code is not converted into GIT_EUSER and instead we propagate the
return value through to the caller. Instead of using the
giterr_capture and giterr_restore functions, we now rely on all
functions to pass back the return value from a callback.
To avoid having a return value with no error message, the user
can call the public giterr_set_str or some such function to set
an error message. There is a new helper 'giterr_set_callback'
that functions can invoke after making a callback which ensures
that some error message was set in case the callback did not set
one.
In places where the sign of the callback return value is
meaningful (e.g. positive to skip, negative to abort), only the
negative values are returned back to the caller, obviously, since
the other values allow for continuing the loop.
The hardest parts of this were in the checkout code where positive
return values were overloaded as meaningful values for checkout.
I fixed this by adding an output parameter to many of the internal
checkout functions and removing the overload. This added some
code, but it is probably a better implementation.
There is some funkiness in the network code where user provided
callbacks could be returning a positive or a negative value and
we want to rely on that to cancel the loop. There are still a
couple places where an user error might get turned into GIT_EUSER
there, I think, though none exercised by the tests.
Diffstat (limited to 'src/config.c')
-rw-r--r-- | src/config.c | 42 |
1 files changed, 15 insertions, 27 deletions
diff --git a/src/config.c b/src/config.c index 3af9d58de..8b3a426ed 100644 --- a/src/config.c +++ b/src/config.c @@ -480,23 +480,23 @@ int git_config_foreach( int git_config_backend_foreach_match( git_config_backend *backend, const char *regexp, - int (*fn)(const git_config_entry *, void *), - void *data) + git_config_foreach_cb cb, + void *payload) { git_config_entry *entry; git_config_iterator* iter; regex_t regex; - int result = 0; + int error = 0; if (regexp != NULL) { - if ((result = regcomp(®ex, regexp, REG_EXTENDED)) < 0) { - giterr_set_regex(®ex, result); + if ((error = regcomp(®ex, regexp, REG_EXTENDED)) < 0) { + giterr_set_regex(®ex, error); regfree(®ex); return -1; } } - if ((result = backend->iterator(&iter, backend)) < 0) { + if ((error = backend->iterator(&iter, backend)) < 0) { iter = NULL; return -1; } @@ -507,10 +507,9 @@ int git_config_backend_foreach_match( continue; /* abort iterator on non-zero return value */ - if (fn(entry, data)) { - result = giterr_user_cancel(); + error = GITERR_CALLBACK( cb(entry, payload) ); + if (error) break; - } } if (regexp != NULL) @@ -518,7 +517,7 @@ int git_config_backend_foreach_match( iter->free(iter); - return result; + return error; } int git_config_foreach_match( @@ -534,12 +533,9 @@ int git_config_foreach_match( if ((error = git_config_iterator_glob_new(&iter, cfg, regexp)) < 0) return error; - while ((error = git_config_next(&entry, iter)) == 0) { - if (cb(entry, payload)) { - error = giterr_user_cancel(); - break; - } - } + while (!(error = git_config_next(&entry, iter)) && + !(error = GITERR_CALLBACK( cb(entry, payload) ))) + /* make callback on each config */; git_config_iterator_free(iter); @@ -798,10 +794,8 @@ int git_config_get_multivar_foreach( while ((err = iter->next(&entry, iter)) == 0) { found = 1; - if (cb(entry, payload)) { - iter->free(iter); - return giterr_user_cancel(); - } + if ((err = GITERR_CALLBACK( cb(entry, payload) )) != 0) + break; } iter->free(iter); @@ -1212,7 +1206,6 @@ struct rename_data { git_config *config; git_buf *name; size_t old_len; - git_error_state error; }; static int rename_config_entries_cb( @@ -1235,8 +1228,7 @@ static int rename_config_entries_cb( if (!error) error = git_config_delete_entry(data->config, entry->name); - /* capture error message as needed, since it will become EUSER */ - return giterr_capture(&data->error, error); + return error; } int git_config_rename_section( @@ -1257,7 +1249,6 @@ int git_config_rename_section( if ((error = git_repository_config__weakptr(&config, repo)) < 0) goto cleanup; - memset(&data, 0, sizeof(data)); data.config = config; data.name = &replace; data.old_len = strlen(old_section_name) + 1; @@ -1277,9 +1268,6 @@ int git_config_rename_section( error = git_config_foreach_match( config, git_buf_cstr(&pattern), rename_config_entries_cb, &data); - if (error == GIT_EUSER) - error = giterr_restore(&data.error); - cleanup: git_buf_free(&pattern); git_buf_free(&replace); |