refs: factor out set_read_ref_cutoffs()

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>

Denton Liu committed Jan 6, 2021 at 01:01 UTC 95c2a71820c8fc2bd333921dee71c35871923716
1 file changed +17 -17
refs.c
+17 -17
@@ -882,25 +882,30 @@ struct read_ref_at_cb {
882 int *cutoff_cnt;
883 };
884
885 +static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
886 + timestamp_t timestamp, int tz, const char *message)
887 +{
888 + if (cb->msg)
889 + *cb->msg = xstrdup(message);
890 + if (cb->cutoff_time)
891 + *cb->cutoff_time = timestamp;
892 + if (cb->cutoff_tz)
893 + *cb->cutoff_tz = tz;
894 + if (cb->cutoff_cnt)
895 + *cb->cutoff_cnt = cb->reccnt;
896 +}
897 +
898 static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
899 const char *email, timestamp_t timestamp, int tz,
900 const char *message, void *cb_data)
901 {
902 struct read_ref_at_cb *cb = cb_data;
903
891 - cb->reccnt++;
904 cb->tz = tz;
905 cb->date = timestamp;
906
907 if (timestamp <= cb->at_time || cb->cnt == 0) {
896 - if (cb->msg)
897 - *cb->msg = xstrdup(message);
898 - if (cb->cutoff_time)
899 - *cb->cutoff_time = timestamp;
900 - if (cb->cutoff_tz)
901 - *cb->cutoff_tz = tz;
902 - if (cb->cutoff_cnt)
903 - *cb->cutoff_cnt = cb->reccnt - 1;
908 + set_read_ref_cutoffs(cb, timestamp, tz, message);
909 /*
910 * we have not yet updated cb->[n|o]oid so they still
911 * 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,
922 warning(_("log for ref %s unexpectedly ended on %s"),
923 cb->refname, show_date(cb->date, cb->tz,
924 DATE_MODE(RFC2822)));
925 + cb->reccnt++;
926 oidcpy(&cb->ooid, ooid);
927 oidcpy(&cb->noid, noid);
928 cb->found_it = 1;
929 return 1;
930 }
931 + cb->reccnt++;
932 oidcpy(&cb->ooid, ooid);
933 oidcpy(&cb->noid, noid);
934 if (cb->cnt > 0)
@@ -935,14 +942,7 @@ static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid
942 {
943 struct read_ref_at_cb *cb = cb_data;
944
938 - if (cb->msg)
939 - *cb->msg = xstrdup(message);
940 - if (cb->cutoff_time)
941 - *cb->cutoff_time = timestamp;
942 - if (cb->cutoff_tz)
943 - *cb->cutoff_tz = tz;
944 - if (cb->cutoff_cnt)
945 - *cb->cutoff_cnt = cb->reccnt;
945 + set_read_ref_cutoffs(cb, timestamp, tz, message);
946 oidcpy(cb->oid, ooid);
947 if (is_null_oid(cb->oid))
948 oidcpy(cb->oid, noid);