@@ -3404,29 +3404,54 @@ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void
34043404
34053405 bp = find_beginning_of_line (buf , scanp );
34063406
3407- if (* bp != '\n' ) {
3408- strbuf_splice (& sb , 0 , 0 , buf , endp - buf );
3409- if (pos )
3410- break ; /* need to fill another block */
3411- scanp = buf - 1 ; /* leave loop */
3412- } else {
3407+ if (* bp == '\n' ) {
34133408 /*
3414- * (bp + 1) thru endp is the beginning of the
3415- * current line we have in sb
3409+ * The newline is the end of the previous line,
3410+ * so we know we have complete line starting
3411+ * at (bp + 1). Prefix it onto any prior data
3412+ * we collected for the line and process it.
34163413 */
34173414 strbuf_splice (& sb , 0 , 0 , bp + 1 , endp - (bp + 1 ));
34183415 scanp = bp ;
34193416 endp = bp + 1 ;
3417+ ret = show_one_reflog_ent (& sb , fn , cb_data );
3418+ strbuf_reset (& sb );
3419+ if (ret )
3420+ break ;
3421+ } else if (!pos ) {
3422+ /*
3423+ * We are at the start of the buffer, and the
3424+ * start of the file; there is no previous
3425+ * line, and we have everything for this one.
3426+ * Process it, and we can end the loop.
3427+ */
3428+ strbuf_splice (& sb , 0 , 0 , buf , endp - buf );
3429+ ret = show_one_reflog_ent (& sb , fn , cb_data );
3430+ strbuf_reset (& sb );
3431+ break ;
34203432 }
3421- ret = show_one_reflog_ent (& sb , fn , cb_data );
3422- strbuf_reset (& sb );
3423- if (ret )
3433+
3434+ if (bp == buf ) {
3435+ /*
3436+ * We are at the start of the buffer, and there
3437+ * is more file to read backwards. Which means
3438+ * we are in the middle of a line. Note that we
3439+ * may get here even if *bp was a newline; that
3440+ * just means we are at the exact end of the
3441+ * previous line, rather than some spot in the
3442+ * middle.
3443+ *
3444+ * Save away what we have to be combined with
3445+ * the data from the next read.
3446+ */
3447+ strbuf_splice (& sb , 0 , 0 , buf , endp - buf );
34243448 break ;
3449+ }
34253450 }
34263451
34273452 }
34283453 if (!ret && sb .len )
3429- ret = show_one_reflog_ent ( & sb , fn , cb_data );
3454+ die ( "BUG: reverse reflog parser had leftover data" );
34303455
34313456 fclose (logfp );
34323457 strbuf_release (& sb );
0 commit comments