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/diff_patch.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/diff_patch.c')
-rw-r--r-- | src/diff_patch.c | 36 |
1 files changed, 10 insertions, 26 deletions
diff --git a/src/diff_patch.c b/src/diff_patch.c index c0910558e..11f02478d 100644 --- a/src/diff_patch.c +++ b/src/diff_patch.c @@ -32,7 +32,6 @@ struct git_patch { git_array_t(git_diff_line) lines; size_t content_size, context_size, header_size; git_pool flattened; - git_error_state error; }; enum { @@ -200,11 +199,12 @@ static int diff_patch_invoke_file_callback( float progress = patch->diff ? ((float)patch->delta_index / patch->diff->deltas.length) : 1.0f; - if (output->file_cb && - output->file_cb(patch->delta, progress, output->payload) != 0) - return giterr_user_cancel(); + if (!output->file_cb) + return 0; - return 0; + return giterr_set_callback( + output->file_cb(patch->delta, progress, output->payload), + "git_patch"); } static int diff_patch_generate(git_patch *patch, git_diff_output *output) @@ -291,7 +291,7 @@ int git_diff_foreach( git_patch_free(&patch); - if (error < 0) + if (error) break; } @@ -331,9 +331,6 @@ static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo) if (!error) error = diff_patch_generate(patch, (git_diff_output *)xo); - if (error == GIT_EUSER) - giterr_clear(); /* don't leave error message set invalidly */ - return error; } @@ -462,9 +459,6 @@ int git_patch_from_blobs( error = diff_patch_from_blobs( pd, &xo, old_blob, old_path, new_blob, new_path, opts); - if (error == GIT_EUSER) - error = giterr_restore(&pd->patch.error); - if (!error) *out = (git_patch *)pd; else @@ -576,9 +570,6 @@ int git_patch_from_blob_and_buffer( error = diff_patch_from_blob_and_buffer( pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts); - if (error == GIT_EUSER) - error = giterr_restore(&pd->patch.error); - if (!error) *out = (git_patch *)pd; else @@ -627,12 +618,9 @@ int git_patch_from_diff( if (!error) error = diff_patch_generate(patch, &xo.output); - if (error == GIT_EUSER) - error = giterr_restore(&patch->error); - if (!error) { - /* if cumulative diff size is < 0.5 total size, flatten the patch */ - /* unload the file content */ + /* TODO: if cumulative diff size is < 0.5 total size, flatten patch */ + /* TODO: and unload the file content */ } if (error || !patch_ptr) @@ -640,8 +628,6 @@ int git_patch_from_diff( else *patch_ptr = patch; - if (error == GIT_EUSER) - giterr_clear(); /* don't leave error message set invalidly */ return error; } @@ -879,8 +865,7 @@ static int diff_patch_hunk_cb( GIT_UNUSED(delta); hunk = git_array_alloc(patch->hunks); - if (!hunk) - return giterr_capture(&patch->error, -1); + GITERR_CHECK_ALLOC(hunk); memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk)); @@ -909,8 +894,7 @@ static int diff_patch_line_cb( assert(hunk); /* programmer error if no hunk is available */ line = git_array_alloc(patch->lines); - if (!line) - return giterr_capture(&patch->error, -1); + GITERR_CHECK_ALLOC(line); memcpy(line, line_, sizeof(*line)); |