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_print.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_print.c')
-rw-r--r-- | src/diff_print.c | 93 |
1 files changed, 32 insertions, 61 deletions
diff --git a/src/diff_print.c b/src/diff_print.c index 712402864..ff477e4c8 100644 --- a/src/diff_print.c +++ b/src/diff_print.c @@ -18,7 +18,6 @@ typedef struct { uint32_t flags; int oid_strlen; git_diff_line line; - git_error_state error; } diff_print_info; static int diff_print_info_init( @@ -34,7 +33,6 @@ static int diff_print_info_init( pi->print_cb = cb; pi->payload = payload; pi->buf = out; - memset(&pi->error, 0, sizeof(pi->error)); if (diff) pi->flags = diff->opts.flags; @@ -104,19 +102,16 @@ static int diff_print_one_name_only( return 0; git_buf_clear(out); - - if (git_buf_puts(out, delta->new_file.path) < 0 || - git_buf_putc(out, '\n')) - return giterr_capture(&pi->error, -1); + git_buf_puts(out, delta->new_file.path); + git_buf_putc(out, '\n'); + if (git_buf_oom(out)) + return -1; pi->line.origin = GIT_DIFF_LINE_FILE_HDR; pi->line.content = git_buf_cstr(out); pi->line.content_len = git_buf_len(out); - if (pi->print_cb(delta, NULL, &pi->line, pi->payload)) - return giterr_user_cancel(); - - return 0; + return pi->print_cb(delta, NULL, &pi->line, pi->payload); } static int diff_print_one_name_status( @@ -150,18 +145,14 @@ static int diff_print_one_name_status( git_buf_printf(out, "%c\t%s%c\n", code, delta->old_file.path, old_suffix); else git_buf_printf(out, "%c\t%s\n", code, delta->old_file.path); - if (git_buf_oom(out)) - return giterr_capture(&pi->error, -1); + return -1; pi->line.origin = GIT_DIFF_LINE_FILE_HDR; pi->line.content = git_buf_cstr(out); pi->line.content_len = git_buf_len(out); - if (pi->print_cb(delta, NULL, &pi->line, pi->payload)) - return giterr_user_cancel(); - - return 0; + return pi->print_cb(delta, NULL, &pi->line, pi->payload); } static int diff_print_one_raw( @@ -198,16 +189,13 @@ static int diff_print_one_raw( delta->old_file.path : delta->new_file.path); if (git_buf_oom(out)) - return giterr_capture(&pi->error, -1); + return -1; pi->line.origin = GIT_DIFF_LINE_FILE_HDR; pi->line.content = git_buf_cstr(out); pi->line.content_len = git_buf_len(out); - if (pi->print_cb(delta, NULL, &pi->line, pi->payload)) - return giterr_user_cancel(); - - return 0; + return pi->print_cb(delta, NULL, &pi->line, pi->payload); } static int diff_print_oid_range( @@ -234,10 +222,7 @@ static int diff_print_oid_range( git_buf_printf(out, "index %s..%s\n", start_oid, end_oid); } - if (git_buf_oom(out)) - return -1; - - return 0; + return git_buf_oom(out) ? -1 : 0; } static int diff_delta_format_with_paths( @@ -281,8 +266,7 @@ int git_diff_delta__format_file_header( git_buf_printf(out, "diff --git %s%s %s%s\n", oldpfx, delta->old_file.path, newpfx, delta->new_file.path); - if (diff_print_oid_range(out, delta, oid_strlen) < 0) - return -1; + GITERR_CHECK_ERROR(diff_print_oid_range(out, delta, oid_strlen)); if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) diff_delta_format_with_paths( @@ -294,6 +278,7 @@ int git_diff_delta__format_file_header( static int diff_print_patch_file( const git_diff_delta *delta, float progress, void *data) { + int error; diff_print_info *pi = data; const char *oldpfx = pi->diff ? pi->diff->opts.old_prefix : DIFF_OLD_PREFIX_DEFAULT; @@ -309,36 +294,33 @@ static int diff_print_patch_file( (pi->flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) == 0)) return 0; - if (git_diff_delta__format_file_header( - pi->buf, delta, oldpfx, newpfx, pi->oid_strlen) < 0) - return giterr_capture(&pi->error, -1); + if ((error = git_diff_delta__format_file_header( + pi->buf, delta, oldpfx, newpfx, pi->oid_strlen)) < 0) + return error; pi->line.origin = GIT_DIFF_LINE_FILE_HDR; pi->line.content = git_buf_cstr(pi->buf); pi->line.content_len = git_buf_len(pi->buf); - if (pi->print_cb(delta, NULL, &pi->line, pi->payload)) - return giterr_user_cancel(); + if ((error = pi->print_cb(delta, NULL, &pi->line, pi->payload)) != 0) + return error; if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) return 0; git_buf_clear(pi->buf); - if (diff_delta_format_with_paths( + if ((error = diff_delta_format_with_paths( pi->buf, delta, oldpfx, newpfx, - "Binary files %s%s and %s%s differ\n") < 0) - return giterr_capture(&pi->error, -1); + "Binary files %s%s and %s%s differ\n")) < 0) + return error; pi->line.origin = GIT_DIFF_LINE_BINARY; pi->line.content = git_buf_cstr(pi->buf); pi->line.content_len = git_buf_len(pi->buf); pi->line.num_lines = 1; - if (pi->print_cb(delta, NULL, &pi->line, pi->payload)) - return giterr_user_cancel(); - - return 0; + return pi->print_cb(delta, NULL, &pi->line, pi->payload); } static int diff_print_patch_hunk( @@ -355,10 +337,7 @@ static int diff_print_patch_hunk( pi->line.content = h->header; pi->line.content_len = h->header_len; - if (pi->print_cb(d, h, &pi->line, pi->payload)) - return giterr_user_cancel(); - - return 0; + return pi->print_cb(d, h, &pi->line, pi->payload); } static int diff_print_patch_line( @@ -372,10 +351,7 @@ static int diff_print_patch_line( if (S_ISDIR(delta->new_file.mode)) return 0; - if (pi->print_cb(delta, hunk, line, pi->payload)) - return giterr_user_cancel(); - - return 0; + return pi->print_cb(delta, hunk, line, pi->payload); } /* print a git_diff to an output callback */ @@ -421,8 +397,8 @@ int git_diff_print( error = git_diff_foreach( diff, print_file, print_hunk, print_line, &pi); - if (error == GIT_EUSER && pi.error.error_code) - error = giterr_restore(&pi.error); + if (error) /* make sure error message is set */ + giterr_set_callback(error, "git_diff_print"); } git_buf_free(&buf); @@ -450,8 +426,8 @@ int git_patch_print( patch, diff_print_patch_file, diff_print_patch_hunk, diff_print_patch_line, &pi); - if (error && error != GIT_EUSER) - error = giterr_restore(&pi.error); + if (error) /* make sure error message is set */ + giterr_set_callback(error, "git_patch_print"); } git_buf_free(&temp); @@ -484,17 +460,12 @@ int git_patch_to_str( int error; git_buf output = GIT_BUF_INIT; - error = git_patch_print(patch, diff_print_to_buffer_cb, &output); - - /* GIT_EUSER means git_buf_put in print_to_buffer_cb returned -1, - * meaning a memory allocation failure, so just map to -1... - */ - if (error == GIT_EUSER) { - giterr_set_oom(); - error = -1; + if (!(error = git_patch_print(patch, diff_print_to_buffer_cb, &output))) + *string = git_buf_detach(&output); + else { + git_buf_free(&output); + *string = NULL; } - *string = git_buf_detach(&output); - return error; } |