summaryrefslogtreecommitdiff
path: root/tree.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-22 23:42:37 (GMT)
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-22 23:42:37 (GMT)
commit94537c78a82619b2625cb827ec0094703579bfed (patch)
tree9258e61a83bd78c41324c96ac57e3bf6e7967d58 /tree.c
parentaba06682473e2d797011f966d66c93043e3ed57a (diff)
downloadgit-94537c78a82619b2625cb827ec0094703579bfed.zip
git-94537c78a82619b2625cb827ec0094703579bfed.tar.gz
git-94537c78a82619b2625cb827ec0094703579bfed.tar.bz2
Move "read_tree()" to "tree.c" to be used as a generic helper function.
Next step: make "diff-cache" use it.
Diffstat (limited to 'tree.c')
-rw-r--r--tree.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/tree.c b/tree.c
index 1aee098..e988aed 100644
--- a/tree.c
+++ b/tree.c
@@ -5,6 +5,71 @@
const char *tree_type = "tree";
+static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
+{
+ int len = strlen(pathname);
+ unsigned int size = cache_entry_size(baselen + len);
+ struct cache_entry *ce = malloc(size);
+
+ memset(ce, 0, size);
+
+ ce->ce_mode = create_ce_mode(mode);
+ ce->ce_flags = create_ce_flags(baselen + len, stage);
+ memcpy(ce->name, base, baselen);
+ memcpy(ce->name + baselen, pathname, len+1);
+ memcpy(ce->sha1, sha1, 20);
+ return add_cache_entry(ce, 1);
+}
+
+static int read_tree_recursive(void *buffer, unsigned long size,
+ const char *base, int baselen, int stage)
+{
+ while (size) {
+ int len = strlen(buffer)+1;
+ unsigned char *sha1 = buffer + len;
+ char *path = strchr(buffer, ' ')+1;
+ unsigned int mode;
+
+ if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
+ return -1;
+
+ buffer = sha1 + 20;
+ size -= len + 20;
+
+ if (S_ISDIR(mode)) {
+ int retval;
+ int pathlen = strlen(path);
+ char *newbase = malloc(baselen + 1 + pathlen);
+ void *eltbuf;
+ char elttype[20];
+ unsigned long eltsize;
+
+ eltbuf = read_sha1_file(sha1, elttype, &eltsize);
+ if (!eltbuf || strcmp(elttype, "tree"))
+ return -1;
+ memcpy(newbase, base, baselen);
+ memcpy(newbase + baselen, path, pathlen);
+ newbase[baselen + pathlen] = '/';
+ retval = read_tree_recursive(eltbuf, eltsize,
+ newbase,
+ baselen + pathlen + 1, stage);
+ free(eltbuf);
+ free(newbase);
+ if (retval)
+ return -1;
+ continue;
+ }
+ if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
+ return -1;
+ }
+ return 0;
+}
+
+int read_tree(void *buffer, unsigned long size, int stage)
+{
+ return read_tree_recursive(buffer, size, "", 0, stage);
+}
+
struct tree *lookup_tree(unsigned char *sha1)
{
struct object *obj = lookup_object(sha1);