]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: factor out set_read_ref_cutoffs()
authorDenton Liu <liu.denton@gmail.com>
Wed, 6 Jan 2021 09:01:53 +0000 (01:01 -0800)
committerJunio C Hamano <gitster@pobox.com>
Sun, 10 Jan 2021 20:24:00 +0000 (12:24 -0800)
This block of code is duplicated twice. In a future commit, it will be
duplicated for a third time. Factor out the common functionality into
set_read_ref_cutoffs().

In the case of read_ref_at_ent(), we are incrementing `cb->reccnt` at the
beginning of the function. Move these to right before the return so that
the `cb->reccnt - 1` is changed to `cb->reccnt` and it can be cleanly
factored out into set_read_ref_cutoffs(). The duplication of the
increment statements will be removed in a future patch.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.c

diff --git a/refs.c b/refs.c
index 13dc2c3291b398aae7c9797155363922def64eff..bfdd04aefd5dee65e8352c1b12c03d1bbe84f398 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -882,25 +882,30 @@ struct read_ref_at_cb {
        int *cutoff_cnt;
 };
 
+static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
+               timestamp_t timestamp, int tz, const char *message)
+{
+       if (cb->msg)
+               *cb->msg = xstrdup(message);
+       if (cb->cutoff_time)
+               *cb->cutoff_time = timestamp;
+       if (cb->cutoff_tz)
+               *cb->cutoff_tz = tz;
+       if (cb->cutoff_cnt)
+               *cb->cutoff_cnt = cb->reccnt;
+}
+
 static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
                const char *email, timestamp_t timestamp, int tz,
                const char *message, void *cb_data)
 {
        struct read_ref_at_cb *cb = cb_data;
 
-       cb->reccnt++;
        cb->tz = tz;
        cb->date = timestamp;
 
        if (timestamp <= cb->at_time || cb->cnt == 0) {
-               if (cb->msg)
-                       *cb->msg = xstrdup(message);
-               if (cb->cutoff_time)
-                       *cb->cutoff_time = timestamp;
-               if (cb->cutoff_tz)
-                       *cb->cutoff_tz = tz;
-               if (cb->cutoff_cnt)
-                       *cb->cutoff_cnt = cb->reccnt - 1;
+               set_read_ref_cutoffs(cb, timestamp, tz, message);
                /*
                 * we have not yet updated cb->[n|o]oid so they still
                 * hold the values for the previous record.
@@ -917,11 +922,13 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
                        warning(_("log for ref %s unexpectedly ended on %s"),
                                cb->refname, show_date(cb->date, cb->tz,
                                                       DATE_MODE(RFC2822)));
+               cb->reccnt++;
                oidcpy(&cb->ooid, ooid);
                oidcpy(&cb->noid, noid);
                cb->found_it = 1;
                return 1;
        }
+       cb->reccnt++;
        oidcpy(&cb->ooid, ooid);
        oidcpy(&cb->noid, noid);
        if (cb->cnt > 0)
@@ -935,14 +942,7 @@ static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid
 {
        struct read_ref_at_cb *cb = cb_data;
 
-       if (cb->msg)
-               *cb->msg = xstrdup(message);
-       if (cb->cutoff_time)
-               *cb->cutoff_time = timestamp;
-       if (cb->cutoff_tz)
-               *cb->cutoff_tz = tz;
-       if (cb->cutoff_cnt)
-               *cb->cutoff_cnt = cb->reccnt;
+       set_read_ref_cutoffs(cb, timestamp, tz, message);
        oidcpy(cb->oid, ooid);
        if (is_null_oid(cb->oid))
                oidcpy(cb->oid, noid);