3 #include "cache-tree.h"
9 struct cache_tree
*cache_tree(void)
11 struct cache_tree
*it
= xcalloc(1, sizeof(struct cache_tree
));
16 void cache_tree_free(struct cache_tree
**it_p
)
19 struct cache_tree
*it
= *it_p
;
23 for (i
= 0; i
< it
->subtree_nr
; i
++)
25 cache_tree_free(&it
->down
[i
]->cache_tree
);
31 static int subtree_name_cmp(const char *one
, int onelen
,
32 const char *two
, int twolen
)
38 return memcmp(one
, two
, onelen
);
41 static int subtree_pos(struct cache_tree
*it
, const char *path
, int pathlen
)
43 struct cache_tree_sub
**down
= it
->down
;
48 int mi
= (lo
+ hi
) / 2;
49 struct cache_tree_sub
*mdl
= down
[mi
];
50 int cmp
= subtree_name_cmp(path
, pathlen
,
51 mdl
->name
, mdl
->namelen
);
62 static struct cache_tree_sub
*find_subtree(struct cache_tree
*it
,
67 struct cache_tree_sub
*down
;
68 int pos
= subtree_pos(it
, path
, pathlen
);
75 if (it
->subtree_alloc
<= it
->subtree_nr
) {
76 it
->subtree_alloc
= alloc_nr(it
->subtree_alloc
);
77 it
->down
= xrealloc(it
->down
, it
->subtree_alloc
*
82 down
= xmalloc(sizeof(*down
) + pathlen
+ 1);
83 down
->cache_tree
= NULL
;
84 down
->namelen
= pathlen
;
85 memcpy(down
->name
, path
, pathlen
);
86 down
->name
[pathlen
] = 0;
88 if (pos
< it
->subtree_nr
)
89 memmove(it
->down
+ pos
+ 1,
91 sizeof(down
) * (it
->subtree_nr
- pos
- 1));
96 struct cache_tree_sub
*cache_tree_sub(struct cache_tree
*it
, const char *path
)
98 int pathlen
= strlen(path
);
99 return find_subtree(it
, path
, pathlen
, 1);
102 void cache_tree_invalidate_path(struct cache_tree
*it
, const char *path
)
105 * ==> invalidate self
106 * ==> find "a", have it invalidate "b/c"
108 * ==> invalidate self
109 * ==> if "a" exists as a subtree, remove it.
113 struct cache_tree_sub
*down
;
116 fprintf(stderr
, "cache-tree invalidate <%s>\n", path
);
121 slash
= strchr(path
, '/');
122 it
->entry_count
= -1;
125 namelen
= strlen(path
);
126 pos
= subtree_pos(it
, path
, namelen
);
128 cache_tree_free(&it
->down
[pos
]->cache_tree
);
133 * move 4 and 5 up one place (2 entries)
134 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
136 memmove(it
->down
+pos
, it
->down
+pos
+1,
137 sizeof(struct cache_tree_sub
*) *
138 (it
->subtree_nr
- pos
- 1));
143 namelen
= slash
- path
;
144 down
= find_subtree(it
, path
, namelen
, 0);
146 cache_tree_invalidate_path(down
->cache_tree
, slash
+ 1);
149 static int verify_cache(struct cache_entry
**cache
,
154 /* Verify that the tree is merged */
156 for (i
= 0; i
< entries
; i
++) {
157 struct cache_entry
*ce
= cache
[i
];
160 fprintf(stderr
, "...\n");
163 fprintf(stderr
, "%s: unmerged (%s)\n",
164 ce
->name
, sha1_to_hex(ce
->sha1
));
170 /* Also verify that the cache does not have path and path/file
171 * at the same time. At this point we know the cache has only
175 for (i
= 0; i
< entries
- 1; i
++) {
176 /* path/file always comes after path because of the way
177 * the cache is sorted. Also path can appear only once,
178 * which means conflicting one would immediately follow.
180 const char *this_name
= cache
[i
]->name
;
181 const char *next_name
= cache
[i
+1]->name
;
182 int this_len
= strlen(this_name
);
183 if (this_len
< strlen(next_name
) &&
184 strncmp(this_name
, next_name
, this_len
) == 0 &&
185 next_name
[this_len
] == '/') {
187 fprintf(stderr
, "...\n");
190 fprintf(stderr
, "You have both %s and %s\n",
191 this_name
, next_name
);
199 static void discard_unused_subtrees(struct cache_tree
*it
)
201 struct cache_tree_sub
**down
= it
->down
;
202 int nr
= it
->subtree_nr
;
204 for (dst
= src
= 0; src
< nr
; src
++) {
205 struct cache_tree_sub
*s
= down
[src
];
209 cache_tree_free(&s
->cache_tree
);
216 int cache_tree_fully_valid(struct cache_tree
*it
)
221 if (it
->entry_count
< 0 || !has_sha1_file(it
->sha1
))
223 for (i
= 0; i
< it
->subtree_nr
; i
++) {
224 if (!cache_tree_fully_valid(it
->down
[i
]->cache_tree
))
230 static int update_one(struct cache_tree
*it
,
231 struct cache_entry
**cache
,
238 struct strbuf buffer
;
241 if (0 <= it
->entry_count
&& has_sha1_file(it
->sha1
))
242 return it
->entry_count
;
245 * We first scan for subtrees and update them; we start by
246 * marking existing subtrees -- the ones that are unmarked
247 * should not be in the result.
249 for (i
= 0; i
< it
->subtree_nr
; i
++)
250 it
->down
[i
]->used
= 0;
253 * Find the subtrees and update them.
255 for (i
= 0; i
< entries
; i
++) {
256 struct cache_entry
*ce
= cache
[i
];
257 struct cache_tree_sub
*sub
;
258 const char *path
, *slash
;
259 int pathlen
, sublen
, subcnt
;
262 pathlen
= ce_namelen(ce
);
263 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
264 break; /* at the end of this level */
266 slash
= strchr(path
+ baselen
, '/');
270 * a/bbb/c (base = a/, slash = /c)
272 * path+baselen = bbb/c, sublen = 3
274 sublen
= slash
- (path
+ baselen
);
275 sub
= find_subtree(it
, path
+ baselen
, sublen
, 1);
276 if (!sub
->cache_tree
)
277 sub
->cache_tree
= cache_tree();
278 subcnt
= update_one(sub
->cache_tree
,
279 cache
+ i
, entries
- i
,
281 baselen
+ sublen
+ 1,
290 discard_unused_subtrees(it
);
293 * Then write out the tree object for this level.
295 strbuf_init(&buffer
, 8192);
297 for (i
= 0; i
< entries
; i
++) {
298 struct cache_entry
*ce
= cache
[i
];
299 struct cache_tree_sub
*sub
;
300 const char *path
, *slash
;
302 const unsigned char *sha1
;
306 pathlen
= ce_namelen(ce
);
307 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
308 break; /* at the end of this level */
310 slash
= strchr(path
+ baselen
, '/');
312 entlen
= slash
- (path
+ baselen
);
313 sub
= find_subtree(it
, path
+ baselen
, entlen
, 0);
315 die("cache-tree.c: '%.*s' in '%s' not found",
316 entlen
, path
+ baselen
, path
);
317 i
+= sub
->cache_tree
->entry_count
- 1;
318 sha1
= sub
->cache_tree
->sha1
;
324 entlen
= pathlen
- baselen
;
326 if (mode
!= S_IFGITLINK
&& !missing_ok
&& !has_sha1_file(sha1
))
327 return error("invalid object %s", sha1_to_hex(sha1
));
329 if (ce
->ce_flags
& CE_REMOVE
)
330 continue; /* entry being removed */
332 strbuf_grow(&buffer
, entlen
+ 100);
333 strbuf_addf(&buffer
, "%o %.*s%c", mode
, entlen
, path
+ baselen
, '\0');
334 strbuf_add(&buffer
, sha1
, 20);
337 fprintf(stderr
, "cache-tree update-one %o %.*s\n",
338 mode
, entlen
, path
+ baselen
);
343 hash_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, it
->sha1
);
345 write_sha1_file(buffer
.buf
, buffer
.len
, tree_type
, it
->sha1
);
346 strbuf_release(&buffer
);
349 fprintf(stderr
, "cache-tree update-one (%d ent, %d subtree) %s\n",
350 it
->entry_count
, it
->subtree_nr
,
351 sha1_to_hex(it
->sha1
));
356 int cache_tree_update(struct cache_tree
*it
,
357 struct cache_entry
**cache
,
363 i
= verify_cache(cache
, entries
);
366 i
= update_one(it
, cache
, entries
, "", 0, missing_ok
, dryrun
);
372 static void write_one(struct strbuf
*buffer
, struct cache_tree
*it
,
373 const char *path
, int pathlen
)
377 /* One "cache-tree" entry consists of the following:
378 * path (NUL terminated)
379 * entry_count, subtree_nr ("%d %d\n")
380 * tree-sha1 (missing if invalid)
381 * subtree_nr "cache-tree" entries for subtrees.
383 strbuf_grow(buffer
, pathlen
+ 100);
384 strbuf_add(buffer
, path
, pathlen
);
385 strbuf_addf(buffer
, "%c%d %d\n", 0, it
->entry_count
, it
->subtree_nr
);
388 if (0 <= it
->entry_count
)
389 fprintf(stderr
, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
390 pathlen
, path
, it
->entry_count
, it
->subtree_nr
,
391 sha1_to_hex(it
->sha1
));
393 fprintf(stderr
, "cache-tree <%.*s> (%d subtree) invalid\n",
394 pathlen
, path
, it
->subtree_nr
);
397 if (0 <= it
->entry_count
) {
398 strbuf_add(buffer
, it
->sha1
, 20);
400 for (i
= 0; i
< it
->subtree_nr
; i
++) {
401 struct cache_tree_sub
*down
= it
->down
[i
];
403 struct cache_tree_sub
*prev
= it
->down
[i
-1];
404 if (subtree_name_cmp(down
->name
, down
->namelen
,
405 prev
->name
, prev
->namelen
) <= 0)
406 die("fatal - unsorted cache subtree");
408 write_one(buffer
, down
->cache_tree
, down
->name
, down
->namelen
);
412 void cache_tree_write(struct strbuf
*sb
, struct cache_tree
*root
)
414 write_one(sb
, root
, "", 0);
417 static struct cache_tree
*read_one(const char **buffer
, unsigned long *size_p
)
419 const char *buf
= *buffer
;
420 unsigned long size
= *size_p
;
423 struct cache_tree
*it
;
427 /* skip name, but make sure name exists */
428 while (size
&& *buf
) {
438 it
->entry_count
= strtol(cp
, &ep
, 10);
442 subtree_nr
= strtol(cp
, &ep
, 10);
445 while (size
&& *buf
&& *buf
!= '\n') {
452 if (0 <= it
->entry_count
) {
455 hashcpy(it
->sha1
, (const unsigned char*)buf
);
461 if (0 <= it
->entry_count
)
462 fprintf(stderr
, "cache-tree <%s> (%d ent, %d subtree) %s\n",
463 *buffer
, it
->entry_count
, subtree_nr
,
464 sha1_to_hex(it
->sha1
));
466 fprintf(stderr
, "cache-tree <%s> (%d subtrees) invalid\n",
467 *buffer
, subtree_nr
);
471 * Just a heuristic -- we do not add directories that often but
472 * we do not want to have to extend it immediately when we do,
475 it
->subtree_alloc
= subtree_nr
+ 2;
476 it
->down
= xcalloc(it
->subtree_alloc
, sizeof(struct cache_tree_sub
*));
477 for (i
= 0; i
< subtree_nr
; i
++) {
478 /* read each subtree */
479 struct cache_tree
*sub
;
480 struct cache_tree_sub
*subtree
;
481 const char *name
= buf
;
483 sub
= read_one(&buf
, &size
);
486 subtree
= cache_tree_sub(it
, name
);
487 subtree
->cache_tree
= sub
;
489 if (subtree_nr
!= it
->subtree_nr
)
490 die("cache-tree: internal error");
496 cache_tree_free(&it
);
500 struct cache_tree
*cache_tree_read(const char *buffer
, unsigned long size
)
503 return NULL
; /* not the whole tree */
504 return read_one(&buffer
, &size
);
507 struct cache_tree
*cache_tree_find(struct cache_tree
*it
, const char *path
)
511 struct cache_tree_sub
*sub
;
513 slash
= strchr(path
, '/');
515 slash
= path
+ strlen(path
);
516 /* between path and slash is the name of the
517 * subtree to look for.
519 sub
= find_subtree(it
, path
, slash
- path
, 0);
522 it
= sub
->cache_tree
;
524 while (*slash
&& *slash
== '/')
526 if (!slash
|| !*slash
)
527 return it
; /* prefix ended with slashes */