2173
2174
if (tmp_hex_sha1_len == 40 && !get_sha1_hex(hex_sha1, sha1)) {
2175
/* This is a note entry */
2176
+ if (fanout == 0xff) {
2177
+ /* Counting mode, no rename */
2178
+ num_notes++;
2179
+ continue;
2180
+ }
2181
construct_path_with_fanout(hex_sha1, fanout, realpath);
2182
if (!strcmp(fullpath, realpath)) {
2183
/* Note entry is in correct location */
2384
leaf.tree);
2385
}
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)
2388
{
2389
const char *p = command_buf.buf + 2;
2390
static struct strbuf uq = STRBUF_INIT;
2395
uint16_t inline_data = 0;
2396
unsigned char new_fanout;
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. */
2415
/* <dataref> or 'inline' */
2416
if (*p == ':') {
2417
char *x;
2472
typename(type), command_buf.buf);
2473
}
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);
2476
if (tree_content_remove(&b->branch_tree, path, NULL))
2477
b->num_notes--;
2478
2659
else if (!prefixcmp(command_buf.buf, "C "))
2660
file_change_cr(b, 0);
2661
else if (!prefixcmp(command_buf.buf, "N "))
2640
- note_change_n(b, prev_fanout);
2662
+ note_change_n(b, &prev_fanout);
2663
else if (!strcmp("deleteall", command_buf.buf))
2664
file_change_deleteall(b);
2665
else if (!prefixcmp(command_buf.buf, "ls "))