summaryrefslogtreecommitdiff
path: root/src/diff.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-12-06 15:07:57 -0800
committerRussell Belfer <rb@github.com>2013-12-11 10:57:49 -0800
commit25e0b1576d5f9e5248603f81d3198a65bfccf0ed (patch)
treedac0da29acfbd6478bed93bcab3557e2877dabaa /src/diff.c
parentfcd324c625d8be3f368c924d787e945e5812d8dd (diff)
downloadlibgit2-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.c')
-rw-r--r--src/diff.c79
1 files changed, 33 insertions, 46 deletions
diff --git a/src/diff.c b/src/diff.c
index af47e86aa..101426f6e 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -49,16 +49,25 @@ static git_diff_delta *diff_delta__alloc(
return delta;
}
-static int diff_notify(
- const git_diff *diff,
- const git_diff_delta *delta,
- const char *matched_pathspec)
+static int diff_insert_delta(
+ git_diff *diff, git_diff_delta *delta, const char *matched_pathspec)
{
- if (!diff->opts.notify_cb)
- return 0;
+ int error = 0;
+
+ if (diff->opts.notify_cb) {
+ error = diff->opts.notify_cb(
+ diff, delta, matched_pathspec, diff->opts.notify_payload);
+
+ if (error) {
+ git__free(delta);
+ return (error > 0) ? 0 : giterr_set_callback(error, "git_diff");
+ }
+ }
+
+ if ((error = git_vector_insert(&diff->deltas, delta)) < 0)
+ git__free(delta);
- return diff->opts.notify_cb(
- diff, delta, matched_pathspec, diff->opts.notify_payload);
+ return error;
}
static int diff_delta__from_one(
@@ -68,7 +77,6 @@ static int diff_delta__from_one(
{
git_diff_delta *delta;
const char *matched_pathspec;
- int notify_res;
if ((entry->flags & GIT_IDXENTRY_VALID) != 0)
return 0;
@@ -111,21 +119,12 @@ static int diff_delta__from_one(
!git_oid_iszero(&delta->new_file.oid))
delta->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
- notify_res = diff_notify(diff, delta, matched_pathspec);
-
- if (notify_res)
- git__free(delta);
- else if (git_vector_insert(&diff->deltas, delta) < 0) {
- git__free(delta);
- return -1;
- }
-
- return notify_res < 0 ? giterr_user_cancel() : 0;
+ return diff_insert_delta(diff, delta, matched_pathspec);
}
static int diff_delta__from_two(
git_diff *diff,
- git_delta_t status,
+ git_delta_t status,
const git_index_entry *old_entry,
uint32_t old_mode,
const git_index_entry *new_entry,
@@ -134,7 +133,6 @@ static int diff_delta__from_two(
const char *matched_pathspec)
{
git_diff_delta *delta;
- int notify_res;
const char *canonical_path = old_entry->path;
if (status == GIT_DELTA_UNMODIFIED &&
@@ -173,16 +171,7 @@ static int diff_delta__from_two(
if (new_oid || !git_oid_iszero(&new_entry->oid))
delta->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
- notify_res = diff_notify(diff, delta, matched_pathspec);
-
- if (notify_res)
- git__free(delta);
- else if (git_vector_insert(&diff->deltas, delta) < 0) {
- git__free(delta);
- return -1;
- }
-
- return notify_res < 0 ? giterr_user_cancel() : 0;
+ return diff_insert_delta(diff, delta, matched_pathspec);
}
static git_diff_delta *diff_delta__last_for_item(
@@ -654,6 +643,7 @@ static int maybe_modified(
unsigned int nmode = nitem->mode;
bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
const char *matched_pathspec;
+ int error = 0;
if (!git_pathspec__match(
&diff->pathspec, oitem->path,
@@ -688,10 +678,9 @@ static int maybe_modified(
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE))
status = GIT_DELTA_TYPECHANGE;
else {
- if (diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem) < 0 ||
- diff_delta__from_one(diff, GIT_DELTA_ADDED, nitem) < 0)
- return -1;
- return 0;
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem)))
+ error = diff_delta__from_one(diff, GIT_DELTA_ADDED, nitem);
+ return error;
}
}
@@ -713,8 +702,8 @@ static int maybe_modified(
/* TODO: add check against index file st_mtime to avoid racy-git */
if (S_ISGITLINK(nmode)) {
- if (maybe_modified_submodule(&status, &noid, diff, info) < 0)
- return -1;
+ if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0)
+ return error;
}
/* if the stat data looks different, then mark modified - this just
@@ -741,9 +730,9 @@ static int maybe_modified(
*/
if (status == GIT_DELTA_MODIFIED && git_oid_iszero(&nitem->oid)) {
if (git_oid_iszero(&noid)) {
- if (git_diff__oid_for_file(diff->repo,
- nitem->path, nitem->mode, nitem->file_size, &noid) < 0)
- return -1;
+ if ((error = git_diff__oid_for_file(diff->repo,
+ nitem->path, nitem->mode, nitem->file_size, &noid)) < 0)
+ return error;
}
/* if oid matches, then mark unmodified (except submodules, where
@@ -898,7 +887,7 @@ static int handle_unmatched_new_item(
git_diff_delta *last;
/* attempt to insert record for this directory */
- if ((error = diff_delta__from_one(diff, delta_type, nitem)) < 0)
+ if ((error = diff_delta__from_one(diff, delta_type, nitem)) != 0)
return error;
/* if delta wasn't created (because of rules), just skip ahead */
@@ -977,7 +966,7 @@ static int handle_unmatched_new_item(
}
/* Actually create the record for this item if necessary */
- if ((error = diff_delta__from_one(diff, delta_type, nitem)) < 0)
+ if ((error = diff_delta__from_one(diff, delta_type, nitem)) != 0)
return error;
/* If user requested TYPECHANGE records, then check for that instead of
@@ -1002,7 +991,7 @@ static int handle_unmatched_old_item(
git_diff *diff, diff_in_progress *info)
{
int error = diff_delta__from_one(diff, GIT_DELTA_DELETED, info->oitem);
- if (error < 0)
+ if (error != 0)
return error;
/* if we are generating TYPECHANGE records then check for that
@@ -1399,10 +1388,8 @@ int git_diff__paired_foreach(
i++; j++;
}
- if (cb(h2i, i2w, payload)) {
- error = giterr_user_cancel();
+ if ((error = GITERR_CALLBACK( cb(h2i, i2w, payload) )) != 0)
break;
- }
}
/* restore case-insensitive delta sort */