@@ -2173,6 +2173,11 @@ static uintmax_t do_change_note_fanout(
2173
2173
2174
2174
if (tmp_hex_sha1_len == 40 && !get_sha1_hex (hex_sha1 , sha1 )) {
2175
2175
/* This is a note entry */
2176
+ if (fanout == 0xff ) {
2177
+ /* Counting mode, no rename */
2178
+ num_notes ++ ;
2179
+ continue ;
2180
+ }
2176
2181
construct_path_with_fanout (hex_sha1 , fanout , realpath );
2177
2182
if (!strcmp (fullpath , realpath )) {
2178
2183
/* Note entry is in correct location */
@@ -2379,7 +2384,7 @@ static void file_change_cr(struct branch *b, int rename)
2379
2384
leaf .tree );
2380
2385
}
2381
2386
2382
- static void note_change_n (struct branch * b , unsigned char old_fanout )
2387
+ static void note_change_n (struct branch * b , unsigned char * old_fanout )
2383
2388
{
2384
2389
const char * p = command_buf .buf + 2 ;
2385
2390
static struct strbuf uq = STRBUF_INIT ;
@@ -2390,6 +2395,23 @@ static void note_change_n(struct branch *b, unsigned char old_fanout)
2390
2395
uint16_t inline_data = 0 ;
2391
2396
unsigned char new_fanout ;
2392
2397
2398
+ /*
2399
+ * When loading a branch, we don't traverse its tree to count the real
2400
+ * number of notes (too expensive to do this for all non-note refs).
2401
+ * This means that recently loaded notes refs might incorrectly have
2402
+ * b->num_notes == 0, and consequently, old_fanout might be wrong.
2403
+ *
2404
+ * Fix this by traversing the tree and counting the number of notes
2405
+ * when b->num_notes == 0. If the notes tree is truly empty, the
2406
+ * calculation should not take long.
2407
+ */
2408
+ if (b -> num_notes == 0 && * old_fanout == 0 ) {
2409
+ /* Invoke change_note_fanout() in "counting mode". */
2410
+ b -> num_notes = change_note_fanout (& b -> branch_tree , 0xff );
2411
+ * old_fanout = convert_num_notes_to_fanout (b -> num_notes );
2412
+ }
2413
+
2414
+ /* Now parse the notemodify command. */
2393
2415
/* <dataref> or 'inline' */
2394
2416
if (* p == ':' ) {
2395
2417
char * x ;
@@ -2450,7 +2472,7 @@ static void note_change_n(struct branch *b, unsigned char old_fanout)
2450
2472
typename (type ), command_buf .buf );
2451
2473
}
2452
2474
2453
- construct_path_with_fanout (sha1_to_hex (commit_sha1 ), old_fanout , path );
2475
+ construct_path_with_fanout (sha1_to_hex (commit_sha1 ), * old_fanout , path );
2454
2476
if (tree_content_remove (& b -> branch_tree , path , NULL ))
2455
2477
b -> num_notes -- ;
2456
2478
@@ -2637,7 +2659,7 @@ static void parse_new_commit(void)
2637
2659
else if (!prefixcmp (command_buf .buf , "C " ))
2638
2660
file_change_cr (b , 0 );
2639
2661
else if (!prefixcmp (command_buf .buf , "N " ))
2640
- note_change_n (b , prev_fanout );
2662
+ note_change_n (b , & prev_fanout );
2641
2663
else if (!strcmp ("deleteall" , command_buf .buf ))
2642
2664
file_change_deleteall (b );
2643
2665
else if (!prefixcmp (command_buf .buf , "ls " ))
0 commit comments