@@ -3404,29 +3404,54 @@ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void
3404
3404
3405
3405
bp = find_beginning_of_line (buf , scanp );
3406
3406
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' ) {
3413
3408
/*
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.
3416
3413
*/
3417
3414
strbuf_splice (& sb , 0 , 0 , bp + 1 , endp - (bp + 1 ));
3418
3415
scanp = bp ;
3419
3416
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 ;
3420
3432
}
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 );
3424
3448
break ;
3449
+ }
3425
3450
}
3426
3451
3427
3452
}
3428
3453
if (!ret && sb .len )
3429
- ret = show_one_reflog_ent ( & sb , fn , cb_data );
3454
+ die ( "BUG: reverse reflog parser had leftover data" );
3430
3455
3431
3456
fclose (logfp );
3432
3457
strbuf_release (& sb );
0 commit comments