#include "cache.h"
-#include "strbuf.h"
#include "tree.h"
#include "cache-tree.h"
/*
* Then write out the tree object for this level.
*/
- strbuf_init(&buffer);
- strbuf_grow(&buffer, 8192);
+ strbuf_init(&buffer, 8192);
for (i = 0; i < entries; i++) {
struct cache_entry *ce = cache[i];
}
else {
sha1 = ce->sha1;
- mode = ntohl(ce->ce_mode);
+ mode = ce->ce_mode;
entlen = pathlen - baselen;
}
if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1))
return error("invalid object %s", sha1_to_hex(sha1));
- if (!ce->ce_mode)
+ if (ce->ce_flags & CE_REMOVE)
continue; /* entry being removed */
strbuf_grow(&buffer, entlen + 100);
return 0;
}
-static void write_one(struct cache_tree *it,
- char *path,
- int pathlen,
- struct strbuf *buffer)
+static void write_one(struct strbuf *buffer, struct cache_tree *it,
+ const char *path, int pathlen)
{
int i;
prev->name, prev->namelen) <= 0)
die("fatal - unsorted cache subtree");
}
- write_one(down->cache_tree, down->name, down->namelen, buffer);
+ write_one(buffer, down->cache_tree, down->name, down->namelen);
}
}
-void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
+void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
{
- char path[PATH_MAX];
- struct strbuf buffer;
-
- path[0] = 0;
- strbuf_init(&buffer);
- write_one(root, path, 0, &buffer);
- *size_p = buffer.len;
- return strbuf_detach(&buffer);
+ write_one(sb, root, "", 0);
}
static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
}
return it;
}
+
+int write_cache_as_tree(unsigned char *sha1, int missing_ok, const char *prefix)
+{
+ int entries, was_valid, newfd;
+
+ /*
+ * We can't free this memory, it becomes part of a linked list
+ * parsed atexit()
+ */
+ struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
+
+ newfd = hold_locked_index(lock_file, 1);
+
+ entries = read_cache();
+ if (entries < 0)
+ return WRITE_TREE_UNREADABLE_INDEX;
+
+ if (!active_cache_tree)
+ active_cache_tree = cache_tree();
+
+ was_valid = cache_tree_fully_valid(active_cache_tree);
+
+ if (!was_valid) {
+ if (cache_tree_update(active_cache_tree,
+ active_cache, active_nr,
+ missing_ok, 0) < 0)
+ return WRITE_TREE_UNMERGED_INDEX;
+ if (0 <= newfd) {
+ if (!write_cache(newfd, active_cache, active_nr) &&
+ !commit_lock_file(lock_file))
+ newfd = -1;
+ }
+ /* Not being able to write is fine -- we are only interested
+ * in updating the cache-tree part, and if the next caller
+ * ends up using the old index with unupdated cache-tree part
+ * it misses the work we did here, but that is just a
+ * performance penalty and not a big deal.
+ */
+ }
+
+ if (prefix) {
+ struct cache_tree *subtree =
+ cache_tree_find(active_cache_tree, prefix);
+ if (!subtree)
+ return WRITE_TREE_PREFIX_ERROR;
+ hashcpy(sha1, subtree->sha1);
+ }
+ else
+ hashcpy(sha1, active_cache_tree->sha1);
+
+ if (0 <= newfd)
+ rollback_lock_file(lock_file);
+
+ return 0;
+}