All of our errors come from diff_get_patch_id(), which has
exactly three error conditions. The first is an internal
assertion, which should be a die("BUG") in the first place.

The other two are caused by an inability to two diff blobs,
which is an indication of a serious problem (probably
repository corruption). All the rest of the diff subsystem
dies immediately on these conditions. By passing up the
error, in theory we can keep going even if patch-id is
unable to function. But in practice this means we may
generate subtly wrong results (e.g., by failing to correlate
two commits). Let's just die(), as we're better off making
it clear to the user that their repository is not
functional.

As a result, we can simplify the calling code.

Signed-off-by: Jeff King <p...@peff.net>
---
This is a prerequisite for patch 3, since it means that
commit_patch_id() stops returning "real" errors. But obviously if this
is distasteful (and it does feel a little weird to convert error() to
die(), even though the rest of the diff code-base behaves this way), we
can teach commit_patch_id() to distinguish between "this has no
patch-id" and "a real error occured" in its return value.

 diff.c      | 18 ++++++++----------
 diff.h      |  2 +-
 patch-ids.c |  3 ++-
 3 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/diff.c b/diff.c
index 534c12e..d0594f6 100644
--- a/diff.c
+++ b/diff.c
@@ -4462,7 +4462,7 @@ static void patch_id_consume(void *priv, char *line, 
unsigned long len)
 }
 
 /* returns 0 upon success, and writes result into sha1 */
-static int diff_get_patch_id(struct diff_options *options, unsigned char 
*sha1, int diff_header_only)
+static void diff_get_patch_id(struct diff_options *options, unsigned char 
*sha1, int diff_header_only)
 {
        struct diff_queue_struct *q = &diff_queued_diff;
        int i;
@@ -4484,7 +4484,7 @@ static int diff_get_patch_id(struct diff_options 
*options, unsigned char *sha1,
                memset(&xpp, 0, sizeof(xpp));
                memset(&xecfg, 0, sizeof(xecfg));
                if (p->status == 0)
-                       return error("internal diff status error");
+                       die("BUG: diff status unset while computing patch_id");
                if (p->status == DIFF_STATUS_UNKNOWN)
                        continue;
                if (diff_unmodified_pair(p))
@@ -4536,7 +4536,7 @@ static int diff_get_patch_id(struct diff_options 
*options, unsigned char *sha1,
 
                if (fill_mmfile(&mf1, p->one) < 0 ||
                    fill_mmfile(&mf2, p->two) < 0)
-                       return error("unable to read files to diff");
+                       die("unable to read files to diff");
 
                if (diff_filespec_is_binary(p->one) ||
                    diff_filespec_is_binary(p->two)) {
@@ -4552,27 +4552,25 @@ static int diff_get_patch_id(struct diff_options 
*options, unsigned char *sha1,
                xecfg.flags = 0;
                if (xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
                                  &xpp, &xecfg))
-                       return error("unable to generate patch-id diff for %s",
-                                    p->one->path);
+                       die("unable to generate patch-id diff for %s",
+                           p->one->path);
        }
 
        git_SHA1_Final(sha1, &ctx);
-       return 0;
 }
 
-int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1, int 
diff_header_only)
+void diff_flush_patch_id(struct diff_options *options, unsigned char *sha1, 
int diff_header_only)
 {
        struct diff_queue_struct *q = &diff_queued_diff;
        int i;
-       int result = diff_get_patch_id(options, sha1, diff_header_only);
+
+       diff_get_patch_id(options, sha1, diff_header_only);
 
        for (i = 0; i < q->nr; i++)
                diff_free_filepair(q->queue[i]);
 
        free(q->queue);
        DIFF_QUEUE_CLEAR(q);
-
-       return result;
 }
 
 static int is_summary_empty(const struct diff_queue_struct *q)
diff --git a/diff.h b/diff.h
index 7883729..f4dcfe1 100644
--- a/diff.h
+++ b/diff.h
@@ -342,7 +342,7 @@ extern int run_diff_files(struct rev_info *revs, unsigned 
int option);
 extern int run_diff_index(struct rev_info *revs, int cached);
 
 extern int do_diff_cache(const unsigned char *, struct diff_options *);
-extern int diff_flush_patch_id(struct diff_options *, unsigned char *, int);
+extern void diff_flush_patch_id(struct diff_options *, unsigned char *, int);
 
 extern int diff_result_code(struct diff_options *, int);
 
diff --git a/patch-ids.c b/patch-ids.c
index 77e4663..0e95220 100644
--- a/patch-ids.c
+++ b/patch-ids.c
@@ -13,7 +13,8 @@ int commit_patch_id(struct commit *commit, struct 
diff_options *options,
        else
                diff_root_tree_sha1(commit->object.oid.hash, "", options);
        diffcore_std(options);
-       return diff_flush_patch_id(options, sha1, diff_header_only);
+       diff_flush_patch_id(options, sha1, diff_header_only);
+       return 0;
 }
 
 /*
-- 
2.10.0.rc2.154.gb4a4b8b

Reply via email to