blame: use a helper function in blame_chunk()

The same code for splitting a blame_entry at a particular line was used twice in blame_chunk(), and I'll use the helper again in an upcoming patch. Signed-off-by: Barret Rhoden <brho@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Barret Rhoden committed May 15, 2019 at 17:44 UTC 55f808fbc540c64d22393231bd55058fda22c3ce
1 file changed +28 -16
blame.c
+28 -16
@@ -838,6 +838,27 @@ static struct blame_entry *reverse_blame(struct blame_entry *head,
838 return tail;
839 }
840
841 +/*
842 + * Splits a blame entry into two entries at 'len' lines. The original 'e'
843 + * consists of len lines, i.e. [e->lno, e->lno + len), and the second part,
844 + * which is returned, consists of the remainder: [e->lno + len, e->lno +
845 + * e->num_lines). The caller needs to sort out the reference counting for the
846 + * new entry's suspect.
847 + */
848 +static struct blame_entry *split_blame_at(struct blame_entry *e, int len,
849 + struct blame_origin *new_suspect)
850 +{
851 + struct blame_entry *n = xcalloc(1, sizeof(struct blame_entry));
852 +
853 + n->suspect = new_suspect;
854 + n->lno = e->lno + len;
855 + n->s_lno = e->s_lno + len;
856 + n->num_lines = e->num_lines - len;
857 + e->num_lines = len;
858 + e->score = 0;
859 + return n;
860 +}
861 +
862 /*
863 * Process one hunk from the patch between the current suspect for
864 * blame_entry e and its parent. This first blames any unfinished
@@ -864,14 +885,9 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
885 */
886 if (e->s_lno + e->num_lines > tlno) {
887 /* Move second half to a new record */
867 - int len = tlno - e->s_lno;
868 - struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
869 - n->suspect = e->suspect;
870 - n->lno = e->lno + len;
871 - n->s_lno = e->s_lno + len;
872 - n->num_lines = e->num_lines - len;
873 - e->num_lines = len;
874 - e->score = 0;
888 + struct blame_entry *n;
889 +
890 + n = split_blame_at(e, tlno - e->s_lno, e->suspect);
891 /* Push new record to diffp */
892 n->next = diffp;
893 diffp = n;
@@ -918,14 +934,10 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
934 * Move second half to a new record to be
935 * processed by later chunks
936 */
921 - int len = same - e->s_lno;
922 - struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
923 - n->suspect = blame_origin_incref(e->suspect);
924 - n->lno = e->lno + len;
925 - n->s_lno = e->s_lno + len;
926 - n->num_lines = e->num_lines - len;
927 - e->num_lines = len;
928 - e->score = 0;
937 + struct blame_entry *n;
938 +
939 + n = split_blame_at(e, same - e->s_lno,
940 + blame_origin_incref(e->suspect));
941 /* Push new record to samep */
942 n->next = samep;
943 samep = n;